mirror of
https://github.com/meshcore-dev/meshcore.js.git
synced 2026-04-20 22:13:49 +00:00
initial serial client implementation for meshcore device
This commit is contained in:
parent
897eeecb24
commit
30c918a09a
6 changed files with 574 additions and 1 deletions
53
src/buffer_reader.js
Normal file
53
src/buffer_reader.js
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
class BufferReader {
|
||||
|
||||
constructor(data) {
|
||||
this.pointer = 0;
|
||||
this.buffer = new Uint8Array(data);
|
||||
}
|
||||
|
||||
readByte() {
|
||||
return this.readBytes(1)[0];
|
||||
}
|
||||
|
||||
readBytes(count) {
|
||||
const data = this.buffer.slice(this.pointer, this.pointer + count);
|
||||
this.pointer += count;
|
||||
return data;
|
||||
}
|
||||
|
||||
readString() {
|
||||
const remainingBytesCount = this.buffer.length - this.pointer;
|
||||
const remainingBytes = this.readBytes(remainingBytesCount);
|
||||
return new TextDecoder().decode(remainingBytes);
|
||||
}
|
||||
|
||||
readCString(maxLength) {
|
||||
const value = [];
|
||||
const bytes = this.readBytes(maxLength);
|
||||
for(const byte of bytes){
|
||||
|
||||
// if we find a null terminator character, we have reached the end of the cstring
|
||||
if(byte === 0){
|
||||
return new TextDecoder().decode(new Uint8Array(value));
|
||||
}
|
||||
|
||||
value.push(byte);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
readUInt16LE() {
|
||||
const bytes = this.readBytes(2);
|
||||
const view = new DataView(bytes.buffer);
|
||||
return view.getUint16(0, true);
|
||||
}
|
||||
|
||||
readUInt32LE() {
|
||||
const bytes = this.readBytes(4);
|
||||
const view = new DataView(bytes.buffer);
|
||||
return view.getUint32(0, true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default BufferReader;
|
||||
Loading…
Add table
Add a link
Reference in a new issue