haptic.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 "progmem.h"
  20. #include "debug.h"
  21. #ifdef DRV2605L
  22. # include "DRV2605L.h"
  23. #endif
  24. #ifdef SOLENOID_ENABLE
  25. # include "solenoid.h"
  26. #endif
  27. haptic_config_t haptic_config;
  28. void haptic_init(void) {
  29. debug_enable = 1; // Debug is ON!
  30. if (!eeconfig_is_enabled()) {
  31. eeconfig_init();
  32. }
  33. haptic_config.raw = eeconfig_read_haptic();
  34. if (haptic_config.mode < 1) {
  35. haptic_config.mode = 1;
  36. }
  37. if (!haptic_config.mode) {
  38. dprintf("No haptic config found in eeprom, setting default configs\n");
  39. haptic_reset();
  40. }
  41. #ifdef SOLENOID_ENABLE
  42. solenoid_setup();
  43. dprintf("Solenoid driver initialized\n");
  44. #endif
  45. #ifdef DRV2605L
  46. DRV_init();
  47. dprintf("DRV2605 driver initialized\n");
  48. #endif
  49. eeconfig_debug_haptic();
  50. }
  51. void haptic_task(void) {
  52. #ifdef SOLENOID_ENABLE
  53. solenoid_check();
  54. #endif
  55. }
  56. void eeconfig_debug_haptic(void) {
  57. dprintf("haptic_config eprom\n");
  58. dprintf("haptic_config.enable = %d\n", haptic_config.enable);
  59. dprintf("haptic_config.mode = %d\n", haptic_config.mode);
  60. }
  61. void haptic_enable(void) {
  62. haptic_config.enable = 1;
  63. xprintf("haptic_config.enable = %u\n", haptic_config.enable);
  64. eeconfig_update_haptic(haptic_config.raw);
  65. }
  66. void haptic_disable(void) {
  67. haptic_config.enable = 0;
  68. xprintf("haptic_config.enable = %u\n", haptic_config.enable);
  69. eeconfig_update_haptic(haptic_config.raw);
  70. }
  71. void haptic_toggle(void) {
  72. if (haptic_config.enable) {
  73. haptic_disable();
  74. } else {
  75. haptic_enable();
  76. }
  77. eeconfig_update_haptic(haptic_config.raw);
  78. }
  79. void haptic_feedback_toggle(void) {
  80. haptic_config.feedback++;
  81. if (haptic_config.feedback >= HAPTIC_FEEDBACK_MAX) haptic_config.feedback = KEY_PRESS;
  82. xprintf("haptic_config.feedback = %u\n", !haptic_config.feedback);
  83. eeconfig_update_haptic(haptic_config.raw);
  84. }
  85. void haptic_buzz_toggle(void) {
  86. bool buzz_stat = !haptic_config.buzz;
  87. haptic_config.buzz = buzz_stat;
  88. haptic_set_buzz(buzz_stat);
  89. }
  90. void haptic_mode_increase(void) {
  91. uint8_t mode = haptic_config.mode + 1;
  92. #ifdef DRV2605L
  93. if (haptic_config.mode >= drv_effect_max) {
  94. mode = 1;
  95. }
  96. #endif
  97. haptic_set_mode(mode);
  98. }
  99. void haptic_mode_decrease(void) {
  100. uint8_t mode = haptic_config.mode - 1;
  101. #ifdef DRV2605L
  102. if (haptic_config.mode < 1) {
  103. mode = (drv_effect_max - 1);
  104. }
  105. #endif
  106. haptic_set_mode(mode);
  107. }
  108. void haptic_dwell_increase(void) {
  109. uint8_t dwell = haptic_config.dwell + 1;
  110. #ifdef SOLENOID_ENABLE
  111. if (haptic_config.dwell >= SOLENOID_MAX_DWELL) {
  112. dwell = 1;
  113. }
  114. solenoid_set_dwell(dwell);
  115. #endif
  116. haptic_set_dwell(dwell);
  117. }
  118. void haptic_dwell_decrease(void) {
  119. uint8_t dwell = haptic_config.dwell - 1;
  120. #ifdef SOLENOID_ENABLE
  121. if (haptic_config.dwell < SOLENOID_MIN_DWELL) {
  122. dwell = SOLENOID_MAX_DWELL;
  123. }
  124. solenoid_set_dwell(dwell);
  125. #endif
  126. haptic_set_dwell(dwell);
  127. }
  128. void haptic_reset(void) {
  129. haptic_config.enable = true;
  130. uint8_t feedback = HAPTIC_FEEDBACK_DEFAULT;
  131. haptic_config.feedback = feedback;
  132. #ifdef DRV2605L
  133. uint8_t mode = HAPTIC_MODE_DEFAULT;
  134. haptic_config.mode = mode;
  135. #endif
  136. #ifdef SOLENOID_ENABLE
  137. uint8_t dwell = SOLENOID_DEFAULT_DWELL;
  138. haptic_config.dwell = dwell;
  139. #endif
  140. eeconfig_update_haptic(haptic_config.raw);
  141. xprintf("haptic_config.feedback = %u\n", haptic_config.feedback);
  142. xprintf("haptic_config.mode = %u\n", haptic_config.mode);
  143. }
  144. void haptic_set_feedback(uint8_t feedback) {
  145. haptic_config.feedback = feedback;
  146. eeconfig_update_haptic(haptic_config.raw);
  147. xprintf("haptic_config.feedback = %u\n", haptic_config.feedback);
  148. }
  149. void haptic_set_mode(uint8_t mode) {
  150. haptic_config.mode = mode;
  151. eeconfig_update_haptic(haptic_config.raw);
  152. xprintf("haptic_config.mode = %u\n", haptic_config.mode);
  153. }
  154. void haptic_set_buzz(uint8_t buzz) {
  155. haptic_config.buzz = buzz;
  156. eeconfig_update_haptic(haptic_config.raw);
  157. xprintf("haptic_config.buzz = %u\n", haptic_config.buzz);
  158. }
  159. void haptic_set_dwell(uint8_t dwell) {
  160. haptic_config.dwell = dwell;
  161. eeconfig_update_haptic(haptic_config.raw);
  162. xprintf("haptic_config.dwell = %u\n", haptic_config.dwell);
  163. }
  164. uint8_t haptic_get_mode(void) {
  165. if (!haptic_config.enable) {
  166. return false;
  167. }
  168. return haptic_config.mode;
  169. }
  170. uint8_t haptic_get_feedback(void) {
  171. if (!haptic_config.enable) {
  172. return false;
  173. }
  174. return haptic_config.feedback;
  175. }
  176. uint8_t haptic_get_dwell(void) {
  177. if (!haptic_config.enable) {
  178. return false;
  179. }
  180. return haptic_config.dwell;
  181. }
  182. void haptic_play(void) {
  183. #ifdef DRV2605L
  184. uint8_t play_eff = 0;
  185. play_eff = haptic_config.mode;
  186. DRV_pulse(play_eff);
  187. #endif
  188. #ifdef SOLENOID_ENABLE
  189. solenoid_fire();
  190. #endif
  191. }
  192. bool process_haptic(uint16_t keycode, keyrecord_t *record) {
  193. if (keycode == HPT_ON && record->event.pressed) {
  194. haptic_enable();
  195. }
  196. if (keycode == HPT_OFF && record->event.pressed) {
  197. haptic_disable();
  198. }
  199. if (keycode == HPT_TOG && record->event.pressed) {
  200. haptic_toggle();
  201. }
  202. if (keycode == HPT_RST && record->event.pressed) {
  203. haptic_reset();
  204. }
  205. if (keycode == HPT_FBK && record->event.pressed) {
  206. haptic_feedback_toggle();
  207. }
  208. if (keycode == HPT_BUZ && record->event.pressed) {
  209. haptic_buzz_toggle();
  210. }
  211. if (keycode == HPT_MODI && record->event.pressed) {
  212. haptic_mode_increase();
  213. }
  214. if (keycode == HPT_MODD && record->event.pressed) {
  215. haptic_mode_decrease();
  216. }
  217. if (keycode == HPT_DWLI && record->event.pressed) {
  218. haptic_dwell_increase();
  219. }
  220. if (keycode == HPT_DWLD && record->event.pressed) {
  221. haptic_dwell_decrease();
  222. }
  223. if (haptic_config.enable) {
  224. if (record->event.pressed) {
  225. // keypress
  226. if (haptic_config.feedback < 2) {
  227. haptic_play();
  228. }
  229. } else {
  230. // keyrelease
  231. if (haptic_config.feedback > 0) {
  232. haptic_play();
  233. }
  234. }
  235. }
  236. return true;
  237. }
  238. void haptic_shutdown(void) {
  239. #ifdef SOLENOID_ENABLE
  240. solenoid_shutdown();
  241. #endif
  242. }