diff --git a/README.md b/README.md index 1bbe913..190978a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Scratch for Arduino Uno Q +# Scratch for Arduino Uno Q ## Installation @@ -13,5 +13,6 @@ curl -sSL https://raw.githubusercontent.com/dido18/scratch-arduino-app/main/inst ### Local development - `task scratch:init` - `task scratch:local:start` -- change the `const wsServerURL = `:7000`;` in the `index.js` -- Open local scratch on http://localhost:8601/ \ No newline at end of file +- `ŧask board:upload` +- change the `const wsServerURL = `ws://:7000`;` in the `index.js` +- Open local scratch on http://localhost:8601/ diff --git a/python/main.py b/python/main.py index 795e8ac..554091d 100644 --- a/python/main.py +++ b/python/main.py @@ -26,11 +26,30 @@ def on_matrix_draw(_, data): print(f"Transformed frame to draw on 8x13 matrix: {frame_8x13}") Bridge.call("matrix_draw", frame_8x13) +def rgb_to_digital(value, threshold=128) -> bool: + """Convert RGB value (0-255) to digital HIGH(1) or LOW(0)""" + return value >= threshold + +def on_set_led_rgb(_, data): + led = data.get("led") + r = data.get("r") + g = data.get("g") + b = data.get("b") + + # Convert RGB values (0-255) to digital HIGH/LOW + r_digital = rgb_to_digital(r) + g_digital = rgb_to_digital(g) + b_digital = rgb_to_digital(b) + + print(f"Setting LED {led} to color: RGB({r},{g},{b}) -> Digital({r_digital},{g_digital},{b_digital})") + Bridge.call("set_led_rgb", led, r_digital, g_digital, b_digital) + ui.on_message("matrix_draw", on_matrix_draw) +ui.on_message("set_led_rgb", on_set_led_rgb) def on_modulino_button_pressed(btn): ui.send_message('modulino_buttons_pressed', {"btn": btn}) Bridge.provide("modulino_button_pressed", on_modulino_button_pressed) -App.run() \ No newline at end of file +App.run() diff --git a/scratch-arduino-extensions/packages/scratch-vm/src/extensions/arduino_basics/index.js b/scratch-arduino-extensions/packages/scratch-vm/src/extensions/arduino_basics/index.js index c3085fd..b2e6376 100644 --- a/scratch-arduino-extensions/packages/scratch-vm/src/extensions/arduino_basics/index.js +++ b/scratch-arduino-extensions/packages/scratch-vm/src/extensions/arduino_basics/index.js @@ -49,6 +49,30 @@ ArduinoBasics.prototype.getInfo = function () { defaultValue: '0101010101100010101000100' } } + }, + { + opcode: 'setLed3', + blockType: BlockType.COMMAND, + text: 'set LED 3 to [HEX]', + func: 'setLed3', + arguments: { + HEX: { + type: ArgumentType.COLOR, + defaultValue: '#ff0000' + } + } + }, + { + opcode: 'setLed4', + blockType: BlockType.COMMAND, + text: 'set LED 4 to [HEX]', + func: 'setLed4', + arguments: { + HEX: { + type: ArgumentType.COLOR, + defaultValue: '#0000ff' + } + } } ] }; @@ -59,4 +83,26 @@ ArduinoBasics.prototype.matrixDraw = function (args) { this.io.emit("matrix_draw", { frame: args.FRAME }); }; +ArduinoBasics.prototype.setLed3 = function (args) { + const hexColor = args.HEX; + const rgb = this.hexToRgb(hexColor); + console.log(`Setting led 3 to: r:${rgb.r}, g:${rgb.g}, b:${rgb.b} (HEX: ${hexColor})`); + this.io.emit("set_led_rgb", {led: "LED3", r: rgb.r, g: rgb.g,b: rgb.b}); +}; + +ArduinoBasics.prototype.setLed4 = function (args) { + const hexColor = args.HEX; + const rgb = this.hexToRgb(hexColor); + console.log(`Setting led 4 to: r:${rgb.r}, g:${rgb.g}, b:${rgb.b} (HEX: ${hexColor})`); + this.io.emit("set_led_rgb", {led: "LED4", r: rgb.r, g: rgb.g,b: rgb.b}); +}; + +ArduinoBasics.prototype.hexToRgb = function (hex) { + hex = hex.replace('#', ''); + const r = parseInt(hex.substring(0, 2), 16); + const g = parseInt(hex.substring(2, 4), 16); + const b = parseInt(hex.substring(4, 6), 16); + return { r, g, b }; +}; + module.exports = ArduinoBasics; diff --git a/sketch/sketch.ino b/sketch/sketch.ino index cafb647..a191a9e 100644 --- a/sketch/sketch.ino +++ b/sketch/sketch.ino @@ -10,9 +10,24 @@ void setup() { Bridge.begin(); Modulino.begin(Wire1); // show led indication if buttons cannot be initilized - buttons.begin(); + buttons.begin(); + pinMode(LED_BUILTIN, OUTPUT); + pinMode(LED_BUILTIN + 1, OUTPUT); + pinMode(LED_BUILTIN + 2, OUTPUT); + pinMode(LED_BUILTIN + 3, OUTPUT); + pinMode(LED_BUILTIN + 4, OUTPUT); + pinMode(LED_BUILTIN + 5, OUTPUT); + + digitalWrite(LED_BUILTIN, HIGH); + digitalWrite(LED_BUILTIN + 1, HIGH); + digitalWrite(LED_BUILTIN + 2, HIGH); + digitalWrite(LED_BUILTIN + 3, HIGH); + digitalWrite(LED_BUILTIN + 4, HIGH); + digitalWrite(LED_BUILTIN + 5, HIGH); + buttons.setLeds(true, true, true); Bridge.provide("matrix_draw", matrix_draw); + Bridge.provide("set_led_rgb", set_led_rgb); } void loop() { @@ -47,4 +62,14 @@ void matrix_draw(String frame){ matrix.draw(shades); } - +void set_led_rgb(String pin, uint8_t r, uint8_t g, uint8_t b) { + if (pin == "LED3") { + digitalWrite(LED_BUILTIN, r ? LOW : HIGH ); + digitalWrite(LED_BUILTIN + 1, g ? LOW : HIGH ); + digitalWrite(LED_BUILTIN + 2, b ? LOW: HIGH ); + } else if (pin == "LED4") { + digitalWrite(LED_BUILTIN + 3, r ? LOW : HIGH ); + digitalWrite(LED_BUILTIN + 4, g ? LOW : HIGH ); + digitalWrite(LED_BUILTIN + 5, b ? LOW : HIGH ); + } +}