Skip to content

Commit

Permalink
stage display fixes, display input state
Browse files Browse the repository at this point in the history
  • Loading branch information
noahm committed Apr 1, 2024
1 parent 9bba668 commit 89a5af6
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 27 deletions.
23 changes: 12 additions & 11 deletions sdk/commands/inputs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { bitFields, uint16_t } from "@nmann/struct-buffer";
import { bits, uint16_t } from "@nmann/struct-buffer";

export type EachSensor<T> = {
up: T;
Expand All @@ -7,6 +7,8 @@ export type EachSensor<T> = {
left: T;
};

export type PanelName = keyof EachPanel<any>;

Check failure on line 10 in sdk/commands/inputs.ts

View workflow job for this annotation

GitHub Actions / Biome Lint

Unexpected any. Specify a different type.

export type EachPanel<T> = {
up_left: T;
up: T;
Expand All @@ -19,15 +21,14 @@ export type EachPanel<T> = {
down_right: T;
};

// TODO: This should be a bits object probably
export const StageInputs = bitFields(uint16_t, {
up_left: 1,
export const StageInputs = bits(uint16_t, {
up_left: 0,
up: 1,
up_right: 1,
left: 1,
center: 1,
right: 1,
down_left: 1,
down: 1,
down_right: 1,
up_right: 2,
left: 3,
center: 4,
right: 5,
down_left: 6,
down: 7,
down_right: 8,
});
8 changes: 7 additions & 1 deletion ui/stage/fsr-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ import type { SMXPanelTestData } from "../../sdk/commands/sensor_test";

interface EnabledProps {
data: SMXPanelTestData;
active: boolean | undefined;
}

export function FsrPanel(props: EnabledProps) {
const { bad_sensor_input, have_data_from_panel, sensor_level } = props.data;
return (
<div className={cn("panel", { active: have_data_from_panel })}>
<div
className={cn("panel", {
commErr: !have_data_from_panel,
active: props.active,
})}
>
<Fsr
className="top horiz"
badInput={bad_sensor_input.up}
Expand Down
4 changes: 2 additions & 2 deletions ui/stage/simple-pad.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface Props {
}

export function SimplePad({ dev }: Props) {
const [panelStates, setPanelStates] = useState<number[]>([]);
const [panelStates, setPanelStates] = useState<boolean[]>([]);

useEffect(
() =>
Expand All @@ -27,7 +27,7 @@ export function SimplePad({ dev }: Props) {
panelStates.down_right,
]);
}),
[dev],
[dev]
);

return (
Expand Down
57 changes: 48 additions & 9 deletions ui/stage/stage-test.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
import { useAtomValue, type Atom } from "jotai";
import { useEffect, useState } from "react";
import type { SMXSensorTestData } from "../../sdk/commands/sensor_test";
import type {
SMXPanelTestData,
SMXSensorTestData,
} from "../../sdk/commands/sensor_test";
import { requestTestData } from "../pad-coms";
import { FsrPanel } from "./fsr-panel";
import { SmxStage } from "../../sdk";
import {
StageInputs,
type EachPanel,
type PanelName,
} from "../../sdk/commands/inputs";
import { HID_REPORT_INPUT_STATE } from "../../sdk/packet";

export function StageTest({
deviceAtom,
}: {
deviceAtom: Atom<HIDDevice | undefined>;
}) {
const device = useAtomValue(deviceAtom);
function useInputState(dev: HIDDevice | undefined) {
const [panelStates, setPanelStates] = useState<
EachPanel<boolean> | undefined
>();
useEffect(() => {
if (!dev) return;
function handleInputReport(e: HIDInputReportEvent) {
if (e.reportId !== HID_REPORT_INPUT_STATE) return;
const state = StageInputs.decode(e.data, true);
setPanelStates(state);
}
dev.addEventListener("inputreport", handleInputReport);
return () => dev.removeEventListener("inputreport", handleInputReport);
}, [dev]);
return panelStates;
}

function useTestData(device: HIDDevice | undefined) {
const [testData, setTestData] = useState<SMXSensorTestData>();

useEffect(() => {
Expand All @@ -29,14 +51,31 @@ export function StageTest({
return () => cancelAnimationFrame(handle);
}, [device]);

return testData;
}

export function StageTest({
deviceAtom,
}: {
deviceAtom: Atom<HIDDevice | undefined>;
}) {
const device = useAtomValue(deviceAtom);
const testData = useTestData(device);
const inputState = useInputState(device);

if (!testData) {
return null;
}

const entries = Object.entries(testData.panels) as [
PanelName,
SMXPanelTestData
][];

return (
<div className="pad">
{Object.entries(testData.panels).map(([key, data]) => (
<FsrPanel key={key} data={data} />
{entries.map(([key, data]) => (
<FsrPanel active={inputState?.[key]} key={key} data={data} />
))}
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion ui/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ export const statusText$ = atom(
);

/** write-only atom. write to this to append a line to statusText */
export const nextStatusTextLine$ = atom(null, (_, set, line: string) => set(statusText$, (prev) => prev + line));
export const nextStatusTextLine$ = atom(null, (_, set, line: string) => set(statusText$, (prev) => prev + "\n" + line));

Check failure on line 34 in ui/state.ts

View workflow job for this annotation

GitHub Actions / Biome Lint

Template literals are preferred over string concatenation.
6 changes: 5 additions & 1 deletion ui/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,17 @@ button:focus {
gap: 1em 1em;
height: 20em;
width: 20em;
margin-right: 1em;
}
.panel {
outline: 2px solid #ddd;
position: relative;
}
.panel.active {
background-color: yellow;
background-color: #00ff00;
}
.panel.commErr {
background-color: red;
}

.fsr {
Expand Down
4 changes: 2 additions & 2 deletions ui/ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ export function UI() {
devices.map((device) => {
console.log(`Found device: ${device.productName}`);
open_smx_device(device);
}),
})
);
}
}, []);

return (
<>
<h1>SMX Web Config</h1>
<StageTest deviceAtom={p1Dev$} />
<StageTest deviceAtom={p2Dev$} />
<StageTest deviceAtom={p1Dev$} />
<PickDeviceButton /> <DebugCommands />
<StatusDisplay />
</>
Expand Down

0 comments on commit 89a5af6

Please sign in to comment.