* refactored the UI_DISCOVER_* feature

This commit is contained in:
Scott Powell
2026-09-17 13:45:33 +10:00
parent 3caf033de2
commit 5c3d9281ff
8 changed files with 112 additions and 108 deletions
+59 -13
View File
@@ -99,7 +99,7 @@ class HomeScreen : public UIScreen {
#if UI_SENSORS_PAGE == 1
SENSORS,
#endif
#if !(defined(UI_NO_DISCOVER_SCREEN) && (UI_NO_DISCOVER_SCREEN + 0 != 0))
#if UI_DISCOVER_SCREEN
DISCOVERY,
#endif
#ifndef UI_NO_HIBERNATE
@@ -115,8 +115,10 @@ class HomeScreen : public UIScreen {
uint8_t _page;
bool _shutdown_init;
AdvertPath recent[UI_RECENT_LIST_SIZE];
#if !(defined(UI_NO_DISCOVER_SCREEN) && (UI_NO_DISCOVER_SCREEN + 0 != 0))
DiscoveredNode discovered[UI_RECENT_LIST_SIZE];
#if UI_DISCOVER_SCREEN
DiscoveredNode discovered[DISCOVERED_NODES_TABLE_SIZE]; // not circular, latest discovered nodes are not kept
uint32_t disc_node_req_tag = 0;
uint32_t disc_nodes_count = 0;
uint32_t discovery_req_time = 0;
bool discovery_disp_names = true; // by default desplay names if available (removes SNR_O)
#endif
@@ -206,6 +208,40 @@ public:
: _task(task), _rtc(rtc), _sensors(sensors), _node_prefs(node_prefs), _page(0),
_shutdown_init(false), sensors_lpp(200) { }
#if UI_DISCOVER_SCREEN
bool sendDiscoverRequest() {
uint8_t cmd_bytes[6];
cmd_bytes[0] = CTL_TYPE_NODE_DISCOVER_REQ | 1; // DISCOVER_REQ | prefix only
cmd_bytes[1] = 0xFF; // Repeaters
the_mesh.getRNG()->random((uint8_t *) &disc_node_req_tag, 4); // generate random tag
memcpy(&cmd_bytes[2], &disc_node_req_tag, 4);
disc_nodes_count = 0;
mesh::Packet* req = the_mesh.createControlData(cmd_bytes, sizeof(cmd_bytes));
if (req) {
the_mesh.sendZeroHop(req);
discovery_req_time = millis();
return true;
}
return false;
}
void handleDiscoverResponse(const mesh::Packet* packet) {
if (disc_nodes_count < DISCOVERED_NODES_TABLE_SIZE && memcmp(&packet->payload[2], &disc_node_req_tag, 4) == 0) {
auto d = &discovered[disc_nodes_count++];
memcpy(d->pubkey_prefix, &packet->payload[6], 8);
d->type = packet->payload[0] & 0xF;
d->snr_out = ((int8_t)packet->payload[1]) / 4.0;
d->snr_in = radio_driver.getLastSNR();
ContactInfo* c = the_mesh.lookupContactByPubKey(&packet->payload[6], 8);
if (c != NULL) {
strncpy(d->name, c->name, 32);
} else {
d->name[0] = 0;
}
}
}
#endif
void poll() override {
if (_shutdown_init && !_task->isButtonPressed()) { // must wait for USR button to be released
_task->shutdown();
@@ -464,9 +500,9 @@ public:
if (sensors_scroll) sensors_scroll_offset = (sensors_scroll_offset+1)%sensors_nb;
else sensors_scroll_offset = 0;
#endif
#if !(defined(UI_NO_DISCOVER_SCREEN) && (UI_NO_DISCOVER_SCREEN + 0 != 0))
#if UI_DISCOVER_SCREEN
} else if (_page == HomePage::DISCOVERY) {
int count = the_mesh.getDiscoveredNodes(discovered, UI_RECENT_LIST_SIZE);
int count = disc_nodes_count;
display.setColor(UIColor::primary_txt);
int y = 20;
for (int i = 0; i < count; i++, y += 11) {
@@ -493,8 +529,8 @@ public:
}
if (millis() < discovery_req_time + 5000) {
return 1000; // more frequent updates just after req
} else if (count < UI_RECENT_LIST_SIZE -1) { // show only 5 sec after last disc
y = 10 + 11 * UI_RECENT_LIST_SIZE;
} else if (count < DISCOVERED_NODES_TABLE_SIZE -1) { // show only 5 sec after last disc
y = 10 + 11 * DISCOVERED_NODES_TABLE_SIZE;
display.drawTextCentered(display.width() / 2, y, "discover: " PRESS_LABEL);
}
#endif
@@ -525,7 +561,7 @@ public:
if (_page == HomePage::RECENT) {
_task->showAlert("Recent adverts", 800);
}
#if !(defined(UI_NO_DISCOVER_SCREEN) && (UI_NO_DISCOVER_SCREEN + 0 != 0))
#if UI_DISCOVER_SCREEN
if (_page == HomePage::DISCOVERY) {
_task->showAlert("Repeater disc", 800);
}
@@ -562,11 +598,10 @@ public:
return true;
}
#endif
#if !(defined(UI_NO_DISCOVER_SCREEN) && (UI_NO_DISCOVER_SCREEN + 0 != 0))
#if UI_DISCOVER_SCREEN
if (c == KEY_ENTER && _page == HomePage::DISCOVERY) {
if (millis() > discovery_req_time + 5000) { // rate limiter
the_mesh.requestRepeatersDiscovery();
discovery_req_time = millis();
sendDiscoverRequest();
}
return true;
}
@@ -751,8 +786,11 @@ switch(t){
#endif
}
void UITask::onMessageRecv(uint8_t path_len, const char* from_name, const char* text) {
((MsgPreviewScreen *) msg_preview)->addPreview(path_len, from_name, text);
void UITask::onMessageRecv(const ContactInfo &from, uint8_t txt_type, uint32_t sender_timestamp, uint8_t path_len, const char* text) {
// we only want to show text messages on display, not cli data
if (!(txt_type == TXT_TYPE_PLAIN || txt_type == TXT_TYPE_SIGNED_PLAIN)) return;
((MsgPreviewScreen *) msg_preview)->addPreview(path_len, from.name, text);
setCurrScreen(msg_preview);
if (_display != NULL) {
@@ -802,6 +840,14 @@ void UITask::onDiscoveredContact(ContactInfo &contact, bool is_new, uint8_t path
}
}
void UITask::onControlDataRecv(const mesh::Packet* packet) {
#if UI_DISCOVER_SCREEN
if (packet->payload_len >= 12 && (packet->payload[0] & 0xF0) == CTL_TYPE_NODE_DISCOVER_RESP) {
((HomeScreen *) home)->handleDiscoverResponse(packet);
}
#endif
}
void UITask::userLedHandler() {
#ifdef PIN_STATUS_LED
int cur_time = millis();