Rotary encoder handler.
RotaryEncoder.h@0:db7f371c8530, 2016-07-20 (annotated)
- Committer:
- Sateg
- Date:
- Wed Jul 20 00:30:23 2016 +0000
- Revision:
- 0:db7f371c8530
Working, conservative rotary encoder handler.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sateg | 0:db7f371c8530 | 1 | #include "mbed.h" |
Sateg | 0:db7f371c8530 | 2 | |
Sateg | 0:db7f371c8530 | 3 | #ifndef ROTARY_ENCODER_H_ |
Sateg | 0:db7f371c8530 | 4 | #define ROTARY_ENCODER_H_ |
Sateg | 0:db7f371c8530 | 5 | |
Sateg | 0:db7f371c8530 | 6 | /** |
Sateg | 0:db7f371c8530 | 7 | Rotary encoder handling class. |
Sateg | 0:db7f371c8530 | 8 | Starts a ticker which checks the status of the encoder and |
Sateg | 0:db7f371c8530 | 9 | sets the rotation directions based on its status. |
Sateg | 0:db7f371c8530 | 10 | */ |
Sateg | 0:db7f371c8530 | 11 | class RotaryEncoder { |
Sateg | 0:db7f371c8530 | 12 | public: |
Sateg | 0:db7f371c8530 | 13 | RotaryEncoder(void (*cb)(void), PinName channelA_, PinName channelB_, PinName pushPin_, float period = 0.01); |
Sateg | 0:db7f371c8530 | 14 | virtual ~RotaryEncoder(); |
Sateg | 0:db7f371c8530 | 15 | |
Sateg | 0:db7f371c8530 | 16 | /// Retrieve rotation direction. |
Sateg | 0:db7f371c8530 | 17 | int8_t getDirection() const; |
Sateg | 0:db7f371c8530 | 18 | /// Retrieve push button status. |
Sateg | 0:db7f371c8530 | 19 | bool getPushButton() const; |
Sateg | 0:db7f371c8530 | 20 | /// Launched by Ticker to determine rotation direction |
Sateg | 0:db7f371c8530 | 21 | void pulse(); |
Sateg | 0:db7f371c8530 | 22 | |
Sateg | 0:db7f371c8530 | 23 | int getCounter() const; |
Sateg | 0:db7f371c8530 | 24 | |
Sateg | 0:db7f371c8530 | 25 | private: |
Sateg | 0:db7f371c8530 | 26 | |
Sateg | 0:db7f371c8530 | 27 | /// Table which helps determine the rotation direction |
Sateg | 0:db7f371c8530 | 28 | static const int8_t encoderTable[]; |
Sateg | 0:db7f371c8530 | 29 | |
Sateg | 0:db7f371c8530 | 30 | int counter; |
Sateg | 0:db7f371c8530 | 31 | int8_t direction; |
Sateg | 0:db7f371c8530 | 32 | int8_t lastDirection; |
Sateg | 0:db7f371c8530 | 33 | InterruptIn channelA; |
Sateg | 0:db7f371c8530 | 34 | InterruptIn channelB; |
Sateg | 0:db7f371c8530 | 35 | InterruptIn pushPin; |
Sateg | 0:db7f371c8530 | 36 | Ticker encoderTicker; |
Sateg | 0:db7f371c8530 | 37 | |
Sateg | 0:db7f371c8530 | 38 | }; |
Sateg | 0:db7f371c8530 | 39 | |
Sateg | 0:db7f371c8530 | 40 | #endif /* ROTARY_ENCODER_H_ */ |