forked from 1010Technologies/pxt-makerbit-ir-receiver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfrared-receiver.ts
333 lines (297 loc) · 8.82 KB
/
infrared-receiver.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// MakerBit blocks supporting a Keyestudio Infrared Wireless Module Kit
// (receiver module)
const enum IrButtonAction {
//% block="pressed"
Pressed = 0,
//% block="released"
Released = 1,
}
const enum IrProtocol {
//% block="Keyestudio"
Keyestudio = 0,
//% block="NEC"
NEC = 1,
}
//% color=#D919FF icon="\u26a1" block="Infrared"
//% category="Infrared"
namespace infrared {
let irState: IrState;
const MICROBIT_MAKERBIT_IR_NEC = 777;
const MICROBIT_MAKERBIT_IR_DATAGRAM = 778;
const MICROBIT_MAKERBIT_IR_BUTTON_PRESSED_ID = 789;
const MICROBIT_MAKERBIT_IR_BUTTON_RELEASED_ID = 790;
const IR_REPEAT = 256;
const IR_INCOMPLETE = 257;
const IR_DATAGRAM = 258;
interface IrState {
protocol: IrProtocol;
hasNewDatagram: boolean;
bitsReceived: uint8;
addressSectionBits: uint16;
commandSectionBits: uint16;
hiword: uint16;
loword: uint16;
}
function appendBitToDatagram(bit: number): number {
irState.bitsReceived += 1;
if (irState.bitsReceived <= 8) {
irState.hiword = (irState.hiword << 1) + bit;
if (irState.protocol === IrProtocol.Keyestudio && bit === 1) {
// recover from missing message bits at the beginning
// Keyestudio address is 0 and thus missing bits can be detected
// by checking for the first inverse address bit (which is a 1)
irState.bitsReceived = 9;
irState.hiword = 1;
}
} else if (irState.bitsReceived <= 16) {
irState.hiword = (irState.hiword << 1) + bit;
} else if (irState.bitsReceived <= 32) {
irState.loword = (irState.loword << 1) + bit;
}
if (irState.bitsReceived === 32) {
irState.addressSectionBits = irState.hiword & 0xffff;
irState.commandSectionBits = irState.loword & 0xffff;
return IR_DATAGRAM;
} else {
return IR_INCOMPLETE;
}
}
function decode(markAndSpace: number): number {
if (markAndSpace < 1600) {
// low bit
return appendBitToDatagram(0);
} else if (markAndSpace < 2700) {
// high bit
return appendBitToDatagram(1);
}
irState.bitsReceived = 0;
if (markAndSpace < 12500) {
// Repeat detected
return IR_REPEAT;
} else if (markAndSpace < 14500) {
// Start detected
return IR_INCOMPLETE;
} else {
return IR_INCOMPLETE;
}
}
function enableIrMarkSpaceDetection(pin: DigitalPin) {
pins.setPull(pin, PinPullMode.PullNone);
let mark = 0;
let space = 0;
pins.onPulsed(pin, PulseValue.Low, () => {
// HIGH, see https://github.com/microsoft/pxt-microbit/issues/1416
mark = pins.pulseDuration();
});
pins.onPulsed(pin, PulseValue.High, () => {
// LOW
space = pins.pulseDuration();
const status = decode(mark + space);
if (status !== IR_INCOMPLETE) {
control.raiseEvent(MICROBIT_MAKERBIT_IR_NEC, status);
}
});
}
/**
* Connects to the IR receiver module at the specified pin and configures the IR protocol.
* @param pin IR receiver pin, eg: DigitalPin.P0
* @param protocol IR protocol, eg: IrProtocol.Keyestudio
*/
//% subcategory="IR Receiver"
//% blockId="makerbit_infrared_connect_receiver"
//% block="connect IR receiver at pin %pin and decode %protocol"
//% pin.fieldEditor="gridpicker"
//% pin.fieldOptions.columns=4
//% pin.fieldOptions.tooltips="false"
//% weight=90
export function connectIrReceiver(
pin: DigitalPin,
protocol: IrProtocol
): void {
if (irState) {
return;
}
irState = {
protocol: protocol,
bitsReceived: 0,
hasNewDatagram: false,
addressSectionBits: 0,
commandSectionBits: 0,
hiword: 0, // TODO replace with uint32
loword: 0,
};
enableIrMarkSpaceDetection(pin);
let activeCommand = -1;
let repeatTimeout = 0;
const REPEAT_TIMEOUT_MS = 120;
control.onEvent(
MICROBIT_MAKERBIT_IR_NEC,
EventBusValue.MICROBIT_EVT_ANY,
() => {
const irEvent = control.eventValue();
// Refresh repeat timer
if (irEvent === IR_DATAGRAM || irEvent === IR_REPEAT) {
repeatTimeout = input.runningTime() + REPEAT_TIMEOUT_MS;
}
if (irEvent === IR_DATAGRAM) {
irState.hasNewDatagram = true;
control.raiseEvent(MICROBIT_MAKERBIT_IR_DATAGRAM, 0);
const newCommand = irState.commandSectionBits >> 8;
// Process a new command
if (newCommand !== activeCommand) {
if (activeCommand >= 0) {
control.raiseEvent(
MICROBIT_MAKERBIT_IR_BUTTON_RELEASED_ID,
activeCommand
);
}
activeCommand = newCommand;
control.raiseEvent(
MICROBIT_MAKERBIT_IR_BUTTON_PRESSED_ID,
newCommand
);
}
}
}
);
control.inBackground(() => {
while (true) {
if (activeCommand === -1) {
// sleep to save CPU cylces
basic.pause(2 * REPEAT_TIMEOUT_MS);
} else {
const now = input.runningTime();
if (now > repeatTimeout) {
// repeat timed out
control.raiseEvent(
MICROBIT_MAKERBIT_IR_BUTTON_RELEASED_ID,
activeCommand
);
activeCommand = -1;
} else {
basic.pause(REPEAT_TIMEOUT_MS);
}
}
}
});
}
/**
* Do something when a specific button is pressed or released on the remote control.
* @param code the button's number to be checked
* @param action the trigger action
* @param handler body code to run when the event is raised
*/
//% subcategory="IR Receiver"
//% blockId=makerbit_infrared_on_ir_button
//% block="on IR button | %code | %action"
//% button.fieldEditor="gridpicker"
//% button.fieldOptions.columns=3
//% button.fieldOptions.tooltips="false"
//% weight=50
export function onIrButton(
code: number | null,
action: IrButtonAction,
handler: () => void
) {
control.onEvent(
action === IrButtonAction.Pressed ? MICROBIT_MAKERBIT_IR_BUTTON_PRESSED_ID : MICROBIT_MAKERBIT_IR_BUTTON_RELEASED_ID,
code == null || code < 0 ? EventBusValue.MICROBIT_EVT_ANY : code,
() => {
handler();
}
);
}
/**
* Returns the code of the IR button that was pressed last. Returns -1 (IrButton.Any) if no button has been pressed yet.
*/
//% subcategory="IR Receiver"
//% blockId=makerbit_infrared_ir_button_pressed
//% block="IR button"
//% weight=70
export function irButton(): number {
if (!irState) {
return -1;
}
return irState.commandSectionBits >> 8;
}
/**
* Do something when an IR datagram is received.
* @param handler body code to run when the event is raised
*/
//% subcategory="IR Receiver"
//% blockId=makerbit_infrared_on_ir_datagram
//% block="on IR datagram received"
//% weight=40
export function onIrDatagram(handler: () => void) {
control.onEvent(
MICROBIT_MAKERBIT_IR_DATAGRAM,
EventBusValue.MICROBIT_EVT_ANY,
() => {
handler();
}
);
}
/**
* Returns the IR datagram as 32-bit hexadecimal string.
* The last received datagram is returned or "0x00000000" if no data has been received yet.
*/
//% subcategory="IR Receiver"
//% blockId=makerbit_infrared_ir_datagram
//% block="IR datagram"
//% weight=30
export function irDatagram(): string {
if (!irState) {
return "0x00000000";
}
return (
"0x" +
ir_rec_to16BitHex(irState.addressSectionBits) +
ir_rec_to16BitHex(irState.commandSectionBits)
);
}
/**
* Returns true if any IR data was received since the last call of this function. False otherwise.
*/
//% subcategory="IR Receiver"
//% blockId=makerbit_infrared_was_any_ir_datagram_received
//% block="IR data was received"
//% weight=80
export function wasIrDataReceived(): boolean {
if (!irState) {
return false;
}
if (irState.hasNewDatagram) {
irState.hasNewDatagram = false;
return true;
} else {
return false;
}
}
/**
* Returns the command code of a specific IR button.
* @param button the button
*/
//% subcategory="IR Receiver"
//% blockId=makerbit_infrared_button_code
//% button.fieldEditor="gridpicker"
//% button.fieldOptions.columns=3
//% button.fieldOptions.tooltips="false"
//% block="IR button code %button"
//% weight=60
//export function irButtonCode(button: IrButton): number {
// return button as number;
//}
function ir_rec_to16BitHex(value: number): string {
let hex = "";
for (let pos = 0; pos < 4; pos++) {
let remainder = value % 16;
if (remainder < 10) {
hex = remainder.toString() + hex;
} else {
hex = String.fromCharCode(55 + remainder) + hex;
}
value = Math.idiv(value, 16);
}
return hex;
}
}