Skip to content

Commit 7594497

Browse files
committed
Refactor variable declarations from var to let for improved scope management in ntag215.js
1 parent 620a409 commit 7594497

File tree

1 file changed

+78
-77
lines changed

1 file changed

+78
-77
lines changed

ntag215.js

Lines changed: 78 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const COMMAND_BLE_PACKET_TEST = 0x00;
6969
*
7070
* 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:
7171
* ```javascript
72-
* var output = Uint8Array(80);
72+
* let output = Uint8Array(80);
7373
* output.set(tags[slot].buffer.slice(0, 8), 0);
7474
* output.set(tags[slot].buffer.slice(16, 24), 8);
7575
* output.set(tags[slot].buffer.slice(32, 52), 20);
@@ -233,37 +233,37 @@ const ENABLE_LEDS = ENABLE_LED1 || ENABLE_LED2 || ENABLE_LED3;
233233
/**
234234
* The active tag index.
235235
*/
236-
var currentTag = 0;
236+
let currentTag = 0;
237237

238238
/**
239239
* Contains the timeout between changing tags.
240240
*/
241-
var changeTagTimeout = null;
241+
let changeTagTimeout = null;
242242

243243
/**
244244
* A buffer used by the NTAG215 emulator.
245245
*/
246-
var txBuffer = new Uint8Array(32);
246+
let txBuffer = new Uint8Array(32);
247247

248248
/**
249249
* An array of the in-memory tags, unused if {@link SAVE_TO_FLASH} is true
250250
*/
251-
var tags = [];
251+
let tags = [];
252252

253253
/**
254254
* If {@link fastRx} should process data.
255255
*/
256-
var rxPaused = false;
256+
let rxPaused = false;
257257

258258
/**
259259
* Auto-sleep timeout reference.
260260
*/
261-
var autoSleepTimeout = null;
261+
let autoSleepTimeout = null;
262262

263263
/**
264264
* If bluetooth is currently connected.
265265
*/
266-
var bluetoothConnected = false;
266+
let bluetoothConnected = false;
267267
// #endregion
268268

269269
// #region Tag initialization
@@ -321,7 +321,7 @@ function fixUid() {
321321
*/
322322
function hexDump(inputData) {
323323
// Initialize an empty string `line`, which will be used to build the output lines containing the hexadecimal values.
324-
var line = "";
324+
let line = "";
325325

326326
// Iterate through each element of the `inputData` array.
327327
for (let i = 0; i < inputData.length; i++) {
@@ -360,23 +360,23 @@ function hexDump(inputData) {
360360
* @returns {Uint8Array} A 9-byte Uint8Array containing the generated UID.
361361
*/
362362
function generateUid() {
363-
var uid = new Uint8Array(9);
363+
let uid = new Uint8Array(9);
364364

365365
// Set the first byte as 0x04
366366
uid[0] = 0x04;
367367

368368
// 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);
371371

372372
// Set the fourth byte using XOR operation with specific values
373373
uid[3] = uid[0] ^ uid[1] ^ uid[2] ^ 0x88;
374374

375375
// 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);
380380

381381
// Set the last byte using XOR operation with specific values
382382
uid[8] = uid[4] ^ uid[5] ^ uid[6] ^ uid[7];
@@ -389,7 +389,7 @@ function generateUid() {
389389
* @returns {Uint8Array} - The generated tag.
390390
*/
391391
function generateBlankTag() {
392-
var tag = new Uint8Array(572);
392+
let tag = new Uint8Array(572);
393393

394394
// Generate blank NTAG215 tags with random, but valid UID.
395395
tag.set(generateUid(), 0);
@@ -518,7 +518,7 @@ function cycleTags() {
518518
*/
519519
function getBufferClone(buffer) {
520520
if (buffer) {
521-
var output = new Uint8Array(buffer.length);
521+
let output = new Uint8Array(buffer.length);
522522
output.set(buffer);
523523

524524
return output;
@@ -549,7 +549,7 @@ function saveTag(slot) {
549549
* Saves all tags to flash.
550550
*/
551551
function saveAllTags() {
552-
for (var i = 0; i < tags.length; i++) {
552+
for (let i = 0; i < tags.length; i++) {
553553
saveTag(i);
554554
}
555555
}
@@ -769,15 +769,15 @@ function onFastModeDisconnect() {
769769
function rxBytes(count, callback) {
770770
rxPaused = true;
771771

772-
var buffer = new Uint8Array(count);
773-
var position = 0;
772+
let buffer = new Uint8Array(count);
773+
let position = 0;
774774

775775
function receive(data) {
776776
buffer.set(new Uint8Array(E.toUint8Array(data), 0, Math.min(data.length, count - position)), position);
777777
position = position + data.length;
778778

779779
if (position >= count) {
780-
var tempBuffer = buffer;
780+
let tempBuffer = buffer;
781781
disconnect();
782782
callback(tempBuffer);
783783
}
@@ -808,40 +808,27 @@ function fastRx(data) {
808808
// data is a string, so you want to convert to an array to work with
809809
data = E.toUint8Array(data);
810810

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-
825811
if (data.length > 0) {
826812
switch (data[0]) {
827-
case COMMAND_BLE_PACKET_TEST: //BLE Packet Test
813+
case COMMAND_BLE_PACKET_TEST: { //BLE Packet Test
828814
_Bluetooth.write(new Uint8Array(data.length > 1 ? data[1] : 255));
829815

830816
return;
817+
}
831818

832-
case COMMAND_SLOT_INFORMATION: //Slot Information <Slot>
819+
case COMMAND_SLOT_INFORMATION: { //Slot Information <Slot>
833820
if (data.length > 1) {
834-
count = data.length > 2 ? data[2] : 1;
821+
let count = data.length > 2 ? data[2] : 1;
835822

836823
//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;
838825

839826
if (slot + count > tags.length) {
840827
count = tags.length - slot;
841828
}
842829

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);
845832

846833
_Bluetooth.write([COMMAND_SLOT_INFORMATION, i]);
847834
_Bluetooth.write(tagData);
@@ -852,32 +839,34 @@ function fastRx(data) {
852839
}
853840

854841
return;
842+
}
855843

856-
case COMMAND_READ: //Read <Slot> <StartPage> <PageCount>
844+
case COMMAND_READ: { //Read <Slot> <StartPage> <PageCount>
857845
//Max pages: 143
858846
//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);
863851

864852
if (ENABLE_LOG) {
865853
//_consoleLog("Reading from slot: " + slot);
866854
//_consoleLog("Read from " + startIdx + " - " + (startIdx + dataSize));
867855
}
868856

869-
response = Uint8Array(4);
857+
let response = Uint8Array(4);
870858
response.set(Uint8Array(data, 0, 4), 0);
871859
response[1] = slot;
872860
_Bluetooth.write(response);
873861
_Bluetooth.write(sourceData);
874862

875863
return;
864+
}
876865

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;
881870

882871
//store data if it fits into memory
883872
if ((startIdx + dataSize) <= 572) {
@@ -893,21 +882,23 @@ function fastRx(data) {
893882
_Bluetooth.write([COMMAND_WRITE, slot, data[2]]);
894883

895884
return;
885+
}
896886

897-
case COMMAND_SAVE: //Save <Slot>
887+
case COMMAND_SAVE: { //Save <Slot>
898888
if (SAVE_TO_FLASH) {
899-
slot = data[1] < tags.length ? data[1] : currentTag;
889+
let slot = data[1] < tags.length ? data[1] : currentTag;
900890

901891
saveTag(slot);
902892
}
903893

904894
_Bluetooth.write([COMMAND_SAVE, data[1]]);
905895

906896
return;
897+
}
907898

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;
911902

912903
if (data.length == 6) {
913904
crc32 = data.slice(2, 6);
@@ -935,29 +926,31 @@ function fastRx(data) {
935926
}, 0);
936927

937928
return;
929+
}
938930

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;
941933

942934
//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;
944936

945937
if (slot + count > tags.length) {
946938
count = tags.length - slot;
947939
}
948940

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);
952944
_Bluetooth.write([COMMAND_FULL_READ, i, crc32[0], crc32[1], crc32[2], crc32[3]]);
953945
_Bluetooth.write(tag);
954946
}
955947

956948
return;
949+
}
957950

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();
961954
getTag(slot).set(tag);
962955

963956
refreshTag(slot);
@@ -969,17 +962,19 @@ function fastRx(data) {
969962
_Bluetooth.write([COMMAND_CLEAR_SLOT, slot, tag[0], tag[1], tag[2], tag[3], tag[4], tag[5], tag[6], tag[7], tag[8]]);
970963

971964
return;
965+
}
972966

973-
case COMMAND_GET_BLUETOOTH_NAME: //Get Bluetooth Name
967+
case COMMAND_GET_BLUETOOTH_NAME: { //Get Bluetooth Name
974968
//Returns the bluetooth name, followed by a null terminator.
975969
_Bluetooth.write(storage.readArrayBuffer(PUCK_NAME_FILE));
976970
_Bluetooth.write([0]); // Null terminator
977971

978972
return;
973+
}
979974

980-
case COMMAND_SET_BLUETOOTH_NAME: //Set Bluetooth Name
975+
case COMMAND_SET_BLUETOOTH_NAME: { //Set Bluetooth Name
981976
// Get the index of the null terminator.
982-
nullIdx = data.indexOf(0);
977+
let nullIdx = data.indexOf(0);
983978

984979
// If the null terminator is not found, set it to the end of the data.
985980
if (nullIdx == -1) {
@@ -1001,8 +996,9 @@ function fastRx(data) {
1001996
_Bluetooth.write(data);
1002997

1003998
return;
999+
}
10041000

1005-
case COMMAND_GET_FIRMWARE: //Get Firmware
1001+
case COMMAND_GET_FIRMWARE: { //Get Firmware
10061002
if (ENABLE_LOG) {
10071003
_consoleLog("Firmware Name:", FIRMWARE_NAME);
10081004
}
@@ -1011,10 +1007,12 @@ function fastRx(data) {
10111007
_Bluetooth.write([0]); // Null terminator
10121008

10131009
return;
1010+
}
1011+
1012+
case COMMAND_MOVE_SLOT: { //Move slot <From> <To>
1013+
let oldSlot = data[1];
1014+
let newSlot = data[2];
10141015

1015-
case COMMAND_MOVE_SLOT: //Move slot <From> <To>
1016-
oldSlot = data[1];
1017-
newSlot = data[2];
10181016
if (oldSlot < tags.length && newSlot < tags.length) {
10191017
tags.splice(newSlot, 0, tags.splice(oldSlot, 1)[0]);
10201018
changeTag(currentTag);
@@ -1023,15 +1021,17 @@ function fastRx(data) {
10231021
_Bluetooth.write([COMMAND_MOVE_SLOT, oldSlot, newSlot]);
10241022

10251023
return;
1024+
}
10261025

1027-
case COMMAND_ENABLE_BLE_UART: //Enable BLE UART
1026+
case COMMAND_ENABLE_BLE_UART: { //Enable BLE UART
10281027
onFastModeDisconnect();
10291028

10301029
return;
1030+
}
10311031

1032-
case COMMAND_RESTART_NFC: //Restart NFC <Slot?>
1032+
case COMMAND_RESTART_NFC: { //Restart NFC <Slot?>
10331033
if (data.length > 1) {
1034-
slot = data[1] >= tags.length ? 0 : data[1];
1034+
let slot = data[1] >= tags.length ? 0 : data[1];
10351035
changeTag(slot);
10361036
_Bluetooth.write([COMMAND_RESTART_NFC, slot]);
10371037
} else {
@@ -1040,6 +1040,7 @@ function fastRx(data) {
10401040
}
10411041

10421042
return;
1043+
}
10431044

10441045
default:
10451046
_Bluetooth.write("Bad Command");
@@ -1130,7 +1131,7 @@ if (typeof _NTAG215 !== "undefined") {
11301131
});
11311132

11321133
// 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++) {
11341135
const filename = "tag" + i + ".bin";
11351136
const buffer = storage.readArrayBuffer(filename);
11361137
const tag = getTag(i);

0 commit comments

Comments
 (0)