comment out gpio stuff

This commit is contained in:
dylan 2024-04-04 08:58:39 -07:00
parent 85cbf665f0
commit 903c66b1d9
3 changed files with 76 additions and 48 deletions

View File

@ -19,19 +19,19 @@ export const GamePage = () => {
url: `/api/ws/room?room=${room}`, url: `/api/ws/room?room=${room}`,
// url: "wss://echo.websocket.org", // url: "wss://echo.websocket.org",
onMessage({message}) { onMessage({message}) {
const msg = message as any; // const msg = message as any;
if (msg.type === "gpio") { // if (msg.type === "gpio") {
if (picoRef.current) { // if (picoRef.current) {
const handle = picoRef.current.getPicoConsoleHandle(); // const handle = picoRef.current.getPicoConsoleHandle();
if (handle) { // if (handle) {
console.log("updating pico gpio"); // console.log("updating pico gpio");
(handle.gpio as any).dontSend = true; // (handle.gpio as any).dontSend = true;
handle.gpio.length = 0; // handle.gpio.length = 0;
handle.gpio.push(...msg.gpio); // handle.gpio.push(...msg.gpio);
(handle.gpio as any).dontSend = false; // (handle.gpio as any).dontSend = false;
} // }
} // }
} // }
console.log('message', message); console.log('message', message);
} }
}) })
@ -95,13 +95,17 @@ export const GamePage = () => {
border: 2px solid limegreen; border: 2px solid limegreen;
} }
`}> `}>
<Pico8Console ref={picoRef} carts={info.release.carts} onGpioChange={(gpio: number[]) => { <Pico8Console
console.log("sending gpio"); ref={picoRef}
socket.sendMessage({ carts={info.release.carts}
type: "gpio", // onGpioChange={(gpio: number[]) => {
gpio, // console.log("sending gpio");
}); // socket.sendMessage({
}} /> // type: "gpio",
// gpio,
// });
// }}
/>
</div> </div>
<div className={css` <div className={css`
display: flex; display: flex;

View File

@ -8,13 +8,16 @@ export type Pico8ConsoleImperatives = {
export type Pico8ConsoleProps = { export type Pico8ConsoleProps = {
carts: PicoCart[], carts: PicoCart[],
onGpioChange?(gpio: number[]): void, // onGpioChange?(gpio: number[]): void,
} }
const noop = () => {}; // const noop = () => {};
export const Pico8Console = forwardRef((props: Pico8ConsoleProps, forwardedRef: ForwardedRef<Pico8ConsoleImperatives>) => { export const Pico8Console = forwardRef((props: Pico8ConsoleProps, forwardedRef: ForwardedRef<Pico8ConsoleImperatives>) => {
const {carts, onGpioChange = noop} = props; const {
carts,
// onGpioChange = noop
} = props;
const [playing, setPlaying] = useState(false); const [playing, setPlaying] = useState(false);
const ref = useRef<HTMLDivElement>(null); const ref = useRef<HTMLDivElement>(null);
const [handle, setHandle] = useState<PicoPlayerHandle | null>(null); const [handle, setHandle] = useState<PicoPlayerHandle | null>(null);
@ -33,7 +36,7 @@ export const Pico8Console = forwardRef((props: Pico8ConsoleProps, forwardedRef:
picoConsole.canvas.focus(); picoConsole.canvas.focus();
} }
setHandle(picoConsole); setHandle(picoConsole);
picoConsole.gpio.subscribe(onGpioChange); // picoConsole.gpio.subscribe(onGpioChange);
picoConsole.canvas.addEventListener('keydown',(event) => { picoConsole.canvas.addEventListener('keydown',(event) => {
if (["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"].includes(event.key)) { if (["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"].includes(event.key)) {
event.preventDefault(); event.preventDefault();

View File

@ -35,7 +35,10 @@ export type PicoPlayerHandle = {
rightClick: boolean; rightClick: boolean;
}) => void; }) => void;
setGamepadCount: (count: number) => void; setGamepadCount: (count: number) => void;
gpio: number[] & {subscribe: (f: (gpio: number[]) => void) => void}; // read + write (should be 256-tuple) gpio: (
number[]
// & {subscribe: (f: (gpio: number[]) => void) => void}
); // read + write (should be 256-tuple)
// state // state
readonly state: { readonly state: {
@ -97,8 +100,7 @@ export const makePicoConsole = async (props: {
handle.pico8_state = {}; handle.pico8_state = {};
handle.pico8_buttons = [0,0,0,0,0,0,0,0]; handle.pico8_buttons = [0,0,0,0,0,0,0,0];
handle.pico8_mouse = [0,0,0]; handle.pico8_mouse = [0,0,0];
let gpioChanged = (gpio: number[]) => {}; handle.pico8_gpio = [
const gpioInner = [
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
@ -116,27 +118,46 @@ export const makePicoConsole = async (props: {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
]; ];
handle.pico8_gpio = new Proxy(gpioInner, { // let gpioChanged = (gpio: number[]) => {};
get(target, prop) { // const gpioInner = [
return target[prop as any]; // 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
}, // 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
set(target, prop, newValue) { // 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
const t = target as any; // 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
if (t.setting) { // 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
return false; // 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
} // 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
const prev = [...target]; // 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
target[prop as any] = newValue; // 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
const next = [...target]; // 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
if (!t.dontSend && prev.some((p, i) => p !== next[i])) { // 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
gpioChanged(target); // 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
} // 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
return true; // 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
} // 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
}); // 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
(handle as any).pico8_gpio.subscribe = (f: (gpio: number[]) => void) => { // ];
gpioChanged = f; // handle.pico8_gpio = new Proxy(gpioInner, {
} // get(target, prop) {
// return target[prop as any];
// },
// set(target, prop, newValue) {
// const t = target as any;
// if (t.setting) {
// return false;
// }
// const prev = [...target];
// target[prop as any] = newValue;
// const next = [...target];
// if (!t.dontSend && prev.some((p, i) => p !== next[i])) {
// gpioChanged(target);
// }
// return true;
// }
// });
// (handle as any).pico8_gpio.subscribe = (f: (gpio: number[]) => void) => {
// gpioChanged = f;
// }
handle.pico8_gamepads = {count: 0}; handle.pico8_gamepads = {count: 0};
return { return {
raw: handle, raw: handle,