Skip to content

Commit

Permalink
fix trace format and chars
Browse files Browse the repository at this point in the history
  • Loading branch information
brianignacio5 committed Nov 29, 2023
1 parent 2843f88 commit d13df30
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 24 deletions.
1 change: 1 addition & 0 deletions examples/typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ disconnectButton.onclick = async () => {
baudrates.style.display = "initial";
connectButton.style.display = "initial";
disconnectButton.style.display = "none";
traceButton.style.display = "none";
eraseButton.style.display = "none";
lblConnTo.style.display = "none";
filesDiv.style.display = "none";
Expand Down
36 changes: 12 additions & 24 deletions src/webserial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,42 +105,30 @@ class Transport {
}
}

private hexify(s: string, uppercase = true): string {
const format_str = uppercase ? "%02X" : "%02x";
return s
.split("")
.map((c) => {
const charCode = c.charCodeAt(0);
return format_str.replace(/%02([xX])/g, (_, caseFormat) =>
caseFormat === "X" ? charCode.toString(16).toUpperCase() : charCode.toString(16),
);
})
hexify(s: Uint8Array) {
return Array.from(s)
.map((byte) => byte.toString(16).padStart(2, "0"))
.join("");
}

private hexConvert(uint8Array: Uint8Array) {
return Array.from(uint8Array)
.map(byte => byte.toString(16).padStart(2, '0'))
.join('');
}

oldHexConvert(buffer: Uint8Array) {
const bufferStr = String.fromCharCode(...[].slice.call(buffer));
if (bufferStr.length > 16) {
hexConvert(uint8Array: Uint8Array, autoSplit = true) {
if (autoSplit && uint8Array.length > 16) {
let result = "";
let s = bufferStr;
let s = uint8Array;

while (s.length > 0) {
const line = s.slice(0, 16);
const ascii_line = line
let line = s.slice(0, 16);

Check failure on line 120 in src/webserial.ts

View workflow job for this annotation

GitHub Actions / ci

'line' is never reassigned. Use 'const' instead
let asciiLine = String.fromCharCode(...line)

Check failure on line 121 in src/webserial.ts

View workflow job for this annotation

GitHub Actions / ci

'asciiLine' is never reassigned. Use 'const' instead
.split("")
.map((c) => (c === " " || (c >= " " && c <= "~" && c !== " ") ? c : "."))
.join("");
s = s.slice(16);
result += `\n ${this.hexify(line.slice(0, 8))} ${this.hexify(line.slice(8))} | ${ascii_line}`;
result += `\n ${this.hexify(line.slice(0, 8))} ${this.hexify(line.slice(8))} | ${asciiLine}`;
}

return result;
} else {
return this.hexify(bufferStr);
return this.hexify(uint8Array);
}
}

Expand Down

0 comments on commit d13df30

Please sign in to comment.