Skip to content

Commit 42ef451

Browse files
committed
refactor: update ArduinoUnoQ integration to use default host and enhance object detection methods
1 parent c26c435 commit 42ef451

File tree

4 files changed

+46
-43
lines changed

4 files changed

+46
-43
lines changed

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
const io = require("./socket.io.min.js");
22

3+
// const DEFAULT_HOST = window.location.hostname;
4+
const DEFAULT_HOST = "192.168.1.39";
5+
36
class ArduinoUnoQ {
4-
constructor(host, port) {
5-
this.serverURL = `wss://${host}:${port}`;
7+
constructor() {
8+
this.serverURL = `wss://${DEFAULT_HOST}:7000`;
69

710
this.io = io(this.serverURL, {
811
path: "/socket.io",
@@ -99,6 +102,34 @@ class ArduinoUnoQ {
99102
const clearFrame = "0".repeat(25);
100103
this.matrixDraw(clearFrame);
101104
}
105+
106+
// AI object detection
107+
108+
detectObjects(imageData) {
109+
this.io.emit("detect_objects", { image: imageData });
110+
console.log("Emitted detect_objects event");
111+
}
112+
113+
// ===== EVENT HANDLING METHODS =====
114+
115+
on(event, callback) {
116+
if (this.io) {
117+
this.io.on(event, callback);
118+
console.log(`Registered event listener for: ${event}`);
119+
} else {
120+
console.error("Socket.io not initialized");
121+
}
122+
}
123+
124+
emit(event, data) {
125+
if (this.io && this.isConnected) {
126+
this.io.emit(event, data);
127+
console.log(`Emitted event: ${event}`, data);
128+
} else {
129+
console.warn(`Cannot emit ${event}: Not connected to Arduino UNO Q`);
130+
}
131+
}
132+
102133
}
103134

104135
module.exports = ArduinoUnoQ;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const menuIconURI = "";
1111
class ArduinoBasics {
1212
constructor(runtime) {
1313
this.runtime = runtime;
14-
this.unoq = new ArduinoUnoQ(`${window.location.hostname}`, 7000);
14+
this.unoq = new ArduinoUnoQ();
1515
this.unoq.connect();
1616
}
1717
}

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

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,23 @@ const BlockType = require("../../../../../../scratch-editor/packages/scratch-vm/
22
const ArgumentType = require(
33
"../../../../../../scratch-editor/packages/scratch-vm/src/extension-support/argument-type",
44
);
5-
const io = require("../socket.io.min.js");
5+
const ArduinoUnoQ = require("../ArduinoUnoQ");
66

7-
/**
8-
* Url of icon to be displayed at the left edge of each extension block.
9-
* @type {string}
10-
*/
11-
// eslint-disable-next-line max-len
7+
// TODO: add icons
128
const iconURI = "";
13-
14-
/**
15-
* Url of icon to be displayed in the toolbox menu for the extension category.
16-
* @type {string}
17-
*/
18-
// eslint-disable-next-line max-len
199
const menuIconURI = "";
2010

2111
const wsServerURL = `${window.location.protocol}//${window.location.hostname}:7000`;
2212

2313
class ArduinoModulino {
2414
constructor(runtime) {
2515
this.runtime = runtime;
26-
this.io = io(wsServerURL, {
27-
path: "/socket.io",
28-
transports: ["polling", "websocket"],
29-
autoConnect: true,
30-
});
16+
this.unoq = new ArduinoUnoQ();
17+
this.unoq.connect();
3118

3219
// TODO: move to ModulinoPeripheral
3320
this._button_pressed = "";
34-
this.io.on("modulino_buttons_pressed", (data) => {
21+
this.unoq.on("modulino_buttons_pressed", (data) => {
3522
console.log(`Modulino button pressed event received: ${data.btn}`);
3623
this._button_pressed = data.btn.toUpperCase();
3724
});

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

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,16 @@ const BlockType = require("../../../../../../scratch-editor/packages/scratch-vm/
22
const ArgumentType = require(
33
"../../../../../../scratch-editor/packages/scratch-vm/src/extension-support/argument-type",
44
);
5-
const io = require("../socket.io.min.js");
65
const Video = require("../../../../../../scratch-editor/packages/scratch-vm/src/io/video");
76
const Rectangle = require("../../../../../../scratch-editor/packages/scratch-render/src/Rectangle.js");
87
const StageLayering = require("../../../../../../scratch-editor/packages/scratch-vm/src/engine/stage-layering.js");
98
const { Detection, MODEL_LABELS } = require("./object_detection");
9+
const ArduinoUnoQ = require("../ArduinoUnoQ");
1010

11-
/**
12-
* Url of icon to be displayed at the left edge of each extension block.
13-
* @type {string}
14-
*/
15-
// eslint-disable-next-line max-len
11+
//TODO add icons
1612
const iconURI = "";
17-
18-
/**
19-
* Url of icon to be displayed in the toolbox menu for the extension category.
20-
* @type {string}
21-
*/
22-
// eslint-disable-next-line max-len
2313
const menuIconURI = "";
2414

25-
const wsServerURL = `${window.location.protocol}//${window.location.hostname}:7000`;
2615

2716
/**
2817
* RGB color constants for confidence visualization
@@ -37,6 +26,9 @@ class ArduinoObjectDetection {
3726
constructor(runtime) {
3827
this.runtime = runtime;
3928

29+
this.unoq = new ArduinoUnoQ();
30+
this.unoq.connect();
31+
4032
/** @type {Array<Detection>} */
4133
this.detectedObjects = [];
4234

@@ -66,15 +58,8 @@ class ArduinoObjectDetection {
6658
}
6759
});
6860

69-
this.io = io(wsServerURL, {
70-
path: "/socket.io",
71-
transports: ["polling", "websocket"],
72-
autoConnect: true,
73-
});
74-
75-
this.io.on("detection_result", (data) => {
61+
this.unoq.on("detection_result", (data) => {
7662
this.detectedObjects = [];
77-
7863
this._clearBoundingBoxes();
7964

8065
data.detection.forEach((detection) => {
@@ -264,7 +249,7 @@ ArduinoObjectDetection.prototype._detectObjects = function(args) {
264249
}
265250
const dataUrl = canvas.toDataURL("image/png");
266251
const base64Frame = dataUrl.split(",")[1];
267-
this.io.emit("detect_objects", { image: base64Frame });
252+
this.unoq.detectObjects(base64Frame);
268253
};
269254

270255
ArduinoObjectDetection.prototype._clearBoundingBoxes = function(args) {

0 commit comments

Comments
 (0)