Skip to content

Commit c398966

Browse files
authored
fix: button trigger reset (#8)
1 parent a2ffae4 commit c398966

File tree

3 files changed

+67
-114
lines changed

3 files changed

+67
-114
lines changed

Taskfile.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ tasks:
3030
scratch:install:
3131
dir: scratch-editor
3232
cmds:
33-
- npm install
34-
- npm build
33+
- npm install
34+
- npm run build
3535

3636
scratch:patch:
3737
cmds:

assets/gui.js

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,10 @@ class Scratch3Arduino {
4646
});
4747

4848
// TODO: move to ModulinoPeripheral
49-
this._button_a_pressed = false;
50-
this._button_b_pressed = false;
51-
this._button_c_pressed = false;
49+
this._button_pressed = '';
5250
this.io.on('modulino_buttons_pressed', data => {
5351
console.log("Modulino button pressed event received: ".concat(data.btn));
54-
if (data.btn.toUpperCase() == 'A') {
55-
this._button_a_pressed = true;
56-
this._button_b_pressed = false;
57-
this._button_c_pressed = false;
58-
return;
59-
}
60-
if (data.btn.toUpperCase() == 'B') {
61-
this._button_a_pressed = false;
62-
this._button_b_pressed = true;
63-
this._button_c_pressed = false;
64-
return;
65-
}
66-
if (data.btn.toUpperCase() == 'C') {
67-
this._button_a_pressed = false;
68-
this._button_b_pressed = false;
69-
this._button_c_pressed = true;
70-
return;
71-
}
72-
return;
52+
this._button_pressed = data.btn.toUpperCase();
7353
});
7454
}
7555
}
@@ -116,12 +96,9 @@ Scratch3Arduino.prototype.matrixDraw = function (args) {
11696
});
11797
};
11898
Scratch3Arduino.prototype.whenModulinoButtonsPressed = function (args) {
119-
if (args.BTN === 'A') {
120-
return this._button_a_pressed;
121-
} else if (args.BTN === 'B') {
122-
return this._button_b_pressed;
123-
} else if (args.BTN === 'C') {
124-
return this._button_c_pressed;
99+
if (args.BTN === this._button_pressed) {
100+
this._button_pressed = '';
101+
return true;
125102
}
126103
return false;
127104
};

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

Lines changed: 60 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -8,109 +8,85 @@ const io = require('./socket.io.min.js');
88
* @type {string}
99
*/
1010
// eslint-disable-next-line max-len
11-
const iconURI = '';
11+
const iconURI = '';
1212

1313
/**
1414
* Url of icon to be displayed in the toolbox menu for the extension category.
1515
* @type {string}
1616
*/
1717
// eslint-disable-next-line max-len
18-
const menuIconURI = ''
18+
const menuIconURI = ''
1919

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

2222
class Scratch3Arduino {
23-
constructor (runtime) {
24-
this.runtime = runtime;
25-
this.io = io(wsServerURL, {
26-
path: '/socket.io',
27-
transports: ['polling','websocket'],
28-
autoConnect: true
29-
});
23+
constructor(runtime) {
24+
this.runtime = runtime;
25+
this.io = io(wsServerURL, {
26+
path: '/socket.io',
27+
transports: ['polling', 'websocket'],
28+
autoConnect: true
29+
});
3030

31-
// TODO: move to ModulinoPeripheral
32-
this._button_a_pressed = false;
33-
this._button_b_pressed = false;
34-
this._button_c_pressed = false;
35-
36-
this.io.on('modulino_buttons_pressed', (data) => {
37-
console.log(`Modulino button pressed event received: ${data.btn}`);
38-
if (data.btn.toUpperCase() == 'A'){
39-
this._button_a_pressed = true;
40-
this._button_b_pressed = false;
41-
this._button_c_pressed = false;
42-
return;
43-
}
44-
if (data.btn.toUpperCase() == 'B'){
45-
this._button_a_pressed = false;
46-
this._button_b_pressed = true;
47-
this._button_c_pressed = false;
48-
return;
49-
}
50-
if (data.btn.toUpperCase() == 'C'){
51-
this._button_a_pressed = false;
52-
this._button_b_pressed = false;
53-
this._button_c_pressed = true;
54-
return;
55-
}
56-
return;
57-
});
58-
}
31+
// TODO: move to ModulinoPeripheral
32+
this._button_pressed = '';
33+
this.io.on('modulino_buttons_pressed', (data) => {
34+
console.log(`Modulino button pressed event received: ${data.btn}`);
35+
this._button_pressed = data.btn.toUpperCase();
36+
});
37+
}
5938
};
6039

6140
Scratch3Arduino.prototype.getInfo = function () {
62-
return {
63-
id: 'arduino',
64-
name: "Arduino",
65-
menuIconURI: menuIconURI,
66-
blockIconURI: iconURI,
67-
blocks: [
68-
{
69-
opcode: 'matrixDraw',
70-
blockType: BlockType.COMMAND,
71-
text: 'draw [FRAME] on matrix',
72-
func: 'matrixDraw',
73-
arguments: {
74-
FRAME: {
75-
type: ArgumentType.MATRIX,
76-
defaultValue: '0101010101100010101000100'
77-
}
78-
}
79-
},
80-
{
81-
opcode: 'whenModulinoButtonsPressed',
82-
blockType: BlockType.HAT,
83-
text: 'when modulino button [BTN] pressed',
84-
func: 'whenModulinoButtonsPressed',
85-
arguments: {
86-
BTN: {
87-
type: ArgumentType.STRING,
88-
menu: 'modulinoButtons',
89-
defaultValue: "A"
90-
}
91-
}
92-
},
93-
],
94-
menus: {
95-
modulinoButtons: ["A", "B", "C"]
96-
}
97-
};
41+
return {
42+
id: 'arduino',
43+
name: "Arduino",
44+
menuIconURI: menuIconURI,
45+
blockIconURI: iconURI,
46+
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+
},
59+
{
60+
opcode: 'whenModulinoButtonsPressed',
61+
blockType: BlockType.HAT,
62+
text: 'when modulino button [BTN] pressed',
63+
func: 'whenModulinoButtonsPressed',
64+
arguments: {
65+
BTN: {
66+
type: ArgumentType.STRING,
67+
menu: 'modulinoButtons',
68+
defaultValue: "A"
69+
}
70+
}
71+
},
72+
],
73+
menus: {
74+
modulinoButtons: ["A", "B", "C"]
75+
}
76+
};
9877
}
9978

10079
Scratch3Arduino.prototype.matrixDraw = function (args) {
101-
console.log(`Drawing frame on matrix: ${args}`);
102-
this.io.emit("matrix_draw", {frame: args.FRAME});
80+
console.log(`Drawing frame on matrix: ${args}`);
81+
this.io.emit("matrix_draw", { frame: args.FRAME });
10382
};
10483

10584
Scratch3Arduino.prototype.whenModulinoButtonsPressed = function (args) {
106-
if (args.BTN === 'A') {
107-
return this._button_a_pressed
108-
} else if (args.BTN === 'B') {
109-
return this._button_b_pressed
110-
} else if (args.BTN === 'C') {
111-
return this._button_c_pressed;
112-
}
113-
return false;
85+
if (args.BTN === this._button_pressed) {
86+
this._button_pressed = '';
87+
return true;
88+
}
89+
return false;
11490
};
11591

116-
module.exports = Scratch3Arduino;
92+
module.exports = Scratch3Arduino;

0 commit comments

Comments
 (0)