From 9316a8918c7ded2dfef9cca1befb0b05b56c1bc9 Mon Sep 17 00:00:00 2001 From: lucarin91 Date: Wed, 22 Oct 2025 01:59:17 +0200 Subject: [PATCH] fix: button trigger reset --- Taskfile.yaml | 6 +- assets/gui.js | 33 +--- .../src/extensions/scratch3_arduino/index.js | 144 ++++++++---------- 3 files changed, 68 insertions(+), 115 deletions(-) diff --git a/Taskfile.yaml b/Taskfile.yaml index 1884e44..98795c8 100644 --- a/Taskfile.yaml +++ b/Taskfile.yaml @@ -30,8 +30,8 @@ tasks: scratch:install: dir: scratch-editor cmds: - - npm install - - npm build + - npm install + - npm run build scratch:patch: cmds: @@ -54,4 +54,4 @@ tasks: - cp build/index.html ../../../assets/index.html - cp build/gui.js ../../../assets/gui.js ## copy (some) of the static files - - mkdir -p ../../../assets/static/blocks-media && cp -r build/static/blocks-media ../../../assets/static/ \ No newline at end of file + - mkdir -p ../../../assets/static/blocks-media && cp -r build/static/blocks-media ../../../assets/static/ diff --git a/assets/gui.js b/assets/gui.js index 76a0b67..d7b8587 100644 --- a/assets/gui.js +++ b/assets/gui.js @@ -46,30 +46,10 @@ class Scratch3Arduino { }); // TODO: move to ModulinoPeripheral - this._button_a_pressed = false; - this._button_b_pressed = false; - this._button_c_pressed = false; + this._button_pressed = ''; this.io.on('modulino_buttons_pressed', data => { console.log("Modulino button pressed event received: ".concat(data.btn)); - if (data.btn.toUpperCase() == 'A') { - this._button_a_pressed = true; - this._button_b_pressed = false; - this._button_c_pressed = false; - return; - } - if (data.btn.toUpperCase() == 'B') { - this._button_a_pressed = false; - this._button_b_pressed = true; - this._button_c_pressed = false; - return; - } - if (data.btn.toUpperCase() == 'C') { - this._button_a_pressed = false; - this._button_b_pressed = false; - this._button_c_pressed = true; - return; - } - return; + this._button_pressed = data.btn.toUpperCase(); }); } } @@ -116,12 +96,9 @@ Scratch3Arduino.prototype.matrixDraw = function (args) { }); }; Scratch3Arduino.prototype.whenModulinoButtonsPressed = function (args) { - if (args.BTN === 'A') { - return this._button_a_pressed; - } else if (args.BTN === 'B') { - return this._button_b_pressed; - } else if (args.BTN === 'C') { - return this._button_c_pressed; + if (args.BTN === this._button_pressed) { + this._button_pressed = ''; + return true; } return false; }; diff --git a/scratch-arduino-extensions/packages/scratch-vm/src/extensions/scratch3_arduino/index.js b/scratch-arduino-extensions/packages/scratch-vm/src/extensions/scratch3_arduino/index.js index 38e2e0d..4572f86 100644 --- a/scratch-arduino-extensions/packages/scratch-vm/src/extensions/scratch3_arduino/index.js +++ b/scratch-arduino-extensions/packages/scratch-vm/src/extensions/scratch3_arduino/index.js @@ -8,109 +8,85 @@ const io = require('./socket.io.min.js'); * @type {string} */ // eslint-disable-next-line max-len -const iconURI = ''; +const iconURI = ''; /** * Url of icon to be displayed in the toolbox menu for the extension category. * @type {string} */ // eslint-disable-next-line max-len -const menuIconURI = '' +const menuIconURI = '' const wsServerURL = `${window.location.protocol}//${window.location.hostname}:7000`; class Scratch3Arduino { - constructor (runtime) { - this.runtime = runtime; - this.io = io(wsServerURL, { - path: '/socket.io', - transports: ['polling','websocket'], - autoConnect: true - }); + constructor(runtime) { + this.runtime = runtime; + this.io = io(wsServerURL, { + path: '/socket.io', + transports: ['polling', 'websocket'], + autoConnect: true + }); - // TODO: move to ModulinoPeripheral - this._button_a_pressed = false; - this._button_b_pressed = false; - this._button_c_pressed = false; - - this.io.on('modulino_buttons_pressed', (data) => { - console.log(`Modulino button pressed event received: ${data.btn}`); - if (data.btn.toUpperCase() == 'A'){ - this._button_a_pressed = true; - this._button_b_pressed = false; - this._button_c_pressed = false; - return; - } - if (data.btn.toUpperCase() == 'B'){ - this._button_a_pressed = false; - this._button_b_pressed = true; - this._button_c_pressed = false; - return; - } - if (data.btn.toUpperCase() == 'C'){ - this._button_a_pressed = false; - this._button_b_pressed = false; - this._button_c_pressed = true; - return; - } - return; - }); - } + // TODO: move to ModulinoPeripheral + this._button_pressed = ''; + this.io.on('modulino_buttons_pressed', (data) => { + console.log(`Modulino button pressed event received: ${data.btn}`); + this._button_pressed = data.btn.toUpperCase(); + }); + } }; Scratch3Arduino.prototype.getInfo = function () { - return { - id: 'arduino', - name: "Arduino", - menuIconURI: menuIconURI, - blockIconURI: iconURI, - blocks: [ - { - opcode: 'matrixDraw', - blockType: BlockType.COMMAND, - text: 'draw [FRAME] on matrix', - func: 'matrixDraw', - arguments: { - FRAME: { - type: ArgumentType.MATRIX, - defaultValue: '0101010101100010101000100' - } - } - }, - { - opcode: 'whenModulinoButtonsPressed', - blockType: BlockType.HAT, - text: 'when modulino button [BTN] pressed', - func: 'whenModulinoButtonsPressed', - arguments: { - BTN: { - type: ArgumentType.STRING, - menu: 'modulinoButtons', - defaultValue: "A" - } - } - }, - ], - menus: { - modulinoButtons: ["A", "B", "C"] - } - }; + return { + id: 'arduino', + name: "Arduino", + menuIconURI: menuIconURI, + blockIconURI: iconURI, + blocks: [ + { + opcode: 'matrixDraw', + blockType: BlockType.COMMAND, + text: 'draw [FRAME] on matrix', + func: 'matrixDraw', + arguments: { + FRAME: { + type: ArgumentType.MATRIX, + defaultValue: '0101010101100010101000100' + } + } + }, + { + opcode: 'whenModulinoButtonsPressed', + blockType: BlockType.HAT, + text: 'when modulino button [BTN] pressed', + func: 'whenModulinoButtonsPressed', + arguments: { + BTN: { + type: ArgumentType.STRING, + menu: 'modulinoButtons', + defaultValue: "A" + } + } + }, + ], + menus: { + modulinoButtons: ["A", "B", "C"] + } + }; } Scratch3Arduino.prototype.matrixDraw = function (args) { - console.log(`Drawing frame on matrix: ${args}`); - this.io.emit("matrix_draw", {frame: args.FRAME}); + console.log(`Drawing frame on matrix: ${args}`); + this.io.emit("matrix_draw", { frame: args.FRAME }); }; Scratch3Arduino.prototype.whenModulinoButtonsPressed = function (args) { - if (args.BTN === 'A') { - return this._button_a_pressed - } else if (args.BTN === 'B') { - return this._button_b_pressed - } else if (args.BTN === 'C') { - return this._button_c_pressed; - } - return false; + if (args.BTN === this._button_pressed) { + this._button_pressed = ''; + return true; + } + return false; }; -module.exports = Scratch3Arduino; \ No newline at end of file +module.exports = Scratch3Arduino;