InputEvents v1.6.0
An easy to use but comprehensive Event Library for Buttons, Encoders, Encoder Buttons, Analog Inputs, Joysticks and Switches.
SlowExpanderEncoderAdapter.h
1#ifndef INPUT_EVENTS_SLOW_EXPANDER_ENCODER_ADAPTER_H
2#define INPUT_EVENTS_SLOW_EXPANDER_ENCODER_ADAPTER_H
3
4
5#include "BaseTableEncoderAdapter.h"
6#include "GpioExpanderAdapter/GpioExpanderAdapter.h"
7
8
17
18
19public:
20
28 SlowExpanderEncoderAdapter( uint8_t encoderPinA, uint8_t encoderPinB, GpioExpanderAdapter& expander )
29 : expanderAdapter(&expander)
30 {
31 _pinA = encoderPinA;
32 _pinB = encoderPinB;
33 }
34
35 bool begin() override {
36 expanderAdapter->attachPin(_pinA);
37 expanderAdapter->attachPin(_pinB);
38 return true;
39 }
40
46 virtual int32_t getPosition() override {
47 update();
48 return _position;
49 }
50
51
52 virtual void setPosition(int32_t pos) {
53 _position = pos;
54 }
55
61 virtual void update() {
62 uint8_t stateA = expanderAdapter->read(_pinA);
63 uint8_t stateB = expanderAdapter->read(_pinB);
64
65 // Detect rising edge on pin A
66 // We are only updating once per four state changes, so multiply position
67 // because EventEncoder expects all positions to be counted.
68 if (prevStateA == LOW && stateA == HIGH) {
69 if (stateB == LOW) {
70 _position -= _positionMultiplier;
71 } else {
72 _position += _positionMultiplier;
73 }
74 }
75 prevStateA = stateA;
76 prevStateB = stateB;
77 }
78
79
80
81
82protected:
83
84
85private:
86 GpioExpanderAdapter* expanderAdapter;
87 uint8_t _pinA = HIGH, _pinB = HIGH, prevStateA = HIGH, prevStateB = HIGH;
88 int32_t _position = 0;
89 uint8_t _positionMultiplier = 4;
90};
91
92
93
94#endif
The base class/interface specification for GPIO expanders.
Definition: GpioExpanderAdapter.h:10
virtual bool read(byte pin)=0
Returns the state of a pin on the expander.
virtual void attachPin(byte pin, int mode=INPUT_PULLUP)=0
Use it to configure individual pin mode, if expander allows it. Not all of them do.
A lightweight adapter abstract class for encoders.
Definition: IEncoderAdapter.h:19
A very basic IEncoderAdapter for slow GPIO Expanders.
Definition: SlowExpanderEncoderAdapter.h:16
virtual int32_t getPosition() override
Get the current position of the encoder.
Definition: SlowExpanderEncoderAdapter.h:46
virtual void setPosition(int32_t pos)
Set the a new position of the encoder. For some libraries this may only allow it to be set to 0.
Definition: SlowExpanderEncoderAdapter.h:52
SlowExpanderEncoderAdapter(uint8_t encoderPinA, uint8_t encoderPinB, GpioExpanderAdapter &expander)
Construct a ExpanderEncoderAdapter.
Definition: SlowExpanderEncoderAdapter.h:28
virtual void update()
Update position using the table.
Definition: SlowExpanderEncoderAdapter.h:61
bool begin() override
For compatibility with the Arduino library convention.
Definition: SlowExpanderEncoderAdapter.h:35