You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: examples/led-matrix-painter/README.md
+25-14Lines changed: 25 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -84,12 +84,35 @@ Web Browser ──► HTTP API ──► Python Backend ──► Router B
84
84
85
85
1.**Web Interface**: The `app.js` script captures clicks on the grid. It debounces these events and sends the pixel data to the backend via the `/persist_frame` endpoint.
86
86
2.**Python Backend**:
87
-
***Persistence**: The `store.py` module uses `SQLStore` to save the frame data (pixels, duration, position) to a `frames` table in a SQLite database.
88
-
***Bridge**: The `main.py` script converts the frame into a raw byte array and calls `Bridge.call("draw", frame_bytes)`.
87
+
***Data Model**: The `AppFrame` class normalizes the data, converting between frontend JSON, database records, and hardware byte arrays.
88
+
***Persistence**: The `store.py` module uses `SQLStore` to save the frame data to a `frames` table in a SQLite database.
89
+
***Bridge**: The `main.py` script sends the raw byte array to the board via `Bridge.call("draw", frame_bytes)`.
89
90
3.**Arduino Sketch**: The sketch receives the raw byte data and uses the `Arduino_LED_Matrix` library to render the grayscale image.
90
91
91
92
## Understanding the Code
92
93
94
+
### 📦 Data Model (`app_frame.py`)
95
+
96
+
The `AppFrame` class is the core data structure that acts as a bridge between the different components. It extends the base `Frame` class to add application-specific metadata like `id`, `name`, `position`, and `duration`.
97
+
98
+
It handles three distinct data contracts:
99
+
-**API Contract**: `to_json()` / `from_json()` formats data for the web frontend.
100
+
-**Database Contract**: `to_record()` / `from_record()` formats data for `SQLStore` storage.
101
+
-**Hardware Contract**: `to_board_bytes()` packs pixels into the specific byte format expected by the Arduino sketch.
102
+
103
+
```python
104
+
classAppFrame(Frame):
105
+
defto_record(self) -> dict:
106
+
"""Convert to a database record dict for storage."""
107
+
return {
108
+
"id": self.id,
109
+
"name": self.name,
110
+
"rows": json.dumps(self.arr.tolist()), # Serialize pixels to JSON string
111
+
"brightness_levels": int(self.brightness_levels),
112
+
# ...
113
+
}
114
+
```
115
+
93
116
### 🔧 Backend (`main.py` & `store.py`)
94
117
95
118
The Python backend manages the application logic, database, and hardware communication.
0 commit comments