forked from xiaozhou-lei/pxt-irRemote4
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ir.ts
209 lines (196 loc) · 4.38 KB
/
ir.ts
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
enum IrPins{
P0= 0,
P1= 1,
P2= 2,
P3= 3,
P4= 4,
P5= 5,
P6= 6,
P7= 7,
P8= 8,
P9= 9,
P10= 10,
P11= 11,
P12= 12,
P13= 13,
P14= 14,
P15= 15,
P16= 16,
P19= 19,
P20= 20
};
enum EM_RemoteButton {
//% block=A
EM_A = 0x45,
//% block=B
EM_B = 0x46,
//% block=C
EM_C = 0x47,
//% block=D
EM_D = 0x44,
//% block=+
EM_Add = 0x43,
//% block=-
EM_Sub = 0x0d,
//% block=UP
EM_UP = 0x40,
//% block=LEFT
EM_Left = 0x07,
//% block=OK
EM_Ok = 0x15,
//% block=RIGHT
EM_Right = 0x09,
//% block=DOWN
EM_Down = 0x19,
//% block=0
EM_NUM0 = 0x16,
//% block=1
EM_NUM1 = 0x0c,
//% block=2
EM_NUM2 = 0x18,
//% block=3
EM_NUM3 = 0x5e,
//% block=4
EM_NUM4 = 0x08,
//% block=5
EM_NUM5 = 0x1c,
//% block=6
EM_NUM6 = 0x5a,
//% block=7
EM_NUM7 = 0x42,
//% block=8
EM_NUM8 = 0x52,
//% block=9
EM_NUM9 = 0x4a
};
enum RemoteButton {
//% block=*
A = 0x16,
//% block=#
B = 0x0D,
//% block=UP
UP = 0x18,
//% block=LEFT
Left = 0x08,
//% block=OK
Ok = 0x1C,
//% block=RIGHT
Right = 0x5A,
//% block=DOWN
Down = 0x52,
//% block=0
NUM0 = 0x19,
//% block=1
NUM1 = 0x45,
//% block=2
NUM2 = 0x46,
//% block=3
NUM3 = 0x47,
//% block=4
NUM4 = 0x44,
//% block=5
NUM5 = 0x40,
//% block=6
NUM6 = 0x43,
//% block=7
NUM7 = 0x07,
//% block=8
NUM8 = 0x15,
//% block=9
NUM9 = 0x09
};
/**
* Custom blocks
*/
//% weight=100 color="#EE6A50" icon="\uf013" block="irRemote"
namespace EM_IR {
let state: number;
let data1: number;
let irstate: number;
let irData: number = -1;
let irPin: number;
/**
* initialises local variables
* @param pin describe parameter here, eg: IrPins.P5
*/
//% weight=70
//% block="ir remote init port | %pin"
export function IrRemote_init(pin: IrPins): void {
irPin = pin;
return;
}
//% shim=EMIR::irCode
function em_irCode(irPin1: number): number {
return 0;
}
/**
* TODO: describe your function here
* @param n describe parameter here, eg: 5
* @param s describe parameter here, eg: "Hello"
* @param e describe parameter here
*/
//% weight=60
//% block="read IR key value"
export function EM_IR_read(): number {
pins.setPull(getPin(), PinPullMode.PullUp)
return valuotokeyConversion();
}
/**
* TODO: describe your function here
* @param value describe value here, eg: 5
*/
//% weight=50
//% blockId=onPressEvent
//% block="on IR received"
//% draggableParameters
export function OnPressEvent(cb: (message: number) => void) {
pins.setPull(getPin(), PinPullMode.PullUp)
state = 1;
control.onEvent(11, 22, function () {
cb(data1)
})
}
basic.forever(() => {
if (state == 1) {
irstate = em_irCode(irPin);
if (irstate != 0) {
data1 = irstate & 0xff;
control.raiseEvent(11, 22)
}
}
basic.pause(50);
})
function valuotokeyConversion(): number {
//serial.writeValue("x", irCode() )
let data = em_irCode(irPin);
if (data == 0) {
} else {
irData = data & 0xff;
}
return irData;
}
function getPin(): number {
switch (irPin) {
case 0: return DigitalPin.P0;
case 1: return DigitalPin.P1;
case 2: return DigitalPin.P2;
case 3: return DigitalPin.P3;
case 4: return DigitalPin.P4;
case 5: return DigitalPin.P5;
case 6: return DigitalPin.P6;
case 7: return DigitalPin.P7;
case 8: return DigitalPin.P8;
case 9: return DigitalPin.P9;
case 10: return DigitalPin.P10;
case 11: return DigitalPin.P11;
case 12: return DigitalPin.P12;
case 13: return DigitalPin.P13;
case 14: return DigitalPin.P14;
case 15: return DigitalPin.P15;
case 16: return DigitalPin.P16;
case 19: return DigitalPin.P19;
case 20: return DigitalPin.P20;
default: return DigitalPin.P0;
}
}
}