Skip to content

Commit 17d693d

Browse files
committed
feat: update README for testing instructions, remove unused object detection code, and add new files for Arduino basics extension
1 parent 836d53d commit 17d693d

File tree

5 files changed

+10
-79
lines changed

5 files changed

+10
-79
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ NOTE: the `https` is needed by the `getUserMedia()` method for security reason.
2929
- Open the `http://localhost:8602?host=BOARD_IP`
3030
- `task watch` watch files changes for both python and sketch, and upload the changes to the board and restart"
3131

32-
3332
For testing on the board
33+
3434
- `ŧask app:build`
3535
- `task board:app:upload`

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: 🐱

python/main.py

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
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
63

7-
object_detection = ObjectDetection()
4+
ui = WebUI(use_ssl=True)
5+
ui.on_connect(lambda sid: (print(f"Client connected: {sid} "),))
86

97

108
def on_matrix_draw(_, data):
@@ -13,68 +11,6 @@ def on_matrix_draw(_, data):
1311
Bridge.call("matrix_draw", frame)
1412

1513

16-
def rgb_to_digital(value, threshold=128) -> bool:
17-
"""Convert RGB value (0-255) to digital HIGH(1) or LOW(0)"""
18-
return value >= threshold
19-
20-
21-
def on_set_led_rgb(_, data):
22-
led = data.get("led")
23-
r = data.get("r")
24-
g = data.get("g")
25-
b = data.get("b")
26-
27-
# Convert RGB values (0-255) to digital HIGH/LOW
28-
r_digital = rgb_to_digital(r)
29-
g_digital = rgb_to_digital(g)
30-
b_digital = rgb_to_digital(b)
31-
32-
print(
33-
f"Setting LED {led} to color: RGB({r},{g},{b}) -> Digital({r_digital},{g_digital},{b_digital})"
34-
)
35-
Bridge.call("set_led_rgb", led, r_digital, g_digital, b_digital)
36-
37-
38-
def on_detect_objects(client_id, data):
39-
"""Callback function to handle object detection requests."""
40-
try:
41-
image_data = data.get("image")
42-
confidence = data.get("confidence", 0.5)
43-
if not image_data:
44-
# TODO: implement the 'detection_error` in the extension
45-
ui.send_message("detection_error", {"error": "No image data"})
46-
return
47-
48-
start_time = time.time() * 1000
49-
results = object_detection.detect(base64.b64decode(image_data), confidence=confidence)
50-
diff = time.time() * 1000 - start_time
51-
52-
if results is None:
53-
ui.send_message("detection_error", {"error": "No results returned"})
54-
return
55-
56-
response = {
57-
"detection": results.get("detection", []),
58-
"detection_count": len(results.get("detection", [])) if results else 0,
59-
"processing_time": f"{diff:.2f} ms",
60-
}
61-
ui.send_message("detection_result", response)
62-
63-
except Exception as e:
64-
ui.send_message("detection_error", {"error": str(e)})
65-
66-
67-
ui = WebUI(use_ssl=True)
68-
ui.on_connect(lambda sid: (print(f"Client connected: {sid} "),))
6914
ui.on_message("matrix_draw", on_matrix_draw)
70-
ui.on_message("set_led_rgb", on_set_led_rgb)
71-
ui.on_message("detect_objects", on_detect_objects)
72-
73-
74-
def on_modulino_button_pressed(btn):
75-
ui.send_message("modulino_buttons_pressed", {"btn": btn})
76-
77-
78-
Bridge.provide("modulino_button_pressed", on_modulino_button_pressed)
7915

8016
App.run()

scratch-prg-extensions/extensions/src/arduino_basics/.filesToBundle.js

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

scratch-prg-extensions/extensions/src/arduino_basics/index.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
type Environment,
3-
extension,
4-
type ExtensionMenuDisplayDetails,
5-
scratch,
6-
} from "$common";
1+
import { type Environment, extension, type ExtensionMenuDisplayDetails, scratch } from "$common";
72
import { io, Socket } from "socket.io-client";
83
import MatrixArgument from "./MatrixArgument.svelte";
94

@@ -20,15 +15,15 @@ const details: ExtensionMenuDisplayDetails = {
2015

2116
// Get Arduino board IP or hostname from URL parameter - required
2217
const getArduinoBoardHost = () => {
23-
if (typeof window !== 'undefined' && window.location) {
18+
if (typeof window !== "undefined" && window.location) {
2419
const urlParams = new URLSearchParams(window.location.search);
25-
const boardHost = urlParams.get('host');
20+
const boardHost = urlParams.get("host");
2621
if (boardHost) {
2722
console.log(`Connecting to Arduino board: ${boardHost}`);
2823
return boardHost;
2924
}
3025
}
31-
throw new Error('Arduino board host required. Add ?host=arduino_board_ip_or_name to the URL');
26+
throw new Error("Arduino board host required. Add ?host=arduino_board_ip_or_name to the URL");
3227
};
3328

3429
// TODO: make the block to support the brightness `0-7' of the leds
@@ -87,8 +82,7 @@ export default class ArduinoBasics extends extension(details, "ui", "customArgum
8782
}
8883
}
8984

90-
91-
@(scratch.command`Clear matrix`)
85+
@scratch.command`Clear matrix`
9286
clearMatrix(matrix: number[][]) {
9387
var matrixString = PATTERNS.empty.flat().join("");
9488
if (this.socket) {

0 commit comments

Comments
 (0)