forked from lovyan03/M5DropMenuSample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPLUSEncoder.h
62 lines (54 loc) · 1.45 KB
/
PLUSEncoder.h
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
#ifndef _PLUSEncoder_H_
#define _PLUSEncoder_H_
#include <M5Stack.h>
class PLUSEncoder
{
public:
const uint16_t HOLD_MSEC = 500;
PLUSEncoder() {}
bool update()
{
if (!Wire.requestFrom(0x62, 2)) return false;
_prevEnc = _enc;
_prevPress = _press;
bool pr = false;
while (Wire.available()){
_rawsum += Wire.read();
pr = (Wire.read() != 0xff);
}
_enc = _rawsum / 3;
if (_enc != 0) _rawsum = 0;
uint32_t msec = millis();
if (pr) {
if (!_prevPress) {
_press = 1;
_pressTime = msec;
} else
if (1 == _prevPress && (_pressTime + HOLD_MSEC < msec)) {
_press = 2;
}
} else {
_press = 0;
}
return true;
}
bool isUp() { return _enc > 0; }
bool isDown() { return _enc < 0; }
bool isPressed() { return _press; }
bool wasUp() { return _enc != _prevEnc && _enc > 0; }
bool wasDown() { return _enc != _prevEnc && _enc < 0; }
bool wasPressed() { return !_prevPress && _press; }
bool isClick() { return _prevPress == 1 && _press == 0; }
bool isLongClick() { return _prevPress == 1 && _press == 2; }
bool wasHolding() { return _prevPress == 2 && _press == 2; }
private:
uint32_t _pressTime;
int8_t _rawsum = 0x00;
int8_t _enc = 0x00;
int8_t _prevEnc = 0x00;
uint8_t _press = 0x00; // 0:release 1:click 2:holding
uint8_t _prevPress = 0x00;
};
PLUSEncoder PlusEncoder;
#endif
extern PLUSEncoder PlusEncoder;