Skip to content

Commit 8b3a938

Browse files
authored
feat: separate the extension in basics and modulino (#13)
1 parent 15b5c09 commit 8b3a938

File tree

4 files changed

+75
-29
lines changed

4 files changed

+75
-29
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// const formatMessage = require('../../../../../../scratch-editor/node_modules/format-message');
2+
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');
5+
6+
/**
7+
* Url of icon to be displayed at the left edge of each extension block.
8+
* @type {string}
9+
*/
10+
// eslint-disable-next-line max-len
11+
const iconURI = '';
12+
13+
/**
14+
* Url of icon to be displayed in the toolbox menu for the extension category.
15+
* @type {string}
16+
*/
17+
// eslint-disable-next-line max-len
18+
const menuIconURI = ''
19+
20+
const wsServerURL = `${window.location.protocol}//${window.location.hostname}:7000`;
21+
22+
class ArduinoBasics {
23+
constructor(runtime) {
24+
this.runtime = runtime;
25+
26+
this.io = io(wsServerURL, {
27+
path: '/socket.io',
28+
transports: ['polling', 'websocket'],
29+
autoConnect: true
30+
});
31+
}
32+
};
33+
34+
ArduinoBasics.prototype.getInfo = function () {
35+
return {
36+
id: 'arduinobasics',
37+
name: "Arduino Basics",
38+
menuIconURI: menuIconURI,
39+
blockIconURI: iconURI,
40+
blocks: [
41+
{
42+
opcode: 'matrixDraw',
43+
blockType: BlockType.COMMAND,
44+
text: 'draw [FRAME] on matrix',
45+
func: 'matrixDraw',
46+
arguments: {
47+
FRAME: {
48+
type: ArgumentType.MATRIX,
49+
defaultValue: '0101010101100010101000100'
50+
}
51+
}
52+
}
53+
]
54+
};
55+
}
56+
57+
ArduinoBasics.prototype.matrixDraw = function (args) {
58+
console.log(`Drawing frame on matrix: ${args}`);
59+
this.io.emit("matrix_draw", { frame: args.FRAME });
60+
};
61+
62+
module.exports = ArduinoBasics;

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

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

66
/**
77
* Url of icon to be displayed at the left edge of each extension block.
@@ -19,7 +19,7 @@ const menuIconURI = ''
1919

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

22-
class Scratch3Arduino {
22+
class ArduinoModulino {
2323
constructor(runtime) {
2424
this.runtime = runtime;
2525
this.io = io(wsServerURL, {
@@ -37,25 +37,13 @@ class Scratch3Arduino {
3737
}
3838
};
3939

40-
Scratch3Arduino.prototype.getInfo = function () {
40+
ArduinoModulino.prototype.getInfo = function () {
4141
return {
42-
id: 'arduino',
43-
name: "Arduino",
42+
id: 'arduinomodulino',
43+
name: "Arduino Modulino",
4444
menuIconURI: menuIconURI,
4545
blockIconURI: iconURI,
4646
blocks: [
47-
{
48-
opcode: 'matrixDraw',
49-
blockType: BlockType.COMMAND,
50-
text: 'draw [FRAME] on matrix',
51-
func: 'matrixDraw',
52-
arguments: {
53-
FRAME: {
54-
type: ArgumentType.MATRIX,
55-
defaultValue: '0101010101100010101000100'
56-
}
57-
}
58-
},
5947
{
6048
opcode: 'whenModulinoButtonsPressed',
6149
blockType: BlockType.HAT,
@@ -76,17 +64,12 @@ Scratch3Arduino.prototype.getInfo = function () {
7664
};
7765
}
7866

79-
Scratch3Arduino.prototype.matrixDraw = function (args) {
80-
console.log(`Drawing frame on matrix: ${args}`);
81-
this.io.emit("matrix_draw", { frame: args.FRAME });
82-
};
83-
84-
Scratch3Arduino.prototype.whenModulinoButtonsPressed = function (args) {
67+
ArduinoModulino.prototype.whenModulinoButtonsPressed = function (args) {
8568
if (args.BTN === this._button_pressed) {
8669
this._button_pressed = '';
8770
return true;
8871
}
8972
return false;
9073
};
9174

92-
module.exports = Scratch3Arduino;
75+
module.exports = ArduinoModulino;

scratch-arduino-extensions/scripts/patch-gui.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ const path = require("path");
22
const fs = require("fs");
33

44
const extensions = [
5-
{ name: "Scratch3Arduino", directory: "scratch3_arduino" },
5+
{ name: "ArduinoBasics", directory: "arduino_basics" },
6+
{ name: "ArduinoModulino", directory: "arduino_modulino" }
67
];
78

89
// base dir is the 'scratch-arduino-extensions' folder
910
const BaseDir = path.resolve(__dirname, "../");
1011

1112
extensions.forEach(extension => {
12-
console.log(`${extension.name} (${extension.directory})`);
13+
console.log(`\n${extension.name} (${extension.directory})`);
1314

1415
process.stdout.write("\t - add symbolic link: ");
1516
const scratchVmExtensionsDir = path.resolve(BaseDir,"../scratch-editor/packages/scratch-vm/src/extensions",extension.directory);
@@ -39,8 +40,8 @@ extensions.forEach(extension => {
3940
if (!vmCode.includes(extension.name)) {
4041
fs.copyFileSync(scratchVmVirtualMachineFile, `${scratchVmVirtualMachineFile}.orig`);
4142
vmCode = vmCode.replace(
42-
/CORE_EXTENSIONS = \[[\s\S]*?\];/,
43-
`$&\n\nCORE_EXTENSIONS.push('${extension.name}');`,
43+
/(CORE_EXTENSIONS = \[[\s\S]*?)\];/,
44+
`$1'${extension.name}',\n];`,
4445
);
4546
fs.writeFileSync(scratchVmVirtualMachineFile, vmCode);
4647
process.stdout.write("done\n");

0 commit comments

Comments
 (0)