haptic.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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)
  82. haptic_config.feedback = KEY_PRESS;
  83. xprintf("haptic_config.feedback = %u\n", !haptic_config.feedback);
  84. eeconfig_update_haptic(haptic_config.raw);
  85. }
  86. void haptic_buzz_toggle(void) {
  87. bool buzz_stat = !haptic_config.buzz;
  88. haptic_config.buzz = buzz_stat;
  89. haptic_set_buzz(buzz_stat);
  90. }
  91. void haptic_mode_increase(void) {
  92. uint8_t mode = haptic_config.mode + 1;
  93. #ifdef DRV2605L
  94. if (haptic_config.mode >= drv_effect_max) {
  95. mode = 1;
  96. }
  97. #endif
  98. haptic_set_mode(mode);
  99. }
  100. void haptic_mode_decrease(void) {
  101. uint8_t mode = haptic_config.mode -1;
  102. #ifdef DRV2605L
  103. if (haptic_config.mode < 1) {
  104. mode = (drv_effect_max - 1);
  105. }
  106. #endif
  107. haptic_set_mode(mode);
  108. }
  109. void haptic_dwell_increase(void) {
  110. uint8_t dwell = haptic_config.dwell + 1;
  111. #ifdef SOLENOID_ENABLE
  112. if (haptic_config.dwell >= SOLENOID_MAX_DWELL) {
  113. dwell = 1;
  114. }
  115. solenoid_set_dwell(dwell);
  116. #endif
  117. haptic_set_dwell(dwell);
  118. }
  119. void haptic_dwell_decrease(void) {
  120. uint8_t dwell = haptic_config.dwell -1;
  121. #ifdef SOLENOID_ENABLE
  122. if (haptic_config.dwell < SOLENOID_MIN_DWELL) {
  123. dwell = SOLENOID_MAX_DWELL;
  124. }
  125. solenoid_set_dwell(dwell);
  126. #endif
  127. haptic_set_dwell(dwell);
  128. }
  129. void haptic_reset(void){
  130. haptic_config.enable = true;
  131. uint8_t feedback = HAPTIC_FEEDBACK_DEFAULT;
  132. haptic_config.feedback = feedback;
  133. #ifdef DRV2605L
  134. uint8_t mode = HAPTIC_MODE_DEFAULT;
  135. haptic_config.mode = mode;
  136. #endif
  137. #ifdef SOLENOID_ENABLE
  138. uint8_t dwell = SOLENOID_DEFAULT_DWELL;
  139. haptic_config.dwell = dwell;
  140. #endif
  141. eeconfig_update_haptic(haptic_config.raw);
  142. xprintf("haptic_config.feedback = %u\n", haptic_config.feedback);
  143. xprintf("haptic_config.mode = %u\n", haptic_config.mode);
  144. }
  145. void haptic_set_feedback(uint8_t feedback) {
  146. haptic_config.feedback = feedback;
  147. eeconfig_update_haptic(haptic_config.raw);
  148. xprintf("haptic_config.feedback = %u\n", haptic_config.feedback);
  149. }
  150. void haptic_set_mode(uint8_t mode) {
  151. haptic_config.mode = mode;
  152. eeconfig_update_haptic(haptic_config.raw);
  153. xprintf("haptic_config.mode = %u\n", haptic_config.mode);
  154. }
  155. void haptic_set_buzz(uint8_t buzz) {
  156. haptic_config.buzz = buzz;
  157. eeconfig_update_haptic(haptic_config.raw);
  158. xprintf("haptic_config.buzz = %u\n", haptic_config.buzz);
  159. }
  160. void haptic_set_dwell(uint8_t dwell) {
  161. haptic_config.dwell = dwell;
  162. eeconfig_update_haptic(haptic_config.raw);
  163. xprintf("haptic_config.dwell = %u\n", haptic_config.dwell);
  164. }
  165. uint8_t haptic_get_mode(void) {
  166. if (!haptic_config.enable){
  167. return false;
  168. }
  169. return haptic_config.mode;
  170. }
  171. uint8_t haptic_get_feedback(void) {
  172. if (!haptic_config.enable){
  173. return false;
  174. }
  175. return haptic_config.feedback;
  176. }
  177. uint8_t haptic_get_dwell(void) {
  178. if (!haptic_config.enable){
  179. return false;
  180. }
  181. return haptic_config.dwell;
  182. }
  183. void haptic_play(void) {
  184. #ifdef DRV2605L
  185. uint8_t play_eff = 0;
  186. play_eff = haptic_config.mode;
  187. DRV_pulse(play_eff);
  188. #endif
  189. #ifdef SOLENOID_ENABLE
  190. solenoid_fire();
  191. #endif
  192. }
  193. bool process_haptic(uint16_t keycode, keyrecord_t *record) {
  194. if (keycode == HPT_ON && record->event.pressed) { haptic_enable(); }
  195. if (keycode == HPT_OFF && record->event.pressed) { haptic_disable(); }
  196. if (keycode == HPT_TOG && record->event.pressed) { haptic_toggle(); }
  197. if (keycode == HPT_RST && record->event.pressed) { haptic_reset(); }
  198. if (keycode == HPT_FBK && record->event.pressed) { haptic_feedback_toggle(); }
  199. if (keycode == HPT_BUZ && record->event.pressed) { haptic_buzz_toggle(); }
  200. if (keycode == HPT_MODI && record->event.pressed) { haptic_mode_increase(); }
  201. if (keycode == HPT_MODD && record->event.pressed) { haptic_mode_decrease(); }
  202. if (keycode == HPT_DWLI && record->event.pressed) { haptic_dwell_increase(); }
  203. if (keycode == HPT_DWLD && record->event.pressed) { haptic_dwell_decrease(); }
  204. if (haptic_config.enable) {
  205. if ( record->event.pressed ) {
  206. // keypress
  207. if (haptic_config.feedback < 2) {
  208. haptic_play();
  209. }
  210. } else {
  211. //keyrelease
  212. if (haptic_config.feedback > 0) {
  213. haptic_play();
  214. }
  215. }
  216. }
  217. return true;
  218. }
  219. void haptic_shutdown(void) {
  220. #ifdef SOLENOID_ENABLE
  221. solenoid_shutdown();
  222. #endif
  223. }