-
Notifications
You must be signed in to change notification settings - Fork 0
/
RotaryEncoder.hpp
68 lines (52 loc) · 1.37 KB
/
RotaryEncoder.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* RotaryEncoder.hpp
*
* Created on: 2020-04-12
* Author: yulay
*/
#ifndef ROTARYENCODER_HPP_
#define ROTARYENCODER_HPP_
#include <stdint.h>
namespace yr
{
enum
{
None = 0,
Btn_Released = -1,
Btn_Pressed = 1,
Btn_PressedLong = 2 | Btn_Pressed,
Increment = 1,
Decrement = -1,
};
class RotaryEncoder
{
public:
RotaryEncoder ()
: mInput(0)
, mButton(0)
, mMax(99)
, mValue(0)
, mPrevInput(0)
{}
~RotaryEncoder () = default;
void Update();
void inc() { ++mInput; }
void dec() { --mInput; }
void pressed() { mButton = Btn_Pressed; }
void longPressed() { mButton = Btn_PressedLong; }
int8_t isPressed() { return mButton; }
void setValue(uint8_t v) { mValue = (mMax < v) ? mMax : v; }
uint8_t getValue() const { return mValue; }
void setMax(uint8_t v) { mMax = v; if(mMax < mValue) mValue = mMax; }
uint8_t getButton();
private:
//current hw encoder tracking
volatile uint8_t mInput;
volatile uint8_t mButton;
//current software encoder state
uint8_t mMax;
int8_t mValue;
int8_t mPrevInput;
};
} /* namespace yr */
#endif /* ROTARYENCODER_HPP_ */