You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

330 lines
8.4 KiB

  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_amplitude(uint8_t amp) {
  155. haptic_config.amplitude = amp;
  156. eeconfig_update_haptic(haptic_config.raw);
  157. xprintf("haptic_config.amplitude = %u\n", haptic_config.amplitude);
  158. #ifdef DRV2605L
  159. DRV_amplitude(amp);
  160. #endif
  161. }
  162. void haptic_set_buzz(uint8_t buzz) {
  163. haptic_config.buzz = buzz;
  164. eeconfig_update_haptic(haptic_config.raw);
  165. xprintf("haptic_config.buzz = %u\n", haptic_config.buzz);
  166. }
  167. void haptic_set_dwell(uint8_t dwell) {
  168. haptic_config.dwell = dwell;
  169. eeconfig_update_haptic(haptic_config.raw);
  170. xprintf("haptic_config.dwell = %u\n", haptic_config.dwell);
  171. }
  172. uint8_t haptic_get_mode(void) {
  173. if (!haptic_config.enable) {
  174. return false;
  175. }
  176. return haptic_config.mode;
  177. }
  178. uint8_t haptic_get_feedback(void) {
  179. if (!haptic_config.enable) {
  180. return false;
  181. }
  182. return haptic_config.feedback;
  183. }
  184. uint8_t haptic_get_dwell(void) {
  185. if (!haptic_config.enable) {
  186. return false;
  187. }
  188. return haptic_config.dwell;
  189. }
  190. void haptic_enable_continuous(void) {
  191. haptic_config.cont = 1;
  192. xprintf("haptic_config.cont = %u\n", haptic_config.cont);
  193. eeconfig_update_haptic(haptic_config.raw);
  194. #ifdef DRV2605L
  195. DRV_rtp_init();
  196. #endif
  197. }
  198. void haptic_disable_continuous(void) {
  199. haptic_config.cont = 0;
  200. xprintf("haptic_config.cont = %u\n", haptic_config.cont);
  201. eeconfig_update_haptic(haptic_config.raw);
  202. #ifdef DRV2605L
  203. DRV_write(DRV_MODE, 0x00);
  204. #endif
  205. }
  206. void haptic_toggle_continuous(void) {
  207. #ifdef DRV2605L
  208. if (haptic_config.cont) {
  209. haptic_disable_continuous();
  210. } else {
  211. haptic_enable_continuous();
  212. }
  213. eeconfig_update_haptic(haptic_config.raw);
  214. #endif
  215. }
  216. void haptic_cont_increase(void) {
  217. uint8_t amp = haptic_config.amplitude + 10;
  218. if (haptic_config.amplitude >= 120) {
  219. amp = 120;
  220. }
  221. haptic_set_amplitude(amp);
  222. }
  223. void haptic_cont_decrease(void) {
  224. uint8_t amp = haptic_config.amplitude - 10;
  225. if (haptic_config.amplitude < 20) {
  226. amp = 20;
  227. }
  228. haptic_set_amplitude(amp);
  229. }
  230. void haptic_play(void) {
  231. #ifdef DRV2605L
  232. uint8_t play_eff = 0;
  233. play_eff = haptic_config.mode;
  234. DRV_pulse(play_eff);
  235. #endif
  236. #ifdef SOLENOID_ENABLE
  237. solenoid_fire();
  238. #endif
  239. }
  240. bool process_haptic(uint16_t keycode, keyrecord_t *record) {
  241. if (keycode == HPT_ON && record->event.pressed) {
  242. haptic_enable();
  243. }
  244. if (keycode == HPT_OFF && record->event.pressed) {
  245. haptic_disable();
  246. }
  247. if (keycode == HPT_TOG && record->event.pressed) {
  248. haptic_toggle();
  249. }
  250. if (keycode == HPT_RST && record->event.pressed) {
  251. haptic_reset();
  252. }
  253. if (keycode == HPT_FBK && record->event.pressed) {
  254. haptic_feedback_toggle();
  255. }
  256. if (keycode == HPT_BUZ && record->event.pressed) {
  257. haptic_buzz_toggle();
  258. }
  259. if (keycode == HPT_MODI && record->event.pressed) {
  260. haptic_mode_increase();
  261. }
  262. if (keycode == HPT_MODD && record->event.pressed) {
  263. haptic_mode_decrease();
  264. }
  265. if (keycode == HPT_DWLI && record->event.pressed) {
  266. haptic_dwell_increase();
  267. }
  268. if (keycode == HPT_DWLD && record->event.pressed) {
  269. haptic_dwell_decrease();
  270. }
  271. if (keycode == HPT_CONT && record->event.pressed) {
  272. haptic_toggle_continuous();
  273. }
  274. if (keycode == HPT_CONI && record->event.pressed) {
  275. haptic_cont_increase();
  276. }
  277. if (keycode == HPT_COND && record->event.pressed) {
  278. haptic_cont_decrease();
  279. }
  280. if (haptic_config.enable) {
  281. if (record->event.pressed) {
  282. // keypress
  283. if (haptic_config.feedback < 2) {
  284. haptic_play();
  285. }
  286. } else {
  287. // keyrelease
  288. if (haptic_config.feedback > 0) {
  289. haptic_play();
  290. }
  291. }
  292. }
  293. return true;
  294. }
  295. void haptic_shutdown(void) {
  296. #ifdef SOLENOID_ENABLE
  297. solenoid_shutdown();
  298. #endif
  299. }