solenoid.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #include <timer.h>
  18. #include "solenoid.h"
  19. #include "haptic.h"
  20. bool solenoid_on = false;
  21. bool solenoid_buzzing = false;
  22. uint16_t solenoid_start = 0;
  23. uint8_t solenoid_dwell = SOLENOID_DEFAULT_DWELL;
  24. extern haptic_config_t haptic_config;
  25. void solenoid_buzz_on(void) { haptic_set_buzz(1); }
  26. void solenoid_buzz_off(void) { haptic_set_buzz(0); }
  27. void solenoid_set_buzz(int buzz) { haptic_set_buzz(buzz); }
  28. void solenoid_dwell_minus(uint8_t solenoid_dwell) {
  29. if (solenoid_dwell > 0) solenoid_dwell--;
  30. }
  31. void solenoid_dwell_plus(uint8_t solenoid_dwell) {
  32. if (solenoid_dwell < SOLENOID_MAX_DWELL) solenoid_dwell++;
  33. }
  34. void solenoid_set_dwell(uint8_t dwell) { solenoid_dwell = dwell; }
  35. void solenoid_stop(void) {
  36. writePinLow(SOLENOID_PIN);
  37. solenoid_on = false;
  38. solenoid_buzzing = false;
  39. }
  40. void solenoid_fire(void) {
  41. if (!haptic_config.buzz && solenoid_on) return;
  42. if (haptic_config.buzz && solenoid_buzzing) return;
  43. solenoid_on = true;
  44. solenoid_buzzing = true;
  45. solenoid_start = timer_read();
  46. writePinHigh(SOLENOID_PIN);
  47. }
  48. void solenoid_check(void) {
  49. uint16_t elapsed = 0;
  50. if (!solenoid_on) return;
  51. elapsed = timer_elapsed(solenoid_start);
  52. // Check if it's time to finish this solenoid click cycle
  53. if (elapsed > solenoid_dwell) {
  54. solenoid_stop();
  55. return;
  56. }
  57. // Check whether to buzz the solenoid on and off
  58. if (haptic_config.buzz) {
  59. if (elapsed / SOLENOID_MIN_DWELL % 2 == 0) {
  60. if (!solenoid_buzzing) {
  61. solenoid_buzzing = true;
  62. writePinHigh(SOLENOID_PIN);
  63. }
  64. } else {
  65. if (solenoid_buzzing) {
  66. solenoid_buzzing = false;
  67. writePinLow(SOLENOID_PIN);
  68. }
  69. }
  70. }
  71. }
  72. void solenoid_setup(void) {
  73. setPinOutput(SOLENOID_PIN);
  74. solenoid_fire();
  75. }
  76. void solenoid_shutdown(void) { writePinLow(SOLENOID_PIN); }