#include #include #include "config.h" #include "config_pins.h" #include "debounce.h" #include "encoder.h" //#define DEBUG_ENCODER static Debouncer click(BTN_ENC); static int click_state = 0; static bool ignore_until_next_unclick = false; #ifdef KILL_PIN static Debouncer kill(KILL_PIN); static int kill_state = 0; #endif // KILL_PIN static RotaryEncoder encoder(BTN_EN1, BTN_EN2, RotaryEncoder::LatchMode::FOUR3); static int pos = 0; void encoder_init(void) { pinMode(BTN_ENC, INPUT_PULLUP); #ifdef KILL_PIN pinMode(KILL_PIN, INPUT_PULLUP); #endif // KILL_PIN } 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; } #ifdef KILL_PIN kill_state = kill.poll(); #endif // KILL_PIN encoder.tick(); } int encoder_change(void) { int new_pos = encoder.getPosition(); int diff = new_pos - pos; pos = new_pos; #ifdef DEBUG_ENCODER if (diff != 0) { Serial.print("encoder_change: "); Serial.print(diff); Serial.print(" ticks: "); Serial.println(new_pos); } #endif // DEBUG_ENCODER 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; #ifdef DEBUG_ENCODER Serial.println("encoder_click: 1"); #endif // DEBUG_ENCODER } return r; } int kill_switch(void) { #ifdef KILL_PIN #ifdef DEBUG_ENCODER if (kill_state == 1) { Serial.println("kill_switch: 1"); } #endif // DEBUG_ENCODER return kill_state; #else return 0; #endif // KILL_PIN }