Skip to content

Commit faaacdd

Browse files
committed
first version with mesaggepakc on the FE
1 parent dfac77e commit faaacdd

File tree

5 files changed

+109
-124
lines changed

5 files changed

+109
-124
lines changed

README.md

Lines changed: 5 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,6 @@
1-
# Linux Blink with UI (JavaScript)
1+
# Mange you board with Scratch
22

3-
The **Linux Blink** example shows a simple Linux application that changes the LED state on the board. It showcases basic event handling and UI updates through a web-based interface.
4-
5-
![Linux Blink App](assets/docs_assets/linux-blink-banner.png)
6-
7-
## Description
8-
9-
This example toggles an LED on the board using a simple web user interface. The application listens for user input through a web browser and updates the LED state accordingly. It shows how to interact with hardware from a Linux environment and provides a basis for building more complex hardware-interfacing applications.
10-
11-
The `assets` folder contains the **frontend** components of the application. Inside, you'll find the JavaScript source files along with the HTML and CSS files that make up the web user interface. The `python` folder instead includes the application **backend**.
12-
13-
The interactive toggle switch UI is generated with JavaScript, while the Arduino sketch manages the LED hardware control. The Router Bridge enables communication between the web interface and the microcontroller.
14-
15-
## Bricks Used
16-
17-
The Linux blink example uses the following Bricks:
18-
19-
- `web_ui`: Brick to create a web interface to display the LED control toggle switch.
20-
21-
## Hardware and Software Requirements
22-
23-
### Hardware
24-
25-
- Arduino UNO Q (x1)
26-
- USB-C® cable (for power and programming) (x1)
27-
28-
### Software
29-
30-
- Arduino App Lab
31-
32-
**Note:** You can run this example using your Arduino UNO Q as a Single Board Computer (SBC) using a [USB-C® hub](https://store.arduino.cc/products/usb-c-to-hdmi-multiport-adapter-with-ethernet-and-usb-hub) with a mouse, keyboard and display attached.
33-
34-
## How to Use the Example
35-
36-
1. Run the App
37-
![Arduino App Lab - Run App](assets/docs_assets/app-lab-run-app.png)
38-
2. Open the App in your browser at `<UNO-Q-IP-ADDRESS>:7000`
39-
3. Click on the circular switch to change the state of the LED
40-
41-
## How it Works
42-
43-
Once the application is running, the device performs the following operations:
44-
45-
- **Serving the web interface and handling WebSocket communication.**
46-
47-
The `web_ui` Brick provides the web server and WebSocket communication:
48-
49-
```python
50-
from arduino.app_bricks.web_ui import WebUI
51-
52-
ui = WebUI()
53-
ui.on_message('toggle_led', toggle_led_state)
54-
ui.on_message('get_initial_state', on_get_initial_state)
55-
```
56-
57-
- **Communicating LED state to the Arduino.**
58-
59-
The Router Bridge sends LED commands to the microcontroller:
60-
61-
```python
62-
Bridge.call("set_led_state", led_is_on)
63-
```
64-
65-
- **Controlling the hardware LED.**
66-
67-
The Arduino sketch handles the LED hardware control:
68-
69-
```cpp
70-
void set_led_state(bool state) {
71-
digitalWrite(LED_BUILTIN, state ? LOW : HIGH);
72-
}
73-
```
74-
75-
The high-level data flow looks like this:
76-
77-
```
78-
Web Browser Toggle → WebSocket → Python Backend → Router Bridge → Arduino LED Control
79-
```
80-
81-
## Understanding the Code
82-
83-
Here is a brief explanation of the application components:
84-
85-
### 🔧 Backend (`main.py`)
86-
87-
The Python code manages the web interface, handles user interactions, and communicates with the Arduino.
88-
89-
- **`ui = WebUI()`:** Initializes the web server that serves the HTML interface and handles WebSocket communication.
90-
91-
- **`ui.on_message('toggle_led', toggle_led_state)`:** Registers a WebSocket message handler that responds when the user clicks the toggle button in the web interface.
92-
93-
- **`ui.send_message('led_status_update', get_led_status())`:** Sends LED status updates to all connected web clients in real-time.
94-
95-
- **`Bridge.call("set_led_state", led_is_on)`:** Calls the Arduino function to physically control the LED hardware.
96-
97-
- **`get_led_status()`:** Returns the current LED state as a dictionary for the web interface.
98-
99-
### 🔧 Frontend (`index.html` + `app.js`)
100-
101-
The web interface provides a simple toggle button for LED control.
102-
103-
- **Socket.IO connection:** Establishes WebSocket communication with the Python backend through the `web_ui` Brick.
104-
105-
- **`socket.emit('toggle_led', {})`:** Sends a toggle message to the backend when the user clicks the button.
106-
107-
- **`socket.on('led_status_update', updateLedStatus)`:** Receives LED status updates and updates the button appearance accordingly.
108-
109-
- **`updateLedStatus(status)`:** Changes the button's visual state (LED IS ON/OFF) based on the received status.
110-
111-
### 🔧 Hardware (`sketch.ino`)
112-
113-
The Arduino code handles LED hardware control and sets up Bridge communication.
114-
115-
- **`pinMode(LED_BUILTIN, OUTPUT)`:** Configures the built-in LED pin as an output for controlling the LED state.
116-
117-
- **`Bridge.begin()`:** Initializes the Router Bridge communication system for receiving commands from Python.
118-
119-
- **`Bridge.provide(...)`:** Registers the `set_led_state` function to be callable from the Python web interface.
120-
121-
- **`set_led_state(bool state)`:** Controls the LED hardware by setting the pin HIGH or LOW based on the received state parameter.
122-
123-
- **Empty `loop()`:** The main loop remains empty since all LED control is event-driven through Bridge function calls.
124-
125-
# scratch-arduino-app
3+
Scratch extension to:
4+
- control the board perhiperals (pins, matrix led)
5+
- mange the arduino Modulino connecte to the board
6+
- manage the python trigger

app.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: scratch-arduino-app
2+
description: Controll the Uno-Q borad using Scractch blocks
3+
ports: []
4+
bricks: []
5+
icon: 💡

python/main.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from arduino.app_utils import App
2+
import time
3+
4+
def loop():
5+
time.sleep(1) # do nothing: only to keep the track the app status
6+
7+
App.run(user_loop=loop)

sketch/sketch.ino

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// SPDX-FileCopyrightText: Copyright (C) 2025 ARDUINO SA <http://www.arduino.cc>
2+
//
3+
// SPDX-License-Identifier: MPL-2.0
4+
5+
#include <Arduino_RouterBridge.h>
6+
#include <Modulino.h>
7+
8+
// TODO: those will go into an header file.
9+
extern "C" void matrixWrite(const uint32_t* buf);
10+
extern "C" void matrixBegin();
11+
12+
ModulinoButtons buttons;
13+
14+
void set_led_state(bool state) {
15+
// LOW state means LED is ON
16+
digitalWrite(LED_BUILTIN, state ? LOW : HIGH);
17+
}
18+
19+
void setup() {
20+
matrixBegin();
21+
//Monitor.begin();
22+
Modulino.begin(Wire1);
23+
24+
while(!buttons.begin()){
25+
set_led_state(true);
26+
delay(1000);
27+
set_led_state(false);
28+
}
29+
buttons.setLeds(true, true, true);
30+
31+
pinMode(LED_BUILTIN, OUTPUT);
32+
// Start with the LED OFF (HIGH state of the PIN)
33+
digitalWrite(LED_BUILTIN, HIGH);
34+
35+
Bridge.begin();
36+
Bridge.provide("set_led_state", set_led_state);
37+
Bridge.provide("matrix_draw", matrix_draw);
38+
}
39+
40+
void loop() {
41+
42+
if (buttons.update()) {
43+
if (buttons.isPressed(0)) {
44+
// Monitor.println("button a pressed");
45+
// Serial.println("Button A pressed!");
46+
Bridge.notify("button_pressed");
47+
set_led_state(true);
48+
delay(500);
49+
} else if (buttons.isPressed(1)) {
50+
// Serial.println("Button B pressed!");
51+
// Monitor.println("button B pressed");
52+
set_led_state(false);
53+
Bridge.notify("button_pressed");
54+
delay(500);
55+
} else if (buttons.isPressed(1)) {
56+
// Serial.println("Button C pressed!");
57+
// Monitor.println("button C pressed");
58+
Bridge.notify("button_pressed");
59+
delay(500);
60+
}
61+
}
62+
}
63+
64+
const uint32_t heart[] = {
65+
0x1f88ffff,
66+
0xe0fe03f0,
67+
0xc1ef1e3d,
68+
0x1f
69+
};
70+
71+
void matrix_draw(){
72+
matrixWrite(heart);
73+
}
74+
75+

sketch/sketch.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
profiles:
2+
default:
3+
fqbn: arduino:zephyr:unoq
4+
platforms:
5+
- platform: arduino:zephyr
6+
libraries:
7+
- MsgPack (0.4.2)
8+
- DebugLog (0.8.4)
9+
- ArxContainer (0.7.0)
10+
- 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)
17+
default_profile: default

0 commit comments

Comments
 (0)