diff options
Diffstat (limited to 'drivers/haptic/solenoid.c')
-rw-r--r-- | drivers/haptic/solenoid.c | 109 |
1 files changed, 109 insertions, 0 deletions
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 | } | ||