From 79d38b2a0fe8fea9fe71ecaa4aa558705e6e3839 Mon Sep 17 00:00:00 2001 From: Emil Muratov Date: Mon, 13 May 2024 18:07:23 +0900 Subject: [PATCH] Implement checkbox in temp config to enable/disable saving last work temperature Setting -> Temperature -> use last temp Checkbox controls if last used work temperature should be saved in NVS and restored on power on. If disabled (default) then work temperature from settings will be used on power on. If enabled, then each time work temp is changed, it will be saved/restored later ref issue: Eddddddddy/Songguo-PTS200#19 --- .gitignore | 1 + SolderingPen_ESP32S2/common.hpp | 3 ++- SolderingPen_ESP32S2/const.h | 8 +++--- SolderingPen_ESP32S2/evtloop.hpp | 2 ++ SolderingPen_ESP32S2/hid.cpp | 35 ++++++++++++++++++++----- SolderingPen_ESP32S2/hid.hpp | 10 ++++++- SolderingPen_ESP32S2/ironcontroller.cpp | 18 ++++++++++++- SolderingPen_ESP32S2/lang/i18n.h | 4 ++- SolderingPen_ESP32S2/lang/lang_en_us.h | 18 ++++++++----- SolderingPen_ESP32S2/nvs.cpp | 8 +++--- 10 files changed, 82 insertions(+), 25 deletions(-) diff --git a/.gitignore b/.gitignore index 895fb10..2959175 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ .DS_Store .idea .lnk +temp/ platformio_*.ini SolderingPen_ESP32S2/build SolderingPen_ESP32S2/.idea diff --git a/SolderingPen_ESP32S2/common.hpp b/SolderingPen_ESP32S2/common.hpp index e3b28d2..7b5949a 100644 --- a/SolderingPen_ESP32S2/common.hpp +++ b/SolderingPen_ESP32S2/common.hpp @@ -31,6 +31,7 @@ enum class ironState_t { // working temperature values struct Temperatures { - int32_t working{TEMP_DEFAULT}, standby{TEMP_SLEEP}, boost{TEMP_BOOST}; + int32_t working{TEMP_DEFAULT}, standby{TEMP_SLEEP}, boost{TEMP_BOOST}, deflt{TEMP_DEFAULT}; + bool savewrk{false}; }; diff --git a/SolderingPen_ESP32S2/const.h b/SolderingPen_ESP32S2/const.h index c8dbd83..45cb384 100644 --- a/SolderingPen_ESP32S2/const.h +++ b/SolderingPen_ESP32S2/const.h @@ -17,8 +17,8 @@ static constexpr const char* T_UI = "UI"; static constexpr const char* T_HID = "HID"; // NVS keys -static constexpr const char* T_timeouts = "timeouts"; -static constexpr const char* T_temperatures = "temperatures"; +static constexpr const char* T_timeouts = "timeouts"; // blob with timeout values +static constexpr const char* T_temperatures = "temperatures"; // blob with temperature values + +static constexpr const char* T_motionThr = "motionThr"; // motion threshold (uint32) -static constexpr const char* T_motionThr = "motionThr"; // motion threshold (uint32) -static constexpr const char* T_lang = "lang"; // UI language diff --git a/SolderingPen_ESP32S2/evtloop.hpp b/SolderingPen_ESP32S2/evtloop.hpp index cb401a3..cfb6af0 100644 --- a/SolderingPen_ESP32S2/evtloop.hpp +++ b/SolderingPen_ESP32S2/evtloop.hpp @@ -53,6 +53,8 @@ enum class iron_t:int32_t { workModeToggle, // toggle working mode on/off boostModeToggle, // toggle boost mode on/off + reloadTemp, // reload temperature configuration + // State notifications stateWorking = 300, stateStandby, // iron controller switched to 'Standby' mode diff --git a/SolderingPen_ESP32S2/hid.cpp b/SolderingPen_ESP32S2/hid.cpp index 37f6829..c6ce5ec 100644 --- a/SolderingPen_ESP32S2/hid.cpp +++ b/SolderingPen_ESP32S2/hid.cpp @@ -124,6 +124,7 @@ void IronHID::switchViSet(viset_evt_t v){ viset = std::make_unique(_btn, _encdr); break; }; + LOGI(T_HID, printf, "heap:%u\n", ESP.getFreeHeap()/1024); } void IronHID::init(){ @@ -236,14 +237,15 @@ ViSet_MainScreen::ViSet_MainScreen(GPIOButton &button, PseudoRot } ViSet_MainScreen::~ViSet_MainScreen(){ - esp_event_handler_instance_unregister_with(evt::get_hndlr(), IRON_VISET, ESP_EVENT_ANY_ID, _evt_snsr_handler); + esp_event_handler_instance_unregister_with(evt::get_hndlr(), SENSOR_DATA, ESP_EVENT_ANY_ID, _evt_snsr_handler); _evt_snsr_handler = nullptr; - esp_event_handler_instance_unregister_with(evt::get_hndlr(), IRON_VISET, ESP_EVENT_ANY_ID, _evt_ntfy_handler); + esp_event_handler_instance_unregister_with(evt::get_hndlr(), IRON_NOTIFY, ESP_EVENT_ANY_ID, _evt_ntfy_handler); _evt_ntfy_handler = nullptr; - esp_event_handler_instance_unregister_with(evt::get_hndlr(), IRON_VISET, ESP_EVENT_ANY_ID, _evt_set_handler); + esp_event_handler_instance_unregister_with(evt::get_hndlr(), IRON_SET_EVT, ESP_EVENT_ANY_ID, _evt_set_handler); _evt_set_handler = nullptr; - esp_event_handler_instance_unregister_with(evt::get_hndlr(), IRON_VISET, ESP_EVENT_ANY_ID, _evt_state_handler); + esp_event_handler_instance_unregister_with(evt::get_hndlr(), IRON_STATE, ESP_EVENT_ANY_ID, _evt_state_handler); _evt_state_handler = nullptr; + LOG(println, "d-tor ViSet_MainScreen"); } void ViSet_MainScreen::drawScreen(){ @@ -577,7 +579,7 @@ void ViSet_MainMenu::_buildMenu(){ [this](size_t idx){ _submenu_selector(idx); }, // action callback MAIN_MENU_Y_SHIFT, 3, // offset for each line of text and total number of lines in menu MAIN_MENU_X_OFFSET, MAIN_MENU_Y_OFFSET, // x,y cursor - MAIN_MENU_FONT2, NULL + MAIN_MENU_FONT2, MAIN_MENU_FONT2 ); // set dynamic list will act as page selector @@ -605,6 +607,21 @@ void ViSet_MainMenu::_submenu_selector(size_t index){ // ************************************** // *** Temperature Control Menu *** +ViSet_TemperatureSetup::ViSet_TemperatureSetup(GPIOButton &button, PseudoRotaryEncoder &encoder) : MuiMenu(button, encoder){ + // load temperature values from NVS + nvs_blob_read(T_IRON, T_temperatures, static_cast(&_temp), sizeof(Temperatures)); + + _buildMenu(); +} + +ViSet_TemperatureSetup::~ViSet_TemperatureSetup(){ + //LOGD(T_HID, println, "save temp settings"); + // save temp settings to NVS + nvs_blob_write(T_IRON, T_temperatures, static_cast(&_temp), sizeof(Temperatures)); + // send command to reload temp settings + EVT_POST(IRON_SET_EVT, e2int(iron_t::reloadTemp)); +} + void ViSet_TemperatureSetup::_buildMenu(){ // create page "Settings->Temperature" muiItemId root_page = makePage(menu_MainConfiguration.at(0)); @@ -643,10 +660,14 @@ void ViSet_TemperatureSetup::_buildMenu(){ // page title element addItemToPage(page_title_id, page_savewrkT); - // checkbox - MuiItem_U8g2_CheckBox *box = new MuiItem_U8g2_CheckBox(u8g2, nextIndex(), "Some box with very long text here...", true, nullptr, MAINSCREEN_FONT, 0, 25); + // save last work temp checkbox + MuiItem_U8g2_CheckBox *box = new MuiItem_U8g2_CheckBox(u8g2, nextIndex(), dictionary[D_SaveLast_box], _temp.savewrk, [this](size_t v){ _temp.savewrk = v; }, MAINSCREEN_FONT, 0, 25); addMuippItem(box, page_savewrkT); + // text hint + MuiItem_U8g2_StaticText *txt = new MuiItem_U8g2_StaticText(u8g2, nextIndex(), dictionary[D_SaveLast_hint], MAIN_MENU_FONT1, 0, 25); + addMuippItem(txt, page_savewrkT); + // generate idx for "Back Button" item muiItemId bbuton_id = nextIndex(); // create "Back Button" item diff --git a/SolderingPen_ESP32S2/hid.hpp b/SolderingPen_ESP32S2/hid.hpp index 420c07a..ca21b59 100644 --- a/SolderingPen_ESP32S2/hid.hpp +++ b/SolderingPen_ESP32S2/hid.hpp @@ -243,12 +243,20 @@ class ViSet_MainMenu : public MuiMenu { * */ class ViSet_TemperatureSetup : public MuiMenu { + // configured temperatures + Temperatures _temp; + + // callback for "Save work temp" option + //void _cb_save_last_temp(size_t v); + // menu builder function void _buildMenu(); public: // c-tor - ViSet_TemperatureSetup(GPIOButton &button, PseudoRotaryEncoder &encoder) : MuiMenu(button, encoder) { _buildMenu(); } + ViSet_TemperatureSetup(GPIOButton &button, PseudoRotaryEncoder &encoder); + // d-tor + ~ViSet_TemperatureSetup(); }; diff --git a/SolderingPen_ESP32S2/ironcontroller.cpp b/SolderingPen_ESP32S2/ironcontroller.cpp index dc232e8..17572d0 100644 --- a/SolderingPen_ESP32S2/ironcontroller.cpp +++ b/SolderingPen_ESP32S2/ironcontroller.cpp @@ -44,6 +44,10 @@ void IronController::init(){ // load temperature values from NVS nvs_blob_read(T_IRON, T_temperatures, static_cast(&_temp), sizeof(Temperatures)); + // if we are not saving working temp, then use default one instead + if (!_temp.savewrk) + _temp.working = _temp.deflt; + // start mode switcher timer if (!_tmr_mode){ _tmr_mode = xTimerCreate("modeT", @@ -218,13 +222,25 @@ void IronController::_evt_commands(esp_event_base_t base, int32_t id, void* data int32_t t = *reinterpret_cast(data); if (t != _temp.working){ _temp.working = *reinterpret_cast(data); - nvs_blob_write(T_IRON, T_temperatures, static_cast(&_temp), sizeof(Temperatures)); + // save new working temp to NVS only if respective flag is set + if (_temp.savewrk) + nvs_blob_write(T_IRON, T_temperatures, static_cast(&_temp), sizeof(Temperatures)); } if (_state == ironState_t::working) EVT_POST_DATA(IRON_SET_EVT, e2int(iron_t::heaterTargetT), &_temp.working, sizeof(_temp.working)); break; } + // reload temp configuration + case evt::iron_t::reloadTemp : { + LOGV(T_HID, println, "reload temp settings"); + // load temperature values from NVS + nvs_blob_read(T_IRON, T_temperatures, static_cast(&_temp), sizeof(Temperatures)); + + // if we are not saving working temp, then use default one instead + if (!_temp.savewrk) + _temp.working = _temp.deflt; + } // some } diff --git a/SolderingPen_ESP32S2/lang/i18n.h b/SolderingPen_ESP32S2/lang/i18n.h index 8d7752b..2aa9eab 100644 --- a/SolderingPen_ESP32S2/lang/i18n.h +++ b/SolderingPen_ESP32S2/lang/i18n.h @@ -37,9 +37,11 @@ enum dict_index { D_idle, D_notip, D_return, + D_SaveLast_box, + D_SaveLast_hint, D_Settings, D_set_t, D_standby, D_Temp_SaveLastWrkDescr, - D_____SIZE + DICT___SIZE }; diff --git a/SolderingPen_ESP32S2/lang/lang_en_us.h b/SolderingPen_ESP32S2/lang/lang_en_us.h index adde608..53893b9 100644 --- a/SolderingPen_ESP32S2/lang/lang_en_us.h +++ b/SolderingPen_ESP32S2/lang/lang_en_us.h @@ -61,10 +61,14 @@ static constexpr const char* T_Information = "Information"; static constexpr const char* T_Language = "Language"; // Temperature settings menu -static constexpr const char* T_Temp_SaveLastWrk = "Save work T"; -static constexpr const char* T_TempDefltWrk = "Default work T"; -static constexpr const char* T_TempSleep = "Standby temp"; -static constexpr const char* T_TempBoost = "Boost increase T"; +static constexpr const char* T_SaveLastT = "Save work temp."; +static constexpr const char* T_TempDefltWrk = "Default temp."; +static constexpr const char* T_TempSleep = "Standby temp."; +static constexpr const char* T_TempBoost = "Boost step T"; +// Temperature settings hints +static constexpr const char* T_SaveLast_box = "use last temp."; +static constexpr const char* T_SaveLast_hint = "instead of default one"; + } // end of namespace lang_en_us @@ -74,13 +78,15 @@ static constexpr const char* T_TempBoost = "Boost increase T"; * order of items MUST match enum dict_index * */ -static constexpr std::array dictionary = { +static constexpr std::array dictionary = { lang_en_us::T_Boost, lang_en_us::T_Error, lang_en_us::T_Heating, lang_en_us::T_Idle, lang_en_us::T_NoTip, lang_en_us::T_return, + lang_en_us::T_SaveLast_box, + lang_en_us::T_SaveLast_hint, lang_en_us::T_Settings, lang_en_us::T_setT, lang_en_us::T_standby, @@ -99,7 +105,7 @@ static constexpr std::array menu_MainConfigura // Main Configuration menu items static constexpr std::array menu_TemperatureOpts = { - lang_en_us::T_Temp_SaveLastWrk, + lang_en_us::T_SaveLastT, lang_en_us::T_TempDefltWrk, lang_en_us::T_TempSleep, lang_en_us::T_TempBoost, diff --git a/SolderingPen_ESP32S2/nvs.cpp b/SolderingPen_ESP32S2/nvs.cpp index f85140e..337417a 100644 --- a/SolderingPen_ESP32S2/nvs.cpp +++ b/SolderingPen_ESP32S2/nvs.cpp @@ -21,13 +21,13 @@ esp_err_t nvs_blob_read(const char* nvsspace, const char* key, void* blob, size_ std::unique_ptr nvs = nvs::open_nvs_handle(nvsspace, NVS_READONLY, &err); if (err != ESP_OK) { - LOGD(T_NVS, printf, "Err opening NVS namespace:%s RO, err:%s\n", nvsspace, esp_err_to_name(err)); + LOGD(T_NVS, printf, "Err opening NVS ns:%s RO, err:%s\n", nvsspace, esp_err_to_name(err)); return err; } err = nvs->get_blob(key, blob, len); if (err != ESP_OK) { - LOGD(T_NVS, printf, "Err reading NVS blob namespace:%s, key:%s, err:%s\n", nvsspace, key, esp_err_to_name(err)); + LOGD(T_NVS, printf, "Err reading NVS blob ns:%s, key:%s, err:%s\n", nvsspace, key, esp_err_to_name(err)); } return err; } @@ -37,13 +37,13 @@ esp_err_t nvs_blob_write(const char* nvsspace, const char* key, void* blob, size std::unique_ptr nvs = nvs::open_nvs_handle(nvsspace, NVS_READWRITE, &err); if (err != ESP_OK) { - LOGD(T_NVS, printf, "Err opening NVS namespace:%s RW, err:%s\n", nvsspace, esp_err_to_name(err)); + LOGD(T_NVS, printf, "Err opening NVS ns:%s RW, err:%s\n", nvsspace, esp_err_to_name(err)); return err; } err = nvs->set_blob(key, blob, len); if (err != ESP_OK) { - LOGD(T_NVS, printf, "Err writing NVS namespace:%s, key:%s, err:%s\n", nvsspace, key, esp_err_to_name(err)); + LOGD(T_NVS, printf, "Err writing NVS ns:%s, key:%s, err:%s\n", nvsspace, key, esp_err_to_name(err)); } return err; }