-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathMbit_IR.cpp
101 lines (90 loc) · 1.76 KB
/
Mbit_IR.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
97
98
99
100
101
/*
Copyright (C): 2020, Shenzhen Yahboom Tech
modified by chengengyue
*/
#include "pxt.h"
#include <map>
#include <vector>
#include "ReceiverIR.h"
using namespace pxt;
typedef vector<Action> vA;
enum class RemoteButton {
Power = 0x0,
Up = 0x1,
Left = 0x4,
Right = 0x6,
Down = 0x9,
Light = 0x2,
BEEP = 0x5,
Plus = 0xc,
Minus = 0xe,
TLeft = 0x8,
TRight = 0xA,
NUM0 = 0xD,
NUM1 = 0x10,
NUM2 = 0x11,
NUM3 = 0x12,
NUM4 = 0x14,
NUM5 = 0x15,
NUM6 = 0x16,
NUM7 = 0x18,
NUM8 = 0x19,
NUM9 = 0x1A
};
enum class Pins{
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
};
namespace Mbit_IR {
map<RemoteButton, vA> actions;
map<RemoteButton, uint32_t> lastact;
Timer tsb;
uint8_t buf[32];
uint32_t now;
ReceiverIR *rx;
RemoteIR::Format fmt = RemoteIR::UNKNOWN;
void cA(vA runner){for(int i=0;i<runner.size();i++){runAction0(runner[i]);} }
void onReceivable(){
int x = rx->getData(&fmt, buf, 32 * 8);
if(actions.find((RemoteButton)buf[2]) == actions.end()) return;
now = tsb.read_ms();
if(now - lastact[(RemoteButton)buf[2]] < 100) return;
lastact[(RemoteButton)buf[2]] = now;
cA(actions[(RemoteButton)buf[2]]);
}
void monitorIR(){
while(1){
while(rx->getState() != ReceiverIR::Received){
uBit.sleep(50);
}
onReceivable();
}
}
//%
void init(Pins pin){
rx = new ReceiverIR((PinName)pin);
tsb.start(); //interrupt timer for debounce
create_fiber(monitorIR);
}
//%
void onPressEvent(RemoteButton btn, Action body) {
actions[btn].push_back(body);
}
}