-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinfrared.ts
366 lines (324 loc) · 11.4 KB
/
infrared.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// MakerBit blocks supporting a keyestudio Infrared Wireless Remote Control
const enum IrButton {
//% block=" "
Unused_1 = -1,
//% block="▲"
Up = 0x62,
//% block=" "
Unused_2 = -2,
//% block="◀"
Left = 0x22,
//% block="OK"
Ok = 0x02,
//% block="▶"
Right = 0xC2,
//% block=" "
Unused_3 = -3,
//% block="▼"
Down = 0xA8,
//% block=" "
Unused_4 = -4,
//% block="1"
Number_1 = 0x68,
//% block="2"
Number_2 = 0x98,
//% block="3"
Number_3 = 0xB0,
//% block="4"
Number_4 = 0x30,
//% block="5"
Number_5 = 0x18,
//% block="6"
Number_6 = 0x7A,
//% block="7"
Number_7 = 0x10,
//% block="8"
Number_8 = 0x38,
//% block="9"
Number_9 = 0x5A,
//% block="*"
Star = 0x42,
//% block="0"
Number_0 = 0x4A,
//% block="#"
Hash = 0x52
}
namespace makerbit {
let irState: IrState
const MICROBIT_MAKERBIT_IR_MARK_SPACE = 777
const MICROBIT_MAKERBIT_IR_BUTTON_PRESSED_ID = 789
const MICROBIT_MAKERBIT_IR_BUTTON_RELEASED_ID = 790
interface IrState {
necIr: NecIr
activeCommand: number
}
enum NecIrState {
DetectStartOrRepeat,
DetectBits
}
class NecIr {
state: NecIrState
bitsReceived: number
commandBits: number
inverseCommandBits: number
frequentlyUsedCommands: number[]
constructor(frequentlyUsedCommands: number[]) {
this.reset()
this.frequentlyUsedCommands = frequentlyUsedCommands
}
reset() {
this.bitsReceived = 0
this.commandBits = 0
this.inverseCommandBits = 0
this.state = NecIrState.DetectStartOrRepeat
}
detectStartOrRepeat(pulseToPulse: number): number {
if (pulseToPulse < 10000) {
return 0
} else if (pulseToPulse < 12500) {
this.state = NecIrState.DetectStartOrRepeat
return 256
} else if (pulseToPulse < 14500) {
this.state = NecIrState.DetectBits
return 0
} else {
return 0
}
}
static calculateCommand(commandBits: number, inverseCommandBits: number, frequentlyUsedCommands: number[]): number {
const controlBits = inverseCommandBits ^ 0xFF
if (commandBits === controlBits) {
return commandBits
} else if (frequentlyUsedCommands.indexOf(commandBits) >= 0) {
return commandBits
} else if (frequentlyUsedCommands.indexOf(controlBits) >= 0) {
return controlBits
} else {
return -1
}
}
pushBit(bit: number): number {
this.bitsReceived += 1
if (this.bitsReceived <= 16) {
// ignore all address bits
} else if (this.bitsReceived <= 24) {
this.commandBits = (this.commandBits << 1) + bit
} else if (this.bitsReceived < 32) {
this.inverseCommandBits = (this.inverseCommandBits << 1) + bit
} else if (this.bitsReceived === 32) {
this.inverseCommandBits = (this.inverseCommandBits << 1) + bit
const command = NecIr.calculateCommand(this.commandBits, this.inverseCommandBits, this.frequentlyUsedCommands)
this.reset()
return command
}
return 0
}
detectBit(pulseToPulse: number): number {
if (pulseToPulse < 1600) {
// low bit
return this.pushBit(0)
} else if (pulseToPulse < 2700) {
// high bit
return this.pushBit(1)
} else {
this.reset()
return -1
}
}
pushMarkSpace(markAndSpace: number): number {
switch (this.state) {
case NecIrState.DetectStartOrRepeat:
return this.detectStartOrRepeat(markAndSpace)
case NecIrState.DetectBits:
return this.detectBit(markAndSpace)
default:
return 0
}
}
}
function enableIrMarkSpaceDetection(pin: MakerBitPin) {
let mark = 0
let space = 0
pins.onPulsed(0 + pin, PulseValue.Low, () => {
// HIGH
mark = pins.pulseDuration()
})
pins.onPulsed(0 + pin, PulseValue.High, () => {
// LOW
space = pins.pulseDuration()
control.raiseEvent(MICROBIT_MAKERBIT_IR_MARK_SPACE, mark + space)
})
}
/**
* Connects to the IR receiver module at the specified pin.
* @param pin IR receiver pin, eg: MakerBitPin.A0
*/
//% subcategory="IR Remote"
//% blockId="makerbit_infrared_connect"
//% block="connect IR receiver at %pin"
//% pin.fieldEditor="gridpicker"
//% pin.fieldOptions.columns=3
//% pin.fieldOptions.tooltips="false"
//% weight=90
export function connectInfrared(pin: MakerBitPin): void {
if (!irState) {
irState = {
necIr: new NecIr([0x62, 0x22, 0x02, 0xC2, 0xA8, 0x68, 0x98, 0xB0, 0x30, 0x18, 0x7A, 0x10, 0x38, 0x5A, 0x42, 0x4A, 0x52]),
activeCommand: 0
}
enableIrMarkSpaceDetection(pin)
let activeCommand = 0
let repeatTimeout = 0
const REPEAT_TIMEOUT_MS = 120
control.onEvent(
MICROBIT_MAKERBIT_IR_MARK_SPACE,
EventBusValue.MICROBIT_EVT_ANY,
() => {
const newCommand = irState.necIr.pushMarkSpace(control.eventValue())
if (newCommand === 256) {
repeatTimeout = input.runningTime() + REPEAT_TIMEOUT_MS
} else if (newCommand > 0 && newCommand !== activeCommand) {
if (activeCommand !== 0) {
control.raiseEvent(MICROBIT_MAKERBIT_IR_BUTTON_RELEASED_ID, activeCommand)
}
repeatTimeout = input.runningTime() + REPEAT_TIMEOUT_MS
irState.activeCommand = newCommand
activeCommand = newCommand
control.raiseEvent(MICROBIT_MAKERBIT_IR_BUTTON_PRESSED_ID, newCommand)
} else if (newCommand < 0 && activeCommand !== 0) {
// Failed to decode command
irState.activeCommand = 0
control.raiseEvent(MICROBIT_MAKERBIT_IR_BUTTON_RELEASED_ID, activeCommand)
activeCommand = 0
}
}
)
control.inBackground(() => {
while (true) {
if (activeCommand === 0) {
basic.pause(REPEAT_TIMEOUT_MS)
} else {
const now = input.runningTime()
if (now > repeatTimeout) {
// repeat timeout
irState.activeCommand = 0
control.raiseEvent(MICROBIT_MAKERBIT_IR_BUTTON_RELEASED_ID, activeCommand)
activeCommand = 0
} else {
basic.pause(repeatTimeout - now + 2)
}
}
}
})
}
}
/**
* Do something when a specific button is pressed on the remote control.
* @param button the button to be checked
* @param handler body code to run when event is raised
*/
//% subcategory="IR Remote"
//% blockId=makerbit_infrared_on_ir_button_pressed
//% block="on IR button | %button pressed"
//% button.fieldEditor="gridpicker"
//% button.fieldOptions.columns=3
//% button.fieldOptions.tooltips="false"
//% weight=69
export function onIrButtonPressed(button: IrButton, handler: () => void) {
control.onEvent(
MICROBIT_MAKERBIT_IR_BUTTON_PRESSED_ID,
button,
handler)
}
/**
* Do something when a specific button is released on the remote control.
* @param button the button to be checked
* @param handler body code to run when event is raised
*/
//% subcategory="IR Remote"
//% blockId=makerbit_infrared_on_ir_button_released
//% block="on IR button | %button released"
//% button.fieldEditor="gridpicker"
//% button.fieldOptions.columns=3
//% button.fieldOptions.tooltips="false"
//% weight=68
export function onIrButtonReleased(button: IrButton, handler: () => void) {
control.onEvent(
MICROBIT_MAKERBIT_IR_BUTTON_RELEASED_ID,
button,
handler)
}
/**
* Returns true if a specific remote button is currently pressed. False otherwise.
* @param button the button to be checked
*/
//% subcategory="IR Remote"
//% blockId=makerbit_infrared_button_pressed
//% block="IR button | %button | is pressed"
//% button.fieldEditor="gridpicker"
//% button.fieldOptions.columns=3
//% button.fieldOptions.tooltips="false"
//% weight=67
export function isIrButtonPressed(button: IrButton): boolean {
return irState.activeCommand === button
}
/**
* Do something when an IR command is received.
* @param handler body code to run when event is raised
*/
//% subcategory="IR Remote"
//% blockId=makerbit_infrared_on_command
//% block="on IR button pressed"
//% weight=59
export function onIrCommandReceived(handler: () => void) {
control.onEvent(MICROBIT_MAKERBIT_IR_BUTTON_PRESSED_ID, EventBusValue.MICROBIT_EVT_ANY, () => {
setupContextAndNotify(handler)
})
}
/**
* Do something when an IR command is expired.
* @param handler body code to run when event is raised
*/
//% subcategory="IR Remote"
//% blockId=makerbit_infrared_on_command_expired
//% block="on IR button released"
//% weight=58
export function onIrCommandExpired(handler: () => void) {
control.onEvent(MICROBIT_MAKERBIT_IR_BUTTON_RELEASED_ID, EventBusValue.MICROBIT_EVT_ANY, () => {
setupContextAndNotify(handler)
})
}
function setupContextAndNotify(handler: () => void) {
const previousCommand = irState.activeCommand
const eventValue = control.eventValue()
irState.activeCommand = eventValue
handler()
if (irState.activeCommand === eventValue) {
irState.activeCommand = previousCommand
}
}
/**
* Returns the command code of the button that is currently pressed and 0 if no button is pressed.
*/
//% subcategory="IR Remote"
//% blockId=makerbit_infrared_command
//% block="IR button"
//% weight=57
export function irCommandCode(): number {
return irState.activeCommand
}
/**
* Turns an IR button into its corresponding command code.
* @param button the button
*/
//% subcategory="IR Remote"
//% blockId=makerbit_infrared_button
//% button.fieldEditor="gridpicker"
//% button.fieldOptions.columns=3
//% button.fieldOptions.tooltips="false"
//% block="IR button %button"
//% weight=56
export function irButton(button: IrButton): number {
return button as number
}
}