aboutsummaryrefslogtreecommitdiff
path: root/drivers/haptic
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/haptic')
-rw-r--r--drivers/haptic/haptic.c53
-rw-r--r--drivers/haptic/solenoid.c10
-rw-r--r--drivers/haptic/solenoid.h18
3 files changed, 56 insertions, 25 deletions
diff --git a/drivers/haptic/haptic.c b/drivers/haptic/haptic.c
index 2ce279b75..de3f40052 100644
--- a/drivers/haptic/haptic.c
+++ b/drivers/haptic/haptic.c
@@ -33,11 +33,18 @@ void haptic_init(void) {
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#ifdef SOLENOID_ENABLE
37 haptic_config.mode = 1; 37 solenoid_set_dwell(haptic_config.dwell);
38 } 38#endif
39 if (!haptic_config.mode) { 39 if ((haptic_config.raw == 0)
40 dprintf("No haptic config found in eeprom, setting default configs\n"); 40#ifdef SOLENOID_ENABLE
41 || (haptic_config.dwell == 0)
42#endif
43 ) {
44 // this will be called, if the eeprom is not corrupt,
45 // but the previous firmware didn't have haptic enabled,
46 // or the previous firmware didn't have solenoid enabled,
47 // and the current one has solenoid enabled.
41 haptic_reset(); 48 haptic_reset();
42 } 49 }
43#ifdef SOLENOID_ENABLE 50#ifdef SOLENOID_ENABLE
@@ -118,25 +125,37 @@ void haptic_mode_decrease(void) {
118} 125}
119 126
120void haptic_dwell_increase(void) { 127void haptic_dwell_increase(void) {
121 uint8_t dwell = haptic_config.dwell + 1;
122#ifdef SOLENOID_ENABLE 128#ifdef SOLENOID_ENABLE
129 int16_t next_dwell = ((int16_t)haptic_config.dwell) + SOLENOID_DWELL_STEP_SIZE;
123 if (haptic_config.dwell >= SOLENOID_MAX_DWELL) { 130 if (haptic_config.dwell >= SOLENOID_MAX_DWELL) {
124 dwell = 1; 131 // if it's already at max, we wrap back to min
132 next_dwell = SOLENOID_MIN_DWELL;
133 } else if (next_dwell > SOLENOID_MAX_DWELL) {
134 // if we overshoot the max, then cap at max
135 next_dwell = SOLENOID_MAX_DWELL;
125 } 136 }
126 solenoid_set_dwell(dwell); 137 solenoid_set_dwell(next_dwell);
138#else
139 int16_t next_dwell = ((int16_t)haptic_config.dwell) + 1;
127#endif 140#endif
128 haptic_set_dwell(dwell); 141 haptic_set_dwell(next_dwell);
129} 142}
130 143
131void haptic_dwell_decrease(void) { 144void haptic_dwell_decrease(void) {
132 uint8_t dwell = haptic_config.dwell - 1;
133#ifdef SOLENOID_ENABLE 145#ifdef SOLENOID_ENABLE
134 if (haptic_config.dwell < SOLENOID_MIN_DWELL) { 146 int16_t next_dwell = ((int16_t)haptic_config.dwell) - SOLENOID_DWELL_STEP_SIZE;
135 dwell = SOLENOID_MAX_DWELL; 147 if (haptic_config.dwell <= SOLENOID_MIN_DWELL) {
148 // if it's already at min, we wrap to max
149 next_dwell = SOLENOID_MAX_DWELL;
150 } else if (next_dwell < SOLENOID_MIN_DWELL) {
151 // if we go below min, then we cap to min
152 next_dwell = SOLENOID_MIN_DWELL;
136 } 153 }
137 solenoid_set_dwell(dwell); 154 solenoid_set_dwell(next_dwell);
155#else
156 int16_t next_dwell = ((int16_t)haptic_config.dwell) - 1;
138#endif 157#endif
139 haptic_set_dwell(dwell); 158 haptic_set_dwell(next_dwell);
140} 159}
141 160
142void haptic_reset(void) { 161void haptic_reset(void) {
@@ -150,6 +169,12 @@ void haptic_reset(void) {
150#ifdef SOLENOID_ENABLE 169#ifdef SOLENOID_ENABLE
151 uint8_t dwell = SOLENOID_DEFAULT_DWELL; 170 uint8_t dwell = SOLENOID_DEFAULT_DWELL;
152 haptic_config.dwell = dwell; 171 haptic_config.dwell = dwell;
172 haptic_config.buzz = SOLENOID_DEFAULT_BUZZ;
173 solenoid_set_dwell(dwell);
174#else
175 // This is to trigger haptic_reset again, if solenoid is enabled in the future.
176 haptic_config.dwell = 0;
177 haptic_config.buzz = 0;
153#endif 178#endif
154 eeconfig_update_haptic(haptic_config.raw); 179 eeconfig_update_haptic(haptic_config.raw);
155 xprintf("haptic_config.feedback = %u\n", haptic_config.feedback); 180 xprintf("haptic_config.feedback = %u\n", haptic_config.feedback);
diff --git a/drivers/haptic/solenoid.c b/drivers/haptic/solenoid.c
index d645c379a..2975ef893 100644
--- a/drivers/haptic/solenoid.c
+++ b/drivers/haptic/solenoid.c
@@ -32,14 +32,6 @@ void solenoid_buzz_off(void) { haptic_set_buzz(0); }
32 32
33void solenoid_set_buzz(int buzz) { haptic_set_buzz(buzz); } 33void solenoid_set_buzz(int buzz) { haptic_set_buzz(buzz); }
34 34
35void solenoid_dwell_minus(uint8_t solenoid_dwell) {
36 if (solenoid_dwell > 0) solenoid_dwell--;
37}
38
39void solenoid_dwell_plus(uint8_t solenoid_dwell) {
40 if (solenoid_dwell < SOLENOID_MAX_DWELL) solenoid_dwell++;
41}
42
43void solenoid_set_dwell(uint8_t dwell) { solenoid_dwell = dwell; } 35void solenoid_set_dwell(uint8_t dwell) { solenoid_dwell = dwell; }
44 36
45void solenoid_stop(void) { 37void solenoid_stop(void) {
@@ -73,7 +65,7 @@ void solenoid_check(void) {
73 65
74 // Check whether to buzz the solenoid on and off 66 // Check whether to buzz the solenoid on and off
75 if (haptic_config.buzz) { 67 if (haptic_config.buzz) {
76 if (elapsed / SOLENOID_MIN_DWELL % 2 == 0) { 68 if ((elapsed % (SOLENOID_BUZZ_ACTUATED + SOLENOID_BUZZ_NONACTUATED)) < SOLENOID_BUZZ_ACTUATED) {
77 if (!solenoid_buzzing) { 69 if (!solenoid_buzzing) {
78 solenoid_buzzing = true; 70 solenoid_buzzing = true;
79 writePinHigh(SOLENOID_PIN); 71 writePinHigh(SOLENOID_PIN);
diff --git a/drivers/haptic/solenoid.h b/drivers/haptic/solenoid.h
index dd6ececa6..f2a3bc4c3 100644
--- a/drivers/haptic/solenoid.h
+++ b/drivers/haptic/solenoid.h
@@ -29,6 +29,22 @@
29# define SOLENOID_MIN_DWELL 4 29# define SOLENOID_MIN_DWELL 4
30#endif 30#endif
31 31
32#ifndef SOLENOID_DWELL_STEP_SIZE
33# define SOLENOID_DWELL_STEP_SIZE 1
34#endif
35
36#ifndef SOLENOID_DEFAULT_BUZZ
37# define SOLENOID_DEFAULT_BUZZ 0
38#endif
39
40#ifndef SOLENOID_BUZZ_ACTUATED
41# define SOLENOID_BUZZ_ACTUATED SOLENOID_MIN_DWELL
42#endif
43
44#ifndef SOLENOID_BUZZ_NONACTUATED
45# define SOLENOID_BUZZ_NONACTUATED SOLENOID_MIN_DWELL
46#endif
47
32#ifndef SOLENOID_PIN 48#ifndef SOLENOID_PIN
33# error SOLENOID_PIN not defined 49# error SOLENOID_PIN not defined
34#endif 50#endif
@@ -37,8 +53,6 @@ void solenoid_buzz_on(void);
37void solenoid_buzz_off(void); 53void solenoid_buzz_off(void);
38void solenoid_set_buzz(int buzz); 54void solenoid_set_buzz(int buzz);
39 55
40void solenoid_dwell_minus(uint8_t solenoid_dwell);
41void solenoid_dwell_plus(uint8_t solenoid_dwell);
42void solenoid_set_dwell(uint8_t dwell); 56void solenoid_set_dwell(uint8_t dwell);
43 57
44void solenoid_stop(void); 58void solenoid_stop(void);