-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from noahm/disabled-sensors-custom
Disabled sensors custom field
- Loading branch information
Showing
9 changed files
with
776 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { sbytes, sview } from "@nmann/struct-buffer"; | ||
import { expect, test, describe } from "vitest"; | ||
import { twoEnabledSensors_t, EnabledSensors } from "./enabled-sensors"; | ||
|
||
describe("twoEnabledSensors_t", () => { | ||
test("encode", () => { | ||
expect(twoEnabledSensors_t.decode(sbytes("F0"), false)).toEqual({ | ||
up0: true, | ||
right0: true, | ||
down0: true, | ||
left0: true, | ||
up1: false, | ||
right1: false, | ||
down1: false, | ||
left1: false, | ||
}); | ||
expect(twoEnabledSensors_t.decode(sbytes("0F"))).toEqual({ | ||
up0: false, | ||
right0: false, | ||
down0: false, | ||
left0: false, | ||
up1: true, | ||
right1: true, | ||
down1: true, | ||
left1: true, | ||
}); | ||
}); | ||
}); | ||
|
||
const enabledSensors = new EnabledSensors(); | ||
|
||
const decodedData = [ | ||
{ | ||
down: false, | ||
left: false, | ||
right: false, | ||
up: false, | ||
}, | ||
{ | ||
down: true, | ||
left: true, | ||
right: true, | ||
up: true, | ||
}, | ||
{ | ||
down: false, | ||
left: false, | ||
right: false, | ||
up: false, | ||
}, | ||
{ | ||
down: true, | ||
left: true, | ||
right: true, | ||
up: true, | ||
}, | ||
{ | ||
down: true, | ||
left: true, | ||
right: true, | ||
up: true, | ||
}, | ||
{ | ||
down: true, | ||
left: true, | ||
right: true, | ||
up: true, | ||
}, | ||
{ | ||
down: false, | ||
left: false, | ||
right: false, | ||
up: false, | ||
}, | ||
{ | ||
down: true, | ||
left: true, | ||
right: true, | ||
up: true, | ||
}, | ||
{ | ||
down: false, | ||
left: false, | ||
right: false, | ||
up: false, | ||
}, | ||
]; | ||
|
||
const encodedData = "0f 0f ff 0f 00"; | ||
|
||
test("decode", () => { | ||
expect(enabledSensors.decode(sbytes(encodedData))).toEqual(decodedData); | ||
}); | ||
|
||
test("encode", () => { | ||
// this currently fails for reasons I don't quite understand. probably endianness or something | ||
expect(sview(enabledSensors.encode(decodedData, false))).toEqual(encodedData); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { StructBuffer, bits, createDataView, uint8_t } from "@nmann/struct-buffer"; | ||
|
||
type Decoded<Struct extends { decode(...args: unknown[]): unknown }> = ReturnType<Struct["decode"]>; | ||
|
||
interface EnabeledSensorSet { | ||
up: boolean; | ||
right: boolean; | ||
down: boolean; | ||
left: boolean; | ||
} | ||
|
||
/** sensors enabled, with two panels packed into a single byte */ | ||
export const twoEnabledSensors_t = bits(uint8_t, { | ||
up0: 7, | ||
right0: 6, | ||
down0: 5, | ||
left0: 4, | ||
up1: 3, | ||
right1: 2, | ||
down1: 1, | ||
left1: 0, | ||
}); | ||
|
||
/** splits packed */ | ||
function splitTwoSensors(decoded: Decoded<typeof twoEnabledSensors_t>): EnabeledSensorSet[] { | ||
return [ | ||
{ | ||
up: decoded.up0, | ||
right: decoded.right0, | ||
down: decoded.down0, | ||
left: decoded.left0, | ||
}, | ||
{ | ||
up: decoded.up1, | ||
right: decoded.right1, | ||
down: decoded.down1, | ||
left: decoded.left1, | ||
}, | ||
]; | ||
} | ||
|
||
function joinTwoSensors( | ||
sensorA: EnabeledSensorSet, | ||
sensorB: EnabeledSensorSet = { | ||
up: false, | ||
right: false, | ||
down: false, | ||
left: false, | ||
}, | ||
): Decoded<typeof twoEnabledSensors_t> { | ||
return { | ||
up0: sensorA.up, | ||
right0: sensorA.right, | ||
down0: sensorA.down, | ||
left0: sensorA.left, | ||
up1: sensorB.up, | ||
right1: sensorB.right, | ||
down1: sensorB.down, | ||
left1: sensorB.left, | ||
}; | ||
} | ||
|
||
export class EnabledSensors extends StructBuffer< | ||
// biome-ignore lint/complexity/noBannedTypes: <explanation> | ||
{}, | ||
EnabeledSensorSet[], | ||
EnabeledSensorSet[] | ||
> { | ||
constructor() { | ||
super("DisabledSensors", {}); | ||
} | ||
|
||
override get count() { | ||
return 5; // always 5 items (5 twoDisabledSensors_t) | ||
} | ||
|
||
override get isList() { | ||
return true; | ||
} | ||
|
||
override decode(view: DataView, littleEndian = false, offset = 0): EnabeledSensorSet[] { | ||
const decoded = twoEnabledSensors_t[this.count] | ||
.decode(view, littleEndian, offset) | ||
.flatMap<EnabeledSensorSet>(splitTwoSensors); | ||
// decoded now has a trailing entry for the 4 bits of padding on the end of data | ||
// so we slice to just the desired data | ||
return decoded.slice(0, 9); | ||
} | ||
|
||
override encode(obj: EnabeledSensorSet[], littleEndian = false, offset = 0, view?: DataView): DataView { | ||
if (obj.length !== 9) { | ||
throw new TypeError("DisabledSensors only encodes sets of 9"); | ||
} | ||
const v = createDataView(this.count, view); | ||
const joined: Array<Decoded<typeof twoEnabledSensors_t>> = []; | ||
for (let i = 0; i < this.count; i++) { | ||
const aIdx = i * 2; | ||
const bIdx = aIdx + 1; | ||
joined.push(joinTwoSensors(obj[aIdx], bIdx < obj.length ? obj[bIdx] : undefined)); | ||
} | ||
twoEnabledSensors_t[this.count].encode(joined, littleEndian, offset, v); | ||
|
||
return v; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.