From 92f37c025882d05d6d3115f033cabd7b53b487fb Mon Sep 17 00:00:00 2001 From: Manuel <71137295+mverch67@users.noreply.github.com> Date: Sat, 10 May 2025 14:27:26 +0200 Subject: [PATCH] calc default channel; keep channel_num if UNSET (#127) --- include/graphics/common/LoRaPresets.h | 3 +- include/graphics/view/TFT/TFTView_320x240.h | 2 +- source/graphics/TFT/TFTView_320x240.cpp | 54 +++++++++++++-------- source/graphics/common/LoRaPresets.cpp | 5 +- 4 files changed, 40 insertions(+), 24 deletions(-) diff --git a/include/graphics/common/LoRaPresets.h b/include/graphics/common/LoRaPresets.h index de804d7..89330cc 100644 --- a/include/graphics/common/LoRaPresets.h +++ b/include/graphics/common/LoRaPresets.h @@ -16,7 +16,8 @@ class LoRaPresets static float getFrequencyStart(meshtastic_Config_LoRaConfig_RegionCode region); static float getFrequencyEnd(meshtastic_Config_LoRaConfig_RegionCode region); static uint16_t getDefaultSlot(meshtastic_Config_LoRaConfig_RegionCode region, - meshtastic_Config_LoRaConfig_ModemPreset preset); + meshtastic_Config_LoRaConfig_ModemPreset preset, + const char* channelName = nullptr); struct ModemPreset { const char *preset; diff --git a/include/graphics/view/TFT/TFTView_320x240.h b/include/graphics/view/TFT/TFTView_320x240.h index 8f7afa1..fa197fe 100644 --- a/include/graphics/view/TFT/TFTView_320x240.h +++ b/include/graphics/view/TFT/TFTView_320x240.h @@ -431,5 +431,5 @@ class TFTView_320x240 : public MeshtasticView meshtastic_DeviceConnectionStatus connectionStatus; // wifi/bluetooth/ethernet }; - meshtastic_DeviceProfile_full db; // full copy of the node's configuration db (except nodeinfos) plus ui data + meshtastic_DeviceProfile_full db{}; // full copy of the node's configuration db (except nodeinfos) plus ui data }; \ No newline at end of file diff --git a/source/graphics/TFT/TFTView_320x240.cpp b/source/graphics/TFT/TFTView_320x240.cpp index bc450fe..e06f774 100644 --- a/source/graphics/TFT/TFTView_320x240.cpp +++ b/source/graphics/TFT/TFTView_320x240.cpp @@ -3614,7 +3614,11 @@ void TFTView_320x240::ui_event_ok(lv_event_t *e) lv_label_set_text(objects.basic_settings_region_label, buf2); meshtastic_Config_LoRaConfig &lora = THIS->db.config.lora; - uint32_t defaultSlot = LoRaPresets::getDefaultSlot(region, THIS->db.config.lora.modem_preset); + uint32_t defaultSlot = lora.region == meshtastic_Config_LoRaConfig_RegionCode_UNSET ? lora.channel_num : 0; + if (defaultSlot == 0) { + defaultSlot = LoRaPresets::getDefaultSlot(region, THIS->db.config.lora.modem_preset, + THIS->db.channel[0].settings.name); + } lora.region = region; lora.channel_num = (defaultSlot <= numChannels ? defaultSlot : 1); THIS->controller->sendConfig(meshtastic_Config_LoRaConfig{lora}, THIS->ownNode); @@ -3699,7 +3703,11 @@ void TFTView_320x240::ui_event_ok(lv_event_t *e) lv_label_set_text(objects.basic_settings_region_label, buf2); meshtastic_Config_LoRaConfig &lora = THIS->db.config.lora; - uint32_t defaultSlot = LoRaPresets::getDefaultSlot(region, THIS->db.config.lora.modem_preset); + uint32_t defaultSlot = lora.region == meshtastic_Config_LoRaConfig_RegionCode_UNSET ? lora.channel_num : 0; + if (defaultSlot == 0) { + defaultSlot = LoRaPresets::getDefaultSlot(region, THIS->db.config.lora.modem_preset, + THIS->db.channel[0].settings.name); + } lora.region = region; lora.channel_num = (defaultSlot <= numChannels ? defaultSlot : 1); THIS->controller->sendConfig(meshtastic_Config_LoRaConfig{lora}, THIS->ownNode); @@ -3953,27 +3961,30 @@ void TFTView_320x240::ui_event_ok(lv_event_t *e) // delete channel THIS->channel_scratch[ch].role = meshtastic_Channel_Role_DISABLED; THIS->channel_scratch[ch].settings.psk.size = 0; - strcpy(THIS->channel_scratch[ch].settings.name, ""); + memset(THIS->channel_scratch[ch].settings.name, 0, sizeof(THIS->channel_scratch[ch].settings.name)); + memset(THIS->channel_scratch[ch].settings.psk.bytes, 0, sizeof(THIS->channel_scratch[ch].settings.psk.bytes)); + THIS->channel_scratch[ch].has_settings = false; lv_label_set_text(THIS->ch_label[btn_id], _("")); THIS->activeSettings = eChannel; } - - int paddings = (4 - strlen(base64) % 4) % 4; - while (paddings-- > 0) { - lv_textarea_add_text(objects.settings_modify_channel_psk_textarea, "="); - } - - if (THIS->base64ToPsk(lv_textarea_get_text(objects.settings_modify_channel_psk_textarea), psk.bytes, psk.size)) { - if (strlen(name) || psk.size) { - // TODO: fill temp storage -> user data - lv_label_set_text(THIS->ch_label[btn_id], name); - strcpy(THIS->channel_scratch[ch].settings.name, name); - memcpy(THIS->channel_scratch[ch].settings.psk.bytes, psk.bytes, 32); - THIS->channel_scratch[ch].settings.psk.size = psk.size; - THIS->activeSettings = eChannel; + else { + int paddings = (4 - strlen(base64) % 4) % 4; + while (paddings-- > 0) { + lv_textarea_add_text(objects.settings_modify_channel_psk_textarea, "="); } + + if (THIS->base64ToPsk(lv_textarea_get_text(objects.settings_modify_channel_psk_textarea), psk.bytes, psk.size)) { + if (strlen(name) || psk.size) { + // TODO: fill temp storage -> user data + lv_label_set_text(THIS->ch_label[btn_id], name); + strcpy(THIS->channel_scratch[ch].settings.name, name); + memcpy(THIS->channel_scratch[ch].settings.psk.bytes, psk.bytes, 32); + THIS->channel_scratch[ch].settings.psk.size = psk.size; + THIS->activeSettings = eChannel; + } + } + THIS->channel_scratch[ch].role = (ch == 0) ? meshtastic_Channel_Role_PRIMARY : meshtastic_Channel_Role_SECONDARY; } - if (THIS->activeSettings == eChannel) { lv_obj_add_flag(objects.settings_modify_channel_panel, LV_OBJ_FLAG_HIDDEN); THIS->enablePanel(objects.settings_channel_panel); @@ -4156,7 +4167,8 @@ void TFTView_320x240::ui_event_modem_preset_dropdown(lv_event_t *e) return; } - uint32_t channel = LoRaPresets::getDefaultSlot(THIS->db.config.lora.region, preset); + uint32_t channel = LoRaPresets::getDefaultSlot(THIS->db.config.lora.region, preset, + THIS->db.channel[0].settings.name); if (channel > numChannels) channel = 1; lv_slider_set_range(objects.frequency_slot_slider, 1, numChannels); @@ -5676,7 +5688,9 @@ void TFTView_320x240::updateLoRaConfig(const meshtastic_Config_LoRaConfig &cfg) lv_slider_set_range(objects.frequency_slot_slider, 1, numChannels); if (!db.config.lora.channel_num) { - db.config.lora.channel_num = LoRaPresets::getDefaultSlot(db.config.lora.region, THIS->db.config.lora.modem_preset); + db.config.lora.channel_num = LoRaPresets::getDefaultSlot(db.config.lora.region, + THIS->db.config.lora.modem_preset, + THIS->db.channel[0].settings.name); } lv_slider_set_value(objects.frequency_slot_slider, db.config.lora.channel_num, LV_ANIM_OFF); diff --git a/source/graphics/common/LoRaPresets.cpp b/source/graphics/common/LoRaPresets.cpp index 300c991..0844f29 100644 --- a/source/graphics/common/LoRaPresets.cpp +++ b/source/graphics/common/LoRaPresets.cpp @@ -36,7 +36,8 @@ float LoRaPresets::getFrequencyEnd(meshtastic_Config_LoRaConfig_RegionCode regio * Default slot number is generated using the same firmware hash algorithm */ uint16_t LoRaPresets::getDefaultSlot(meshtastic_Config_LoRaConfig_RegionCode region, - meshtastic_Config_LoRaConfig_ModemPreset preset) + meshtastic_Config_LoRaConfig_ModemPreset preset, + const char* channelName) { auto hash = [](const char *str) -> uint32_t { uint32_t hash = 5381; @@ -47,7 +48,7 @@ uint16_t LoRaPresets::getDefaultSlot(meshtastic_Config_LoRaConfig_RegionCode reg }; uint32_t numChannels = getNumChannels(region, preset); - return numChannels == 0 ? 1 : hash(modemPreset[preset].preset) % numChannels + 1; + return numChannels == 0 ? 1 : hash(channelName ? channelName : modemPreset[preset].preset) % numChannels + 1; } float LoRaPresets::getBandwidth(meshtastic_Config_LoRaConfig_ModemPreset preset)