Skip to content

Commit

Permalink
write 128 bytes at a time in direct mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Eike Ahmels committed Nov 18, 2024
1 parent 524afcd commit 4c82f78
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ logs

# Local Netlify folder
.netlify
stack.env
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
restart: unless-stopped
env_file: stack.env
ports:
- "3000:3000"
- "${HTTP_PORT:-3000}:3000"
networks:
- proxy
- redis-net
Expand Down
34 changes: 29 additions & 5 deletions src/communication/direct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class Direct {
const init = new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0x0D, 'B'.charCodeAt(0), 'L'.charCodeAt(0), 'H'.charCodeAt(0), 'e'.charCodeAt(0), 'l'.charCodeAt(0), 'i'.charCodeAt(0), 0xF4, 0x7D
]);
const result = await serial.write(init, 2000);
const result = await serial.write(init.buffer, 2000);
if (result) {
const infoBuffer = result.subarray(init.length);
const message: FourWayResponse = {
Expand Down Expand Up @@ -134,10 +134,34 @@ export class Direct {

info.layoutSize = Mcu.LAYOUT_SIZE;

const eeprom = await this.writeCommand(DIRECT_COMMANDS.cmd_SetAddress, eepromOffset);
let eeprom = await this.writeCommand(DIRECT_COMMANDS.cmd_SetAddress, eepromOffset);
if (eeprom?.at(0) === DIRECT_RESPONSES.GOOD_ACK) {
await delay(200);
const settingsArray = await this.writeCommand(DIRECT_COMMANDS.cmd_ReadFlash, 0, new Uint8Array([info.layoutSize]));
let settingsArray = new Uint8Array(info.layoutSize);
if (info.layoutSize > 128) {
let currentLayoutSize = 0;
while (currentLayoutSize < info.layoutSize) {
const clampedLayoutSize = Math.min(128, info.layoutSize - currentLayoutSize);
const settingsPart = await this.writeCommand(DIRECT_COMMANDS.cmd_ReadFlash, 0, new Uint8Array([clampedLayoutSize]));
if (!settingsPart) {
this.logError('Failed to read settings part');
throw new Error('Failed to read settings part');
}
settingsArray = mergeUint8Arrays(settingsArray, (settingsPart as Uint8Array)) as Uint8Array;

currentLayoutSize += 128;

eeprom = await this.writeCommand(DIRECT_COMMANDS.cmd_SetAddress, eepromOffset + currentLayoutSize);
if (eeprom?.at(0) !== DIRECT_RESPONSES.GOOD_ACK) {
this.logError('Failed to set address');
throw new Error('Failed to set address');
}
await delay(200);
}
} else {
const tmp = await this.writeCommand(DIRECT_COMMANDS.cmd_ReadFlash, 0, new Uint8Array([info.layoutSize]));
settingsArray = new Uint8Array(tmp?.buffer as ArrayBuffer ?? new ArrayBuffer(0));
}
info.settings = bufferToSettings(settingsArray!);
info.settingsBuffer = settingsArray!;

Expand Down Expand Up @@ -187,14 +211,14 @@ export class Direct {
buffer = Array.from(payload!);
break;
case DIRECT_COMMANDS.cmd_Reset:
return serial.write(new Uint8Array([0x00, 0x00, 0x00, 0x00])).then(() => delay(5000));
return serial.write(new Uint8Array([0x00, 0x00, 0x00, 0x00]).buffer).then(() => delay(5000));
default:
break;
}
const crc = this.makeCRC(buffer);
buffer.push(crc & 0xFF);
buffer.push(crc >> 8 & 0xFF);
return serial.write(new Uint8Array(buffer)).then(result => result?.subarray(buffer.length));
return serial.write(new Uint8Array(buffer).buffer).then(result => result?.subarray(buffer.length));
}

async writeBufferToAddress (address: number, payload: Uint8Array, retries = 10) {
Expand Down

0 comments on commit 4c82f78

Please sign in to comment.