InputEvents v1.6.0
An easy to use but comprehensive Event Library for Buttons, Encoders, Encoder Buttons, Analog Inputs, Joysticks and Switches.
BaseTableEncoderAdapter.h
1#ifndef INPUT_EVENTS_BASE_TABLE_ENCODER_ADAPTER_H
2#define INPUT_EVENTS_BASE_TABLE_ENCODER_ADAPTER_H
3
4#include "IEncoderAdapter.h"
5#include <Arduino.h>
6
12public:
13
19 virtual int32_t getPosition() override {
20 if ( !_externalUpdate ) update();
21 return _position;
22 }
23
24
25 virtual void setPosition(int32_t pos) {
26 _position = pos;
27 }
28
33 virtual void update() {
34 uint8_t state = (readPin(_pinA) << 1) | readPin(_pinB);
35 uint8_t idx = (_prevState << 2) | state;
36 _position += table[idx];
37 _prevState = state;
38 }
39
40 virtual ~BaseTableEncoderAdapter() {}
41
42protected:
43
50 virtual uint8_t readPin(uint8_t pin) const = 0;
51
52 // Quadrature lookup table
53 static constexpr int8_t table[16] = {
54 0, 1, -1, 0,
55 -1, 0, 0, 1,
56 1, 0, 0, -1,
57 0, -1, 1, 0
58 };
59
60 uint8_t _pinA, _pinB;
61 uint8_t _prevState = 0;
62 int32_t _position = 0;
63 bool _externalUpdate = false;
64
65};
66
67
68
69#endif
A base class for encoder adapters that uses a quadrature encoder table to determine posion changes fr...
Definition: BaseTableEncoderAdapter.h:11
virtual int32_t getPosition() override
Get the current position of the encoder.
Definition: BaseTableEncoderAdapter.h:19
virtual uint8_t readPin(uint8_t pin) const =0
Concrete implementations must provide a method to read pin values.
virtual void update()
Update position using the table.
Definition: BaseTableEncoderAdapter.h:33
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: BaseTableEncoderAdapter.h:25
bool _externalUpdate
Some implementations may allow the update via an interupt.
Definition: BaseTableEncoderAdapter.h:63
A lightweight adapter abstract class for encoders.
Definition: IEncoderAdapter.h:19