@@ -69,7 +69,7 @@ const COMMAND_BLE_PACKET_TEST = 0x00;
69
69
*
70
70
* If the slot is specified, returns one byte indicating the command, the slot number used, and then 80 bytes of data made from the following code:
71
71
* ```javascript
72
- * var output = Uint8Array(80);
72
+ * let output = Uint8Array(80);
73
73
* output.set(tags[slot].buffer.slice(0, 8), 0);
74
74
* output.set(tags[slot].buffer.slice(16, 24), 8);
75
75
* output.set(tags[slot].buffer.slice(32, 52), 20);
@@ -233,37 +233,37 @@ const ENABLE_LEDS = ENABLE_LED1 || ENABLE_LED2 || ENABLE_LED3;
233
233
/**
234
234
* The active tag index.
235
235
*/
236
- var currentTag = 0 ;
236
+ let currentTag = 0 ;
237
237
238
238
/**
239
239
* Contains the timeout between changing tags.
240
240
*/
241
- var changeTagTimeout = null ;
241
+ let changeTagTimeout = null ;
242
242
243
243
/**
244
244
* A buffer used by the NTAG215 emulator.
245
245
*/
246
- var txBuffer = new Uint8Array ( 32 ) ;
246
+ let txBuffer = new Uint8Array ( 32 ) ;
247
247
248
248
/**
249
249
* An array of the in-memory tags, unused if {@link SAVE_TO_FLASH} is true
250
250
*/
251
- var tags = [ ] ;
251
+ let tags = [ ] ;
252
252
253
253
/**
254
254
* If {@link fastRx} should process data.
255
255
*/
256
- var rxPaused = false ;
256
+ let rxPaused = false ;
257
257
258
258
/**
259
259
* Auto-sleep timeout reference.
260
260
*/
261
- var autoSleepTimeout = null ;
261
+ let autoSleepTimeout = null ;
262
262
263
263
/**
264
264
* If bluetooth is currently connected.
265
265
*/
266
- var bluetoothConnected = false ;
266
+ let bluetoothConnected = false ;
267
267
// #endregion
268
268
269
269
// #region Tag initialization
@@ -321,7 +321,7 @@ function fixUid() {
321
321
*/
322
322
function hexDump ( inputData ) {
323
323
// Initialize an empty string `line`, which will be used to build the output lines containing the hexadecimal values.
324
- var line = "" ;
324
+ let line = "" ;
325
325
326
326
// Iterate through each element of the `inputData` array.
327
327
for ( let i = 0 ; i < inputData . length ; i ++ ) {
@@ -360,23 +360,23 @@ function hexDump(inputData) {
360
360
* @returns {Uint8Array } A 9-byte Uint8Array containing the generated UID.
361
361
*/
362
362
function generateUid ( ) {
363
- var uid = new Uint8Array ( 9 ) ;
363
+ let uid = new Uint8Array ( 9 ) ;
364
364
365
365
// Set the first byte as 0x04
366
366
uid [ 0 ] = 0x04 ;
367
367
368
368
// Set the next two bytes as random values between 0 and 255
369
- uid [ 1 ] = Math . round ( Math . random ( ) * 255 ) ;
370
- uid [ 2 ] = Math . round ( Math . random ( ) * 255 ) ;
369
+ uid [ 1 ] = _MathRound ( _MathRandom ( ) * 255 ) ;
370
+ uid [ 2 ] = _MathRound ( _MathRandom ( ) * 255 ) ;
371
371
372
372
// Set the fourth byte using XOR operation with specific values
373
373
uid [ 3 ] = uid [ 0 ] ^ uid [ 1 ] ^ uid [ 2 ] ^ 0x88 ;
374
374
375
375
// Set the next four bytes as random values between 0 and 255
376
- uid [ 4 ] = Math . round ( Math . random ( ) * 255 ) ;
377
- uid [ 5 ] = Math . round ( Math . random ( ) * 255 ) ;
378
- uid [ 6 ] = Math . round ( Math . random ( ) * 255 ) ;
379
- uid [ 7 ] = Math . round ( Math . random ( ) * 255 ) ;
376
+ uid [ 4 ] = _MathRound ( _MathRandom ( ) * 255 ) ;
377
+ uid [ 5 ] = _MathRound ( _MathRandom ( ) * 255 ) ;
378
+ uid [ 6 ] = _MathRound ( _MathRandom ( ) * 255 ) ;
379
+ uid [ 7 ] = _MathRound ( _MathRandom ( ) * 255 ) ;
380
380
381
381
// Set the last byte using XOR operation with specific values
382
382
uid [ 8 ] = uid [ 4 ] ^ uid [ 5 ] ^ uid [ 6 ] ^ uid [ 7 ] ;
@@ -389,7 +389,7 @@ function generateUid() {
389
389
* @returns {Uint8Array } - The generated tag.
390
390
*/
391
391
function generateBlankTag ( ) {
392
- var tag = new Uint8Array ( 572 ) ;
392
+ let tag = new Uint8Array ( 572 ) ;
393
393
394
394
// Generate blank NTAG215 tags with random, but valid UID.
395
395
tag . set ( generateUid ( ) , 0 ) ;
@@ -518,7 +518,7 @@ function cycleTags() {
518
518
*/
519
519
function getBufferClone ( buffer ) {
520
520
if ( buffer ) {
521
- var output = new Uint8Array ( buffer . length ) ;
521
+ let output = new Uint8Array ( buffer . length ) ;
522
522
output . set ( buffer ) ;
523
523
524
524
return output ;
@@ -549,7 +549,7 @@ function saveTag(slot) {
549
549
* Saves all tags to flash.
550
550
*/
551
551
function saveAllTags ( ) {
552
- for ( var i = 0 ; i < tags . length ; i ++ ) {
552
+ for ( let i = 0 ; i < tags . length ; i ++ ) {
553
553
saveTag ( i ) ;
554
554
}
555
555
}
@@ -769,15 +769,15 @@ function onFastModeDisconnect() {
769
769
function rxBytes ( count , callback ) {
770
770
rxPaused = true ;
771
771
772
- var buffer = new Uint8Array ( count ) ;
773
- var position = 0 ;
772
+ let buffer = new Uint8Array ( count ) ;
773
+ let position = 0 ;
774
774
775
775
function receive ( data ) {
776
776
buffer . set ( new Uint8Array ( E . toUint8Array ( data ) , 0 , Math . min ( data . length , count - position ) ) , position ) ;
777
777
position = position + data . length ;
778
778
779
779
if ( position >= count ) {
780
- var tempBuffer = buffer ;
780
+ let tempBuffer = buffer ;
781
781
disconnect ( ) ;
782
782
callback ( tempBuffer ) ;
783
783
}
@@ -808,40 +808,27 @@ function fastRx(data) {
808
808
// data is a string, so you want to convert to an array to work with
809
809
data = E . toUint8Array ( data ) ;
810
810
811
- // define some variables to use.
812
- var slot ,
813
- startIdx ,
814
- dataSize ,
815
- sourceData ,
816
- oldSlot ,
817
- newSlot ,
818
- response ,
819
- nullIdx ,
820
- crc32 ,
821
- tag ,
822
- count ,
823
- i ;
824
-
825
811
if ( data . length > 0 ) {
826
812
switch ( data [ 0 ] ) {
827
- case COMMAND_BLE_PACKET_TEST : //BLE Packet Test
813
+ case COMMAND_BLE_PACKET_TEST : { //BLE Packet Test
828
814
_Bluetooth . write ( new Uint8Array ( data . length > 1 ? data [ 1 ] : 255 ) ) ;
829
815
830
816
return ;
817
+ }
831
818
832
- case COMMAND_SLOT_INFORMATION : //Slot Information <Slot>
819
+ case COMMAND_SLOT_INFORMATION : { //Slot Information <Slot>
833
820
if ( data . length > 1 ) {
834
- count = data . length > 2 ? data [ 2 ] : 1 ;
821
+ let count = data . length > 2 ? data [ 2 ] : 1 ;
835
822
836
823
//Returns a subset of data for identifying
837
- slot = data [ 1 ] < tags . length ? data [ 1 ] : currentTag ;
824
+ let slot = data [ 1 ] < tags . length ? data [ 1 ] : currentTag ;
838
825
839
826
if ( slot + count > tags . length ) {
840
827
count = tags . length - slot ;
841
828
}
842
829
843
- for ( i = slot ; i < ( slot + count ) ; i ++ ) {
844
- var tagData = getTagInfo ( i ) ;
830
+ for ( let i = slot ; i < ( slot + count ) ; i ++ ) {
831
+ let tagData = getTagInfo ( i ) ;
845
832
846
833
_Bluetooth . write ( [ COMMAND_SLOT_INFORMATION , i ] ) ;
847
834
_Bluetooth . write ( tagData ) ;
@@ -852,32 +839,34 @@ function fastRx(data) {
852
839
}
853
840
854
841
return ;
842
+ }
855
843
856
- case COMMAND_READ : //Read <Slot> <StartPage> <PageCount>
844
+ case COMMAND_READ : { //Read <Slot> <StartPage> <PageCount>
857
845
//Max pages: 143
858
846
//Returns 0x02 <Slot> <StartPage> <PageCount> <Data>
859
- startIdx = data [ 2 ] * 4 ;
860
- dataSize = data [ 3 ] * 4 ;
861
- slot = data [ 1 ] < tags . length ? data [ 1 ] : currentTag ;
862
- sourceData = getTag ( slot ) . slice ( startIdx , startIdx + dataSize ) ;
847
+ let startIdx = data [ 2 ] * 4 ;
848
+ let dataSize = data [ 3 ] * 4 ;
849
+ let slot = data [ 1 ] < tags . length ? data [ 1 ] : currentTag ;
850
+ let sourceData = getTag ( slot ) . slice ( startIdx , startIdx + dataSize ) ;
863
851
864
852
if ( ENABLE_LOG ) {
865
853
//_consoleLog("Reading from slot: " + slot);
866
854
//_consoleLog("Read from " + startIdx + " - " + (startIdx + dataSize));
867
855
}
868
856
869
- response = Uint8Array ( 4 ) ;
857
+ let response = Uint8Array ( 4 ) ;
870
858
response . set ( Uint8Array ( data , 0 , 4 ) , 0 ) ;
871
859
response [ 1 ] = slot ;
872
860
_Bluetooth . write ( response ) ;
873
861
_Bluetooth . write ( sourceData ) ;
874
862
875
863
return ;
864
+ }
876
865
877
- case COMMAND_WRITE : //Write <Slot> <StartPage> <Data>
878
- startIdx = data [ 2 ] * 4 ;
879
- dataSize = data . length - 3 ;
880
- slot = data [ 1 ] < tags . length ? data [ 1 ] : currentTag ;
866
+ case COMMAND_WRITE : { //Write <Slot> <StartPage> <Data>
867
+ let startIdx = data [ 2 ] * 4 ;
868
+ let dataSize = data . length - 3 ;
869
+ let slot = data [ 1 ] < tags . length ? data [ 1 ] : currentTag ;
881
870
882
871
//store data if it fits into memory
883
872
if ( ( startIdx + dataSize ) <= 572 ) {
@@ -893,21 +882,23 @@ function fastRx(data) {
893
882
_Bluetooth . write ( [ COMMAND_WRITE , slot , data [ 2 ] ] ) ;
894
883
895
884
return ;
885
+ }
896
886
897
- case COMMAND_SAVE : //Save <Slot>
887
+ case COMMAND_SAVE : { //Save <Slot>
898
888
if ( SAVE_TO_FLASH ) {
899
- slot = data [ 1 ] < tags . length ? data [ 1 ] : currentTag ;
889
+ let slot = data [ 1 ] < tags . length ? data [ 1 ] : currentTag ;
900
890
901
891
saveTag ( slot ) ;
902
892
}
903
893
904
894
_Bluetooth . write ( [ COMMAND_SAVE , data [ 1 ] ] ) ;
905
895
906
896
return ;
897
+ }
907
898
908
- case COMMAND_FULL_WRITE : //Full Write <Slot>
909
- slot = data [ 1 ] ;
910
- crc32 = null ;
899
+ case COMMAND_FULL_WRITE : { //Full Write <Slot>
900
+ let slot = data [ 1 ] ;
901
+ let crc32 = null ;
911
902
912
903
if ( data . length == 6 ) {
913
904
crc32 = data . slice ( 2 , 6 ) ;
@@ -935,29 +926,31 @@ function fastRx(data) {
935
926
} , 0 ) ;
936
927
937
928
return ;
929
+ }
938
930
939
- case COMMAND_FULL_READ : //Full Read <Slot> <Count>
940
- count = data . length > 2 ? data [ 2 ] : 1 ;
931
+ case COMMAND_FULL_READ : { //Full Read <Slot> <Count>
932
+ let count = data . length > 2 ? data [ 2 ] : 1 ;
941
933
942
934
//Returns a subset of data for identifying
943
- slot = data [ 1 ] < tags . length ? data [ 1 ] : currentTag ;
935
+ let slot = data [ 1 ] < tags . length ? data [ 1 ] : currentTag ;
944
936
945
937
if ( slot + count > tags . length ) {
946
938
count = tags . length - slot ;
947
939
}
948
940
949
- for ( i = slot ; i < ( slot + count ) ; i ++ ) {
950
- tag = getTag ( i ) ;
951
- crc32 = getCRC32 ( tag ) ;
941
+ for ( let i = slot ; i < ( slot + count ) ; i ++ ) {
942
+ let tag = getTag ( i ) ;
943
+ let crc32 = getCRC32 ( tag ) ;
952
944
_Bluetooth . write ( [ COMMAND_FULL_READ , i , crc32 [ 0 ] , crc32 [ 1 ] , crc32 [ 2 ] , crc32 [ 3 ] ] ) ;
953
945
_Bluetooth . write ( tag ) ;
954
946
}
955
947
956
948
return ;
949
+ }
957
950
958
- case COMMAND_CLEAR_SLOT : //Clear Slot <Slot>
959
- slot = data [ 1 ] ;
960
- tag = generateBlankTag ( ) ;
951
+ case COMMAND_CLEAR_SLOT : { //Clear Slot <Slot>
952
+ let slot = data [ 1 ] ;
953
+ let tag = generateBlankTag ( ) ;
961
954
getTag ( slot ) . set ( tag ) ;
962
955
963
956
refreshTag ( slot ) ;
@@ -969,17 +962,19 @@ function fastRx(data) {
969
962
_Bluetooth . write ( [ COMMAND_CLEAR_SLOT , slot , tag [ 0 ] , tag [ 1 ] , tag [ 2 ] , tag [ 3 ] , tag [ 4 ] , tag [ 5 ] , tag [ 6 ] , tag [ 7 ] , tag [ 8 ] ] ) ;
970
963
971
964
return ;
965
+ }
972
966
973
- case COMMAND_GET_BLUETOOTH_NAME : //Get Bluetooth Name
967
+ case COMMAND_GET_BLUETOOTH_NAME : { //Get Bluetooth Name
974
968
//Returns the bluetooth name, followed by a null terminator.
975
969
_Bluetooth . write ( storage . readArrayBuffer ( PUCK_NAME_FILE ) ) ;
976
970
_Bluetooth . write ( [ 0 ] ) ; // Null terminator
977
971
978
972
return ;
973
+ }
979
974
980
- case COMMAND_SET_BLUETOOTH_NAME : //Set Bluetooth Name
975
+ case COMMAND_SET_BLUETOOTH_NAME : { //Set Bluetooth Name
981
976
// Get the index of the null terminator.
982
- nullIdx = data . indexOf ( 0 ) ;
977
+ let nullIdx = data . indexOf ( 0 ) ;
983
978
984
979
// If the null terminator is not found, set it to the end of the data.
985
980
if ( nullIdx == - 1 ) {
@@ -1001,8 +996,9 @@ function fastRx(data) {
1001
996
_Bluetooth . write ( data ) ;
1002
997
1003
998
return ;
999
+ }
1004
1000
1005
- case COMMAND_GET_FIRMWARE : //Get Firmware
1001
+ case COMMAND_GET_FIRMWARE : { //Get Firmware
1006
1002
if ( ENABLE_LOG ) {
1007
1003
_consoleLog ( "Firmware Name:" , FIRMWARE_NAME ) ;
1008
1004
}
@@ -1011,10 +1007,12 @@ function fastRx(data) {
1011
1007
_Bluetooth . write ( [ 0 ] ) ; // Null terminator
1012
1008
1013
1009
return ;
1010
+ }
1011
+
1012
+ case COMMAND_MOVE_SLOT : { //Move slot <From> <To>
1013
+ let oldSlot = data [ 1 ] ;
1014
+ let newSlot = data [ 2 ] ;
1014
1015
1015
- case COMMAND_MOVE_SLOT : //Move slot <From> <To>
1016
- oldSlot = data [ 1 ] ;
1017
- newSlot = data [ 2 ] ;
1018
1016
if ( oldSlot < tags . length && newSlot < tags . length ) {
1019
1017
tags . splice ( newSlot , 0 , tags . splice ( oldSlot , 1 ) [ 0 ] ) ;
1020
1018
changeTag ( currentTag ) ;
@@ -1023,15 +1021,17 @@ function fastRx(data) {
1023
1021
_Bluetooth . write ( [ COMMAND_MOVE_SLOT , oldSlot , newSlot ] ) ;
1024
1022
1025
1023
return ;
1024
+ }
1026
1025
1027
- case COMMAND_ENABLE_BLE_UART : //Enable BLE UART
1026
+ case COMMAND_ENABLE_BLE_UART : { //Enable BLE UART
1028
1027
onFastModeDisconnect ( ) ;
1029
1028
1030
1029
return ;
1030
+ }
1031
1031
1032
- case COMMAND_RESTART_NFC : //Restart NFC <Slot?>
1032
+ case COMMAND_RESTART_NFC : { //Restart NFC <Slot?>
1033
1033
if ( data . length > 1 ) {
1034
- slot = data [ 1 ] >= tags . length ? 0 : data [ 1 ] ;
1034
+ let slot = data [ 1 ] >= tags . length ? 0 : data [ 1 ] ;
1035
1035
changeTag ( slot ) ;
1036
1036
_Bluetooth . write ( [ COMMAND_RESTART_NFC , slot ] ) ;
1037
1037
} else {
@@ -1040,6 +1040,7 @@ function fastRx(data) {
1040
1040
}
1041
1041
1042
1042
return ;
1043
+ }
1043
1044
1044
1045
default :
1045
1046
_Bluetooth . write ( "Bad Command" ) ;
@@ -1130,7 +1131,7 @@ if (typeof _NTAG215 !== "undefined") {
1130
1131
} ) ;
1131
1132
1132
1133
// Initialize the tags in ram, and load any saved tags from flash.
1133
- for ( var i = 0 ; i < tags . length ; i ++ ) {
1134
+ for ( let i = 0 ; i < tags . length ; i ++ ) {
1134
1135
const filename = "tag" + i + ".bin" ;
1135
1136
const buffer = storage . readArrayBuffer ( filename ) ;
1136
1137
const tag = getTag ( i ) ;
0 commit comments