1 /* Copyright 2018 mtdjr - modified by ishtob
2 * Driver for solenoid 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/>.
22 bool solenoid_on = false;
23 bool solenoid_buzzing = false;
24 uint16_t solenoid_start = 0;
25 uint8_t solenoid_dwell = SOLENOID_DEFAULT_DWELL;
27 extern haptic_config_t haptic_config;
30 void solenoid_buzz_on(void) {
34 void solenoid_buzz_off(void) {
38 void solenoid_set_buzz(int buzz) {
39 haptic_set_buzz(buzz);
43 void solenoid_dwell_minus(uint8_t solenoid_dwell) {
44 if (solenoid_dwell > 0) solenoid_dwell--;
47 void solenoid_dwell_plus(uint8_t solenoid_dwell) {
48 if (solenoid_dwell < SOLENOID_MAX_DWELL) solenoid_dwell++;
51 void solenoid_set_dwell(uint8_t dwell) {
52 solenoid_dwell = dwell;
55 void solenoid_stop(void) {
56 writePinLow(SOLENOID_PIN);
58 solenoid_buzzing = false;
61 void solenoid_fire(void) {
62 if (!haptic_config.buzz && solenoid_on) return;
63 if (haptic_config.buzz && solenoid_buzzing) return;
66 solenoid_buzzing = true;
67 solenoid_start = timer_read();
68 writePinHigh(SOLENOID_PIN);
71 void solenoid_check(void) {
74 if (!solenoid_on) return;
76 elapsed = timer_elapsed(solenoid_start);
78 //Check if it's time to finish this solenoid click cycle
79 if (elapsed > solenoid_dwell) {
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);
93 if (solenoid_buzzing) {
94 solenoid_buzzing = false;
95 writePinLow(SOLENOID_PIN);
101 void solenoid_setup(void) {
102 setPinOutput(SOLENOID_PIN);
106 void solenoid_shutdown(void) {
107 writePinLow(SOLENOID_PIN);