-
Notifications
You must be signed in to change notification settings - Fork 0
/
ir.cpp
126 lines (118 loc) · 2.33 KB
/
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "pxt.h"
#include <map>
#include <vector>
#include "ReceiverIR.h"
using namespace pxt;
typedef vector<Action> vA;
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
};
enum class RemoteButton {
//% block=A
A = 0x45,
//% block=B
B = 0x46,
//% block=C
C = 0x47,
//% block=D
D = 0x44,
//% block=UP
UP = 0x40,
//% block=+
Add = 0x43,
//% block=LEFT
Left = 0x07,
//% block=OK
Ok = 0x15,
//% block=RIGHT
Right = 0x09,
//% block=DOWN
Down = 0x19,
//% block=-
EQ = 0x0d,
//% block=0
NUM0 = 0x16,
//% block=1
NUM1 = 0x0c,
//% block=2
NUM2 = 0x18,
//% block=3
NUM3 = 0x5e,
//% block=4
NUM4 = 0x8,
//% block=5
NUM5 = 0x1c,
//% block=6
NUM6 = 0x5a,
//% block=7
NUM7 = 0x42,
//% block=8
NUM8 = 0x52,
//% block=9
NUM9 = 0x4A
};
//% color=50 weight=19
//% icon="\uf1eb"
namespace motorIR {
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;
/**
* button pushed.
*/
//% blockId=ir_received_left_event
//% block="on |%btn| button pressed"
void onPressEvent(RemoteButton btn, Action body) {
//if(actions.find(btn) == actions.end()) actions[btn] = new vector();
actions[btn].push_back(body);
}
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();
}
}
/**
* initialises local variablesssss
*/
//% blockId=ir_init
//% block="connect ir receiver to %pin"
void init(Pins pin){
rx = new ReceiverIR((PinName)pin);
tsb.start(); //interrupt timer for debounce
create_fiber(monitorIR);
}
}