Skip to content

Commit e96b6c0

Browse files
authored
feat: using PRG RAISE MIT scratch playground (#43)
1 parent 2111a9e commit e96b6c0

File tree

23 files changed

+483
-980
lines changed

23 files changed

+483
-980
lines changed

.github/workflows/release.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,15 @@ jobs:
3131
- name: Install Task
3232
uses: arduino/setup-task@v2
3333

34-
- name: Install dprint
34+
- name: Install
3535
run: |
3636
task install
3737
38+
- name: use pnpm
39+
uses: pnpm/action-setup@v4
40+
with:
41+
version: 10
42+
3843
- name: Fmt check
3944
run: |
4045
task fmt:check

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
scratch-editor
2+
prg-raise-playground
23
build/
34
.bin/
4-
.cache/
5+
.cache/

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ NOTE: the `https` is needed by the `getUserMedia()` method for security reason.
2525
## Local development
2626

2727
- `task scratch:init`
28-
- `task scratch:local:start`
29-
- `ŧask board:upload`
30-
- change the `const DEFAULT_HOST =`<YOUR_IP|BOARD_NAME>`;` in the `scratch-arduino-extensions/packages/scratch-vm/src/extensions/ArduinoUnoQ.js`
31-
- Open local scratch on http://localhost:8601/
28+
- `task scratch:watch` watch scratch GUI files and reload on save
29+
- Open the `http://localhost:8602?host=BOARD_IP`
30+
- `task watch` watch files changes for both python and sketch, and upload the changes to the board and restart"
31+
32+
For testing on the board
33+
34+
- `ŧask app:build`
35+
- `task board:app:upload`

Taskfile.yaml

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
version: "3"
22
vars:
3-
SCRATCH_EDITOR_VERSION: v12.0.1
43
DPRINT_VERSION: 0.48.0
54

65
tasks:
76
install:
87
cmds:
98
- curl -fsSL https://dprint.dev/install.sh | sh -s {{ .DPRINT_VERSION }}
109
- mkdir -p .bin && cp $HOME/.dprint/bin/dprint .bin/dprint # workaround for local install
10+
- curl -fsSL https://get.pnpm.io/install.sh | sh - && source ~/.bashrc
1111

1212
fmt:
1313
desc: Run format
@@ -20,21 +20,17 @@ tasks:
2020
- ${PWD}/.bin/dprint check
2121

2222
scratch:init:
23-
cmds:
24-
- rm -rf scratch-editor
25-
- git clone --depth 1 --branch {{ .SCRATCH_EDITOR_VERSION }} https://github.com/scratchfoundation/scratch-editor.git
26-
- cd scratch-editor && npm install
27-
- cd scratch-editor && npm run build
28-
- task scratch:patch
29-
30-
scratch:patch:
31-
cmds:
32-
- cd scratch-editor/packages/scratch-gui && node ../../../scratch-arduino-extensions/scripts/patch-gui.js
23+
- git config --global url."https://github.com/mitmedialab/prg-raise-playground-scratch-gui.git".insteadOf "git@github.com:mitmedialab/prg-raise-playground-scratch-gui.git"
24+
- git config --global url."https://github.com/mitmedialab/prg-raise-playground-scratch-vm.git".insteadOf "git@github.com:mitmedialab/prg-raise-playground-scratch-vm.git"
25+
- git clone --recurse-submodules https://github.com/mitmedialab/prg-raise-playground.git
26+
- cd prg-raise-playground && git switch dev && pnpm install
27+
# copy the extension to the rigth place
28+
- ln -s $PWD/scratch-prg-extensions/extensions/src/arduino_basics $PWD/prg-raise-playground/extensions/src/arduino_basics
29+
- cd scratch-prg-extensions/extensions/src/arduino_basics && pnpm install
3330

34-
scratch:local:start:
35-
dir: scratch-editor
31+
scratch:watch:
3632
cmds:
37-
- npm start --workspace @scratch/scratch-gui
33+
- cd prg-raise-playground && pnpm dev -i arduino_basics
3834

3935
app:build:
4036
desc: "Copy app files (python, assets, app.yaml) to a build directory"
@@ -46,17 +42,12 @@ tasks:
4642
- cp -r ./python build/scratch-arduino-app/python
4743
- cp -r ./certs build/scratch-arduino-app/certs
4844
- task scratch:build
49-
- mkdir -p build/scratch-arduino-app/assets
50-
- cp scratch-editor/packages/scratch-gui/build/index.html build/scratch-arduino-app/assets/index.html
51-
- cp scratch-editor/packages/scratch-gui/build/gui.js build/scratch-arduino-app/assets/gui.js
52-
- mkdir -p build/scratch-arduino-app/assets/static
53-
- cp -r scratch-editor/packages/scratch-gui/build/static/blocks-media build/scratch-arduino-app/assets/static/blocks-media
45+
- cp -r prg-raise-playground/build/. build/scratch-arduino-app/assets
5446

5547
scratch:build:
56-
desc: "Build Scratch GUI files"
57-
dir: scratch-editor/packages/scratch-gui
48+
dir: prg-raise-playground
5849
cmds:
59-
- npm run build:dev --workspace @scratch/scratch-gui
50+
- CI=true pnpm build
6051

6152
board:app:upload:
6253
desc: "Upload zip file to Arduino board, unzip and deploy to /home/arduino/ArduinoApps"
@@ -73,6 +64,9 @@ tasks:
7364
adb shell "cd /tmp && unzip -o $ZIP_BASENAME && mkdir -p /home/arduino/ArduinoApps && rm -rf /home/arduino/ArduinoApps/scratch-arduino-app && mv scratch-arduino-app /home/arduino/ArduinoApps/ && rm $ZIP_BASENAME"
7465
echo "App deployed to /home/arduino/ArduinoApps/scratch-arduino-app"
7566
67+
app:start:
68+
- adb shell "arduino-app-cli app start user:scratch-arduino-app"
69+
7670
app:zip:
7771
desc: "Create a zip file with version (defaults to git commit hash)"
7872
vars:
@@ -88,7 +82,7 @@ tasks:
8882
- cd build && zip -r scratch-arduino-app-{{.APP_VERSION}}.zip scratch-arduino-app && cd ..
8983

9084
watch:
91-
desc: "wath cfile changes for both python and sketch, and upload the changes to the board and restart"
85+
desc: "watch files changes for both python and sketch, and upload the changes to the board and restart"
9286
deps:
9387
- python:watch
9488
- sketch:watch

app.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ ports:
44
- 7000
55
bricks:
66
- arduino:web_ui
7-
- arduino:object_detection
87
icon: 🐱

doc/scratch-unoq.png

-3.89 KB
Loading

python/main.py

Lines changed: 5 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,16 @@
11
from arduino.app_utils import App, Bridge
22
from arduino.app_bricks.web_ui import WebUI
3-
from arduino.app_bricks.object_detection import ObjectDetection
4-
import time
5-
import base64
6-
7-
object_detection = ObjectDetection()
8-
9-
10-
def on_matrix_draw(_, data):
11-
print(f"Received frame to draw on matrix: {data}")
12-
# from 5x5 to 8x13 matrix
13-
frame_5x5 = data.get("frame")
14-
row0 = "0" * 13
15-
row1 = "0" * 4 + frame_5x5[0:5] + "0" * 4
16-
row2 = "0" * 4 + frame_5x5[5:10] + "0" * 4
17-
row3 = "0" * 4 + frame_5x5[10:15] + "0" * 4
18-
row4 = "0" * 4 + frame_5x5[15:20] + "0" * 4
19-
row5 = "0" * 4 + frame_5x5[20:25] + "0" * 4
20-
row6 = "0" * 13
21-
row7 = "0" * 13
22-
frame_8x13 = row0 + row1 + row2 + row3 + row4 + row5 + row6 + row7
23-
print(f"Transformed frame to draw on 8x13 matrix: {frame_8x13}")
24-
Bridge.call("matrix_draw", frame_8x13)
25-
26-
27-
def rgb_to_digital(value, threshold=128) -> bool:
28-
"""Convert RGB value (0-255) to digital HIGH(1) or LOW(0)"""
29-
return value >= threshold
30-
31-
32-
def on_set_led_rgb(_, data):
33-
led = data.get("led")
34-
r = data.get("r")
35-
g = data.get("g")
36-
b = data.get("b")
37-
38-
# Convert RGB values (0-255) to digital HIGH/LOW
39-
r_digital = rgb_to_digital(r)
40-
g_digital = rgb_to_digital(g)
41-
b_digital = rgb_to_digital(b)
42-
43-
print(
44-
f"Setting LED {led} to color: RGB({r},{g},{b}) -> Digital({r_digital},{g_digital},{b_digital})"
45-
)
46-
Bridge.call("set_led_rgb", led, r_digital, g_digital, b_digital)
47-
48-
49-
def on_detect_objects(client_id, data):
50-
"""Callback function to handle object detection requests."""
51-
try:
52-
image_data = data.get("image")
53-
confidence = data.get("confidence", 0.5)
54-
if not image_data:
55-
# TODO: implement the 'detection_error` in the extension
56-
ui.send_message("detection_error", {"error": "No image data"})
57-
return
58-
59-
start_time = time.time() * 1000
60-
results = object_detection.detect(base64.b64decode(image_data), confidence=confidence)
61-
diff = time.time() * 1000 - start_time
62-
63-
if results is None:
64-
ui.send_message("detection_error", {"error": "No results returned"})
65-
return
66-
67-
response = {
68-
"detection": results.get("detection", []),
69-
"detection_count": len(results.get("detection", [])) if results else 0,
70-
"processing_time": f"{diff:.2f} ms",
71-
}
72-
ui.send_message("detection_result", response)
73-
74-
except Exception as e:
75-
ui.send_message("detection_error", {"error": str(e)})
76-
773

784
ui = WebUI(use_ssl=True)
795
ui.on_connect(lambda sid: (print(f"Client connected: {sid} "),))
80-
ui.on_message("matrix_draw", on_matrix_draw)
81-
ui.on_message("set_led_rgb", on_set_led_rgb)
82-
ui.on_message("detect_objects", on_detect_objects)
836

847

85-
def on_modulino_button_pressed(btn):
86-
ui.send_message("modulino_buttons_pressed", {"btn": btn})
8+
def on_matrix_draw(_, data):
9+
frame = data.get("frame")
10+
print(f"Frame to draw on 8x13 matrix: {frame}")
11+
Bridge.call("matrix_draw", frame)
8712

8813

89-
Bridge.provide("modulino_button_pressed", on_modulino_button_pressed)
14+
ui.on_message("matrix_draw", on_matrix_draw)
9015

9116
App.run()

scratch-arduino-extensions/packages/scratch-vm/src/extensions/ArduinoUnoQ.js

Lines changed: 0 additions & 133 deletions
This file was deleted.

0 commit comments

Comments
 (0)