ctrlpad: move controls to array

pull/3383/head
Rob Pilling 2023-08-28 12:39:44 +01:00
parent d2315d59b2
commit 40a1b1a209
1 changed files with 48 additions and 22 deletions

View File

@ -25,6 +25,10 @@
}; };
} }
type Control = {
text: string,
cb: () => void,
};
class Overlay { class Overlay {
g2: Graphics; g2: Graphics;
@ -78,12 +82,14 @@
const centreY = this.height / 2; const centreY = this.height / 2;
const circleGapY = 30; const circleGapY = 30;
this.drawCtrl(this.width / 4 - 10, centreY - circleGapY, "<"); const controls = getControls();
this.drawCtrl(this.width / 2, centreY - circleGapY, "@");
this.drawCtrl(this.width * 3/4 + 10, centreY - circleGapY, ">");
this.drawCtrl(this.width / 3, centreY + circleGapY, "-"); this.drawCtrl(this.width / 4 - 10, centreY - circleGapY, controls[0].text);
this.drawCtrl(this.width * 2/3, centreY + circleGapY, "+"); this.drawCtrl(this.width / 2, centreY - circleGapY, controls[1].text);
this.drawCtrl(this.width * 3/4 + 10, centreY - circleGapY, controls[2].text);
this.drawCtrl(this.width / 3, centreY + circleGapY, controls[3].text);
this.drawCtrl(this.width * 2/3, centreY + circleGapY, controls[4].text);
} }
drawCtrl(x: number, y: number, label: string): void { drawCtrl(x: number, y: number, label: string): void {
@ -118,6 +124,28 @@
let overlay: Overlay | undefined; let overlay: Overlay | undefined;
let touchDown = false; let touchDown = false;
type Controls = [Control, Control, Control, Control, Control];
const getControls = (): Controls => {
const connected = NRF.getSecurityStatus().connected;
if (connected) {
return [
{ text: "<", cb: hid.next },
{ text: "@", cb: hid.toggle },
{ text: ">", cb: hid.prev },
{ text: "-", cb: hid.down },
{ text: "+", cb: hid.up },
];
}
return [
{ text: "a", cb: () => {} },
{ text: "b", cb: () => {} },
{ text: "c", cb: () => {} },
{ text: "d", cb: () => {} },
{ text: "e", cb: () => {} },
];
};
const onDrag = (e => { const onDrag = (e => {
const dragDistance = 30; const dragDistance = 30;
@ -220,25 +248,23 @@
}; };
//const DEBUG = true; //const DEBUG = true;
/* const sendHid = (code: number) => {
const sendHid = (code: number) => { //if(DEBUG) return;
//if(DEBUG) return; try{
try{ NRF.sendHIDReport(
NRF.sendHIDReport( [1, code],
[1, code], () => NRF.sendHIDReport([1, 0]),
() => NRF.sendHIDReport([1, 0]), );
); }catch(e){
}catch(e){ console.log("sendHIDReport:", e);
console.log("sendHIDReport:", e); }
}
}; };
const hid = { const hid = {
next: () => sendHid(0x01), next: () => sendHid(0x01),
prev: () => sendHid(0x02), prev: () => sendHid(0x02),
toggle: () => sendHid(0x10), toggle: () => sendHid(0x10),
up: () => sendHid(0x40), up: () => sendHid(0x40),
down: () => sendHid(0x80), down: () => sendHid(0x80),
}; };
*/
})() })()