diff options
| author | Joel Challis <git@zvecr.com> | 2021-07-26 03:14:58 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-25 19:14:58 -0700 |
| commit | f945c352e7db3fedb16c90f9f176b3df6e0b62ae (patch) | |
| tree | 8b2cd252ad036b5199603c9c755c01be5cb326a2 /quantum/haptic.c | |
| parent | 4bb595f94b5c77e7a961ff69d2b4d9c53a9094fc (diff) | |
| download | qmk_firmware-f945c352e7db3fedb16c90f9f176b3df6e0b62ae.tar.gz qmk_firmware-f945c352e7db3fedb16c90f9f176b3df6e0b62ae.zip | |
Haptic: driver-> feature (#13713)
Diffstat (limited to 'quantum/haptic.c')
| -rw-r--r-- | quantum/haptic.c | 295 |
1 files changed, 295 insertions, 0 deletions
diff --git a/quantum/haptic.c b/quantum/haptic.c new file mode 100644 index 000000000..65abcc15f --- /dev/null +++ b/quantum/haptic.c | |||
| @@ -0,0 +1,295 @@ | |||
| 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 "debug.h" | ||
| 20 | #ifdef DRV2605L | ||
| 21 | # include "DRV2605L.h" | ||
| 22 | #endif | ||
| 23 | #ifdef SOLENOID_ENABLE | ||
| 24 | # include "solenoid.h" | ||
| 25 | #endif | ||
| 26 | |||
| 27 | haptic_config_t haptic_config; | ||
| 28 | |||
| 29 | void haptic_init(void) { | ||
| 30 | if (!eeconfig_is_enabled()) { | ||
| 31 | eeconfig_init(); | ||
| 32 | } | ||
| 33 | haptic_config.raw = eeconfig_read_haptic(); | ||
| 34 | #ifdef SOLENOID_ENABLE | ||
| 35 | solenoid_set_dwell(haptic_config.dwell); | ||
| 36 | #endif | ||
| 37 | if ((haptic_config.raw == 0) | ||
| 38 | #ifdef SOLENOID_ENABLE | ||
| 39 | || (haptic_config.dwell == 0) | ||
| 40 | #endif | ||
| 41 | ) { | ||
| 42 | // this will be called, if the eeprom is not corrupt, | ||
| 43 | // but the previous firmware didn't have haptic enabled, | ||
| 44 | // or the previous firmware didn't have solenoid enabled, | ||
| 45 | // and the current one has solenoid enabled. | ||
| 46 | haptic_reset(); | ||
| 47 | } | ||
| 48 | #ifdef SOLENOID_ENABLE | ||
| 49 | solenoid_setup(); | ||
| 50 | dprintf("Solenoid driver initialized\n"); | ||
| 51 | #endif | ||
| 52 | #ifdef DRV2605L | ||
| 53 | DRV_init(); | ||
| 54 | dprintf("DRV2605 driver initialized\n"); | ||
| 55 | #endif | ||
| 56 | eeconfig_debug_haptic(); | ||
| 57 | } | ||
| 58 | |||
| 59 | void haptic_task(void) { | ||
| 60 | #ifdef SOLENOID_ENABLE | ||
| 61 | solenoid_check(); | ||
| 62 | #endif | ||
| 63 | } | ||
| 64 | |||
| 65 | void eeconfig_debug_haptic(void) { | ||
| 66 | dprintf("haptic_config eeprom\n"); | ||
| 67 | dprintf("haptic_config.enable = %d\n", haptic_config.enable); | ||
| 68 | dprintf("haptic_config.mode = %d\n", haptic_config.mode); | ||
| 69 | } | ||
| 70 | |||
| 71 | void haptic_enable(void) { | ||
| 72 | haptic_config.enable = 1; | ||
| 73 | xprintf("haptic_config.enable = %u\n", haptic_config.enable); | ||
| 74 | eeconfig_update_haptic(haptic_config.raw); | ||
| 75 | } | ||
| 76 | |||
| 77 | void haptic_disable(void) { | ||
| 78 | haptic_config.enable = 0; | ||
| 79 | xprintf("haptic_config.enable = %u\n", haptic_config.enable); | ||
| 80 | eeconfig_update_haptic(haptic_config.raw); | ||
| 81 | } | ||
| 82 | |||
| 83 | void haptic_toggle(void) { | ||
| 84 | if (haptic_config.enable) { | ||
| 85 | haptic_disable(); | ||
| 86 | } else { | ||
| 87 | haptic_enable(); | ||
| 88 | } | ||
| 89 | eeconfig_update_haptic(haptic_config.raw); | ||
| 90 | } | ||
| 91 | |||
| 92 | void haptic_feedback_toggle(void) { | ||
| 93 | haptic_config.feedback++; | ||
| 94 | if (haptic_config.feedback >= HAPTIC_FEEDBACK_MAX) haptic_config.feedback = KEY_PRESS; | ||
| 95 | xprintf("haptic_config.feedback = %u\n", !haptic_config.feedback); | ||
| 96 | eeconfig_update_haptic(haptic_config.raw); | ||
| 97 | } | ||
| 98 | |||
| 99 | void haptic_buzz_toggle(void) { | ||
| 100 | bool buzz_stat = !haptic_config.buzz; | ||
| 101 | haptic_config.buzz = buzz_stat; | ||
| 102 | haptic_set_buzz(buzz_stat); | ||
| 103 | } | ||
| 104 | |||
| 105 | void haptic_mode_increase(void) { | ||
| 106 | uint8_t mode = haptic_config.mode + 1; | ||
| 107 | #ifdef DRV2605L | ||
| 108 | if (haptic_config.mode >= drv_effect_max) { | ||
| 109 | mode = 1; | ||
| 110 | } | ||
| 111 | #endif | ||
| 112 | haptic_set_mode(mode); | ||
| 113 | } | ||
| 114 | |||
| 115 | void haptic_mode_decrease(void) { | ||
| 116 | uint8_t mode = haptic_config.mode - 1; | ||
| 117 | #ifdef DRV2605L | ||
| 118 | if (haptic_config.mode < 1) { | ||
| 119 | mode = (drv_effect_max - 1); | ||
| 120 | } | ||
| 121 | #endif | ||
| 122 | haptic_set_mode(mode); | ||
| 123 | } | ||
| 124 | |||
| 125 | void haptic_dwell_increase(void) { | ||
| 126 | #ifdef SOLENOID_ENABLE | ||
| 127 | int16_t next_dwell = ((int16_t)haptic_config.dwell) + SOLENOID_DWELL_STEP_SIZE; | ||
| 128 | if (haptic_config.dwell >= SOLENOID_MAX_DWELL) { | ||
| 129 | // if it's already at max, we wrap back to min | ||
| 130 | next_dwell = SOLENOID_MIN_DWELL; | ||
| 131 | } else if (next_dwell > SOLENOID_MAX_DWELL) { | ||
| 132 | // if we overshoot the max, then cap at max | ||
| 133 | next_dwell = SOLENOID_MAX_DWELL; | ||
| 134 | } | ||
| 135 | solenoid_set_dwell(next_dwell); | ||
| 136 | #else | ||
| 137 | int16_t next_dwell = ((int16_t)haptic_config.dwell) + 1; | ||
| 138 | #endif | ||
| 139 | haptic_set_dwell(next_dwell); | ||
| 140 | } | ||
| 141 | |||
| 142 | void haptic_dwell_decrease(void) { | ||
| 143 | #ifdef SOLENOID_ENABLE | ||
| 144 | int16_t next_dwell = ((int16_t)haptic_config.dwell) - SOLENOID_DWELL_STEP_SIZE; | ||
| 145 | if (haptic_config.dwell <= SOLENOID_MIN_DWELL) { | ||
| 146 | // if it's already at min, we wrap to max | ||
| 147 | next_dwell = SOLENOID_MAX_DWELL; | ||
| 148 | } else if (next_dwell < SOLENOID_MIN_DWELL) { | ||
| 149 | // if we go below min, then we cap to min | ||
| 150 | next_dwell = SOLENOID_MIN_DWELL; | ||
| 151 | } | ||
| 152 | solenoid_set_dwell(next_dwell); | ||
| 153 | #else | ||
| 154 | int16_t next_dwell = ((int16_t)haptic_config.dwell) - 1; | ||
| 155 | #endif | ||
| 156 | haptic_set_dwell(next_dwell); | ||
| 157 | } | ||
| 158 | |||
| 159 | void haptic_reset(void) { | ||
| 160 | haptic_config.enable = true; | ||
| 161 | uint8_t feedback = HAPTIC_FEEDBACK_DEFAULT; | ||
| 162 | haptic_config.feedback = feedback; | ||
| 163 | #ifdef DRV2605L | ||
| 164 | uint8_t mode = HAPTIC_MODE_DEFAULT; | ||
| 165 | haptic_config.mode = mode; | ||
| 166 | #endif | ||
| 167 | #ifdef SOLENOID_ENABLE | ||
| 168 | uint8_t dwell = SOLENOID_DEFAULT_DWELL; | ||
| 169 | haptic_config.dwell = dwell; | ||
| 170 | haptic_config.buzz = SOLENOID_DEFAULT_BUZZ; | ||
| 171 | solenoid_set_dwell(dwell); | ||
| 172 | #else | ||
| 173 | // This is to trigger haptic_reset again, if solenoid is enabled in the future. | ||
| 174 | haptic_config.dwell = 0; | ||
| 175 | haptic_config.buzz = 0; | ||
| 176 | #endif | ||
| 177 | eeconfig_update_haptic(haptic_config.raw); | ||
| 178 | xprintf("haptic_config.feedback = %u\n", haptic_config.feedback); | ||
| 179 | xprintf("haptic_config.mode = %u\n", haptic_config.mode); | ||
| 180 | } | ||
| 181 | |||
| 182 | void haptic_set_feedback(uint8_t feedback) { | ||
| 183 | haptic_config.feedback = feedback; | ||
| 184 | eeconfig_update_haptic(haptic_config.raw); | ||
| 185 | xprintf("haptic_config.feedback = %u\n", haptic_config.feedback); | ||
| 186 | } | ||
| 187 | |||
| 188 | void haptic_set_mode(uint8_t mode) { | ||
| 189 | haptic_config.mode = mode; | ||
| 190 | eeconfig_update_haptic(haptic_config.raw); | ||
| 191 | xprintf("haptic_config.mode = %u\n", haptic_config.mode); | ||
| 192 | } | ||
| 193 | |||
| 194 | void haptic_set_amplitude(uint8_t amp) { | ||
| 195 | haptic_config.amplitude = amp; | ||
| 196 | eeconfig_update_haptic(haptic_config.raw); | ||
| 197 | xprintf("haptic_config.amplitude = %u\n", haptic_config.amplitude); | ||
| 198 | #ifdef DRV2605L | ||
| 199 | DRV_amplitude(amp); | ||
| 200 | #endif | ||
| 201 | } | ||
| 202 | |||
| 203 | void haptic_set_buzz(uint8_t buzz) { | ||
| 204 | haptic_config.buzz = buzz; | ||
| 205 | eeconfig_update_haptic(haptic_config.raw); | ||
| 206 | xprintf("haptic_config.buzz = %u\n", haptic_config.buzz); | ||
| 207 | } | ||
| 208 | |||
| 209 | void haptic_set_dwell(uint8_t dwell) { | ||
| 210 | haptic_config.dwell = dwell; | ||
| 211 | eeconfig_update_haptic(haptic_config.raw); | ||
| 212 | xprintf("haptic_config.dwell = %u\n", haptic_config.dwell); | ||
| 213 | } | ||
| 214 | |||
| 215 | uint8_t haptic_get_enable(void) { return haptic_config.enable; } | ||
| 216 | |||
| 217 | uint8_t haptic_get_mode(void) { | ||
| 218 | if (!haptic_config.enable) { | ||
| 219 | return false; | ||
| 220 | } | ||
| 221 | return haptic_config.mode; | ||
| 222 | } | ||
| 223 | |||
| 224 | uint8_t haptic_get_feedback(void) { | ||
| 225 | if (!haptic_config.enable) { | ||
| 226 | return false; | ||
| 227 | } | ||
| 228 | return haptic_config.feedback; | ||
| 229 | } | ||
| 230 | |||
| 231 | uint8_t haptic_get_dwell(void) { | ||
| 232 | if (!haptic_config.enable) { | ||
| 233 | return false; | ||
| 234 | } | ||
| 235 | return haptic_config.dwell; | ||
| 236 | } | ||
| 237 | |||
| 238 | void 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 | |||
| 247 | void 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 | |||
| 256 | void haptic_toggle_continuous(void) { | ||
| 257 | if (haptic_config.cont) { | ||
| 258 | haptic_disable_continuous(); | ||
| 259 | } else { | ||
| 260 | haptic_enable_continuous(); | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 | void haptic_cont_increase(void) { | ||
| 265 | uint8_t amp = haptic_config.amplitude + 10; | ||
| 266 | if (haptic_config.amplitude >= 120) { | ||
| 267 | amp = 120; | ||
| 268 | } | ||
| 269 | haptic_set_amplitude(amp); | ||
| 270 | } | ||
| 271 | |||
| 272 | void haptic_cont_decrease(void) { | ||
| 273 | uint8_t amp = haptic_config.amplitude - 10; | ||
| 274 | if (haptic_config.amplitude < 20) { | ||
| 275 | amp = 20; | ||
| 276 | } | ||
| 277 | haptic_set_amplitude(amp); | ||
| 278 | } | ||
| 279 | |||
| 280 | void haptic_play(void) { | ||
| 281 | #ifdef DRV2605L | ||
| 282 | uint8_t play_eff = 0; | ||
| 283 | play_eff = haptic_config.mode; | ||
| 284 | DRV_pulse(play_eff); | ||
| 285 | #endif | ||
| 286 | #ifdef SOLENOID_ENABLE | ||
| 287 | solenoid_fire(); | ||
| 288 | #endif | ||
| 289 | } | ||
| 290 | |||
| 291 | void haptic_shutdown(void) { | ||
| 292 | #ifdef SOLENOID_ENABLE | ||
| 293 | solenoid_shutdown(); | ||
| 294 | #endif | ||
| 295 | } | ||
