aboutsummaryrefslogtreecommitdiff
path: root/drivers
diff options
context:
space:
mode:
authorJoel Challis <git@zvecr.com>2021-07-26 03:14:58 +0100
committerGitHub <noreply@github.com>2021-07-25 19:14:58 -0700
commitf945c352e7db3fedb16c90f9f176b3df6e0b62ae (patch)
tree8b2cd252ad036b5199603c9c755c01be5cb326a2 /drivers
parent4bb595f94b5c77e7a961ff69d2b4d9c53a9094fc (diff)
downloadqmk_firmware-f945c352e7db3fedb16c90f9f176b3df6e0b62ae.tar.gz
qmk_firmware-f945c352e7db3fedb16c90f9f176b3df6e0b62ae.zip
Haptic: driver-> feature (#13713)
Diffstat (limited to 'drivers')
-rw-r--r--drivers/haptic/haptic.c422
-rw-r--r--drivers/haptic/haptic.h81
2 files changed, 0 insertions, 503 deletions
diff --git a/drivers/haptic/haptic.c b/drivers/haptic/haptic.c
deleted file mode 100644
index 3fab1be1a..000000000
--- a/drivers/haptic/haptic.c
+++ /dev/null
@@ -1,422 +0,0 @@
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
28haptic_config_t haptic_config;
29
30void haptic_init(void) {
31 debug_enable = 1; // Debug is ON!
32 if (!eeconfig_is_enabled()) {
33 eeconfig_init();
34 }
35 haptic_config.raw = eeconfig_read_haptic();
36#ifdef SOLENOID_ENABLE
37 solenoid_set_dwell(haptic_config.dwell);
38#endif
39 if ((haptic_config.raw == 0)
40#ifdef SOLENOID_ENABLE
41 || (haptic_config.dwell == 0)
42#endif
43 ) {
44 // this will be called, if the eeprom is not corrupt,
45 // but the previous firmware didn't have haptic enabled,
46 // or the previous firmware didn't have solenoid enabled,
47 // and the current one has solenoid enabled.
48 haptic_reset();
49 }
50#ifdef SOLENOID_ENABLE
51 solenoid_setup();
52 dprintf("Solenoid driver initialized\n");
53#endif
54#ifdef DRV2605L
55 DRV_init();
56 dprintf("DRV2605 driver initialized\n");
57#endif
58 eeconfig_debug_haptic();
59}
60
61void haptic_task(void) {
62#ifdef SOLENOID_ENABLE
63 solenoid_check();
64#endif
65}
66
67void eeconfig_debug_haptic(void) {
68 dprintf("haptic_config eprom\n");
69 dprintf("haptic_config.enable = %d\n", haptic_config.enable);
70 dprintf("haptic_config.mode = %d\n", haptic_config.mode);
71}
72
73void haptic_enable(void) {
74 haptic_config.enable = 1;
75 xprintf("haptic_config.enable = %u\n", haptic_config.enable);
76 eeconfig_update_haptic(haptic_config.raw);
77}
78
79void haptic_disable(void) {
80 haptic_config.enable = 0;
81 xprintf("haptic_config.enable = %u\n", haptic_config.enable);
82 eeconfig_update_haptic(haptic_config.raw);
83}
84
85void haptic_toggle(void) {
86 if (haptic_config.enable) {
87 haptic_disable();
88 } else {
89 haptic_enable();
90 }
91 eeconfig_update_haptic(haptic_config.raw);
92}
93
94void haptic_feedback_toggle(void) {
95 haptic_config.feedback++;
96 if (haptic_config.feedback >= HAPTIC_FEEDBACK_MAX) haptic_config.feedback = KEY_PRESS;
97 xprintf("haptic_config.feedback = %u\n", !haptic_config.feedback);
98 eeconfig_update_haptic(haptic_config.raw);
99}
100
101void haptic_buzz_toggle(void) {
102 bool buzz_stat = !haptic_config.buzz;
103 haptic_config.buzz = buzz_stat;
104 haptic_set_buzz(buzz_stat);
105}
106
107void haptic_mode_increase(void) {
108 uint8_t mode = haptic_config.mode + 1;
109#ifdef DRV2605L
110 if (haptic_config.mode >= drv_effect_max) {
111 mode = 1;
112 }
113#endif
114 haptic_set_mode(mode);
115}
116
117void haptic_mode_decrease(void) {
118 uint8_t mode = haptic_config.mode - 1;
119#ifdef DRV2605L
120 if (haptic_config.mode < 1) {
121 mode = (drv_effect_max - 1);
122 }
123#endif
124 haptic_set_mode(mode);
125}
126
127void haptic_dwell_increase(void) {
128#ifdef SOLENOID_ENABLE
129 int16_t next_dwell = ((int16_t)haptic_config.dwell) + SOLENOID_DWELL_STEP_SIZE;
130 if (haptic_config.dwell >= SOLENOID_MAX_DWELL) {
131 // if it's already at max, we wrap back to min
132 next_dwell = SOLENOID_MIN_DWELL;
133 } else if (next_dwell > SOLENOID_MAX_DWELL) {
134 // if we overshoot the max, then cap at max
135 next_dwell = SOLENOID_MAX_DWELL;
136 }
137 solenoid_set_dwell(next_dwell);
138#else
139 int16_t next_dwell = ((int16_t)haptic_config.dwell) + 1;
140#endif
141 haptic_set_dwell(next_dwell);
142}
143
144void haptic_dwell_decrease(void) {
145#ifdef SOLENOID_ENABLE
146 int16_t next_dwell = ((int16_t)haptic_config.dwell) - SOLENOID_DWELL_STEP_SIZE;
147 if (haptic_config.dwell <= SOLENOID_MIN_DWELL) {
148 // if it's already at min, we wrap to max
149 next_dwell = SOLENOID_MAX_DWELL;
150 } else if (next_dwell < SOLENOID_MIN_DWELL) {
151 // if we go below min, then we cap to min
152 next_dwell = SOLENOID_MIN_DWELL;
153 }
154 solenoid_set_dwell(next_dwell);
155#else
156 int16_t next_dwell = ((int16_t)haptic_config.dwell) - 1;
157#endif
158 haptic_set_dwell(next_dwell);
159}
160
161void haptic_reset(void) {
162 haptic_config.enable = true;
163 uint8_t feedback = HAPTIC_FEEDBACK_DEFAULT;
164 haptic_config.feedback = feedback;
165#ifdef DRV2605L
166 uint8_t mode = HAPTIC_MODE_DEFAULT;
167 haptic_config.mode = mode;
168#endif
169#ifdef SOLENOID_ENABLE
170 uint8_t dwell = SOLENOID_DEFAULT_DWELL;
171 haptic_config.dwell = dwell;
172 haptic_config.buzz = SOLENOID_DEFAULT_BUZZ;
173 solenoid_set_dwell(dwell);
174#else
175 // This is to trigger haptic_reset again, if solenoid is enabled in the future.
176 haptic_config.dwell = 0;
177 haptic_config.buzz = 0;
178#endif
179 eeconfig_update_haptic(haptic_config.raw);
180 xprintf("haptic_config.feedback = %u\n", haptic_config.feedback);
181 xprintf("haptic_config.mode = %u\n", haptic_config.mode);
182}
183
184void haptic_set_feedback(uint8_t feedback) {
185 haptic_config.feedback = feedback;
186 eeconfig_update_haptic(haptic_config.raw);
187 xprintf("haptic_config.feedback = %u\n", haptic_config.feedback);
188}
189
190void haptic_set_mode(uint8_t mode) {
191 haptic_config.mode = mode;
192 eeconfig_update_haptic(haptic_config.raw);
193 xprintf("haptic_config.mode = %u\n", haptic_config.mode);
194}
195
196void haptic_set_amplitude(uint8_t amp) {
197 haptic_config.amplitude = amp;
198 eeconfig_update_haptic(haptic_config.raw);
199 xprintf("haptic_config.amplitude = %u\n", haptic_config.amplitude);
200#ifdef DRV2605L
201 DRV_amplitude(amp);
202#endif
203}
204
205void haptic_set_buzz(uint8_t buzz) {
206 haptic_config.buzz = buzz;
207 eeconfig_update_haptic(haptic_config.raw);
208 xprintf("haptic_config.buzz = %u\n", haptic_config.buzz);
209}
210
211void haptic_set_dwell(uint8_t dwell) {
212 haptic_config.dwell = dwell;
213 eeconfig_update_haptic(haptic_config.raw);
214 xprintf("haptic_config.dwell = %u\n", haptic_config.dwell);
215}
216
217uint8_t haptic_get_mode(void) {
218 if (!haptic_config.enable) {
219 return false;
220 }
221 return haptic_config.mode;
222}
223
224uint8_t haptic_get_feedback(void) {
225 if (!haptic_config.enable) {
226 return false;
227 }
228 return haptic_config.feedback;
229}
230
231uint8_t haptic_get_dwell(void) {
232 if (!haptic_config.enable) {
233 return false;
234 }
235 return haptic_config.dwell;
236}
237
238void haptic_enable_continuous(void) {
239 haptic_config.cont = 1;
240 xprintf("haptic_config.cont = %u\n", haptic_config.cont);
241 eeconfig_update_haptic(haptic_config.raw);
242#ifdef DRV2605L
243 DRV_rtp_init();
244#endif
245}
246
247void haptic_disable_continuous(void) {
248 haptic_config.cont = 0;
249 xprintf("haptic_config.cont = %u\n", haptic_config.cont);
250 eeconfig_update_haptic(haptic_config.raw);
251#ifdef DRV2605L
252 DRV_write(DRV_MODE, 0x00);
253#endif
254}
255
256void haptic_toggle_continuous(void) {
257#ifdef DRV2605L
258 if (haptic_config.cont) {
259 haptic_disable_continuous();
260 } else {
261 haptic_enable_continuous();
262 }
263 eeconfig_update_haptic(haptic_config.raw);
264#endif
265}
266
267void haptic_cont_increase(void) {
268 uint8_t amp = haptic_config.amplitude + 10;
269 if (haptic_config.amplitude >= 120) {
270 amp = 120;
271 }
272 haptic_set_amplitude(amp);
273}
274
275void haptic_cont_decrease(void) {
276 uint8_t amp = haptic_config.amplitude - 10;
277 if (haptic_config.amplitude < 20) {
278 amp = 20;
279 }
280 haptic_set_amplitude(amp);
281}
282
283void haptic_play(void) {
284#ifdef DRV2605L
285 uint8_t play_eff = 0;
286 play_eff = haptic_config.mode;
287 DRV_pulse(play_eff);
288#endif
289#ifdef SOLENOID_ENABLE
290 solenoid_fire();
291#endif
292}
293
294__attribute__((weak)) bool get_haptic_enabled_key(uint16_t keycode, keyrecord_t *record) {
295 switch(keycode) {
296# ifdef NO_HAPTIC_MOD
297 case QK_MOD_TAP ... QK_MOD_TAP_MAX:
298 if (record->tap.count == 0) return false;
299 break;
300 case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX:
301 if (record->tap.count != TAPPING_TOGGLE) return false;
302 break;
303 case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
304 if (record->tap.count == 0) return false;
305 break;
306 case KC_LCTRL ... KC_RGUI:
307 case QK_MOMENTARY ... QK_MOMENTARY_MAX:
308# endif
309# ifdef NO_HAPTIC_FN
310 case KC_FN0 ... KC_FN31:
311# endif
312# ifdef NO_HAPTIC_ALPHA
313 case KC_A ... KC_Z:
314# endif
315# ifdef NO_HAPTIC_PUNCTUATION
316 case KC_ENTER:
317 case KC_ESCAPE:
318 case KC_BSPACE:
319 case KC_SPACE:
320 case KC_MINUS:
321 case KC_EQUAL:
322 case KC_LBRACKET:
323 case KC_RBRACKET:
324 case KC_BSLASH:
325 case KC_NONUS_HASH:
326 case KC_SCOLON:
327 case KC_QUOTE:
328 case KC_GRAVE:
329 case KC_COMMA:
330 case KC_SLASH:
331 case KC_DOT:
332 case KC_NONUS_BSLASH:
333# endif
334# ifdef NO_HAPTIC_LOCKKEYS
335 case KC_CAPSLOCK:
336 case KC_SCROLLLOCK:
337 case KC_NUMLOCK:
338# endif
339# ifdef NO_HAPTIC_NAV
340 case KC_PSCREEN:
341 case KC_PAUSE:
342 case KC_INSERT:
343 case KC_DELETE:
344 case KC_PGDOWN:
345 case KC_PGUP:
346 case KC_LEFT:
347 case KC_UP:
348 case KC_RIGHT:
349 case KC_DOWN:
350 case KC_END:
351 case KC_HOME:
352# endif
353# ifdef NO_HAPTIC_NUMERIC
354 case KC_1 ... KC_0:
355# endif
356 return false;
357 }
358 return true;
359}
360
361bool process_haptic(uint16_t keycode, keyrecord_t *record) {
362 if (keycode == HPT_ON && record->event.pressed) {
363 haptic_enable();
364 }
365 if (keycode == HPT_OFF && record->event.pressed) {
366 haptic_disable();
367 }
368 if (keycode == HPT_TOG && record->event.pressed) {
369 haptic_toggle();
370 }
371 if (keycode == HPT_RST && record->event.pressed) {
372 haptic_reset();
373 }
374 if (keycode == HPT_FBK && record->event.pressed) {
375 haptic_feedback_toggle();
376 }
377 if (keycode == HPT_BUZ && record->event.pressed) {
378 haptic_buzz_toggle();
379 }
380 if (keycode == HPT_MODI && record->event.pressed) {
381 haptic_mode_increase();
382 }
383 if (keycode == HPT_MODD && record->event.pressed) {
384 haptic_mode_decrease();
385 }
386 if (keycode == HPT_DWLI && record->event.pressed) {
387 haptic_dwell_increase();
388 }
389 if (keycode == HPT_DWLD && record->event.pressed) {
390 haptic_dwell_decrease();
391 }
392 if (keycode == HPT_CONT && record->event.pressed) {
393 haptic_toggle_continuous();
394 }
395 if (keycode == HPT_CONI && record->event.pressed) {
396 haptic_cont_increase();
397 }
398 if (keycode == HPT_COND && record->event.pressed) {
399 haptic_cont_decrease();
400 }
401
402 if (haptic_config.enable) {
403 if (record->event.pressed) {
404 // keypress
405 if (haptic_config.feedback < 2 && get_haptic_enabled_key(keycode, record)) {
406 haptic_play();
407 }
408 } else {
409 // keyrelease
410 if (haptic_config.feedback > 0 && get_haptic_enabled_key(keycode, record)) {
411 haptic_play();
412 }
413 }
414 }
415 return true;
416}
417
418void haptic_shutdown(void) {
419#ifdef SOLENOID_ENABLE
420 solenoid_shutdown();
421#endif
422}
diff --git a/drivers/haptic/haptic.h b/drivers/haptic/haptic.h
deleted file mode 100644
index ba8e0d20b..000000000
--- a/drivers/haptic/haptic.h
+++ /dev/null
@@ -1,81 +0,0 @@
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
18#pragma once
19#include <stdint.h>
20#include <stdbool.h>
21#include "quantum.h"
22#ifdef DRV2605L
23# include "DRV2605L.h"
24#endif
25
26#ifndef HAPTIC_FEEDBACK_DEFAULT
27# define HAPTIC_FEEDBACK_DEFAULT 0
28#endif
29#ifndef HAPTIC_MODE_DEFAULT
30# define HAPTIC_MODE_DEFAULT DRV_MODE_DEFAULT
31#endif
32
33/* EEPROM config settings */
34typedef union {
35 uint32_t raw;
36 struct {
37 bool enable : 1;
38 uint8_t feedback : 2;
39 uint8_t mode : 7;
40 bool buzz : 1;
41 uint8_t dwell : 7;
42 bool cont : 1;
43 uint8_t amplitude : 8;
44 uint8_t reserved : 5;
45 };
46} haptic_config_t;
47
48typedef enum HAPTIC_FEEDBACK {
49 KEY_PRESS,
50 KEY_PRESS_RELEASE,
51 KEY_RELEASE,
52 HAPTIC_FEEDBACK_MAX,
53} HAPTIC_FEEDBACK;
54
55bool process_haptic(uint16_t keycode, keyrecord_t *record);
56void haptic_init(void);
57void haptic_task(void);
58void eeconfig_debug_haptic(void);
59void haptic_enable(void);
60void haptic_disable(void);
61void haptic_toggle(void);
62void haptic_feedback_toggle(void);
63void haptic_mode_increase(void);
64void haptic_mode_decrease(void);
65void haptic_mode(uint8_t mode);
66void haptic_reset(void);
67void haptic_set_feedback(uint8_t feedback);
68void haptic_set_mode(uint8_t mode);
69void haptic_set_dwell(uint8_t dwell);
70void haptic_set_buzz(uint8_t buzz);
71void haptic_buzz_toggle(void);
72uint8_t haptic_get_mode(void);
73uint8_t haptic_get_feedback(void);
74void haptic_dwell_increase(void);
75void haptic_dwell_decrease(void);
76void haptic_toggle_continuous(void);
77void haptic_cont_increase(void);
78void haptic_cont_decrease(void);
79
80void haptic_play(void);
81void haptic_shutdown(void);