Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
TwoSquirrels committed Jul 9, 2024
1 parent 618d242 commit dc77c01
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ const elms = {
let host = "http://localhost";
let interval = 200;
let showButtons = true;
let intervalId = null;

async function postJoystick(angle = Math.PI / 2, force = 1) {
const query = [
Math.round((angle * 128) / Math.PI + (force >= 0 ? 256 : 384)) % 256,
Math.min(Math.abs(Math.floor(force * 255)), 255),
]
.map((num) => num.toString(16).toUpperCase().padStart(2, "0"))
.join("");
console.log("POST joystick", { angle, force, query });
const res = await fetch(`${host}/joystick?q=${query}`, { method: "POST" });
console.log("Responded joystick", await res.arrayBuffer(), await res.text());
}

elms.settings.host.oninput = ({ target: input }) => {
console.log(`Modify host ${host} -> ${input.value}`);
Expand All @@ -42,6 +55,31 @@ const joystickManager = nipplejs.create({
mode: "dynamic",
});

let angle, force;

joystickManager
.on("start,end,move", (_event, nipple) => {
angle = nipple.angle.radian;
force = nipple.force;
})
.on("start", (_event, nipple) => {
clearInterval(intervalId);
intervalId = setInterval(() => postJoystick(angle, force), interval);
})
.on("end", (_event, nipple) => {
clearInterval(intervalId);
postJoystick(angle, force);
});

for (const button of elms.main.buttonsZone.childNodes) {
button.onclick = async ({ target: button }) => {
const id = button.innerText.toLowerCase();
console.log("POST button", id);
const res = await fetch(`${host}/${id}`, { method: "POST" });
console.log("Responded button", await res.arrayBuffer(), await res.text());
};
}

document.addEventListener("DOMContentLoaded", ({}) => {
for (const input of Object.values(elms.settings)) {
input.oninput?.({ target: input });
Expand Down

0 comments on commit dc77c01

Please sign in to comment.