From acefcb0cd857742135234f50940108e09ceaab03 Mon Sep 17 00:00:00 2001 From: Matthieu Bessat Date: Sun, 26 Mar 2023 15:35:25 +0200 Subject: [PATCH] refactor: abstracted Joystick component with custom zones --- src/app.css | 21 +- src/lib/Controller.svelte | 397 ++++++++++++++++-------------------- src/lib/Joystick.svelte | 230 +++++++++++++++++++++ src/lib/WebSocketService.js | 3 +- src/lib/utils.ts | 27 +++ 5 files changed, 441 insertions(+), 237 deletions(-) create mode 100644 src/lib/Joystick.svelte create mode 100644 src/lib/utils.ts diff --git a/src/app.css b/src/app.css index e599988..41b31c0 100644 --- a/src/app.css +++ b/src/app.css @@ -2,28 +2,17 @@ @tailwind components; @tailwind utilities; -body { - background: black; - color: white; -} - input { background-color: white; - color:black + color: black; } -.controller-back { - width: 10em; - height: 10em; - background: red; - border-radius: 50%; - position: relative; -} .controller-joystick { position: absolute; width: 1em; height: 1em; } + .controller-joystick-ins { position: relative; /* left: -50%; */ @@ -41,3 +30,9 @@ input { /* heigth: 10em; */ background: gray; } + +.joystick-bank { + display: grid; + grid-template-columns: 50% 50%; + height: 20em; +} diff --git a/src/lib/Controller.svelte b/src/lib/Controller.svelte index cfd433f..03bcc6e 100644 --- a/src/lib/Controller.svelte +++ b/src/lib/Controller.svelte @@ -1,21 +1,44 @@ -

Controller

+

Robot Controller

     angle: {angleFullRangeDeg.toFixed(2)} deg
     dir: {direction}
     speed: {finalSpeed}
     vec: {normalizedVec.x.toFixed(4)} {normalizedVec.y.toFixed(4)}
         
-
joystick: {prettyGpState}
+
joystick: {prettyGpState}
- - - - - -
+ {#if joystickBankEnabled} +
+ + + + + + + +
+ {/if} + + +
+
+
+

Range settings

+
+
frontLeft:
+
+ + +
+
+
+
frontRight:
+
+ + +
+
+
+
backLeft:
+
+ + +
+
+
+
backRight:
+
+ +
diff --git a/src/lib/Joystick.svelte b/src/lib/Joystick.svelte new file mode 100644 index 0000000..bd798d5 --- /dev/null +++ b/src/lib/Joystick.svelte @@ -0,0 +1,230 @@ + + +
+
diff --git a/src/lib/WebSocketService.js b/src/lib/WebSocketService.js index e712c7a..84d5a6a 100644 --- a/src/lib/WebSocketService.js +++ b/src/lib/WebSocketService.js @@ -16,6 +16,7 @@ export default class WebSocketService extends EventTarget { } /* Various other things */ + this.isEnabled = true this.recovery = false this.isConnected = false this.handlersList = [] @@ -119,7 +120,7 @@ export default class WebSocketService extends EventTarget { send(cmd, args = {}) { const payload = {cmd, args}; - if (!this.ws.readyState) { + if (!this.ws.readyState || !this.ws.isEnabled) { console.log("Would have sent", payload) return } diff --git a/src/lib/utils.ts b/src/lib/utils.ts new file mode 100644 index 0000000..e19c730 --- /dev/null +++ b/src/lib/utils.ts @@ -0,0 +1,27 @@ +/** +* Return the clamped value of a number in a bounded interval +*/ +export function clamp(v: number, min: number, max: number): number { + return Math.min(Math.max(v, min), max); +} + +/** +* Return the sign as integer of a number +*/ +export function sign(x: number): number { + if (x == 0) { + return 0; + } + if (x > 0) { + return 1; + } + return -1; +} + +/** +* Crude way to tell if two objects are the same +*/ +export function areSameObjects(a: any, b: any): boolean { + return JSON.stringify(a) == JSON.stringify(b); +} +