From 49b731c26cdf7ac3f480b5e2e4628d0451ff244d Mon Sep 17 00:00:00 2001 From: Serge NOEL Date: Mon, 23 Feb 2026 13:13:32 +0100 Subject: [PATCH] Update mouse --- data/css/style.css | 18 +++++++++++ data/{ => img}/Speed.png | Bin data/js/mouse.js | 67 +++++++++++++++++++++++++++++++++++++++ data/mouse.html | 67 ++++++++++++++++++++------------------- 4 files changed, 120 insertions(+), 32 deletions(-) create mode 100644 data/css/style.css rename data/{ => img}/Speed.png (100%) create mode 100644 data/js/mouse.js diff --git a/data/css/style.css b/data/css/style.css new file mode 100644 index 0000000..d2c2c59 --- /dev/null +++ b/data/css/style.css @@ -0,0 +1,18 @@ +body { + background: #f4f4f4; + font-family: Arial, sans-serif; +} + +.container { + max-width: 400px; + margin: 40px auto; + background: #fff; + border-radius: 8px; + box-shadow: 0 2px 8px rgba(0,0,0,0.1); + padding: 32px; +} + +#speedometer { + display: block; + margin: 0 auto; +} \ No newline at end of file diff --git a/data/Speed.png b/data/img/Speed.png similarity index 100% rename from data/Speed.png rename to data/img/Speed.png diff --git a/data/js/mouse.js b/data/js/mouse.js new file mode 100644 index 0000000..de24acf --- /dev/null +++ b/data/js/mouse.js @@ -0,0 +1,67 @@ +// Enum available loco mode +var LOCO_MODE = { + ANALOG: 0, // Analog mode is for old DC locomotives that use a variable voltage on the track to control speed. + MARKLIN: 1, // Märklin mode is for old marklin analog AC (with relay dir) Märklin locomotives. + DCC: 2 // DCC mode is for Digital Command Control, a standard for digital model railway control. +}; + +var LOCO_DIR = { + FORWARD: 0, // Forward direction + BACKWARD: 1 // Backward direction +}; + +var Loco = { + Mode: LOCO_MODE.DCC, + ID: 0, + Speed: 0, + Dir: LOCO_DIR.FORWARD, + Scale: 1, // Example real loco speed is 45Kmh/max, so what are step (128 for digital to represent 0-200Kmh) per Kmh + MaxSpeed: 100, // Max speed in DCC step (<129) to reach scale speed (e.g. 45Kmh) + setMode: function(mode) { + this.Mode = mode; + }, + setID: function(id) { + this.ID = id; + }, + setSpeed: function(speed) { + this.Speed = speed; + setSpeed(speed); // Update the speedometer needle + }, + setDir: function(dir) { + this.Dir = dir; + } +}; + +/** + * Update the speedometer based on the slider value. The slider value is expected to be in the range 0-128, + * when a value is received, it is converted to a speed in Kmh based on the loco's scale and max speed, + * then the speedometer needle is updated accordingly. + * + * @param {int} value + */ +function updateSpeedometer(value) { + Loco.setSpeed(parseInt(value)); +} + +/** + * Speed is expected to be in the range 0-200. This function maps it to an angle for the needle. + * at 100Kmh, needle is vertical, it's length is 50px, and it rotates from -120deg (0 speed) to +120deg (max speed). + * At speed=100, x2=150, y2=70 + */ +function setSpeed(speed) +{ + // Clamp speed + speed = Math.max(0, Math.min(200, speed)); + // Map speed to angle: 0 = -120deg, 100 = 0deg, 200 = +120deg + var angle = -120 + (speed / 200) * 240; + var rad = angle * Math.PI / 180; + var r = 50; // needle length + var cx = 150, cy = 120; + var x2 = cx + r * Math.sin(rad); // Use sin for x, cos for y to match SVG coordinate system + var y2 = cy - r * Math.cos(rad); + document.getElementById('needle').setAttribute('x2', x2); + document.getElementById('needle').setAttribute('y2', y2); +} + +// Example: set speed to 0 initially +setSpeed(0); \ No newline at end of file diff --git a/data/mouse.html b/data/mouse.html index 6dd2cc3..f014f09 100644 --- a/data/mouse.html +++ b/data/mouse.html @@ -4,50 +4,53 @@ Mouse Speedometer - +

Mouse Speedometer

+
+ + + + 34 + + Analogique + + + + + + +
+
+ + Loco +
- +
- - - + + +
+
+ +
F0
+
F1
+
F2
+
F3
+
F4
+
F5
+
F6
+
F7
- + +