12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- #include <Arduino.h>
- #include <RotaryEncoder.h>
-
- #include "config.h"
- #include "config_pins.h"
- #include "debounce.h"
- #include "encoder.h"
-
- static Debouncer click(BTN_ENC);
- static int click_state = 0;
- static bool ignore_until_next_unclick = false;
-
- static Debouncer kill(KILL_PIN);
- static int kill_state = 0;
-
- static RotaryEncoder encoder(BTN_EN1, BTN_EN2, RotaryEncoder::LatchMode::TWO03);
- static int pos = 0;
-
- void encoder_init(void) {
- pinMode(BTN_ENC, INPUT);
- pinMode(KILL_PIN, INPUT);
- }
-
- void encoder_run(void) {
- int cs = click.poll();
- if (ignore_until_next_unclick) {
- // dont set to 1 again until it was 0 once
- if (cs == 0) {
- ignore_until_next_unclick = false;
- }
- } else {
- click_state = cs;
- }
-
- kill_state = kill.poll();
-
- encoder.tick();
- }
-
- int encoder_change(void) {
- int new_pos = encoder.getPosition();
- int diff = new_pos - pos;
- pos = new_pos;
- return diff;
- }
-
- int encoder_click(void) {
- int r = click_state;
-
- // only return 1 once for each click
- if (r == 1) {
- click_state = 0;
- ignore_until_next_unclick = true;
- }
-
- return r;
- }
-
- int kill_switch(void) {
- return kill_state;
- }
|