-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodeV0.ino
More file actions
156 lines (127 loc) · 4.26 KB
/
Copy pathNodeV0.ino
File metadata and controls
156 lines (127 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <Arduino.h>
#include <GxEPD2_3C.h>
#include <U8g2_for_Adafruit_GFX.h>
#include <QRCodeGFX.h>
// --- PIN CONFIGURATION (WeAct ESP32-C3) ---
#define EPD_CS 7
#define EPD_DC 1
#define EPD_RST 2
#define EPD_BUSY 10
// SCK (4) and MOSI (6) pins are managed by the C3 default hardware SPI
// --- DATA STRUCTURE ---
struct BadgeData {
String name = "Codemotion";
String surname = "Milan 2025";
String role = "Attendee";
String company = "Community";
String qrLink = "https://codemotion.com";
};
// --- GLOBAL OBJECTS ---
GxEPD2_3C<GxEPD2_290_C90c, GxEPD2_290_C90c::HEIGHT> display(GxEPD2_290_C90c(EPD_CS, EPD_DC, EPD_RST, EPD_BUSY));
U8G2_FOR_ADAFRUIT_GFX u8g2;
QRCodeGFX qrcode(display);
BadgeData badge;
bool needRefresh = true;
// Helper to extract data from the semicolon-separated string
String getValue(String data, char separator, int index) {
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length() - 1;
for (int i = 0; i <= maxIndex && found <= index; i++) {
if (data.charAt(i) == separator || i == maxIndex) {
found++;
strIndex[0] = strIndex[1] + 1;
strIndex[1] = (i == maxIndex) ? i + 1 : i;
}
}
return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
void parseSerialData() {
if (Serial.available() > 0) {
String rx = Serial.readStringUntil('\n');
rx.trim();
if (rx.length() < 5) return; // Ignore noise or empty inputs
// Expected format: Name;Surname;Role;Company;QRLink
badge.name = getValue(rx, ';', 0);
badge.surname = getValue(rx, ';', 1);
badge.role = getValue(rx, ';', 2);
badge.company = getValue(rx, ';', 3);
badge.qrLink = getValue(rx, ';', 4);
// If data is missing, use placeholders to avoid graphical crashes
if(badge.qrLink == "") badge.qrLink = "https://google.com";
Serial.println("Dati ricevuti. Aggiornamento in corso...");
needRefresh = true;
}
}
// --- GRAPHIC LOGIC ---
void drawContent() {
// Font and Color Setup
u8g2.setBackgroundColor(GxEPD_WHITE);
u8g2.setForegroundColor(GxEPD_BLACK);
int x_margin = 5;
int y_cursor = 0;
// 1. NAME
u8g2.setFont(u8g2_font_crox5tb_tf); // Medium Bold Font
y_cursor += u8g2.getFontAscent() + 10;
u8g2.setCursor(x_margin, y_cursor);
u8g2.print(badge.name);
// 2. SURNAME
y_cursor += u8g2.getFontAscent() + 10; // Automatic spacing based on font
u8g2.setCursor(x_margin, y_cursor);
u8g2.print(badge.surname);
// 3. ROLE (Smaller font)
u8g2.setFont(u8g2_font_luBIS10_tf);
y_cursor += u8g2.getFontAscent() + 35; // A bit more space
u8g2.setCursor(x_margin, y_cursor);
u8g2.print(badge.role);
// 4. COMPANY (In Red)
u8g2.setForegroundColor(GxEPD_RED);
u8g2.setFont(u8g2_font_helvB18_tf);
y_cursor += u8g2.getFontAscent() + 5;
u8g2.setCursor(x_margin, y_cursor);
u8g2.print(badge.company);
// 5. QR CODE
// generate the qr code
qrcode.generateData(badge.qrLink);
// Calculate position to place it on the right, vertically centered or at the bottom
qrcode.setScale(3); // QR Size
int qrSize = qrcode.getSideLength();
int qrX = display.width() - qrSize; // Right-aligned with margin
int qrYCentered = (display.height() - qrSize) / 2; // Vertically centered on the display [Not used]
// Note: qrcode.draw uses graphic primitives, so it respects display rotation
qrcode.draw(qrX, 0);
}
void refreshDisplay() {
display.setRotation(3);
// Paged loop mandatory for E-ink on ESP32 (RAM saving)
display.setFullWindow();
display.firstPage();
do {
display.fillScreen(GxEPD_WHITE); // Clear buffer
drawContent();
} while (display.nextPage());
display.hibernate(); // Turn off display controller to save power
needRefresh = false;
Serial.println("Display Aggiornato.");
}
// --- SETUP & LOOP ---
void setup() {
Serial.begin(9600);
delay(1000);
// Init Display
display.init(115200, true, 50, false);
u8g2.begin(display); // Connect u8g2 to GxEPD2
// Draw for the first time with default data
refreshDisplay();
}
void loop() {
// Listen to serial always
parseSerialData();
// Update only if needed
if (needRefresh) {
refreshDisplay();
}
// Small delay to avoid unnecessary CPU saturation,
// but we don't use deep sleep because we need to listen to serial.
delay(100);
}