1 /* Copyright 2019 ishtob
2 * Driver for haptic feedback written for QMK
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.
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.
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/>.
24 #ifdef SOLENOID_ENABLE
28 haptic_config_t haptic_config;
30 void haptic_init(void) {
31 debug_enable = 1; //Debug is ON!
32 if (!eeconfig_is_enabled()) {
35 haptic_config.raw = eeconfig_read_haptic();
36 if (haptic_config.mode < 1){
37 haptic_config.mode = 1;
39 if (!haptic_config.mode){
40 dprintf("No haptic config found in eeprom, setting default configs\n");
43 #ifdef SOLENOID_ENABLE
45 dprintf("Solenoid driver initialized\n");
49 dprintf("DRV2605 driver initialized\n");
51 eeconfig_debug_haptic();
54 void haptic_task(void) {
55 #ifdef SOLENOID_ENABLE
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);
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);
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);
78 void haptic_toggle(void) {
79 if (haptic_config.enable) {
84 eeconfig_update_haptic(haptic_config.raw);
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);
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);
101 void haptic_mode_increase(void) {
102 uint8_t mode = haptic_config.mode + 1;
104 if (haptic_config.mode >= drv_effect_max) {
108 haptic_set_mode(mode);
111 void haptic_mode_decrease(void) {
112 uint8_t mode = haptic_config.mode -1;
114 if (haptic_config.mode < 1) {
115 mode = (drv_effect_max - 1);
118 haptic_set_mode(mode);
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) {
127 solenoid_set_dwell(dwell);
129 haptic_set_dwell(dwell);
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;
138 solenoid_set_dwell(dwell);
140 haptic_set_dwell(dwell);
143 void haptic_reset(void){
144 haptic_config.enable = true;
145 uint8_t feedback = HAPTIC_FEEDBACK_DEFAULT;
146 haptic_config.feedback = feedback;
148 uint8_t mode = HAPTIC_MODE_DEFAULT;
149 haptic_config.mode = mode;
151 #ifdef SOLENOID_ENABLE
152 uint8_t dwell = SOLENOID_DEFAULT_DWELL;
153 haptic_config.dwell = dwell;
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);
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);
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);
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);
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);
184 uint8_t haptic_get_mode(void) {
185 if (!haptic_config.enable){
188 return haptic_config.mode;
191 uint8_t haptic_get_feedback(void) {
192 if (!haptic_config.enable){
195 return haptic_config.feedback;
198 uint8_t haptic_get_dwell(void) {
199 if (!haptic_config.enable){
202 return haptic_config.dwell;
205 void haptic_play(void) {
207 uint8_t play_eff = 0;
208 play_eff = haptic_config.mode;
211 #ifdef SOLENOID_ENABLE
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 ) {
230 if (haptic_config.feedback < 2) {
235 if (haptic_config.feedback > 0) {
243 void haptic_shutdown(void) {
244 #ifdef SOLENOID_ENABLE