Skip to content

Commit

Permalink
Update README and ntag215.js
Browse files Browse the repository at this point in the history
- Fix issue with FULL_WRITE
- Remove `COMMAND_FULL_READ`
- Add `formatHexData` utility for hex formatting
- Output sent and received data on the devtool console
  • Loading branch information
DanTheMan827 committed Jan 19, 2025
1 parent 5f6865c commit 82c6fcb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 40 deletions.
13 changes: 0 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,6 @@ Writes a full slot. The command should be sent as one BLE packet, then an acknow

---

#### 0x06 - Full Read

Reads all data from a specified slot.

**Parameters:**
- `slot`: The slot number to read from. If the slot is out of range, the current slot is used.

**Returns:**
- The command, slot used, four bytes for a CRC32 checksum encoded in little-endian, and the 572 bytes of data.
- Total number of bytes: 578.

---

#### 0xFA - Get Bluetooth Name

Requests the Bluetooth name.
Expand Down
32 changes: 6 additions & 26 deletions ntag215.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,6 @@ const COMMAND_SAVE = 0x04;
*/
const COMMAND_FULL_WRITE = 0x05;

/**
* Reads all data from a specified slot.
* @value 0x06
* @param {number} slot - The slot number to read from. If the slot is out of range, the current slot is used.
* @returns The command, slot used, four bytes for a CRC32 checksum encoded in little-endian, and the 572 bytes of data.
*
* Total number of bytes: 578
*/
const COMMAND_FULL_READ = 0x06;

/**
* Requests the Bluetooth name. Returns the name followed by a null terminator.
* @value 0xFA
Expand Down Expand Up @@ -548,7 +538,7 @@ function flashLed(led, interval, times, callback) {
* @returns {Uint8Array} A Uint8Array containing the CRC32 checksum bytes in little-endian order.
*/
function getCRC32(data) {
let crc32 = E.CRC32(data);
const crc32 = E.CRC32(data);
return new Uint8Array([
crc32 & 0xFF,
(crc32 >> 8) & 0xFF,
Expand Down Expand Up @@ -766,7 +756,8 @@ function fastRx(data) {
oldSlot,
newSlot,
response,
nullIdx;
nullIdx,
crc32;

if (data.length > 0) {
switch (data[0]) {
Expand Down Expand Up @@ -847,15 +838,15 @@ function fastRx(data) {
crc32 = null;

if (data.length == 6) {
crc32 = Uint8Array(data.slice(2, 6));
crc32 = data.slice(2, 6);
}

_setTimeout(function() {
rxBytes(572, (rxData) => {
var receivedCrc32 = getCRC32(rxData);
const receivedCrc32 = getCRC32(rxData);

// Only store the tag if the target CRC32 is not set, or if the received CRC32 matches the target.
if (crc32 == null || !compareArrays(crc32, receivedCrc32)) {
if (crc32 === null || compareArrays(crc32, receivedCrc32)) {
getTag(slot).set(rxData, 0, 0);
}

Expand All @@ -867,17 +858,6 @@ function fastRx(data) {

return;

case COMMAND_FULL_READ: //Full Read <Slot>
slot = data[1] < tags.length ? data[1] : currentTag;
var tagData = getTag(slot);
var crc32 = getCRC32(tagData);

_Bluetooth.write([COMMAND_FULL_READ, slot]);
_Bluetooth.write(crc32);
_Bluetooth.write(tagData);

return;

case COMMAND_GET_BLUETOOTH_NAME: //Get Bluetooth Name
//Returns the bluetooth name, followed by a null terminator.
_Bluetooth.write(storage.readArrayBuffer(PUCK_NAME_FILE));
Expand Down
22 changes: 22 additions & 0 deletions puck-ntag215-manager/src/formatHexData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Formats a Uint8Array into a string of hex data, 16 bytes per line.
*
* @param data - The Uint8Array to format.
* @returns A string where each line contains 16 bytes in hexadecimal format.
* @throws Will throw an error if the input is not a Uint8Array.
*/
export function formatHexData(data: Uint8Array): string {
if (!(data instanceof Uint8Array)) {
throw new Error("Input should be a Uint8Array");
}

let result = '';
for (let i = 0; i < data.length; i += 16) {
let line = '';
for (let j = 0; j < 16 && i + j < data.length; j++) {
line += data[i + j].toString(16).padStart(2, '0').toUpperCase() + ' ';
}
result += line.trim() + '\n';
}
return result.trim();
}
6 changes: 5 additions & 1 deletion puck-ntag215-manager/src/puck.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { sleep } from "./sleep"
import { formatHexData } from "./formatHexData"
import { NORDIC_RX, NORDIC_SERVICE, NORDIC_TX } from "./uart/nordic"

export const PUCK_SERVICE = "78290001-d52e-473f-a9f4-f03da7c67dd1"
Expand Down Expand Up @@ -101,6 +102,7 @@ export class Puck {
}

const finishName = (ev: CharacteristicEvent) => {
console.log(`Received: \n${formatHexData(new Uint8Array(this.returnCharacteristic.value.buffer))}`)
if (count > 0) {
const response = new Uint8Array(this.returnCharacteristic.value.buffer)

Expand Down Expand Up @@ -128,7 +130,9 @@ export class Puck {

for (var i = 0; i < bytes.length; i = i + this.packetSize) {
resetTimeout()
await this.commandCharacteristic.writeValueWithResponse(bytes.slice(i, i + this.packetSize))
const data = bytes.slice(i, i + this.packetSize)
console.log(`Sending: \n${formatHexData(data)}`)
await this.commandCharacteristic.writeValueWithResponse(data)
}
})
}
Expand Down

0 comments on commit 82c6fcb

Please sign in to comment.