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