forked from lioujj/pxt-IR
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.ts
302 lines (282 loc) · 10.3 KB
/
main.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
//% weight=0 color=#87bc4b icon="\uf1eb" block="InfraRed"
namespace IR {
export enum encodingType {
//% block="NEC"
NEC,
//% block="SONY"
SONY
}
let tempHandler: Action;
let irLed = AnalogPin.P16;
const pwmPeriod = 26;
pins.analogWritePin(irLed, 0);
pins.analogSetPeriod(irLed, pwmPeriod);
let send_init = false;
let rec_init = false;
let arr: number[] = []
let received = false
let first = true
let rec_Type = ""
let messageStr = ""
let recPin = DigitalPin.P8
let thereIsHandler = false
arr = []
function transmitBit(highTime: number, lowTime: number): void {
pins.analogWritePin(irLed, 512);
control.waitMicros(highTime);
pins.analogWritePin(irLed, 0);
control.waitMicros(lowTime);
}
/**
* set the infrared LED pin.
*/
//% blockId=setIR_pin block="set IR LED pin: %myPin" blockExternalInputs=false
//% weight=90 blockGap=10
//% myPin.fieldEditor="gridpicker" myPin.fieldOptions.columns=4
//% myPin.fieldOptions.tooltips="false" myPin.fieldOptions.width="300"
export function setIR_pin(myPin: AnalogPin) {
irLed = myPin;
pins.analogWritePin(irLed, 0);
pins.analogSetPeriod(irLed, pwmPeriod);
send_init = true;
}
/**
* set the IR receiver pin.
*/
//% blockId=setREC_pin block="set IR receiver pin: %myPin" blockExternalInputs=false
//% weight=85 blockGap=10
//% myPin.fieldEditor="gridpicker" myPin.fieldOptions.columns=4
//% myPin.fieldOptions.tooltips="false" myPin.fieldOptions.width="300"
export function setREC_pin(myPin: DigitalPin) {
recPin = myPin;
pins.setEvents(recPin, PinEventType.Pulse)
pins.setPull(recPin, PinPullMode.PullUp)
pins.onPulsed(recPin, PulseValue.Low, function () {
arr.push(input.runningTimeMicros())
})
pins.onPulsed(recPin, PulseValue.High, function () {
arr.push(input.runningTimeMicros())
})
control.onEvent(recPin, DAL.MICROBIT_PIN_EVENT_ON_TOUCH, tempHandler);
rec_init = true;
}
/**
* send message from IR LED. You must set the message encoding type, send how many times, and the message.
*/
//% blockId=sendMyMessage1 block="send message: %msg| ,%times| times, encoding type:%myType"
//% weight=80 blockGap=10
export function sendMyMessage1(msg: string, times: number, myType: encodingType): void {
if (send_init) {
//control.inBackground(() => {
sendMessage(convertHexStrToNum(msg), times, myType);
//})
}
}
/**
* send message from IR LED. You must set the message encoding type, send how many times, and the message.
*/
//% blockId=sendMyMessage2 block="send message: %msg| ,%times| times, encoding type:%myType"
//% weight=75 blockGap=10
export function sendMyMessage2(msg: string, times: number, myType: string): void {
if (send_init) {
if (myType == "NEC") {
sendMessage(convertHexStrToNum(msg), times, encodingType.NEC);
} else if (myType == "SONY") {
sendMessage(convertHexStrToNum(msg), times, encodingType.SONY);
}
}
}
function encode(myCode: number, bits: number, trueHigh: number, trueLow: number, falseHigh: number, falseLow: number): void {
const MESSAGE_BITS = bits;
for (let mask = 1 << (MESSAGE_BITS - 1); mask > 0; mask >>= 1) {
if (myCode & mask) {
transmitBit(trueHigh, trueLow);
} else {
transmitBit(falseHigh, falseLow);
}
}
}
function sendNEC(message: number, times: number): void {
const enum NEC {
startHigh = 9000,
startLow = 4500,
stopHigh = 560,
stopLow = 0,
trueHigh = 560,
trueLow = 1690,
falseHigh = 560,
falseLow = 560,
interval = 110000
}
let address = message >> 16;
let command = message % 0x010000;
const MESSAGE_BITS = 16;
let startTime = 0;
let betweenTime = 0;
for (let sendCount = 0; sendCount < times; sendCount++) {
startTime = input.runningTimeMicros();
transmitBit(NEC.startHigh, NEC.startLow);
encode(address, 16, NEC.trueHigh, NEC.trueLow, NEC.falseHigh, NEC.falseLow);
encode(command, 16, NEC.trueHigh, NEC.trueLow, NEC.falseHigh, NEC.falseLow);
transmitBit(NEC.stopHigh, NEC.stopLow);
betweenTime = input.runningTimeMicros() - startTime
if (times > 0)
control.waitMicros(NEC.interval - betweenTime);
}
}
function sendSONY(message: number, times: number): void {
const enum SONY {
startHigh = 2300,
startLow = 500,
trueHigh = 1100,
trueLow = 500,
falseHigh = 500,
falseLow = 500,
interval = 45000
}
const MESSAGE_BITS = 12;
let startTime = 0;
let betweenTime = 0;
for (let sendCount = 0; sendCount < times; sendCount++) {
startTime = input.runningTimeMicros();
transmitBit(SONY.startHigh, SONY.startLow);
encode(message, 12, SONY.trueHigh, SONY.trueLow, SONY.falseHigh, SONY.falseLow);
betweenTime = input.runningTimeMicros() - startTime
if (times > 0)
control.waitMicros(SONY.interval - betweenTime);
}
}
export function sendMessage(message: number, times: number, myType: encodingType): void {
switch (myType) {
case encodingType.NEC: sendNEC(message, times);
case encodingType.SONY: sendSONY(message, times);
default: sendNEC(message, times);
}
}
function convertHexStrToNum(myMsg: string): number {
let myNum = 0
for (let i = 0; i < myMsg.length; i++) {
if ((myMsg.charCodeAt(i) > 47) && (myMsg.charCodeAt(i) < 58)) {
myNum += (myMsg.charCodeAt(i) - 48) * (16 ** (myMsg.length - 1 - i))
} else if ((myMsg.charCodeAt(i) > 96) && (myMsg.charCodeAt(i) < 103)) {
myNum += (myMsg.charCodeAt(i) - 87) * (16 ** (myMsg.length - 1 - i))
} else if ((myMsg.charCodeAt(i) > 64) && (myMsg.charCodeAt(i) < 71)) {
myNum += (myMsg.charCodeAt(i) - 55) * (16 ** (myMsg.length - 1 - i))
} else {
myNum = 0
break
}
}
return myNum
}
//------------------receiver-------------
function resetReceiver() {
arr = []
received = false
}
control.inBackground(function () {
basic.forever(function () {
if ((!received) && (rec_init)) {
if (arr.length > 20) {
if ((input.runningTimeMicros() - arr[arr.length - 1]) > 120000) {
if (first) {
resetReceiver()
first = false
} else {
received = true
decodeIR();
}
}
}
}
})
})
function decodeIR() {
let addr = 0
let command = 0
messageStr = ""
rec_Type = ""
for (let i = 0; i <= arr.length - 1 - 1; i++) {
arr[i] = arr[i + 1] - arr[i]
}
if (((arr[0] + arr[1]) > 13000) && ((arr[0] + arr[1]) < 14000)) {
rec_Type = "NEC"
arr.removeAt(1)
arr.removeAt(0)
addr = pulseToDigit(0, 15, 1600)
command = pulseToDigit(16, 31, 1600)
messageStr = convertNumToHexStr(addr, 4) + convertNumToHexStr(command, 4)
arr = [];
if (thereIsHandler) {
tempHandler();
}
} else if (((arr[0] + arr[1]) > 2600) && ((arr[0] + arr[1]) < 3200)) {
rec_Type = "SONY"
arr.removeAt(1)
arr.removeAt(0)
command = pulseToDigit(0, 11, 1300)
messageStr = convertNumToHexStr(command, 3)
arr = [];
if (thereIsHandler) {
tempHandler();
}
}
resetReceiver();
}
function pulseToDigit(beginBit: number, endBit: number, duration: number): number {
let myNum = 0
for (let i = beginBit; i <= endBit; i++) {
myNum <<= 1
if ((arr[i * 2] + arr[i * 2 + 1]) < duration) {
myNum += 0
} else {
myNum += 1
}
}
return myNum
}
function convertNumToHexStr(myNum: number, digits: number): string {
let tempDiv = 0
let tempMod = 0
let myStr = ""
tempDiv = myNum
while (tempDiv > 0) {
tempMod = tempDiv % 16
if (tempMod > 9) {
myStr = String.fromCharCode(tempMod - 10 + 97) + myStr
} else {
myStr = tempMod + myStr
}
tempDiv = Math.idiv(tempDiv, 16)
}
while (myStr.length != digits) {
myStr = "0" + myStr
}
return myStr
}
/**
* Do something when a receive IR
*/
//% blockId=onReceivedIR block="on IR message received" blockInlineInputs=true
//% weight=70 blockGap=10
export function onReceivedIR(handler: Action): void {
tempHandler = handler
thereIsHandler = true
}
/**
* return the encoding type of the received IR
*/
//% blockId=getRecType block="the received IR encoding type"
//% weight=60 blockGap=10
export function getRecType(): string {
return rec_Type
}
/**
* return the message of the received IR
*/
//% blockId=getMessage block="the received IR message"
//% weight=60 blockGap=10
export function getMessage(): string {
return messageStr
}
}