Skip to content

Commit d22ae15

Browse files
committed
chore: add draw matrix block
1 parent 9dfb849 commit d22ae15

File tree

7 files changed

+110
-85
lines changed

7 files changed

+110
-85
lines changed

Taskfile.yaml

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,28 @@ vars:
55
tasks:
66
scratch:init:
77
- git clone --depth 1 --branch {{ .SCRATCH_EDITOR_VERSION }} https://github.com/scratchfoundation/scratch-editor.git
8-
- cd scratch-editor && npm install && npm run build
9-
- cd scratch-editor/packages/scratch-gui && npm install
10-
- cd scratch-editor/packages/scratch-vm && npm install
8+
9+
scratch:install:
10+
dir: scratch-editor
11+
cmds:
12+
- npm install
13+
- npm run build --workspace @scratch/scratch-svg-renderer
14+
- npm run build --workspace @scratch/scratch-vm
15+
- npm run build:dev --workspace @scratch/scratch-gui
1116

1217
scratch:patch:
1318
- cd scratch-editor/packages/scratch-gui && node ../../../scratch-arduino-extensions/scripts/patch-gui.js
1419

1520
scratch:start:
16-
- cd scratch-editor/packages/scratch-gui && npm start
21+
dir: scratch-editor
22+
- npm start --workspace @scratch/scratch-gui
1723

1824
scratch:clean:
1925
- rm -rf scratch-editor
26+
27+
board:upload:
28+
- adb push ./python/main.py /home/arduino/ArduinoApps/scratch-arduino-app/python/main.py
29+
- adb push ./sketch/sketch.ino /home/arduino/ArduinoApps/scratch-arduino-app/sketch/sketch.ino
30+
- adb push ./sketch/sketch.yaml /home/arduino/ArduinoApps/scratch-arduino-app/sketch/sketch.yaml
31+
- adb push ./app.yaml /home/arduino/ArduinoApps/scratch-arduino-app/app.yaml
32+
# - adb shell arduino-app-cli app restart user:scratch-arduino-app

app.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
name: scratch-arduino-app
22
description: Controll the Uno-Q borad using Scractch blocks
3-
ports: []
4-
bricks: []
3+
ports:
4+
- 7000
5+
bricks:
6+
- arduino:web_ui
57
icon: 💡

python/main.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
1-
from arduino.app_utils import App
1+
from arduino.app_utils import App, Bridge
2+
from arduino.app_bricks.web_ui import WebUI
23
import time
34

4-
def loop():
5-
time.sleep(1) # do nothing: only to keep the track the app status
6-
7-
App.run(user_loop=loop)
5+
ui = WebUI()
6+
7+
def on_matrix_draw(_, data):
8+
print(f"Received frame to draw on matrix: {data}")
9+
# from 5x5 to 8x13 matrix
10+
frame_5x5 = data.get('frame')
11+
row0 = "0"*13
12+
row1 = "0"*4 + frame_5x5[0:5] + "0"*4
13+
row2 = "0"*4 + frame_5x5[5:10] + "0"*4
14+
row3 = "0"*4 + frame_5x5[10:15] + "0"*4
15+
row4 = "0"*4 + frame_5x5[15:20] + "0"*4
16+
row5 = "0"*4 + frame_5x5[20:25] + "0"*4
17+
row6 = "0"*13
18+
row7 = "0"*13
19+
frame_8x13 = row0 + row1 + row2 + row3 + row4 + row5 + row6 + row7
20+
print(f"Transformed frame to draw on 8x13 matrix: {frame_8x13}")
21+
Bridge.call("matrix_draw", frame_8x13)
22+
23+
ui.on_message("matrix_draw", on_matrix_draw)
24+
25+
ui.on_connect(
26+
lambda sid: (
27+
print(f"Client connected: {sid} "),
28+
)
29+
)
30+
App.run()

scratch-arduino-extensions/packages/scratch-vm/src/extensions/scratch3_arduino/index.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
// Option 1: Relative path to cousin folder
21
// const formatMessage = require('../../../../../../scratch-editor/node_modules/format-message');
32
const BlockType = require('../../../../../../scratch-editor/packages/scratch-vm/src/extension-support/block-type');
3+
const ArgumentType = require('../../../../../../scratch-editor/packages/scratch-vm/src/extension-support/argument-type');
4+
const io = require('./socket.io.min.js');
45

56
/**
67
* Url of icon to be displayed at the left edge of each extension block.
@@ -16,9 +17,16 @@ const iconURI = '';
1617
// eslint-disable-next-line max-len
1718
const menuIconURI = ''
1819

20+
const wsServerURL = 'http://192.168.1.39:7000';
21+
1922
class Scratch3Arduino {
2023
constructor (runtime) {
2124
this.runtime = runtime;
25+
this.io = io(wsServerURL, {
26+
path: '/socket.io',
27+
transports: ['polling','websocket'],
28+
autoConnect: true
29+
});
2230
}
2331
};
2432

@@ -30,17 +38,24 @@ Scratch3Arduino.prototype.getInfo = function () {
3038
blockIconURI: iconURI,
3139
blocks: [
3240
{
33-
opcode: 'noop',
41+
opcode: 'matrixDraw',
3442
blockType: BlockType.COMMAND,
35-
text: 'do nothing',
36-
func: 'noop'
43+
text: 'draw [FRAME] on matrix',
44+
func: 'matrixDraw',
45+
arguments: {
46+
FRAME: {
47+
type: ArgumentType.MATRIX,
48+
defaultValue: '0101010101100010101000100'
49+
}
50+
}
3751
},
3852
],
39-
4053
};
4154
}
4255

43-
Scratch3Arduino.prototype.noop = function () {
56+
Scratch3Arduino.prototype.matrixDraw = function (args) {
57+
console.log(`Drawing frame on matrix: ${args}`);
58+
this.io.emit("matrix_draw", {frame: args.FRAME});
4459
};
4560

4661
module.exports = Scratch3Arduino;

scratch-arduino-extensions/packages/scratch-vm/src/extensions/scratch3_arduino/socket.io.min.js

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sketch/sketch.ino

Lines changed: 28 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,41 @@
1-
// SPDX-FileCopyrightText: Copyright (C) 2025 ARDUINO SA <http://www.arduino.cc>
2-
//
3-
// SPDX-License-Identifier: MPL-2.0
4-
51
#include <Arduino_RouterBridge.h>
6-
#include <Modulino.h>
7-
8-
// TODO: those will go into an header file.
9-
extern "C" void matrixWrite(const uint32_t* buf);
10-
extern "C" void matrixBegin();
11-
12-
ModulinoButtons buttons;
2+
#include "Arduino_LED_Matrix.h"
133

14-
void set_led_state(bool state) {
15-
// LOW state means LED is ON
16-
digitalWrite(LED_BUILTIN, state ? LOW : HIGH);
17-
}
4+
Arduino_LED_Matrix matrix;
185

196
void setup() {
20-
matrixBegin();
21-
//Monitor.begin();
22-
Modulino.begin(Wire1);
23-
24-
while(!buttons.begin()){
25-
set_led_state(true);
26-
delay(1000);
27-
set_led_state(false);
28-
}
29-
buttons.setLeds(true, true, true);
30-
31-
pinMode(LED_BUILTIN, OUTPUT);
32-
// Start with the LED OFF (HIGH state of the PIN)
33-
digitalWrite(LED_BUILTIN, HIGH);
34-
7+
matrix.begin();
358
Bridge.begin();
36-
Bridge.provide("set_led_state", set_led_state);
379
Bridge.provide("matrix_draw", matrix_draw);
3810
}
3911

40-
void loop() {
41-
42-
if (buttons.update()) {
43-
if (buttons.isPressed(0)) {
44-
// Monitor.println("button a pressed");
45-
// Serial.println("Button A pressed!");
46-
Bridge.notify("button_pressed");
47-
set_led_state(true);
48-
delay(500);
49-
} else if (buttons.isPressed(1)) {
50-
// Serial.println("Button B pressed!");
51-
// Monitor.println("button B pressed");
52-
set_led_state(false);
53-
Bridge.notify("button_pressed");
54-
delay(500);
55-
} else if (buttons.isPressed(1)) {
56-
// Serial.println("Button C pressed!");
57-
// Monitor.println("button C pressed");
58-
Bridge.notify("button_pressed");
59-
delay(500);
60-
}
61-
}
62-
}
63-
64-
const uint32_t heart[] = {
65-
0x1f88ffff,
66-
0xe0fe03f0,
67-
0xc1ef1e3d,
68-
0x1f
12+
void loop() {}
13+
14+
uint8_t shades[104] = {
15+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
17+
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
18+
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
19+
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
20+
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
21+
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
22+
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
6923
};
7024

71-
void matrix_draw(){
72-
matrixWrite(heart);
25+
26+
void matrix_draw(String frame){
27+
if (frame.length() != 104) {
28+
Serial.println("Error: Frame length must be 104 characters.");
29+
return;
30+
}
31+
for (int i = 0; i < 104; i++) {
32+
if (frame.charAt(i) == '1') {
33+
shades[i] = 1;
34+
} else{
35+
shades[i] = 0;
36+
}
37+
}
38+
matrix.draw(shades);
7339
}
7440

7541

sketch/sketch.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ profiles:
88
- DebugLog (0.8.4)
99
- ArxContainer (0.7.0)
1010
- ArxTypeTraits (0.3.1)
11-
- Modulino (0.5.1)
12-
- Arduino_HS300x (1.0.0)
13-
- STM32duino VL53L4CD (1.0.5)
14-
- STM32duino VL53L4ED (1.0.1)
15-
- Arduino_LSM6DSOX (1.1.2)
16-
- Arduino_LPS22HB (1.0.2)
11+
# - Modulino (0.5.1)
12+
# - Arduino_HS300x (1.0.0)
13+
# - STM32duino VL53L4CD (1.0.5)
14+
# - STM32duino VL53L4ED (1.0.1)
15+
# - Arduino_LSM6DSOX (1.1.2)
16+
# - Arduino_LPS22HB (1.0.2)
1717
default_profile: default

0 commit comments

Comments
 (0)