Skip to content

Commit a41909a

Browse files
authored
feat: integrate Modulino buttons (#2)
1 parent 15e7af7 commit a41909a

File tree

5 files changed

+102
-24
lines changed

5 files changed

+102
-24
lines changed

Taskfile.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,13 @@ tasks:
2828
- adb push ./python/main.py /home/arduino/ArduinoApps/scratch-arduino-app/python/main.py
2929
- adb push ./sketch/sketch.ino /home/arduino/ArduinoApps/scratch-arduino-app/sketch/sketch.ino
3030
- adb push ./sketch/sketch.yaml /home/arduino/ArduinoApps/scratch-arduino-app/sketch/sketch.yaml
31+
- adb push ./sketch/Arduino_Modulino /home/arduino/ArduinoApps/scratch-arduino-app/sketch/Arduino_Modulino
3132
- adb push ./app.yaml /home/arduino/ArduinoApps/scratch-arduino-app/app.yaml
32-
# - adb shell arduino-app-cli app restart user:scratch-arduino-app
33+
# - adb shell arduino-app-cli app restart user:scratch-arduino-app
34+
35+
modulino:patch:
36+
# See https://github.com/arduino-libraries/Arduino_Modulino/pull/42
37+
# Any Version<0.6.0 the build fails if Both modulino and ArduinoLedMatrix are used in the UnoQ
38+
- mkdir -p sketch/Arduino_Modulino
39+
- git clone --depth 1 git@github.com:arduino-libraries/Arduino_Modulino.git sketch/Arduino_Modulino
40+
- cd sketch/Arduino_Modulino && git checkout 480e9d183a3b3dede0c68170e469410a6d710bee

python/main.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
import time
44

55
ui = WebUI()
6+
ui.on_connect(
7+
lambda sid: (
8+
print(f"Client connected: {sid} "),
9+
)
10+
)
11+
612

713
def on_matrix_draw(_, data):
814
print(f"Received frame to draw on matrix: {data}")
@@ -22,9 +28,9 @@ def on_matrix_draw(_, data):
2228

2329
ui.on_message("matrix_draw", on_matrix_draw)
2430

25-
ui.on_connect(
26-
lambda sid: (
27-
print(f"Client connected: {sid} "),
28-
)
29-
)
31+
def on_modulino_button_pressed(btn):
32+
ui.send_message('modulino_buttons_pressed', {"btn": btn})
33+
34+
Bridge.provide("modulino_button_pressed", on_modulino_button_pressed)
35+
3036
App.run()

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,34 @@ class Scratch3Arduino {
2727
transports: ['polling','websocket'],
2828
autoConnect: true
2929
});
30+
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+
});
3058
}
3159
};
3260

@@ -49,7 +77,23 @@ Scratch3Arduino.prototype.getInfo = function () {
4977
}
5078
}
5179
},
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+
},
5293
],
94+
menus: {
95+
modulinoButtons: ["A", "B", "C"]
96+
}
5397
};
5498
}
5599

@@ -58,4 +102,15 @@ Scratch3Arduino.prototype.matrixDraw = function (args) {
58102
this.io.emit("matrix_draw", {frame: args.FRAME});
59103
};
60104

105+
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;
114+
};
115+
61116
module.exports = Scratch3Arduino;

sketch/sketch.ino

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
11
#include <Arduino_RouterBridge.h>
22
#include "Arduino_LED_Matrix.h"
3+
#include <Arduino_Modulino.h>
34

45
Arduino_LED_Matrix matrix;
6+
ModulinoButtons buttons;
57

68
void setup() {
79
matrix.begin();
810
Bridge.begin();
11+
Modulino.begin(Wire1);
12+
// show led indication if buttons cannot be initilized
13+
buttons.begin();
14+
buttons.setLeds(true, true, true);
915
Bridge.provide("matrix_draw", matrix_draw);
1016
}
1117

12-
void loop() {}
13-
14-
uint8_t shades[104] = {
15-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16-
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
17-
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
18-
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
19-
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
20-
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
21-
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
22-
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
23-
};
18+
void loop() {
19+
if (buttons.update()) {
20+
if (buttons.isPressed("A")) {
21+
Bridge.notify("modulino_button_pressed", "A");
22+
buttons.setLeds(true, false, false);
23+
} else if (buttons.isPressed("B")) {
24+
Bridge.notify("modulino_button_pressed", "B");
25+
buttons.setLeds(false, true, false);
26+
} else if (buttons.isPressed("C")) {
27+
Bridge.notify("modulino_button_pressed", "C");
28+
buttons.setLeds(false, false, true);
29+
}
30+
}
31+
}
2432

33+
uint8_t shades[104];
2534

2635
void matrix_draw(String frame){
2736
if (frame.length() != 104) {

sketch/sketch.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ profiles:
88
- DebugLog (0.8.4)
99
- ArxContainer (0.7.0)
1010
- ArxTypeTraits (0.3.1)
11-
# - Modulino (0.5.1)
12-
# - Arduino_HS300x (1.0.0)
13-
# - STM32duino VL53L4CD (1.0.5)
14-
# - STM32duino VL53L4ED (1.0.1)
15-
# - Arduino_LSM6DSOX (1.1.2)
16-
# - Arduino_LPS22HB (1.0.2)
11+
- dir: Arduino_Modulino
12+
- Arduino_HS300x (1.0.0)
13+
- STM32duino VL53L4CD (1.0.5)
14+
- STM32duino VL53L4ED (1.0.1)
15+
- Arduino_LSM6DSOX (1.1.2)
16+
- Arduino_LPS22HB (1.0.2)
1717
default_profile: default

0 commit comments

Comments
 (0)