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 4bd4c7a
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ 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 angle256 =
Math.round((angle * 128) / Math.PI + (force >= 0 ? 256 : 384)) % 256;
const force256 = Math.min(Math.abs(Math.floor(force * 255)), 255);
const query = [angle256, force256]
.map((num) => num.toString(16).toUpperCase().padStart(2, "0"))
.join("");
console.log(`POST joystick`);
const response = await fetch(host + "/joystick?q=" + query, {
method: "POST",
});
}

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

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

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

0 comments on commit 4bd4c7a

Please sign in to comment.