-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathespnow_ui_test.cc
More file actions
428 lines (364 loc) · 15.3 KB
/
espnow_ui_test.cc
File metadata and controls
428 lines (364 loc) · 15.3 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#include "espnow_ui_test.h"
#include "espnow_manager.h"
#include "board.h"
#include "display/display.h"
#include "system_info.h"
#include <esp_log.h>
#include <esp_wifi.h>
#include <esp_event.h>
#include <esp_timer.h>
#include <nvs_flash.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <lvgl.h>
#include <algorithm>
#include <string>
static const char* TAG = "EspNowUITest";
using namespace espnow;
// UI elements
static lv_obj_t* screen_ = nullptr;
static lv_obj_t* header_label_ = nullptr;
static lv_obj_t* status_label_ = nullptr;
static lv_obj_t* peer_list_container_ = nullptr;
static lv_obj_t* peer_labels_[5] = {nullptr}; // Top 5 peers
static lv_obj_t* message_log_container_ = nullptr;
static lv_obj_t* message_labels_[3] = {nullptr}; // Last 3 messages
static lv_obj_t* stats_label_ = nullptr;
// Colors
static const lv_color_t COLOR_BG = lv_color_hex(0x0A0E27);
static const lv_color_t COLOR_HEADER = lv_color_hex(0x1A1F3A);
static const lv_color_t COLOR_PEER_BG = lv_color_hex(0x2A2F4A);
static const lv_color_t COLOR_TEXT = lv_color_hex(0xFFFFFF);
static const lv_color_t COLOR_ACCENT = lv_color_hex(0x00D9FF);
static const lv_color_t COLOR_SUCCESS = lv_color_hex(0x00FF88);
static const lv_color_t COLOR_WARNING = lv_color_hex(0xFFDD00);
// Statistics
struct TestStats {
uint32_t messages_sent = 0;
uint32_t messages_received = 0;
uint32_t send_errors = 0;
uint32_t pairing_requests = 0;
};
static TestStats stats;
static std::vector<std::string> message_history;
static bool test_running = false;
// Serial input helper
static int read_serial_mac(char* buffer, size_t max_len, uint32_t timeout_ms) {
int idx = 0;
uint32_t start_time = esp_timer_get_time() / 1000;
while (idx < max_len - 1) {
if ((esp_timer_get_time() / 1000 - start_time) > timeout_ms) {
break;
}
int c = fgetc(stdin);
if (c == '\n' || c == '\r') {
break;
}
if (c != EOF && c != -1) {
buffer[idx++] = c;
fputc(c, stdout);
fflush(stdout);
}
vTaskDelay(pdMS_TO_TICKS(10));
}
buffer[idx] = '\0';
printf("\n");
return idx;
}
static void add_message_to_history(const std::string& msg) {
message_history.insert(message_history.begin(), msg);
if (message_history.size() > 10) {
message_history.resize(10);
}
}
static void create_ui() {
auto& board = Board::GetInstance();
auto display = board.GetDisplay();
if (!display) {
ESP_LOGE(TAG, "No display available");
return;
}
DisplayLockGuard lock(display);
// Create screen
screen_ = lv_obj_create(nullptr);
lv_obj_set_style_bg_color(screen_, COLOR_BG, 0);
lv_obj_set_scrollbar_mode(screen_, LV_SCROLLBAR_MODE_OFF);
// Header
lv_obj_t* header = lv_obj_create(screen_);
lv_obj_set_size(header, LV_HOR_RES, 50);
lv_obj_align(header, LV_ALIGN_TOP_MID, 0, 0);
lv_obj_set_style_bg_color(header, COLOR_HEADER, 0);
lv_obj_set_style_border_width(header, 0, 0);
lv_obj_set_style_radius(header, 0, 0);
lv_obj_set_style_pad_all(header, 8, 0);
header_label_ = lv_label_create(header);
lv_label_set_text(header_label_, "ESP-NOW Test");
lv_obj_set_style_text_color(header_label_, COLOR_ACCENT, 0);
lv_obj_align(header_label_, LV_ALIGN_TOP_LEFT, 0, 0);
// Status label
status_label_ = lv_label_create(header);
lv_label_set_text(status_label_, "Initializing...");
lv_obj_set_style_text_color(status_label_, COLOR_WARNING, 0);
lv_obj_align(status_label_, LV_ALIGN_TOP_LEFT, 0, 20);
// Peer list container
peer_list_container_ = lv_obj_create(screen_);
lv_obj_set_size(peer_list_container_, LV_HOR_RES - 20, 120);
lv_obj_align(peer_list_container_, LV_ALIGN_TOP_MID, 0, 60);
lv_obj_set_style_bg_color(peer_list_container_, COLOR_BG, 0);
lv_obj_set_style_border_width(peer_list_container_, 1, 0);
lv_obj_set_style_border_color(peer_list_container_, COLOR_HEADER, 0);
lv_obj_set_style_pad_all(peer_list_container_, 5, 0);
lv_obj_set_scrollbar_mode(peer_list_container_, LV_SCROLLBAR_MODE_OFF);
// Create peer labels
for (int i = 0; i < 5; i++) {
peer_labels_[i] = lv_label_create(peer_list_container_);
lv_label_set_text(peer_labels_[i], "");
lv_obj_set_style_text_color(peer_labels_[i], COLOR_TEXT, 0);
lv_obj_align(peer_labels_[i], LV_ALIGN_TOP_LEFT, 0, i * 22);
}
// Message log container
message_log_container_ = lv_obj_create(screen_);
lv_obj_set_size(message_log_container_, LV_HOR_RES - 20, 70);
lv_obj_align(message_log_container_, LV_ALIGN_TOP_MID, 0, 190);
lv_obj_set_style_bg_color(message_log_container_, COLOR_BG, 0);
lv_obj_set_style_border_width(message_log_container_, 1, 0);
lv_obj_set_style_border_color(message_log_container_, COLOR_HEADER, 0);
lv_obj_set_style_pad_all(message_log_container_, 5, 0);
lv_obj_set_scrollbar_mode(message_log_container_, LV_SCROLLBAR_MODE_OFF);
// Create message labels
for (int i = 0; i < 3; i++) {
message_labels_[i] = lv_label_create(message_log_container_);
lv_label_set_text(message_labels_[i], "");
lv_obj_set_style_text_color(message_labels_[i], COLOR_TEXT, 0);
lv_obj_align(message_labels_[i], LV_ALIGN_TOP_LEFT, 0, i * 20);
}
// Stats footer
stats_label_ = lv_label_create(screen_);
lv_label_set_text(stats_label_, "Sent: 0 | Rcv: 0 | Err: 0");
lv_obj_set_style_text_color(stats_label_, COLOR_ACCENT, 0);
lv_obj_align(stats_label_, LV_ALIGN_BOTTOM_MID, 0, -5);
// Load screen
lv_scr_load(screen_);
ESP_LOGI(TAG, "UI created successfully");
}
static void update_ui() {
auto& board = Board::GetInstance();
auto display = board.GetDisplay();
if (!display || !screen_) {
return;
}
DisplayLockGuard lock(display);
auto& manager = EspNowManager::GetInstance();
auto peers = manager.GetPairedDevices();
// Update status
char status_buf[64];
snprintf(status_buf, sizeof(status_buf), "MAC: %s", manager.GetLocalMacString().c_str());
lv_label_set_text(status_label_, status_buf);
lv_obj_set_style_text_color(status_label_,
peers.empty() ? COLOR_WARNING : COLOR_SUCCESS, 0);
// Update peer list
for (int i = 0; i < 5; i++) {
if (i < peers.size()) {
const auto& peer = peers[i];
char buf[80];
uint32_t last_seen_sec = (esp_timer_get_time() / 1000000) - (peer.last_seen / 1000);
snprintf(buf, sizeof(buf), "%s\n %s | %lus ago",
peer.name.c_str(),
EspNowManager::MacToString(peer.mac).c_str(),
(unsigned long)last_seen_sec);
lv_label_set_text(peer_labels_[i], buf);
} else {
lv_label_set_text(peer_labels_[i], "");
}
}
// Update message log
for (int i = 0; i < 3 && i < message_history.size(); i++) {
lv_label_set_text(message_labels_[i], message_history[i].c_str());
}
// Update stats
char stats_buf[64];
snprintf(stats_buf, sizeof(stats_buf), "Sent:%lu Rcv:%lu Err:%lu",
(unsigned long)stats.messages_sent,
(unsigned long)stats.messages_received,
(unsigned long)stats.send_errors);
lv_label_set_text(stats_label_, stats_buf);
}
static void ui_update_task(void* arg) {
while (test_running) {
update_ui();
vTaskDelay(pdMS_TO_TICKS(2000)); // Update every 2 seconds
}
vTaskDelete(NULL);
}
static void message_sender_task(void* arg) {
auto& manager = EspNowManager::GetInstance();
auto& board = Board::GetInstance();
std::string device_uuid = board.GetUuid();
std::string device_name = "PlaiPin-" + device_uuid.substr(0, 6);
int message_count = 0;
while (test_running) {
vTaskDelay(pdMS_TO_TICKS(15000)); // Every 15 seconds
auto peers = manager.GetPairedDevices();
if (!peers.empty()) {
message_count++;
char msg_text[128];
snprintf(msg_text, sizeof(msg_text), "Test #%d from %s",
message_count, device_name.c_str());
if (manager.BroadcastTextMessage(device_uuid, device_name, msg_text)) {
// Note: messages_sent is incremented by OnSend callback
add_message_to_history(std::string("→ ") + msg_text);
ESP_LOGI(TAG, "Sent: %s", msg_text);
} else {
// Note: send_errors is incremented by OnSend callback
ESP_LOGE(TAG, "Failed to send message");
}
}
}
vTaskDelete(NULL);
}
void start_espnow_ui_test() {
ESP_LOGI(TAG, "");
ESP_LOGI(TAG, "╔═══════════════════════════════════════════════════════╗");
ESP_LOGI(TAG, "║ ESP-NOW UI TEST MODE ║");
ESP_LOGI(TAG, "╚═══════════════════════════════════════════════════════╝");
ESP_LOGI(TAG, "");
// Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
ESP_LOGI(TAG, "✓ NVS initialized");
// Initialize WiFi
ESP_LOGI(TAG, "Initializing WiFi...");
ESP_ERROR_CHECK(esp_netif_init());
// Create event loop (may already exist in IDF 5.5)
ret = esp_event_loop_create_default();
if (ret != ESP_OK && ret != ESP_ERR_INVALID_STATE) {
ESP_LOGE(TAG, "Failed to create event loop: %s", esp_err_to_name(ret));
return;
}
esp_netif_create_default_wifi_sta();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_ERROR_CHECK(esp_wifi_set_channel(1, WIFI_SECOND_CHAN_NONE));
ESP_LOGI(TAG, "✓ WiFi initialized");
// Get device info
auto& board = Board::GetInstance();
std::string device_uuid = board.GetUuid();
std::string device_name = "PlaiPin-" + device_uuid.substr(0, 6);
ESP_LOGI(TAG, "Device: %s", device_name.c_str());
// Initialize display
auto display = board.GetDisplay();
if (!display) {
ESP_LOGW(TAG, "No display available - falling back to console mode");
ESP_LOGI(TAG, "Please use the console test instead");
return;
}
// Create UI
create_ui();
// Initialize ESP-NOW
ESP_LOGI(TAG, "Initializing ESP-NOW...");
auto& manager = EspNowManager::GetInstance();
if (!manager.Initialize()) {
ESP_LOGE(TAG, "Failed to initialize ESP-NOW");
return;
}
ESP_LOGI(TAG, "✓ ESP-NOW initialized");
ESP_LOGI(TAG, " Local MAC: %s", manager.GetLocalMacString().c_str());
// Set up callbacks
manager.OnPairingRequest([&](const uint8_t* mac, const PairingRequestPayload& payload) {
stats.messages_received++;
ESP_LOGI(TAG, "Pairing request from %s", payload.device_name);
manager.SendPairingResponse(mac, device_uuid, device_name, "", true);
add_message_to_history(std::string("← Paired: ") + payload.device_name);
});
manager.OnPairingResponse([](const uint8_t* mac, const PairingResponsePayload& payload) {
stats.messages_received++;
ESP_LOGI(TAG, "Pairing response from %s: %s",
payload.device_name,
payload.accepted ? "ACCEPTED" : "REJECTED");
if (payload.accepted) {
add_message_to_history(std::string("→ Paired: ") + payload.device_name);
}
});
manager.OnTextMessage([](const uint8_t* mac, const TextMessagePayload& payload) {
stats.messages_received++;
ESP_LOGI(TAG, "Message from %s: %s", payload.sender_name, payload.message_text);
add_message_to_history(std::string("← ") + payload.message_text);
});
manager.OnSend([](const uint8_t* mac, esp_now_send_status_t status) {
if (status == ESP_NOW_SEND_SUCCESS) {
stats.messages_sent++;
} else {
stats.send_errors++;
}
});
// Check for pre-configured peer MAC (Kconfig)
#ifdef CONFIG_ESPNOW_TEST_MANUAL_KCONFIG
const char* preconfigured_mac = CONFIG_ESPNOW_TEST_PEER_MAC;
if (preconfigured_mac && strlen(preconfigured_mac) > 0) {
ESP_LOGI(TAG, "");
ESP_LOGI(TAG, "📋 Pre-configured peer MAC: %s", preconfigured_mac);
uint8_t peer_mac[6];
if (EspNowManager::StringToMac(preconfigured_mac, peer_mac)) {
ESP_LOGI(TAG, "✓ Initiating pairing with pre-configured peer");
ESP_LOGI(TAG, "");
manager.SendPairingRequest(peer_mac, device_uuid, device_name, "", 0);
} else {
ESP_LOGE(TAG, "✗ Invalid pre-configured MAC address format!");
ESP_LOGI(TAG, " Falling back to passive mode");
ESP_LOGI(TAG, "");
}
} else {
ESP_LOGI(TAG, "");
ESP_LOGI(TAG, "⏸ No peer MAC configured, entering passive mode");
ESP_LOGI(TAG, "");
}
#else
// Prompt for peer MAC via serial
ESP_LOGI(TAG, "");
ESP_LOGI(TAG, "Enter peer MAC (XX:XX:XX:XX:XX:XX) or '-' for passive mode:");
printf("> ");
fflush(stdout);
char mac_input[18] = {0};
int len = read_serial_mac(mac_input, sizeof(mac_input), 10000); // 10 second timeout
// Check for passive mode trigger
if (len == 1 && mac_input[0] == '-') {
ESP_LOGI(TAG, "");
ESP_LOGI(TAG, "⏸ Passive mode explicitly requested");
} else if (len > 0) {
uint8_t peer_mac[6];
if (EspNowManager::StringToMac(mac_input, peer_mac)) {
ESP_LOGI(TAG, "");
ESP_LOGI(TAG, "✓ Initiating pairing with: %s", mac_input);
ESP_LOGI(TAG, "");
manager.SendPairingRequest(peer_mac, device_uuid, device_name, "", 0);
} else {
ESP_LOGE(TAG, "✗ Invalid MAC address format");
ESP_LOGI(TAG, " Falling back to passive mode");
}
} else {
ESP_LOGI(TAG, "");
ESP_LOGI(TAG, "⏸ No input received, entering passive mode");
}
ESP_LOGI(TAG, "");
#endif
// Start tasks
test_running = true;
xTaskCreate(ui_update_task, "ui_update", 4096, NULL, 5, NULL);
xTaskCreate(message_sender_task, "sender", 4096, NULL, 5, NULL);
ESP_LOGI(TAG, "");
ESP_LOGI(TAG, "╔═══════════════════════════════════════════════════════╗");
ESP_LOGI(TAG, "║ ESP-NOW UI TEST ACTIVE - CHECK SCREEN! ║");
ESP_LOGI(TAG, "╚═══════════════════════════════════════════════════════╝");
ESP_LOGI(TAG, "");
// Main loop
while (true) {
vTaskDelay(pdMS_TO_TICKS(1000));
}
}