haptic.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. #include "keyboard.h"
  23. #ifdef HAPTIC_DRV2605L
  24. # include "drv2605l.h"
  25. #endif
  26. #ifdef HAPTIC_SOLENOID
  27. # include "solenoid.h"
  28. #endif
  29. #if defined(SPLIT_KEYBOARD) && defined(SPLIT_HAPTIC_ENABLE)
  30. extern uint8_t split_haptic_play;
  31. #endif
  32. haptic_config_t haptic_config;
  33. static void update_haptic_enable_gpios(void) {
  34. if (haptic_config.enable && ((!HAPTIC_OFF_IN_LOW_POWER) || (usb_device_state_get_configure_state() == USB_DEVICE_STATE_CONFIGURED))) {
  35. #if defined(HAPTIC_ENABLE_PIN)
  36. HAPTIC_ENABLE_PIN_WRITE_ACTIVE();
  37. #endif
  38. #if defined(HAPTIC_ENABLE_STATUS_LED)
  39. HAPTIC_ENABLE_STATUS_LED_WRITE_ACTIVE();
  40. #endif
  41. } else {
  42. #if defined(HAPTIC_ENABLE_PIN)
  43. HAPTIC_ENABLE_PIN_WRITE_INACTIVE();
  44. #endif
  45. #if defined(HAPTIC_ENABLE_STATUS_LED)
  46. HAPTIC_ENABLE_STATUS_LED_WRITE_INACTIVE();
  47. #endif
  48. }
  49. }
  50. static void set_haptic_config_enable(bool enabled) {
  51. haptic_config.enable = enabled;
  52. update_haptic_enable_gpios();
  53. }
  54. void haptic_init(void) {
  55. // only initialize on secondary boards if the user desires
  56. #if defined(SPLIT_KEYBOARD) && !defined(SPLIT_HAPTIC_ENABLE)
  57. if (!is_keyboard_master()) return;
  58. #endif
  59. if (!eeconfig_is_enabled()) {
  60. eeconfig_init();
  61. }
  62. eeconfig_read_haptic(&haptic_config);
  63. #ifdef HAPTIC_SOLENOID
  64. solenoid_set_dwell(haptic_config.dwell);
  65. #endif
  66. if ((haptic_config.raw == 0)
  67. #ifdef HAPTIC_SOLENOID
  68. || (haptic_config.dwell == 0)
  69. #endif
  70. ) {
  71. // this will be called, if the eeprom is not corrupt,
  72. // but the previous firmware didn't have haptic enabled,
  73. // or the previous firmware didn't have solenoid enabled,
  74. // and the current one has solenoid enabled.
  75. haptic_reset();
  76. } else {
  77. // Haptic configuration has been loaded through the "raw" union item.
  78. // This is to execute any side effects of the configuration.
  79. set_haptic_config_enable(haptic_config.enable);
  80. }
  81. #ifdef HAPTIC_SOLENOID
  82. solenoid_setup();
  83. dprintf("Solenoid driver initialized\n");
  84. #endif
  85. #ifdef HAPTIC_DRV2605L
  86. drv2605l_init();
  87. dprintf("DRV2605 driver initialized\n");
  88. #endif
  89. eeconfig_debug_haptic();
  90. #ifdef HAPTIC_ENABLE_PIN
  91. gpio_set_pin_output(HAPTIC_ENABLE_PIN);
  92. #endif
  93. #ifdef HAPTIC_ENABLE_STATUS_LED
  94. gpio_set_pin_output(HAPTIC_ENABLE_STATUS_LED);
  95. #endif
  96. }
  97. void haptic_task(void) {
  98. #ifdef HAPTIC_SOLENOID
  99. // Only run task on seconary boards if the user desires
  100. # if defined(SPLIT_KEYBOARD) && !defined(SPLIT_HAPTIC_ENABLE)
  101. if (!is_keyboard_master()) return;
  102. # endif
  103. solenoid_check();
  104. #endif // HAPTIC_SOLENOID
  105. }
  106. void eeconfig_debug_haptic(void) {
  107. dprintf("haptic_config eeprom\n");
  108. dprintf("haptic_config.enable = %d\n", haptic_config.enable);
  109. dprintf("haptic_config.mode = %d\n", haptic_config.mode);
  110. }
  111. void haptic_enable(void) {
  112. set_haptic_config_enable(true);
  113. dprintf("haptic_config.enable = %u\n", haptic_config.enable);
  114. eeconfig_update_haptic(&haptic_config);
  115. }
  116. void haptic_disable(void) {
  117. set_haptic_config_enable(false);
  118. dprintf("haptic_config.enable = %u\n", haptic_config.enable);
  119. eeconfig_update_haptic(&haptic_config);
  120. }
  121. void haptic_toggle(void) {
  122. if (haptic_config.enable) {
  123. haptic_disable();
  124. } else {
  125. haptic_enable();
  126. }
  127. }
  128. void haptic_feedback_toggle(void) {
  129. haptic_config.feedback++;
  130. if (haptic_config.feedback >= HAPTIC_FEEDBACK_MAX) haptic_config.feedback = KEY_PRESS;
  131. dprintf("haptic_config.feedback = %u\n", !haptic_config.feedback);
  132. eeconfig_update_haptic(&haptic_config);
  133. }
  134. void haptic_buzz_toggle(void) {
  135. bool buzz_stat = !haptic_config.buzz;
  136. haptic_config.buzz = buzz_stat;
  137. haptic_set_buzz(buzz_stat);
  138. }
  139. void haptic_mode_increase(void) {
  140. uint8_t mode = haptic_config.mode + 1;
  141. #ifdef HAPTIC_DRV2605L
  142. if (haptic_config.mode >= DRV2605L_EFFECT_COUNT) {
  143. mode = 1;
  144. }
  145. #endif
  146. haptic_set_mode(mode);
  147. }
  148. void haptic_mode_decrease(void) {
  149. uint8_t mode = haptic_config.mode - 1;
  150. #ifdef HAPTIC_DRV2605L
  151. if (haptic_config.mode < 1) {
  152. mode = (DRV2605L_EFFECT_COUNT - 1);
  153. }
  154. #endif
  155. haptic_set_mode(mode);
  156. }
  157. void haptic_dwell_increase(void) {
  158. #ifdef HAPTIC_SOLENOID
  159. int16_t next_dwell = ((int16_t)haptic_config.dwell) + SOLENOID_DWELL_STEP_SIZE;
  160. if (haptic_config.dwell >= SOLENOID_MAX_DWELL) {
  161. // if it's already at max, we wrap back to min
  162. next_dwell = SOLENOID_MIN_DWELL;
  163. } else if (next_dwell > SOLENOID_MAX_DWELL) {
  164. // if we overshoot the max, then cap at max
  165. next_dwell = SOLENOID_MAX_DWELL;
  166. }
  167. solenoid_set_dwell(next_dwell);
  168. #else
  169. int16_t next_dwell = ((int16_t)haptic_config.dwell) + 1;
  170. #endif
  171. haptic_set_dwell(next_dwell);
  172. }
  173. void haptic_dwell_decrease(void) {
  174. #ifdef HAPTIC_SOLENOID
  175. int16_t next_dwell = ((int16_t)haptic_config.dwell) - SOLENOID_DWELL_STEP_SIZE;
  176. if (haptic_config.dwell <= SOLENOID_MIN_DWELL) {
  177. // if it's already at min, we wrap to max
  178. next_dwell = SOLENOID_MAX_DWELL;
  179. } else if (next_dwell < SOLENOID_MIN_DWELL) {
  180. // if we go below min, then we cap to min
  181. next_dwell = SOLENOID_MIN_DWELL;
  182. }
  183. solenoid_set_dwell(next_dwell);
  184. #else
  185. int16_t next_dwell = ((int16_t)haptic_config.dwell) - 1;
  186. #endif
  187. haptic_set_dwell(next_dwell);
  188. }
  189. void haptic_reset(void) {
  190. set_haptic_config_enable(true);
  191. uint8_t feedback = HAPTIC_DEFAULT_FEEDBACK;
  192. haptic_config.feedback = feedback;
  193. #ifdef HAPTIC_DRV2605L
  194. uint8_t mode = HAPTIC_DEFAULT_MODE;
  195. haptic_config.mode = mode;
  196. #endif
  197. #ifdef HAPTIC_SOLENOID
  198. uint8_t dwell = SOLENOID_DEFAULT_DWELL;
  199. haptic_config.dwell = dwell;
  200. haptic_config.buzz = SOLENOID_DEFAULT_BUZZ;
  201. solenoid_set_dwell(dwell);
  202. #else
  203. // This is to trigger haptic_reset again, if solenoid is enabled in the future.
  204. haptic_config.dwell = 0;
  205. haptic_config.buzz = 0;
  206. #endif
  207. eeconfig_update_haptic(&haptic_config);
  208. dprintf("haptic_config.feedback = %u\n", haptic_config.feedback);
  209. dprintf("haptic_config.mode = %u\n", haptic_config.mode);
  210. }
  211. void haptic_set_feedback(uint8_t feedback) {
  212. haptic_config.feedback = feedback;
  213. eeconfig_update_haptic(&haptic_config);
  214. dprintf("haptic_config.feedback = %u\n", haptic_config.feedback);
  215. }
  216. void haptic_set_mode(uint8_t mode) {
  217. haptic_config.mode = mode;
  218. eeconfig_update_haptic(&haptic_config);
  219. dprintf("haptic_config.mode = %u\n", haptic_config.mode);
  220. }
  221. void haptic_set_amplitude(uint8_t amp) {
  222. haptic_config.amplitude = amp;
  223. eeconfig_update_haptic(&haptic_config);
  224. dprintf("haptic_config.amplitude = %u\n", haptic_config.amplitude);
  225. #ifdef HAPTIC_DRV2605L
  226. drv2605l_amplitude(amp);
  227. #endif
  228. }
  229. void haptic_set_buzz(uint8_t buzz) {
  230. haptic_config.buzz = buzz;
  231. eeconfig_update_haptic(&haptic_config);
  232. dprintf("haptic_config.buzz = %u\n", haptic_config.buzz);
  233. }
  234. void haptic_set_dwell(uint8_t dwell) {
  235. haptic_config.dwell = dwell;
  236. eeconfig_update_haptic(&haptic_config);
  237. dprintf("haptic_config.dwell = %u\n", haptic_config.dwell);
  238. }
  239. uint8_t haptic_get_enable(void) {
  240. return haptic_config.enable;
  241. }
  242. uint8_t haptic_get_mode(void) {
  243. if (!haptic_config.enable) {
  244. return false;
  245. }
  246. return haptic_config.mode;
  247. }
  248. uint8_t haptic_get_feedback(void) {
  249. if (!haptic_config.enable) {
  250. return false;
  251. }
  252. return haptic_config.feedback;
  253. }
  254. uint8_t haptic_get_dwell(void) {
  255. if (!haptic_config.enable) {
  256. return false;
  257. }
  258. return haptic_config.dwell;
  259. }
  260. void haptic_enable_continuous(void) {
  261. haptic_config.cont = 1;
  262. dprintf("haptic_config.cont = %u\n", haptic_config.cont);
  263. eeconfig_update_haptic(&haptic_config);
  264. #ifdef HAPTIC_DRV2605L
  265. drv2605l_rtp_init();
  266. #endif
  267. }
  268. void haptic_disable_continuous(void) {
  269. haptic_config.cont = 0;
  270. dprintf("haptic_config.cont = %u\n", haptic_config.cont);
  271. eeconfig_update_haptic(&haptic_config);
  272. #ifdef HAPTIC_DRV2605L
  273. drv2605l_write(DRV2605L_REG_MODE, 0x00);
  274. #endif
  275. }
  276. void haptic_toggle_continuous(void) {
  277. if (haptic_config.cont) {
  278. haptic_disable_continuous();
  279. } else {
  280. haptic_enable_continuous();
  281. }
  282. }
  283. void haptic_cont_increase(void) {
  284. uint8_t amp = haptic_config.amplitude + 10;
  285. if (haptic_config.amplitude >= 120) {
  286. amp = 120;
  287. }
  288. haptic_set_amplitude(amp);
  289. }
  290. void haptic_cont_decrease(void) {
  291. uint8_t amp = haptic_config.amplitude - 10;
  292. if (haptic_config.amplitude < 20) {
  293. amp = 20;
  294. }
  295. haptic_set_amplitude(amp);
  296. }
  297. void haptic_play(void) {
  298. #ifdef HAPTIC_DRV2605L
  299. uint8_t play_eff = 0;
  300. play_eff = haptic_config.mode;
  301. drv2605l_pulse(play_eff);
  302. # if defined(SPLIT_KEYBOARD) && defined(SPLIT_HAPTIC_ENABLE)
  303. split_haptic_play = haptic_config.mode;
  304. # endif
  305. #endif
  306. #ifdef HAPTIC_SOLENOID
  307. solenoid_fire_handler();
  308. # if defined(SPLIT_KEYBOARD) && defined(SPLIT_HAPTIC_ENABLE)
  309. split_haptic_play = 1;
  310. # endif
  311. #endif
  312. }
  313. void haptic_shutdown(void) {
  314. #ifdef HAPTIC_SOLENOID
  315. solenoid_shutdown();
  316. #endif
  317. }
  318. void haptic_notify_usb_device_state_change(void) {
  319. update_haptic_enable_gpios();
  320. #if defined(HAPTIC_ENABLE_PIN)
  321. gpio_set_pin_output(HAPTIC_ENABLE_PIN);
  322. #endif
  323. #if defined(HAPTIC_ENABLE_STATUS_LED)
  324. gpio_set_pin_output(HAPTIC_ENABLE_STATUS_LED);
  325. #endif
  326. }