-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotaryEncoder.cpp
96 lines (85 loc) · 2.16 KB
/
rotaryEncoder.cpp
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "pxt.h"
#include "mbed.h"
using namespace pxt;
enum class YFPins{
P0= 3,
P1= 2,
P2= 1,
P3= 4,
P4= 5,
P5= 17,
P6= 12,
P7= 11,
P8= 18,
P9= 10,
P10= 6,
P11= 26,
P12= 20,
P13= 23,
P14= 22,
P15= 21,
P16= 16,
P19= 0,
P20= 30
};
enum class YFRotationDirection{
Left = 0,
Right = 1
};
namespace YFRotaryEncoder {
uint32_t lri = 0, lbi=0;
InterruptIn *ri;
DigitalIn *dv, *dsw;
Timer tsb;
Action leftRotate, rightRotate, pressRotate;
//%
void onRotateEvent(YFRotationDirection dir, Action body) {
//if(dir == YFRotationDirection::Left) leftRotate.push_back(body);
//else rightRotate.push_back(body);
if(dir == YFRotationDirection::Left) leftRotate = body;
else rightRotate = body;
}
//%
void onPressEvent(Action body){
//pressRotate.push_back(body);
pressRotate = body;
}
//void cA(vA runner){for(int i=0;i<runner.size();i++){runAction0(runner[i]);} }
void onLR(){runAction0(leftRotate);}
void onRR(){runAction0(rightRotate);}
void onPress(){
runAction0(pressRotate);
}
class RotateHandler {
public:
void onRotated(){
uint32_t now = tsb.read_ms();
if(now - lri < 50) return;
lri = now;
if(dv->read()) create_fiber(onLR);//fire right rotate
else create_fiber(onRR); //fire left rotate
}
};
RotateHandler rh;
void monitorPress(){
//printf("entering fiber\r\n");
while(1){
uBit.sleep(50);
if(dsw->read()) continue;
uint32_t now = tsb.read_ms();
if(now - lbi < 50) continue;
lbi = now;
onPress();
}
}
//%
// void init(YFPins clk, YFPins dt, YFPins sw){
void init(YFPins clk, YFPins dt){
ri = new InterruptIn((PinName)clk);
dv = new DigitalIn((PinName)dt);
// dsw = new DigitalIn((PinName)sw);
create_fiber(monitorPress);
tsb.start(); //interrupt timer for debounce
ri->fall(&rh, &RotateHandler::onRotated);
}
}