diff --git a/src/ArduinoComponents/Components/RotaryEncoder.cpp b/src/ArduinoComponents/Components/RotaryEncoder.cpp new file mode 100644 index 0000000..8fc3ad7 --- /dev/null +++ b/src/ArduinoComponents/Components/RotaryEncoder.cpp @@ -0,0 +1,51 @@ +#include "RotaryEncoder.h" +namespace components +{ + RotaryEncoder::RotaryEncoder( uint8_t a, uint8_t b, uint8_t clicks ) + : Component(), + A( a, TriggerOn::Low, InputPull::Up ), + B( b, TriggerOn::Low, InputPull::Up ), + detents( clicks ) + { + RegisterChild(A); + A.onPress( [&] { + if ( B.isPressed() == LOW ) + { + rotation--; + onRotate(-1); + } + else + { + rotation++; + onRotate(+1); + } + delay(5); + } ); + RegisterChild(B); + B.onPress( [&] { + delay(5); + } ); + } + + int32_t RotaryEncoder::onRotate( int8_t dr ) + { + if ( dr > 0 ) + { + Serial.print( 'R' ); + } + else + { + Serial.print( 'L' ); + } + Serial.print( ' ' ); + if ( detents ) + rotation %= detents; + Serial.println( rotation ); + return rotation; + } + + void RotaryEncoder::privateLoop( void ) + { + + } +} \ No newline at end of file diff --git a/src/ArduinoComponents/Components/RotaryEncoder.h b/src/ArduinoComponents/Components/RotaryEncoder.h new file mode 100644 index 0000000..50ae495 --- /dev/null +++ b/src/ArduinoComponents/Components/RotaryEncoder.h @@ -0,0 +1,17 @@ +#include "TactileButton.h" +#include "Component.h" +namespace components +{ + class RotaryEncoder : public Component + { + public: + RotaryEncoder( uint8_t a, uint8_t b, uint8_t clicks = 0 ); + private: + int32_t rotation; + int32_t onRotate( int8_t dr ); + uint8_t detents; + TactileButton A; + TactileButton B; + void privateLoop( void ); + }; +} \ No newline at end of file