No Description
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.

encoder.cpp 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <Arduino.h>
  2. #include <RotaryEncoder.h>
  3. #include "config.h"
  4. #include "config_pins.h"
  5. #include "debounce.h"
  6. #include "encoder.h"
  7. static Debouncer click(BTN_ENC);
  8. static int click_state = 0;
  9. static bool ignore_until_next_unclick = false;
  10. static Debouncer kill(KILL_PIN);
  11. static int kill_state = 0;
  12. static RotaryEncoder encoder(BTN_EN1, BTN_EN2, RotaryEncoder::LatchMode::TWO03);
  13. static int pos = 0;
  14. void encoder_init(void) {
  15. pinMode(BTN_ENC, INPUT);
  16. pinMode(KILL_PIN, INPUT);
  17. }
  18. void encoder_run(void) {
  19. int cs = click.poll();
  20. if (ignore_until_next_unclick) {
  21. // dont set to 1 again until it was 0 once
  22. if (cs == 0) {
  23. ignore_until_next_unclick = false;
  24. }
  25. } else {
  26. click_state = cs;
  27. }
  28. kill_state = kill.poll();
  29. encoder.tick();
  30. }
  31. int encoder_change(void) {
  32. int new_pos = encoder.getPosition();
  33. int diff = new_pos - pos;
  34. pos = new_pos;
  35. return diff;
  36. }
  37. int encoder_click(void) {
  38. int r = click_state;
  39. // only return 1 once for each click
  40. if (r == 1) {
  41. click_state = 0;
  42. ignore_until_next_unclick = true;
  43. }
  44. return r;
  45. }
  46. int kill_switch(void) {
  47. return kill_state;
  48. }