-
Notifications
You must be signed in to change notification settings - Fork 0
/
lorabit.ts
844 lines (743 loc) · 18.7 KB
/
lorabit.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
/**
*
* Extension block for lorabit LoRaWAN I2C-Device
* Itti Srisumalai: 2019
*
*/
const enum loraBit_freq_Plan {
//% block=AS923
AS923 = 0,
};
const enum loraJoin_Mode {
//% block=ABP
ABP = 0,
//% block=OTAA
OTAA = 1,
};
const enum loraBit_Confirmed {
//% block=Uncomfirmed
Uncomfirmed,
//% block=Confirmed
Confirmed,
};
const enum loraBit_ADR {
//% block=Off
Off = 0,
//% block=On
On = 1,
};
const enum loraJoin_State {
RESET = 0,
NOT_JOINED = 1,
JOINING = 2,
JOINED = 3,
JOIN_FAIL = 4,
};
const enum loraBit_Event {
UNKNOWN = -1,
RESET = 0,
INITED = 1,
JOINING = 2,
JOINED = 4,
JOIN_FAIL = 8, //JOIN_FAIL = 4, JOIN_DENIED = 4,
TX_COMPLETE = 16,
ACK_NOT_RECEIVED = 32,
TXRXPEND = 128,
}
const enum loraBit_Cmd {
GET_STATUS = 1,
RESET,
JOIN,
SEND,
SET_BUF_PTR,
GET_BUF_LEN,
GET_BUF_DATA,
SLEEP = 15,
CONFIG = 16,
DEVEUI_REG,
APPEUI_REG,
APPKEY_REG,
DEVADDR_REG = 33,
NWKSKEY_REG,
APPSKEY_REG,
NETID_REG,
}
const enum loraBit_Reg {
RX_REG = 1,
TIMER_REG,
DEVADDR_REG,
}
const enum Verbose_Mode {
//% block=Off
Off = 0,
//% block=On
On = 1,
};
const enum Running_State {
RUN = 0,
PENDING = -1,
SLEEP = 1
};
/**
* Custom blocks uf1eb
*/
//% color=#0071bc icon="\uf012" weight=96
namespace loraBit {
//%
function byteToHexString(value: number): string {
return (("0123456789ABCDEF"[value >> 4]) + ("0123456789ABCDEF"[value & 0xF]))
}
//%
function HexStringToVal(text: string): Buffer {
let boffset = text.length % 2
let len = (text.length / 2) + boffset
let temp = pins.createBuffer(len)
let b = 0
let h = 0
let offset = 0
let v = 0
for (let i = 0; i < text.length; i++) {
h = text.charCodeAt(i)
if ((h >= 48) && (h <= 57)) //'0' - '9'
offset = 48
else if ((h >= 65) && (h <= 70)) //'A' - 'F'
offset = 55
else if ((h >= 97) && (h <= 102)) //'a' - 'f'
offset = 87
else
offset = h
if (((i + boffset) % 2) != 0) {
temp[b] = v | (h - offset) //temp.setNumber(NumberFormat.UInt8LE, b, v | (h - offset));
b++;
} else
v = (h - offset) << 4
}
return temp
}
const I2C_ADDR: number = 0x63
const LORA_EVENT_ID: number = 8888
const RX_PAYLOAD_MAX_LEN: number = 32
const TX_TIMEOUT_1: number = 300000 //mSec
const TX_TIMEOUT_2: number = 600000 //mSec
let joinMode: number = loraJoin_Mode.OTAA
let ReceivedPort: number = 0
let ReceivedPayload: string = "0123456789ABCDEF0123456789ABCDEF"
let config: number = (1 * 64) + (5 % 8) * 8 + (2 % 8) //(joinMode * 128) +
//let APPEUI: Buffer = pins.createBuffer(8); APPEUI.fill(0);
//let DEVEUI: Buffer = pins.createBuffer(8); DEVEUI.fill(0);
//let APPKEY: Buffer = pins.createBuffer(16); APPKEY.fill(0);
//let NETID: number = 19 //TTN Network ID
//let DEVADDR: Buffer = pins.createBuffer(4); DEVADDR.fill(0);
//let NWKSKEY: Buffer = pins.createBuffer(16); NWKSKEY.fill(0);
//let APPSKEY: Buffer = pins.createBuffer(16); APPSKEY.fill(0);
let APPEUI: Buffer;
let DEVEUI: Buffer;
let APPKEY: Buffer;
let NETID: number = 19 //TTN Network ID
let DEVADDR: Buffer;
let NWKSKEY: Buffer;
let APPSKEY: Buffer;
let pause: boolean = false
let txrxpend: boolean = false
let pending: number = 0
let rxWindows: boolean = false
let joinState: number = loraJoin_State.JOINED
let rejoin: boolean = false
let nack: number = 0
let txto: number = input.runningTime() + TX_TIMEOUT_2 // TX,Rejoin timout
let verbose: number = Verbose_Mode.Off
let sleepmode: number = Running_State.RUN
//%
function timer_reset(ms = 300000) {
txto = input.runningTime() + ms
}
function timeout(): boolean {
//console.log(input.runningTime().toString())
//console.log(txto.toString())
if (input.runningTime() > txto)
return (true)
else
return (false)
}
function msg(m: string) {
if (verbose == Verbose_Mode.On)
console.log(m)
}
function readByte(register: number): number {
let cmd: Buffer = pins.createBuffer(1)
let temp: Buffer = pins.createBuffer(1)
cmd[0] = register
pins.i2cWriteBuffer(I2C_ADDR, cmd, false)
temp = pins.i2cReadBuffer(I2C_ADDR, 1, false)
return temp[0]
}
function readBuffer(len: number): Buffer {
let cmd: Buffer = pins.createBuffer(1)
let temp: Buffer = pins.createBuffer(len)
let x: number
cmd[0] = loraBit_Cmd.GET_BUF_DATA
pins.i2cWriteBuffer(I2C_ADDR, cmd, false)
for (x = 0; x < len; x++) {
cmd = pins.i2cReadBuffer(I2C_ADDR, 1, false)
temp[x] = cmd[0]
}
return temp
}
//%
function writeByte(register: number, value: number): void {
let temp: Buffer = pins.createBuffer(2);
temp[0] = register;
temp[1] = value;
pins.i2cWriteBuffer(I2C_ADDR, temp, false);
basic.pause(100)
}
//%
function writeBuffer(register: number, buf: Buffer): void {
let temp: Buffer = pins.createBuffer(buf.length + 1);
let x: number
temp[0] = register;
for (x = 0; x < buf.length; x++)
temp[x + 1] = buf[x]
pins.i2cWriteBuffer(I2C_ADDR, temp, false);
basic.pause(100)
}
//%
function getStatus(): number {
return readByte(loraBit_Cmd.GET_STATUS)
}
//%
function wakeup(): void {
if (sleepmode != Running_State.RUN) {
if (sleepmode == Running_State.SLEEP) {
while (!timeout())
basic.pause(100)
}
sleepmode = Running_State.RUN
timer_reset()
msg(">Wake up")
}
}
control.inBackground(() => {
let s0: number = loraBit_Event.UNKNOWN
let s = s0
let rxbuffer: Buffer = pins.createBuffer(RX_PAYLOAD_MAX_LEN)
let tmp: Buffer = pins.createBuffer(1)
let i: number
let len: number
let dlmsg: string
let wait: boolean
while (true) {
wait = true
while (wait) {
basic.pause(100)
if (pending > 0)
pause = true
else if (sleepmode == Running_State.SLEEP)
s0 = loraBit_Event.UNKNOWN
else if (txrxpend)
wait = false
}
if (timeout()) {
txrxpend = false
timer_reset()
joinState = loraJoin_State.NOT_JOINED
s0 = loraBit_Event.UNKNOWN
msg(">NOT JOINED")
msg(">WAIT JOIN")
}
s = getStatus()
if (s != s0) {
//console.log(s.toString())
s0 = s
if (!(s == loraBit_Event.RESET || (s & loraBit_Event.TXRXPEND) != 0)) {
if (joinState != loraJoin_State.JOINED) { // RESET) || NOT_JOINED) || JOIN_FAIL
if (s & loraBit_Event.JOINED) {
joinState = loraJoin_State.JOINED
txrxpend = false
timer_reset()
tmp[0] = config + (joinMode * 128)
writeBuffer(loraBit_Cmd.CONFIG, tmp)
msg("EV_JOINED")
}
else if (s & loraBit_Event.JOINING) {
joinState = loraJoin_State.JOINING
msg("EV_JOINING")
}
}
else {
if (!(s & loraBit_Event.JOINED)) {
if (s & loraBit_Event.JOIN_FAIL) {
joinState = loraJoin_State.JOIN_FAIL
timer_reset()
msg("EV_JOIN_FAILED")
msg(">WAIT REJOIN")
}
else {
joinState = loraJoin_State.NOT_JOINED
timer_reset()
msg(">NOT JOINED")
msg(">WAIT JOIN")
}
}
else if (txrxpend) {
if (s & loraBit_Event.TX_COMPLETE) {
dlmsg = ""
ReceivedPort = 0
ReceivedPayload = ""
if (s & loraBit_Event.ACK_NOT_RECEIVED) {
nack = 1
dlmsg = ",NA"
}
else {
writeByte(loraBit_Cmd.SET_BUF_PTR, loraBit_Reg.RX_REG)
len = readByte(loraBit_Cmd.GET_BUF_LEN)
//console.log(len.toString())
if (len > 0) {
dlmsg = ",A"
if (len > RX_PAYLOAD_MAX_LEN)
len = RX_PAYLOAD_MAX_LEN
rxbuffer = readBuffer(len)
ReceivedPort = rxbuffer[0]
if (len > 1) {
for (i = 0; i < len - 1; i++)
ReceivedPayload = ReceivedPayload + byteToHexString(rxbuffer[i + 1])
dlmsg = dlmsg + "," + byteToHexString(ReceivedPort) + "," + ReceivedPayload
}
}
}
txrxpend = false
timer_reset()
if (rxWindows && ((nack != 0) || (len > 0)))
control.raiseEvent(LORA_EVENT_ID, 1)
msg("EV_TXCOMPLETE" + dlmsg)
if (sleepmode == Running_State.PENDING) {
sleepmode = Running_State.SLEEP
writeByte(loraBit_Cmd.SLEEP, 0)
timer_reset(2000)
msg(">Sleep Mode")
}
}//TX_COMPLETE
}//txrxpend
}
}
} //s!=s0
}
})
//%
function do_reset(): boolean {
let s: number
let t1: number
let t0: number = 5 //times
let r = false
pause = false
pending = pending + 1
while (!pause)
basic.pause(100)
msg(">RESET")
do {
writeByte(loraBit_Cmd.RESET, 0)
t1 = 100 //10s
do {
basic.pause(100)
s = getStatus()
t1--
} while ((s != loraBit_Event.INITED) && (t1 > 0))
t0--
} while ((s != loraBit_Event.INITED) && (t0 > 0))
if (s == loraBit_Event.INITED) {
r = true
msg("EV_RESET")
}
pending = pending - 1
return (r)
}
/**
* Reset loraBit
*/
//% subcategory=Settings
//% weight=100
//% blockId="LoraBit_reset"
//% block="Reset loraBit"
export function reset(): void {
wakeup()
pause = false
pending = pending + 1
while (!pause)
basic.pause(100)
if (do_reset())
joinState = loraJoin_State.RESET
ReceivedPort = 0
ReceivedPayload = ""
txrxpend = false
timer_reset()
pending = 0
}
//%
function do_config(): void {
let tmp = pins.createBuffer(1)
pause = false
pending = pending + 1
while (!pause)
basic.pause(100)
tmp[0] = config + (joinMode * 128)
writeBuffer(loraBit_Cmd.CONFIG, tmp)
pending = pending - 1
}
/**
* Set Verbose mode
* @param mode
*/
//% subcategory=Settings
//% weight=99
//% help=loraBit/Verbose
//% blockId="loraBit_Verbose"
//% block="Set Verbose |mode %mode"
//% mode.defl=Verbose_Mode.On
export function Verbose(mode: Verbose_Mode): void {
verbose = mode
}
/**
* Set Configuration Parameter for The Air Authentication (OTAA)
* @param Datarate[0-6]
* @param Retrans[0-7]
* @param ADR
*/
//% subcategory=Settings
//% weight=99
//% help=loraBit/param_Config
//% blockId="loraBit_param_Config"
//% block="Set Configuration Parameter|Data Rate %Datarate|Retransmissions %Retrans|Adaptative Data Rate %ADR"
//% Datarate.min=0 Datarate.max=6 Datarate.defl=2
//% Retrans.min=0 Retrans.max=7 Retrans.defl=5
//% ADR.defl=Off
//% inlineInputMode=external
export function param_Config(Datarate: number, Retrans: number, ADR: loraBit_ADR): void {
wakeup()
pause = false
pending = pending + 1
while (!pause)
basic.pause(100)
config = (ADR * 64) + (Retrans % 8) * 8 + (Datarate % 8)
do_config()
txrxpend = false
timer_reset()
pending = pending - 1
}
/**
* Set Join Parameter for The Air Authentication (OTAA)
* @param DevEUI Device EUI Unique 8 bytes, Hexstring BE
* @param AppEUI Application EUI Unique 8 bytes, Hexstring BE
* @param AppKey AppKey 16 bytes, Hexstring BE
*/
//% subcategory=Settings
//% weight=99
//% help=loraBit/param_OTAA
//% blockId="loraBit_param_OTAA"
//% block="Set Join Parameter|Device EUI %DevEUI|Application EUI %AppEUI|App Key %AppKey"
//% DevEUI.defl="0011223344556677" AppEUI.defl="0011223344556677" AppKey.defl="00112233445566778899AABBCCDDEEFF"
export function param_OTAA(DevEUI: string, AppEUI: string, AppKey: string, dummy = 0): void {
pause = false
pending = pending + 1
while (!pause)
basic.pause(100)
DEVEUI = pins.createBuffer(8); DEVEUI.fill(0);
APPEUI = pins.createBuffer(8); APPEUI.fill(0);
APPSKEY = pins.createBuffer(16); APPSKEY.fill(0);
DEVEUI = HexStringToVal(DevEUI)
APPEUI = HexStringToVal(AppEUI)
APPKEY = HexStringToVal(AppKey)
joinMode = 1
rejoin = true
pending = pending - 1
}
/**
* Set Parameter for activating a device by personalization (ABP)
* @param DevAddr Device Address Unique 4 bytes, Hexstring BE
* @param NwkSKey Network Session Key 16 bytes, Hexstring BE
* @param AppSKey App Session Key 16 bytes, Hexstring BE
*/
//% subcategory=Settings
//% weight=98
//% help=loraBit/param_ABP
//% blockId="loraBit_param_ABP"
//% block="Set Session Parameter|Device Address %DevAddr|Network Session Key %NwkSKey|App Session Key %AppSKey"
//% DevAddr.defl="00112233" NwkSKey.defl="00112233445566778899AABBCCDDEEFF" AppSKey.defl="00112233445566778899AABBCCDDEEFF"
export function param_ABP(DevAddr: string, NwkSKey: string, AppSKey: string, NetID = 19): void {
pause = false
pending = pending + 1
while (!pause)
basic.pause(100)
DEVADDR = pins.createBuffer(4); DEVADDR.fill(0);
NWKSKEY = pins.createBuffer(16); NWKSKEY.fill(0);
APPSKEY = pins.createBuffer(16); APPSKEY.fill(0);
DEVADDR = HexStringToVal(DevAddr)
NWKSKEY = HexStringToVal(NwkSKey)
APPSKEY = HexStringToVal(AppSKey)
NETID = NetID
joinMode = 0
rejoin = true
pending = pending - 1
}
//%
function do_join(): void {
let s: number
let tmp: Buffer
let timeout: number
pause = false
pending = pending + 1
while (!pause)
basic.pause(100)
if (joinState != loraJoin_State.RESET)
if (do_reset()) joinState = loraJoin_State.RESET
if (joinState == loraJoin_State.RESET) {
msg(">JOIN")
let tmp = pins.createBuffer(1)
tmp[0] = config + (joinMode * 128)
writeBuffer(loraBit_Cmd.CONFIG, tmp)
if (rejoin) {
if (joinMode == loraJoin_Mode.ABP) {
writeBuffer(loraBit_Cmd.DEVADDR_REG, DEVADDR)
writeBuffer(loraBit_Cmd.NWKSKEY_REG, NWKSKEY)
writeBuffer(loraBit_Cmd.APPSKEY_REG, APPSKEY)
tmp = pins.createBuffer(4)
tmp.setNumber(NumberFormat.UInt32LE, 1, NETID);
writeBuffer(loraBit_Cmd.NETID_REG, tmp)
}
else {
writeBuffer(loraBit_Cmd.DEVEUI_REG, DEVEUI)
writeBuffer(loraBit_Cmd.APPEUI_REG, APPEUI)
writeBuffer(loraBit_Cmd.APPKEY_REG, APPKEY)
}
}
writeByte(loraBit_Cmd.JOIN, 0)
//console.log("EV_JOINING")
}
pending = pending - 1
}
/**
* Join network
*/
//% subcategory=Settings
//% weight=100
//% help=loraBit/join
//% blockId="loraBit_join"
//% block="Join Network|%freq"
//% freq.defl=loraBit_freq_Plan.AS923
export function join(freq: loraBit_freq_Plan = loraBit_freq_Plan.AS923): void {
wakeup()
pause = false
pending = pending + 1
while (!pause)
basic.pause(100)
if (joinState == loraJoin_State.RESET) {
do_join()
rejoin = false
ReceivedPort = 0
ReceivedPayload = ""
timer_reset(TX_TIMEOUT_2)
txrxpend = true
}
pending = pending - 1
}
/**
* When receive a LoRa packet
* @param handler code to run
*/
//% weight=98
//% blockId="loraBit_whenReceived"
//% block="When Receive"
export function whenReceived(handler: Action): void {
rxWindows = true
control.onEvent(LORA_EVENT_ID, 1, handler)
}
/**
* is Joined
*/
//% weight=98
//% blockId="loraBit_joined"
//% block="Joined"
//% icon="\uf085"
export function joined(): boolean {
if (joinState == loraJoin_State.JOINED)
return true
else
return false
}
/**
* is Not Acknowledged
*/
//% weight=98
//% blockId="loraBit_nacknowledged"
//% block="Not Acknowledged"
//% icon="\uf085"
export function nacknowledged(): boolean {
if (nack == 1)
return true
return false
}
/**
* Available
*/
//% weight=98
//% blockId="loraBit_available"
//% block="available"
//% icon="\uf085"
export function available(): boolean {
if (!txrxpend) // && (joinState == loraJoin_State.JOINED))
return true
else
return false
}
/**
* Received Payload Hextstring
*/
//% weight=98
//% blockId="loraBit_getReceivedPayload"
//% block="Received Payload"
//% icon="\uf085"
export function getReceivedPayload(): string {
return ReceivedPayload
}
/**
* Received Port
*/
//% weight=98
//% blockId="loraBit_getReceivedPort"
//% block="Received Port"
//% icon="\uf085"
export function getReceivedPort(): number {
return ReceivedPort
}
/**
* Enter sleep mode
* @param none
*/
//% weight=99
//% help=loraBit/sleep
//% blockId="loraBit_sleep"
//% block="Sleep"
export function sleep(): void {
if (txrxpend || (pending > 0))
sleepmode = Running_State.PENDING
else {
pause = false
pending = pending + 1
while (!pause)
basic.pause(100)
sleepmode = Running_State.SLEEP
writeByte(loraBit_Cmd.SLEEP, 0)
timer_reset(2000)
msg(">Sleep Mode")
pending = pending - 1
}
}
/**
* Send Confirmed/Unconfirmed LoRaWAN packet
* @param confirmed Confirm
* @param port LoRaWAN Port
* @param payload Hex String
*/
//% weight=99
//% help=loraBit/sendPacket
//% blockId="loraBit_sendPacket"
//% block="Transmitt %confirmed| at Port %port| with Payload %payload"
//% port.min=1 port.max=253
//% port.defl=1
//% payload.defl="48656c6c6f2c20576f726c6421"
//% confirmed.defl=loraBit_Confirmed.Uncomfirmed
export function sendPacket(confirmed: loraBit_Confirmed, port: number, payload: string): void {
wakeup()
if (txrxpend || (pending > 0))
msg("OP_TXRXPEND")
else {
pause = false
pending = pending + 1
while (!pause)
basic.pause(100)
if (rejoin)
msg(">Send packet: Join require")
else {
if (joinState != loraJoin_State.JOINED) {
if (joinState == loraJoin_State.JOINING)
msg(">Send packet: Wait joining")
else {
do_join()
rejoin = false
timer_reset(TX_TIMEOUT_2)
txrxpend = true
}
}
else {
let len = (payload.length / 2) + (payload.length % 2)
let buf: Buffer = pins.createBuffer(2 + len)
nack = 0
if (confirmed == loraBit_Confirmed.Uncomfirmed)
buf[0] = 0
else {
nack = -1
buf[0] = 1
}
buf[1] = port
let tmp: Buffer
tmp = HexStringToVal(payload);
for (let i = 0; i < tmp.length; i++)
buf[i + 2] = tmp[i]
writeBuffer(loraBit_Cmd.SEND, buf);
ReceivedPort = 0
ReceivedPayload = ""
timer_reset()
txrxpend = true
msg(">Send packet")
}
}
pending = pending - 1
}
}
/**
* Pack Text to HexString.
* @param text to convert, eg: "Hello"
*/
//% weight=98
//% help=loraBit/packHexString
//% blockId="loraBit_packHexString"
//% block="Convert|%text to hex string"
//% icon="\uf085"
//% text.defl="Hello, World!"
export function packHexString(text: string): string {
let hexstr = ""
for (let i = 0; i < text.length; i++)
hexstr = hexstr + byteToHexString(text.charCodeAt(i))
return hexstr
}
/**
* Unpack Hexstring to Text.
* @param text to convert, eg: "313233414243" -> "123ABC"
*/
//% weight=98
//% help=loraBit/unpackHexString
//% blockId="loraBit_unpackHexString"
//% block="Convert hex string|%text to text"
//% icon="\uf085"
//% text.defl="313233414243"
export function unpackHexString(text: string): string {
let str = ""
let temp: Buffer = HexStringToVal(text)
for (let i = 0; i < temp.length; i++)
str = str + String.fromCharCode(temp[i])
return str
}
/**
* Convert Int8 to Hexstring.
* @param to convert, eg: 10 -> "0A"
*/
//% weight=98
//% help=loraBit/toHexString
//% blockId="loraBit_toHexString"
//% block="Convert Int8|%num to Hexstring"
//% icon="\uf085"
//% num.min=0 num.max=255
//% num.defl=0
export function toHexString(num: number): string {
return byteToHexString(num)
}
}