diff options
Diffstat (limited to 'drivers/haptic')
-rw-r--r-- | drivers/haptic/DRV2605L.c | 24 | ||||
-rw-r--r-- | drivers/haptic/DRV2605L.h | 26 | ||||
-rw-r--r-- | drivers/haptic/haptic.c | 248 | ||||
-rw-r--r-- | drivers/haptic/haptic.h | 82 | ||||
-rw-r--r-- | drivers/haptic/solenoid.c | 109 | ||||
-rw-r--r-- | drivers/haptic/solenoid.h | 54 |
6 files changed, 523 insertions, 20 deletions
diff --git a/drivers/haptic/DRV2605L.c b/drivers/haptic/DRV2605L.c index 97ca292b9..215e6be3e 100644 --- a/drivers/haptic/DRV2605L.c +++ b/drivers/haptic/DRV2605L.c | |||
@@ -21,7 +21,7 @@ | |||
21 | #include <math.h> | 21 | #include <math.h> |
22 | 22 | ||
23 | 23 | ||
24 | uint8_t DRV2605L_transfer_buffer[20]; | 24 | uint8_t DRV2605L_transfer_buffer[2]; |
25 | uint8_t DRV2605L_tx_register[0]; | 25 | uint8_t DRV2605L_tx_register[0]; |
26 | uint8_t DRV2605L_read_buffer[0]; | 26 | uint8_t DRV2605L_read_buffer[0]; |
27 | uint8_t DRV2605L_read_register; | 27 | uint8_t DRV2605L_read_register; |
@@ -34,6 +34,11 @@ void DRV_write(uint8_t drv_register, uint8_t settings) { | |||
34 | } | 34 | } |
35 | 35 | ||
36 | uint8_t DRV_read(uint8_t regaddress) { | 36 | uint8_t DRV_read(uint8_t regaddress) { |
37 | #ifdef __AVR__ | ||
38 | i2c_readReg(DRV2605L_BASE_ADDRESS << 1, | ||
39 | regaddress, DRV2605L_read_buffer, 1, 100); | ||
40 | DRV2605L_read_register = (uint8_t)DRV2605L_read_buffer[0]; | ||
41 | #else | ||
37 | DRV2605L_tx_register[0] = regaddress; | 42 | DRV2605L_tx_register[0] = regaddress; |
38 | if (MSG_OK != i2c_transmit_receive(DRV2605L_BASE_ADDRESS << 1, | 43 | if (MSG_OK != i2c_transmit_receive(DRV2605L_BASE_ADDRESS << 1, |
39 | DRV2605L_tx_register, 1, | 44 | DRV2605L_tx_register, 1, |
@@ -42,14 +47,13 @@ uint8_t DRV_read(uint8_t regaddress) { | |||
42 | printf("err reading reg \n"); | 47 | printf("err reading reg \n"); |
43 | } | 48 | } |
44 | DRV2605L_read_register = (uint8_t)DRV2605L_read_buffer[0]; | 49 | DRV2605L_read_register = (uint8_t)DRV2605L_read_buffer[0]; |
50 | #endif | ||
45 | return DRV2605L_read_register; | 51 | return DRV2605L_read_register; |
46 | } | 52 | } |
47 | 53 | ||
48 | void DRV_init(void) | 54 | void DRV_init(void) |
49 | { | 55 | { |
50 | i2c_init(); | 56 | i2c_init(); |
51 | i2c_start(DRV2605L_BASE_ADDRESS); | ||
52 | |||
53 | /* 0x07 sets DRV2605 into calibration mode */ | 57 | /* 0x07 sets DRV2605 into calibration mode */ |
54 | DRV_write(DRV_MODE,0x07); | 58 | DRV_write(DRV_MODE,0x07); |
55 | 59 | ||
@@ -104,21 +108,17 @@ void DRV_init(void) | |||
104 | C4_SET.Bits.C4_AUTO_CAL_TIME = AUTO_CAL_TIME; | 108 | C4_SET.Bits.C4_AUTO_CAL_TIME = AUTO_CAL_TIME; |
105 | DRV_write(DRV_CTRL_4, (uint8_t) C4_SET.Byte); | 109 | DRV_write(DRV_CTRL_4, (uint8_t) C4_SET.Byte); |
106 | DRV_write(DRV_LIB_SELECTION,LIB_SELECTION); | 110 | DRV_write(DRV_LIB_SELECTION,LIB_SELECTION); |
107 | //start autocalibration | 111 | |
108 | DRV_write(DRV_GO, 0x01); | 112 | DRV_write(DRV_GO, 0x01); |
109 | 113 | ||
110 | /* 0x00 sets DRV2605 out of standby and to use internal trigger | 114 | /* 0x00 sets DRV2605 out of standby and to use internal trigger |
111 | * 0x01 sets DRV2605 out of standby and to use external trigger */ | 115 | * 0x01 sets DRV2605 out of standby and to use external trigger */ |
112 | DRV_write(DRV_MODE,0x00); | 116 | DRV_write(DRV_MODE,0x00); |
113 | |||
114 | /* 0x06: LRA library */ | ||
115 | DRV_write(DRV_WAVEFORM_SEQ_1, 0x01); | ||
116 | |||
117 | /* 0xB9: LRA, 4x brake factor, medium gain, 7.5x back EMF | ||
118 | * 0x39: ERM, 4x brake factor, medium gain, 1.365x back EMF */ | ||
119 | |||
120 | /* TODO: setup auto-calibration as part of initiation */ | ||
121 | 117 | ||
118 | //Play greeting sequence | ||
119 | DRV_write(DRV_GO, 0x00); | ||
120 | DRV_write(DRV_WAVEFORM_SEQ_1, DRV_GREETING); | ||
121 | DRV_write(DRV_GO, 0x01); | ||
122 | } | 122 | } |
123 | 123 | ||
124 | void DRV_pulse(uint8_t sequence) | 124 | void DRV_pulse(uint8_t sequence) |
diff --git a/drivers/haptic/DRV2605L.h b/drivers/haptic/DRV2605L.h index de9d294e9..836e9cbcd 100644 --- a/drivers/haptic/DRV2605L.h +++ b/drivers/haptic/DRV2605L.h | |||
@@ -31,13 +31,6 @@ | |||
31 | #define FB_LOOPGAIN 1 /* For Low:0, Medium:1, High:2, Very High:3 */ | 31 | #define FB_LOOPGAIN 1 /* For Low:0, Medium:1, High:2, Very High:3 */ |
32 | #endif | 32 | #endif |
33 | 33 | ||
34 | #ifndef RATED_VOLTAGE | ||
35 | #define RATED_VOLTAGE 2 /* 2v as safe range in case device voltage is not set */ | ||
36 | #ifndef V_PEAK | ||
37 | #define V_PEAK 2.8 | ||
38 | #endif | ||
39 | #endif | ||
40 | |||
41 | /* LRA specific settings */ | 34 | /* LRA specific settings */ |
42 | #if FB_ERM_LRA == 1 | 35 | #if FB_ERM_LRA == 1 |
43 | #ifndef V_RMS | 36 | #ifndef V_RMS |
@@ -49,6 +42,16 @@ | |||
49 | #ifndef F_LRA | 42 | #ifndef F_LRA |
50 | #define F_LRA 205 | 43 | #define F_LRA 205 |
51 | #endif | 44 | #endif |
45 | #ifndef RATED_VOLTAGE | ||
46 | #define RATED_VOLTAGE 2 /* 2v as safe range in case device voltage is not set */ | ||
47 | #endif | ||
48 | #endif | ||
49 | |||
50 | #ifndef RATED_VOLTAGE | ||
51 | #define RATED_VOLTAGE 2 /* 2v as safe range in case device voltage is not set */ | ||
52 | #endif | ||
53 | #ifndef V_PEAK | ||
54 | #define V_PEAK 2.8 | ||
52 | #endif | 55 | #endif |
53 | 56 | ||
54 | /* Library Selection */ | 57 | /* Library Selection */ |
@@ -60,6 +63,13 @@ | |||
60 | #endif | 63 | #endif |
61 | #endif | 64 | #endif |
62 | 65 | ||
66 | #ifndef DRV_GREETING | ||
67 | #define DRV_GREETING alert_750ms | ||
68 | #endif | ||
69 | #ifndef DRV_MODE_DEFAULT | ||
70 | #define DRV_MODE_DEFAULT strong_click1 | ||
71 | #endif | ||
72 | |||
63 | /* Control 1 register settings */ | 73 | /* Control 1 register settings */ |
64 | #ifndef DRIVE_TIME | 74 | #ifndef DRIVE_TIME |
65 | #define DRIVE_TIME 25 | 75 | #define DRIVE_TIME 25 |
@@ -162,7 +172,6 @@ void DRV_write(const uint8_t drv_register, const uint8_t settings); | |||
162 | uint8_t DRV_read(const uint8_t regaddress); | 172 | uint8_t DRV_read(const uint8_t regaddress); |
163 | void DRV_pulse(const uint8_t sequence); | 173 | void DRV_pulse(const uint8_t sequence); |
164 | 174 | ||
165 | |||
166 | typedef enum DRV_EFFECT{ | 175 | typedef enum DRV_EFFECT{ |
167 | clear_sequence = 0, | 176 | clear_sequence = 0, |
168 | strong_click = 1, | 177 | strong_click = 1, |
@@ -288,6 +297,7 @@ typedef enum DRV_EFFECT{ | |||
288 | smooth_hum3_30 = 121, | 297 | smooth_hum3_30 = 121, |
289 | smooth_hum4_20 = 122, | 298 | smooth_hum4_20 = 122, |
290 | smooth_hum5_10 = 123, | 299 | smooth_hum5_10 = 123, |
300 | drv_effect_max = 124, | ||
291 | } DRV_EFFECT; | 301 | } DRV_EFFECT; |
292 | 302 | ||
293 | /* Register bit array unions */ | 303 | /* Register bit array unions */ |
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 | |||
28 | haptic_config_t haptic_config; | ||
29 | |||
30 | void 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 | |||
54 | void haptic_task(void) { | ||
55 | #ifdef SOLENOID_ENABLE | ||
56 | solenoid_check(); | ||
57 | #endif | ||
58 | } | ||
59 | |||
60 | void 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 | |||
66 | void 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 | |||
72 | void 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 | |||
78 | void haptic_toggle(void) { | ||
79 | if (haptic_config.enable) { | ||
80 | haptic_disable(); | ||
81 | } else { | ||
82 | haptic_enable(); | ||
83 | } | ||
84 | eeconfig_update_haptic(haptic_config.raw); | ||
85 | } | ||
86 | |||
87 | void 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 | |||
95 | void 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 | |||
101 | void 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 | |||
111 | void 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 | |||
121 | void 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 | |||
132 | void 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 | |||
143 | void 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 | |||
160 | void 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 | |||
166 | void 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 | |||
172 | void 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 | |||
178 | void 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 | |||
184 | uint8_t haptic_get_mode(void) { | ||
185 | if (!haptic_config.enable){ | ||
186 | return false; | ||
187 | } | ||
188 | return haptic_config.mode; | ||
189 | } | ||
190 | |||
191 | uint8_t haptic_get_feedback(void) { | ||
192 | if (!haptic_config.enable){ | ||
193 | return false; | ||
194 | } | ||
195 | return haptic_config.feedback; | ||
196 | } | ||
197 | |||
198 | uint8_t haptic_get_dwell(void) { | ||
199 | if (!haptic_config.enable){ | ||
200 | return false; | ||
201 | } | ||
202 | return haptic_config.dwell; | ||
203 | } | ||
204 | |||
205 | void 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 | |||
216 | bool 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 | |||
243 | void haptic_shutdown(void) { | ||
244 | #ifdef SOLENOID_ENABLE | ||
245 | solenoid_shutdown(); | ||
246 | #endif | ||
247 | |||
248 | } | ||
diff --git a/drivers/haptic/haptic.h b/drivers/haptic/haptic.h new file mode 100644 index 000000000..d39dc5c3b --- /dev/null +++ b/drivers/haptic/haptic.h | |||
@@ -0,0 +1,82 @@ | |||
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 | |||
27 | #ifndef HAPTIC_FEEDBACK_DEFAULT | ||
28 | #define HAPTIC_FEEDBACK_DEFAULT 0 | ||
29 | #endif | ||
30 | #ifndef HAPTIC_MODE_DEFAULT | ||
31 | #define HAPTIC_MODE_DEFAULT DRV_MODE_DEFAULT | ||
32 | #endif | ||
33 | |||
34 | /* EEPROM config settings */ | ||
35 | typedef union { | ||
36 | uint32_t raw; | ||
37 | struct { | ||
38 | bool enable :1; | ||
39 | uint8_t feedback :2; | ||
40 | uint8_t mode :7; | ||
41 | bool buzz :1; | ||
42 | uint8_t dwell :7; | ||
43 | uint16_t reserved :16; | ||
44 | }; | ||
45 | } haptic_config_t; | ||
46 | |||
47 | typedef enum HAPTIC_FEEDBACK{ | ||
48 | KEY_PRESS, | ||
49 | KEY_PRESS_RELEASE, | ||
50 | KEY_RELEASE, | ||
51 | HAPTIC_FEEDBACK_MAX, | ||
52 | } HAPTIC_FEEDBACK; | ||
53 | |||
54 | bool process_haptic(uint16_t keycode, keyrecord_t *record); | ||
55 | void haptic_init(void); | ||
56 | void haptic_task(void); | ||
57 | void eeconfig_debug_haptic(void); | ||
58 | void haptic_enable(void); | ||
59 | void haptic_disable(void); | ||
60 | void haptic_toggle(void); | ||
61 | void haptic_feedback_toggle(void); | ||
62 | void haptic_mode_increase(void); | ||
63 | void haptic_mode_decrease(void); | ||
64 | void haptic_mode(uint8_t mode); | ||
65 | void haptic_reset(void); | ||
66 | void haptic_set_feedback(uint8_t feedback); | ||
67 | void haptic_set_mode(uint8_t mode); | ||
68 | void haptic_set_dwell(uint8_t dwell); | ||
69 | void haptic_set_buzz(uint8_t buzz); | ||
70 | void haptic_buzz_toggle(void); | ||
71 | uint8_t haptic_get_mode(void); | ||
72 | uint8_t haptic_get_feedback(void); | ||
73 | void haptic_dwell_increase(void); | ||
74 | void haptic_dwell_decrease(void); | ||
75 | |||
76 | void haptic_play(void); | ||
77 | void haptic_shutdown(void); | ||
78 | |||
79 | |||
80 | |||
81 | |||
82 | |||
diff --git a/drivers/haptic/solenoid.c b/drivers/haptic/solenoid.c new file mode 100644 index 000000000..2d39dbc17 --- /dev/null +++ b/drivers/haptic/solenoid.c | |||
@@ -0,0 +1,109 @@ | |||
1 | /* Copyright 2018 mtdjr - modified by ishtob | ||
2 | * Driver for solenoid 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 | #include <timer.h> | ||
19 | #include "solenoid.h" | ||
20 | #include "haptic.h" | ||
21 | |||
22 | bool solenoid_on = false; | ||
23 | bool solenoid_buzzing = false; | ||
24 | uint16_t solenoid_start = 0; | ||
25 | uint8_t solenoid_dwell = SOLENOID_DEFAULT_DWELL; | ||
26 | |||
27 | extern haptic_config_t haptic_config; | ||
28 | |||
29 | |||
30 | void solenoid_buzz_on(void) { | ||
31 | haptic_set_buzz(1); | ||
32 | } | ||
33 | |||
34 | void solenoid_buzz_off(void) { | ||
35 | haptic_set_buzz(0); | ||
36 | } | ||
37 | |||
38 | void solenoid_set_buzz(int buzz) { | ||
39 | haptic_set_buzz(buzz); | ||
40 | } | ||
41 | |||
42 | |||
43 | void solenoid_dwell_minus(uint8_t solenoid_dwell) { | ||
44 | if (solenoid_dwell > 0) solenoid_dwell--; | ||
45 | } | ||
46 | |||
47 | void solenoid_dwell_plus(uint8_t solenoid_dwell) { | ||
48 | if (solenoid_dwell < SOLENOID_MAX_DWELL) solenoid_dwell++; | ||
49 | } | ||
50 | |||
51 | void solenoid_set_dwell(uint8_t dwell) { | ||
52 | solenoid_dwell = dwell; | ||
53 | } | ||
54 | |||
55 | void solenoid_stop(void) { | ||
56 | writePinLow(SOLENOID_PIN); | ||
57 | solenoid_on = false; | ||
58 | solenoid_buzzing = false; | ||
59 | } | ||
60 | |||
61 | void solenoid_fire(void) { | ||
62 | if (!haptic_config.buzz && solenoid_on) return; | ||
63 | if (haptic_config.buzz && solenoid_buzzing) return; | ||
64 | |||
65 | solenoid_on = true; | ||
66 | solenoid_buzzing = true; | ||
67 | solenoid_start = timer_read(); | ||
68 | writePinHigh(SOLENOID_PIN); | ||
69 | } | ||
70 | |||
71 | void solenoid_check(void) { | ||
72 | uint16_t elapsed = 0; | ||
73 | |||
74 | if (!solenoid_on) return; | ||
75 | |||
76 | elapsed = timer_elapsed(solenoid_start); | ||
77 | |||
78 | //Check if it's time to finish this solenoid click cycle | ||
79 | if (elapsed > solenoid_dwell) { | ||
80 | solenoid_stop(); | ||
81 | return; | ||
82 | } | ||
83 | |||
84 | //Check whether to buzz the solenoid on and off | ||
85 | if (haptic_config.buzz) { | ||
86 | if (elapsed / SOLENOID_MIN_DWELL % 2 == 0){ | ||
87 | if (!solenoid_buzzing) { | ||
88 | solenoid_buzzing = true; | ||
89 | writePinHigh(SOLENOID_PIN); | ||
90 | } | ||
91 | } | ||
92 | else { | ||
93 | if (solenoid_buzzing) { | ||
94 | solenoid_buzzing = false; | ||
95 | writePinLow(SOLENOID_PIN); | ||
96 | } | ||
97 | } | ||
98 | } | ||
99 | } | ||
100 | |||
101 | void solenoid_setup(void) { | ||
102 | setPinOutput(SOLENOID_PIN); | ||
103 | solenoid_fire(); | ||
104 | } | ||
105 | |||
106 | void solenoid_shutdown(void) { | ||
107 | writePinLow(SOLENOID_PIN); | ||
108 | |||
109 | } | ||
diff --git a/drivers/haptic/solenoid.h b/drivers/haptic/solenoid.h new file mode 100644 index 000000000..a08f62a11 --- /dev/null +++ b/drivers/haptic/solenoid.h | |||
@@ -0,0 +1,54 @@ | |||
1 | /* Copyright 2018 mtdjr - modified by ishtob | ||
2 | * Driver for solenoid 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 | |||
20 | #ifndef SOLENOID_DEFAULT_DWELL | ||
21 | #define SOLENOID_DEFAULT_DWELL 12 | ||
22 | #endif | ||
23 | |||
24 | #ifndef SOLENOID_MAX_DWELL | ||
25 | #define SOLENOID_MAX_DWELL 100 | ||
26 | #endif | ||
27 | |||
28 | #ifndef SOLENOID_MIN_DWELL | ||
29 | #define SOLENOID_MIN_DWELL 4 | ||
30 | #endif | ||
31 | |||
32 | #ifndef SOLENOID_ACTIVE | ||
33 | #define SOLENOID_ACTIVE false | ||
34 | #endif | ||
35 | |||
36 | #ifndef SOLENOID_PIN | ||
37 | #define SOLENOID_PIN F6 | ||
38 | #endif | ||
39 | |||
40 | void solenoid_buzz_on(void); | ||
41 | void solenoid_buzz_off(void); | ||
42 | void solenoid_set_buzz(int buzz); | ||
43 | |||
44 | void solenoid_dwell_minus(uint8_t solenoid_dwell); | ||
45 | void solenoid_dwell_plus(uint8_t solenoid_dwell); | ||
46 | void solenoid_set_dwell(uint8_t dwell); | ||
47 | |||
48 | void solenoid_stop(void); | ||
49 | void solenoid_fire(void); | ||
50 | |||
51 | void solenoid_check(void); | ||
52 | |||
53 | void solenoid_setup(void); | ||
54 | void solenoid_shutdown(void); | ||