Skip to content

Commit

Permalink
Fix handling for values being unsigned.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marus committed Jan 30, 2018
1 parent a037752 commit 592cefa
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/frontend/peripheral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,12 @@ export class PeripheralNode extends BaseNode {
this.children.sort((c1, c2) => c1.offset > c2.offset ? 1 : -1);
}

getBytes(offset: number, size: number): number[] {
getBytes(offset: number, size: number): Uint8Array {
try {
return this.currentValue.slice(offset, offset + size);
return new Uint8Array(this.currentValue.slice(offset, offset + size));
}
catch(e) {
return [];
return new Uint8Array(0);
}
}

Expand Down Expand Up @@ -266,7 +266,7 @@ export class ClusterNode extends BaseNode {
this.children.sort((r1, r2) => r1.offset > r2.offset ? 1 : -1);
}

getBytes(offset: number, size: number): number[] {
getBytes(offset: number, size: number): Uint8Array {
return this.parent.getBytes(this.offset + offset, size);
}

Expand Down Expand Up @@ -430,13 +430,22 @@ export class RegisterNode extends BaseNode {

update(): Thenable<boolean> {
let bc = this.size / 8;
let bytes = this.parent.getBytes(this.offset, bc).reverse();
let cv = 0;
for(var i = 0; i < bc; i++) {
cv = cv << 8;
cv |= bytes[i];
let bytes = this.parent.getBytes(this.offset, bc);
let buffer = new Buffer(bytes);
switch (bc) {
case 1:
this.currentValue = buffer.readUInt8(0);
break;
case 2:
this.currentValue = buffer.readUInt16LE(0);
break;
case 4:
this.currentValue = buffer.readUInt32LE(0);
break;
default:
vscode.window.showErrorMessage(`Register ${this.name} has invalid size: ${this.size}. Should be 8, 16 or 32.`);
break;
}
this.currentValue = cv;
this.children.forEach(f => f.update());

return Promise.resolve(true);
Expand Down

0 comments on commit 592cefa

Please sign in to comment.