You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.5 KiB

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