Skip to content

Commit 836d53d

Browse files
committed
feat: update README and Taskfile for improved local development instructions and fix Arduino board connection handling
1 parent 4fcd4ff commit 836d53d

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

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+
33+
For testing on the board
34+
- `ŧask app:build`
35+
- `task board:app:upload`

Taskfile.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ tasks:
2929
- cd scratch-prg-extensions/extensions/src/arduino_basics && pnpm install
3030

3131
scratch:watch:
32-
- cd prg-raise-playground && pnpm dev -i arduino_basics
32+
cmds:
33+
- cd prg-raise-playground && pnpm dev -i arduino_basics &
3334

3435
app:build:
3536
desc: "Copy app files (python, assets, app.yaml) to a build directory"
@@ -81,7 +82,7 @@ tasks:
8182
- cd build && zip -r scratch-arduino-app-{{.APP_VERSION}}.zip scratch-arduino-app && cd ..
8283

8384
watch:
84-
desc: "wathc files 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"
8586
deps:
8687
- python:watch
8788
- sketch:watch

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import {
2-
type BlockUtilityWithID,
32
type Environment,
43
extension,
54
type ExtensionMenuDisplayDetails,
6-
Language,
75
scratch,
86
} from "$common";
97
import { io, Socket } from "socket.io-client";
@@ -20,9 +18,20 @@ const details: ExtensionMenuDisplayDetails = {
2018
menuSelectColor: "#62AEB2",
2119
};
2220

23-
const DEFAULT_HOST = "192.168.1.39";
21+
// Get Arduino board IP or hostname from URL parameter - required
22+
const getArduinoBoardHost = () => {
23+
if (typeof window !== 'undefined' && window.location) {
24+
const urlParams = new URLSearchParams(window.location.search);
25+
const boardHost = urlParams.get('host');
26+
if (boardHost) {
27+
console.log(`Connecting to Arduino board: ${boardHost}`);
28+
return boardHost;
29+
}
30+
}
31+
throw new Error('Arduino board host required. Add ?host=arduino_board_ip_or_name to the URL');
32+
};
2433

25-
// TODO: support the brightness `0-7' of the leds
34+
// TODO: make the block to support the brightness `0-7' of the leds
2635
const PATTERNS = {
2736
heart: [
2837
[0, 0, 0, 7, 7, 0, 0, 0, 7, 7, 0, 0, 0],
@@ -41,7 +50,8 @@ export default class ArduinoBasics extends extension(details, "ui", "customArgum
4150
private socket: Socket | null = null;
4251

4352
init(env: Environment) {
44-
var serverURL = `wss://${DEFAULT_HOST}:7000`; // Changed from wss to ws
53+
const arduinoBoardHost = getArduinoBoardHost();
54+
var serverURL = `wss://${arduinoBoardHost}:7000`;
4555

4656
this.socket = io(serverURL, {
4757
path: "/socket.io",
@@ -77,9 +87,12 @@ export default class ArduinoBasics extends extension(details, "ui", "customArgum
7787
}
7888
}
7989

80-
@scratch.command`Clear matrix`
90+
91+
@(scratch.command`Clear matrix`)
8192
clearMatrix(matrix: number[][]) {
8293
var matrixString = PATTERNS.empty.flat().join("");
83-
this.socket.emit("matrix_draw", { frame: matrixString });
94+
if (this.socket) {
95+
this.socket.emit("matrix_draw", { frame: matrixString });
96+
}
8497
}
8598
}

0 commit comments

Comments
 (0)