Skip to content

Commit

Permalink
Clean up SMXConfig
Browse files Browse the repository at this point in the history
- Convert EnabledSensors to bool arrays and fix byteLength
- Clean up packet handling
  • Loading branch information
fchorney committed Apr 4, 2024
1 parent 8392333 commit 83fd439
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 206 deletions.
105 changes: 15 additions & 90 deletions sdk/commands/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,6 @@ import { EnabledSensors } from "./enabled-sensors.ts";

export type Decoded<Struct extends { decode(...args: unknown[]): unknown }> = ReturnType<Struct["decode"]>;

/**
* Each FSR panel has 4 sensors. Make read/write easier by
* making them a struct
*/
const packed_sensor_t = new StructBuffer("packed_sensor_t", {
up: uint8_t,
right: uint8_t,
down: uint8_t,
left: uint8_t,
});

/**
* Each panel has various thresholds that are used based on
* if it's a LoadCell or FSR panel.
Expand All @@ -27,9 +16,10 @@ const packed_panel_settings_t = new StructBuffer("packed_panel_settings_t", {

/**
* FSR Thresholds
* 4 Sensors per threshold
*/
fsrLowThreshold: packed_sensor_t,
fsrHighThreshold: packed_sensor_t,
fsrLowThreshold: uint8_t[4],
fsrHighThreshold: uint8_t[4],

/**
* TODO: Not sure what these are for
Expand All @@ -44,22 +34,6 @@ const packed_panel_settings_t = new StructBuffer("packed_panel_settings_t", {
reserved: uint16_t,
});

/**
* Just an intermediate struct so you can more easily dig into
* each panels sensors
*/
const panel_settings_t = new StructBuffer("panel_settings_t", {
up_left: packed_panel_settings_t,
up: packed_panel_settings_t,
up_right: packed_panel_settings_t,
left: packed_panel_settings_t,
center: packed_panel_settings_t,
right: packed_panel_settings_t,
down_left: packed_panel_settings_t,
down: packed_panel_settings_t,
down_right: packed_panel_settings_t,
});

/**
* Flags for Panel Config stored in a uint8_t
*/
Expand All @@ -86,22 +60,6 @@ const rgb_t = new StructBuffer("rbg_t", {
b: uint8_t,
});

/**
* Just an intermediate struct so you can more easily dig into
* each panels step color RGB values
*/
const step_colors_t = new StructBuffer("step_colors_t", {
up_left: rgb_t,
up: rgb_t,
up_right: rgb_t,
left: rgb_t,
center: rgb_t,
right: rgb_t,
down_left: rgb_t,
down: rgb_t,
down_right: rgb_t,
});

/**
* The configuration for a connected controller. This can be retrieved with SMX_GetConfig
* and modified with SMX_SetConfig.
Expand Down Expand Up @@ -172,14 +130,18 @@ export const smx_config_t = new StructBuffer("smx_config_t", {
/**
* The color to use for each panel when auto-lighting in master mode. This doesn't
* apply when the pads are in autonomous lighting mode (no master), since they don't
* store any configuration by themselves. These colors should be scaled to the 0-170
* range.
* store any configuration by themselves.
*
* These colors are scaled to the 0-170 range.
*
* TODO: We can unscale them to 0-255 when reading, but then we would need to scale them back
* when writing.
*/
stepColor: step_colors_t,
stepColor: rgb_t[9],

/**
* The default color to set the platform (underside) LED strips to.
* RGB values from 0-255
* RGB values from 0-255
*/
platformStripColor: rgb_t,

Expand Down Expand Up @@ -210,7 +172,7 @@ export const smx_config_t = new StructBuffer("smx_config_t", {
*
* Setting a value to 0xFF disables that threshold.
*/
panelSettings: panel_settings_t,
panelSettings: packed_panel_settings_t[9],

/**
* This is an internal tunable and should be left unchanged.
Expand All @@ -226,61 +188,24 @@ export const smx_config_t = new StructBuffer("smx_config_t", {
padding: uint8_t[49],
});

/**
* Class to represent all 4 sensors on a panel.
*/
class Panel {
up = false;
right = false;
down = false;
left = false;

/**
* Convert the first 4 Least Significant Bits to represent all 4 sensors on a panel
* TODO: Determine if this ordering is actually correct
*/
constructor(byte: number) {
this.up = byte & 0x08 ? true : false;
this.right = byte & 0x04 ? true : false;
this.down = byte & 0x02 ? true : false;
this.left = byte & 0x01 ? true : false;
}

/**
* Convert a panels 4 sensors back into a 4-bit LSB byte
* TODO: Determine if this ordering is actually correct
*/
toByte(): number {
return (this.up ? 1 << 3 : 0) + (this.right ? 1 << 2 : 0) + (this.down ? 1 << 1 : 0) + (this.left ? 1 : 0);
}
}

/**
* The configuration for a connected SMX Stage.
*/
export class SMXConfig {
private data: Array<number>;
public config: Decoded<typeof smx_config_t>;

/**
* Take in the data array and decode it into this.
*/
constructor(data: Array<number>) {
this.data = data;
this.config = smx_config_t.decode(this.data.slice(2, -1), true);
this.config = smx_config_t.decode(data.slice(2, -1), true);
}

/**
* TODO: Make this private again later, and maybe make a function called
* "write_to_stage" or something? Depends on how we want to handle writing/reading
*/
encode(): DataView | null {
if (!this.config) {
return null;
}

const encoded_data = smx_config_t.encode(this.config, true);

return encoded_data;
encode(): DataView {
return smx_config_t.encode(this.config, true);
}
}
2 changes: 1 addition & 1 deletion sdk/commands/data_info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const data_info_packet_t = new StructBuffer("data_info_packet_t", {
cmd: char,
// Not Used
packet_size: uint8_t,
/** '0' for P1, '1' for P2 (Note these are the characters '0' and '1', not the numbers 0 and 1) */
// '0' for P1, '1' for P2 (Note these are the characters '0' and '1', not the numbers 0 and 1)
player: char,
// Unused and Unknown
unused2: char,
Expand Down
84 changes: 20 additions & 64 deletions sdk/commands/enabled-sensors.test.ts
Original file line number Diff line number Diff line change
@@ -1,89 +1,46 @@
import { sbytes, sview } from "@nmann/struct-buffer";
import { expect, test, describe } from "vitest";
import { twoEnabledSensors_t, EnabledSensors } from "./enabled-sensors";
import { SENSOR_COUNT } from "../api";

describe("twoEnabledSensors_t", () => {
test("encode", () => {
expect(twoEnabledSensors_t.decode(sbytes("F0"), false)).toEqual({
up0: true,
left0: true,
right0: true,
up0: true,
down0: true,
left0: true,
up1: false,
left1: false,
right1: false,
up1: false,
down1: false,
left1: false,
});

expect(twoEnabledSensors_t.decode(sbytes("0F"))).toEqual({
up0: false,
left0: false,
right0: false,
up0: false,
down0: false,
left0: false,
up1: true,
left1: true,
right1: true,
up1: 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,
},
Array(SENSOR_COUNT).fill(false),
Array(SENSOR_COUNT).fill(true),
Array(SENSOR_COUNT).fill(false),
Array(SENSOR_COUNT).fill(true),
Array(SENSOR_COUNT).fill(true),
Array(SENSOR_COUNT).fill(true),
Array(SENSOR_COUNT).fill(false),
Array(SENSOR_COUNT).fill(true),
Array(SENSOR_COUNT).fill(false),
];

const encodedData = "0f 0f ff 0f 00";
Expand All @@ -93,6 +50,5 @@ test("decode", () => {
});

test("encode", () => {
// this currently fails for reasons I don't quite understand. probably endianness or something
expect(sview(enabledSensors.encode(decodedData, false))).toEqual(encodedData);
expect(sview(enabledSensors.encode(decodedData))).toEqual(encodedData);
});
Loading

0 comments on commit 83fd439

Please sign in to comment.