aboutsummaryrefslogtreecommitdiff
path: root/drivers/haptic
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/haptic')
-rw-r--r--drivers/haptic/haptic.c355
-rw-r--r--drivers/haptic/haptic.h81
-rw-r--r--drivers/haptic/solenoid.c1
3 files changed, 1 insertions, 436 deletions
diff --git a/drivers/haptic/haptic.c b/drivers/haptic/haptic.c
deleted file mode 100644
index de3f40052..000000000
--- a/drivers/haptic/haptic.c
+++ /dev/null
@@ -1,355 +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
294bool process_haptic(uint16_t keycode, keyrecord_t *record) {
295 if (keycode == HPT_ON && record->event.pressed) {
296 haptic_enable();
297 }
298 if (keycode == HPT_OFF && record->event.pressed) {
299 haptic_disable();
300 }
301 if (keycode == HPT_TOG && record->event.pressed) {
302 haptic_toggle();
303 }
304 if (keycode == HPT_RST && record->event.pressed) {
305 haptic_reset();
306 }
307 if (keycode == HPT_FBK && record->event.pressed) {
308 haptic_feedback_toggle();
309 }
310 if (keycode == HPT_BUZ && record->event.pressed) {
311 haptic_buzz_toggle();
312 }
313 if (keycode == HPT_MODI && record->event.pressed) {
314 haptic_mode_increase();
315 }
316 if (keycode == HPT_MODD && record->event.pressed) {
317 haptic_mode_decrease();
318 }
319 if (keycode == HPT_DWLI && record->event.pressed) {
320 haptic_dwell_increase();
321 }
322 if (keycode == HPT_DWLD && record->event.pressed) {
323 haptic_dwell_decrease();
324 }
325 if (keycode == HPT_CONT && record->event.pressed) {
326 haptic_toggle_continuous();
327 }
328 if (keycode == HPT_CONI && record->event.pressed) {
329 haptic_cont_increase();
330 }
331 if (keycode == HPT_COND && record->event.pressed) {
332 haptic_cont_decrease();
333 }
334
335 if (haptic_config.enable) {
336 if (record->event.pressed) {
337 // keypress
338 if (haptic_config.feedback < 2) {
339 haptic_play();
340 }
341 } else {
342 // keyrelease
343 if (haptic_config.feedback > 0) {
344 haptic_play();
345 }
346 }
347 }
348 return true;
349}
350
351void haptic_shutdown(void) {
352#ifdef SOLENOID_ENABLE
353 solenoid_shutdown();
354#endif
355}
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);
diff --git a/drivers/haptic/solenoid.c b/drivers/haptic/solenoid.c
index 3e61d5a17..25cf34465 100644
--- a/drivers/haptic/solenoid.c
+++ b/drivers/haptic/solenoid.c
@@ -18,6 +18,7 @@
18#include "timer.h" 18#include "timer.h"
19#include "solenoid.h" 19#include "solenoid.h"
20#include "haptic.h" 20#include "haptic.h"
21#include "gpio.h"
21 22
22bool solenoid_on = false; 23bool solenoid_on = false;
23bool solenoid_buzzing = false; 24bool solenoid_buzzing = false;