haptic.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /* Copyright 2019 ishtob
  2. * Driver for haptic feedback written for QMK
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "haptic.h"
  18. #include "eeconfig.h"
  19. #include "debug.h"
  20. #include "usb_device_state.h"
  21. #include "gpio.h"
  22. #ifdef DRV2605L
  23. # include "DRV2605L.h"
  24. #endif
  25. #ifdef SOLENOID_ENABLE
  26. # include "solenoid.h"
  27. #endif
  28. #if defined(SPLIT_KEYBOARD) && defined(SPLIT_HAPTIC_ENABLE)
  29. extern uint8_t split_haptic_play;
  30. #endif
  31. haptic_config_t haptic_config;
  32. static void update_haptic_enable_gpios(void) {
  33. if (haptic_config.enable && ((!HAPTIC_OFF_IN_LOW_POWER) || (usb_device_state == USB_DEVICE_STATE_CONFIGURED))) {
  34. #if defined(HAPTIC_ENABLE_PIN)
  35. HAPTIC_ENABLE_PIN_WRITE_ACTIVE();
  36. #endif
  37. #if defined(HAPTIC_ENABLE_STATUS_LED)
  38. HAPTIC_ENABLE_STATUS_LED_WRITE_ACTIVE();
  39. #endif
  40. } else {
  41. #if defined(HAPTIC_ENABLE_PIN)
  42. HAPTIC_ENABLE_PIN_WRITE_INACTIVE();
  43. #endif
  44. #if defined(HAPTIC_ENABLE_STATUS_LED)
  45. HAPTIC_ENABLE_STATUS_LED_WRITE_INACTIVE();
  46. #endif
  47. }
  48. }
  49. static void set_haptic_config_enable(bool enabled) {
  50. haptic_config.enable = enabled;
  51. update_haptic_enable_gpios();
  52. }
  53. void haptic_init(void) {
  54. if (!eeconfig_is_enabled()) {
  55. eeconfig_init();
  56. }
  57. haptic_config.raw = eeconfig_read_haptic();
  58. #ifdef SOLENOID_ENABLE
  59. solenoid_set_dwell(haptic_config.dwell);
  60. #endif
  61. if ((haptic_config.raw == 0)
  62. #ifdef SOLENOID_ENABLE
  63. || (haptic_config.dwell == 0)
  64. #endif
  65. ) {
  66. // this will be called, if the eeprom is not corrupt,
  67. // but the previous firmware didn't have haptic enabled,
  68. // or the previous firmware didn't have solenoid enabled,
  69. // and the current one has solenoid enabled.
  70. haptic_reset();
  71. } else {
  72. // Haptic configuration has been loaded through the "raw" union item.
  73. // This is to execute any side effects of the configuration.
  74. set_haptic_config_enable(haptic_config.enable);
  75. }
  76. #ifdef SOLENOID_ENABLE
  77. solenoid_setup();
  78. dprintf("Solenoid driver initialized\n");
  79. #endif
  80. #ifdef DRV2605L
  81. DRV_init();
  82. dprintf("DRV2605 driver initialized\n");
  83. #endif
  84. eeconfig_debug_haptic();
  85. #ifdef HAPTIC_ENABLE_PIN
  86. setPinOutput(HAPTIC_ENABLE_PIN);
  87. #endif
  88. #ifdef HAPTIC_ENABLE_STATUS_LED
  89. setPinOutput(HAPTIC_ENABLE_STATUS_LED);
  90. #endif
  91. }
  92. void haptic_task(void) {
  93. #ifdef SOLENOID_ENABLE
  94. solenoid_check();
  95. #endif
  96. }
  97. void eeconfig_debug_haptic(void) {
  98. dprintf("haptic_config eeprom\n");
  99. dprintf("haptic_config.enable = %d\n", haptic_config.enable);
  100. dprintf("haptic_config.mode = %d\n", haptic_config.mode);
  101. }
  102. void haptic_enable(void) {
  103. set_haptic_config_enable(true);
  104. xprintf("haptic_config.enable = %u\n", haptic_config.enable);
  105. eeconfig_update_haptic(haptic_config.raw);
  106. }
  107. void haptic_disable(void) {
  108. set_haptic_config_enable(false);
  109. xprintf("haptic_config.enable = %u\n", haptic_config.enable);
  110. eeconfig_update_haptic(haptic_config.raw);
  111. }
  112. void haptic_toggle(void) {
  113. if (haptic_config.enable) {
  114. haptic_disable();
  115. } else {
  116. haptic_enable();
  117. }
  118. eeconfig_update_haptic(haptic_config.raw);
  119. }
  120. void haptic_feedback_toggle(void) {
  121. haptic_config.feedback++;
  122. if (haptic_config.feedback >= HAPTIC_FEEDBACK_MAX) haptic_config.feedback = KEY_PRESS;
  123. xprintf("haptic_config.feedback = %u\n", !haptic_config.feedback);
  124. eeconfig_update_haptic(haptic_config.raw);
  125. }
  126. void haptic_buzz_toggle(void) {
  127. bool buzz_stat = !haptic_config.buzz;
  128. haptic_config.buzz = buzz_stat;
  129. haptic_set_buzz(buzz_stat);
  130. }
  131. void haptic_mode_increase(void) {
  132. uint8_t mode = haptic_config.mode + 1;
  133. #ifdef DRV2605L
  134. if (haptic_config.mode >= drv_effect_max) {
  135. mode = 1;
  136. }
  137. #endif
  138. haptic_set_mode(mode);
  139. }
  140. void haptic_mode_decrease(void) {
  141. uint8_t mode = haptic_config.mode - 1;
  142. #ifdef DRV2605L
  143. if (haptic_config.mode < 1) {
  144. mode = (drv_effect_max - 1);
  145. }
  146. #endif
  147. haptic_set_mode(mode);
  148. }
  149. void haptic_dwell_increase(void) {
  150. #ifdef SOLENOID_ENABLE
  151. int16_t next_dwell = ((int16_t)haptic_config.dwell) + SOLENOID_DWELL_STEP_SIZE;
  152. if (haptic_config.dwell >= SOLENOID_MAX_DWELL) {
  153. // if it's already at max, we wrap back to min
  154. next_dwell = SOLENOID_MIN_DWELL;
  155. } else if (next_dwell > SOLENOID_MAX_DWELL) {
  156. // if we overshoot the max, then cap at max
  157. next_dwell = SOLENOID_MAX_DWELL;
  158. }
  159. solenoid_set_dwell(next_dwell);
  160. #else
  161. int16_t next_dwell = ((int16_t)haptic_config.dwell) + 1;
  162. #endif
  163. haptic_set_dwell(next_dwell);
  164. }
  165. void haptic_dwell_decrease(void) {
  166. #ifdef SOLENOID_ENABLE
  167. int16_t next_dwell = ((int16_t)haptic_config.dwell) - SOLENOID_DWELL_STEP_SIZE;
  168. if (haptic_config.dwell <= SOLENOID_MIN_DWELL) {
  169. // if it's already at min, we wrap to max
  170. next_dwell = SOLENOID_MAX_DWELL;
  171. } else if (next_dwell < SOLENOID_MIN_DWELL) {
  172. // if we go below min, then we cap to min
  173. next_dwell = SOLENOID_MIN_DWELL;
  174. }
  175. solenoid_set_dwell(next_dwell);
  176. #else
  177. int16_t next_dwell = ((int16_t)haptic_config.dwell) - 1;
  178. #endif
  179. haptic_set_dwell(next_dwell);
  180. }
  181. void haptic_reset(void) {
  182. set_haptic_config_enable(true);
  183. uint8_t feedback = HAPTIC_FEEDBACK_DEFAULT;
  184. haptic_config.feedback = feedback;
  185. #ifdef DRV2605L
  186. uint8_t mode = HAPTIC_MODE_DEFAULT;
  187. haptic_config.mode = mode;
  188. #endif
  189. #ifdef SOLENOID_ENABLE
  190. uint8_t dwell = SOLENOID_DEFAULT_DWELL;
  191. haptic_config.dwell = dwell;
  192. haptic_config.buzz = SOLENOID_DEFAULT_BUZZ;
  193. solenoid_set_dwell(dwell);
  194. #else
  195. // This is to trigger haptic_reset again, if solenoid is enabled in the future.
  196. haptic_config.dwell = 0;
  197. haptic_config.buzz = 0;
  198. #endif
  199. eeconfig_update_haptic(haptic_config.raw);
  200. xprintf("haptic_config.feedback = %u\n", haptic_config.feedback);
  201. xprintf("haptic_config.mode = %u\n", haptic_config.mode);
  202. }
  203. void haptic_set_feedback(uint8_t feedback) {
  204. haptic_config.feedback = feedback;
  205. eeconfig_update_haptic(haptic_config.raw);
  206. xprintf("haptic_config.feedback = %u\n", haptic_config.feedback);
  207. }
  208. void haptic_set_mode(uint8_t mode) {
  209. haptic_config.mode = mode;
  210. eeconfig_update_haptic(haptic_config.raw);
  211. xprintf("haptic_config.mode = %u\n", haptic_config.mode);
  212. }
  213. void haptic_set_amplitude(uint8_t amp) {
  214. haptic_config.amplitude = amp;
  215. eeconfig_update_haptic(haptic_config.raw);
  216. xprintf("haptic_config.amplitude = %u\n", haptic_config.amplitude);
  217. #ifdef DRV2605L
  218. DRV_amplitude(amp);
  219. #endif
  220. }
  221. void haptic_set_buzz(uint8_t buzz) {
  222. haptic_config.buzz = buzz;
  223. eeconfig_update_haptic(haptic_config.raw);
  224. xprintf("haptic_config.buzz = %u\n", haptic_config.buzz);
  225. }
  226. void haptic_set_dwell(uint8_t dwell) {
  227. haptic_config.dwell = dwell;
  228. eeconfig_update_haptic(haptic_config.raw);
  229. xprintf("haptic_config.dwell = %u\n", haptic_config.dwell);
  230. }
  231. uint8_t haptic_get_enable(void) {
  232. return haptic_config.enable;
  233. }
  234. uint8_t haptic_get_mode(void) {
  235. if (!haptic_config.enable) {
  236. return false;
  237. }
  238. return haptic_config.mode;
  239. }
  240. uint8_t haptic_get_feedback(void) {
  241. if (!haptic_config.enable) {
  242. return false;
  243. }
  244. return haptic_config.feedback;
  245. }
  246. uint8_t haptic_get_dwell(void) {
  247. if (!haptic_config.enable) {
  248. return false;
  249. }
  250. return haptic_config.dwell;
  251. }
  252. void haptic_enable_continuous(void) {
  253. haptic_config.cont = 1;
  254. xprintf("haptic_config.cont = %u\n", haptic_config.cont);
  255. eeconfig_update_haptic(haptic_config.raw);
  256. #ifdef DRV2605L
  257. DRV_rtp_init();
  258. #endif
  259. }
  260. void haptic_disable_continuous(void) {
  261. haptic_config.cont = 0;
  262. xprintf("haptic_config.cont = %u\n", haptic_config.cont);
  263. eeconfig_update_haptic(haptic_config.raw);
  264. #ifdef DRV2605L
  265. DRV_write(DRV_MODE, 0x00);
  266. #endif
  267. }
  268. void haptic_toggle_continuous(void) {
  269. if (haptic_config.cont) {
  270. haptic_disable_continuous();
  271. } else {
  272. haptic_enable_continuous();
  273. }
  274. }
  275. void haptic_cont_increase(void) {
  276. uint8_t amp = haptic_config.amplitude + 10;
  277. if (haptic_config.amplitude >= 120) {
  278. amp = 120;
  279. }
  280. haptic_set_amplitude(amp);
  281. }
  282. void haptic_cont_decrease(void) {
  283. uint8_t amp = haptic_config.amplitude - 10;
  284. if (haptic_config.amplitude < 20) {
  285. amp = 20;
  286. }
  287. haptic_set_amplitude(amp);
  288. }
  289. void haptic_play(void) {
  290. #ifdef DRV2605L
  291. uint8_t play_eff = 0;
  292. play_eff = haptic_config.mode;
  293. DRV_pulse(play_eff);
  294. # if defined(SPLIT_KEYBOARD) && defined(SPLIT_HAPTIC_ENABLE)
  295. split_haptic_play = haptic_config.mode;
  296. # endif
  297. #endif
  298. #ifdef SOLENOID_ENABLE
  299. solenoid_fire_handler();
  300. # if defined(SPLIT_KEYBOARD) && defined(SPLIT_HAPTIC_ENABLE)
  301. split_haptic_play = 1;
  302. # endif
  303. #endif
  304. }
  305. void haptic_shutdown(void) {
  306. #ifdef SOLENOID_ENABLE
  307. solenoid_shutdown();
  308. #endif
  309. }
  310. void haptic_notify_usb_device_state_change(void) {
  311. update_haptic_enable_gpios();
  312. #if defined(HAPTIC_ENABLE_PIN)
  313. setPinOutput(HAPTIC_ENABLE_PIN);
  314. #endif
  315. #if defined(HAPTIC_ENABLE_STATUS_LED)
  316. setPinOutput(HAPTIC_ENABLE_STATUS_LED);
  317. #endif
  318. }