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