#include <Button_SL.hpp>
#include <RotaryEncoder.h>
constexpr uint8_t PIN_BTN {4}; // SW on rotary encoder
constexpr uint8_t PIN_IN1 {2}; // DT ---- " ----
constexpr uint8_t PIN_IN2 {3}; // CLK ---- " ----
constexpr int8_t Rotation_360_DEGREE {20};
RotaryEncoder encoder{PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::FOUR3};
Btn::ButtonSL btn {PIN_BTN};
//////////////////////////////////////////////////////////////////////////////
/// @brief The encoder signals are evaluated
///
/// @param enc Reference on encoder object
/// @param value Reference encoder value
/// @return true if the an encoder signal was evaluated
/// @return false if no encoder signal was evaluated
//////////////////////////////////////////////////////////////////////////////
bool askEncoder(RotaryEncoder &enc, int8_t &value) {
bool flag{true};
enc.tick();
switch (enc.getDirection()) {
case RotaryEncoder::Direction::NOROTATION: flag = false; break;
case RotaryEncoder::Direction::CLOCKWISE: ++value; break;
case RotaryEncoder::Direction::COUNTERCLOCKWISE: --value; break;
}
return flag;
}
//////////////////////////////////////////////////////////////////////////////
/// @brief Initialization part of the main program
///
//////////////////////////////////////////////////////////////////////////////
void setup(void) {
Serial.begin(115200);
Serial.println(F("Start..."));
btn.begin();
btn.releaseOn(); // Release after "long" Press (default = 1000ms);
btn.setDebounceTime_ms(100);
}
//////////////////////////////////////////////////////////////////////////////
/// @brief main program
///
//////////////////////////////////////////////////////////////////////////////
void loop() {
static int8_t encValue {0};
static int fullRotations {0};
// Encoder abfragen
if (askEncoder(encoder, encValue )) {
if (encValue == 20 || encValue == -20) {
fullRotations = (encValue < 0) ? fullRotations - 1 : fullRotations + 1;
encValue = 0;
}
Serial.print(encValue); Serial.print(" ");Serial.println(fullRotations);
}
// Tasterstatus ermitteln
if (btn.tick() != Btn::ButtonState::notPressed) {
Serial.println(F("Taster gedrückt"));
}
}