Skip to content

Commit

Permalink
Implement checkbox in temp config to enable/disable saving last work …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
vortigont committed May 13, 2024
1 parent aeb2673 commit 79d38b2
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
.DS_Store
.idea
.lnk
temp/
platformio_*.ini
SolderingPen_ESP32S2/build
SolderingPen_ESP32S2/.idea
Expand Down
3 changes: 2 additions & 1 deletion SolderingPen_ESP32S2/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
};

8 changes: 4 additions & 4 deletions SolderingPen_ESP32S2/const.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions SolderingPen_ESP32S2/evtloop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 28 additions & 7 deletions SolderingPen_ESP32S2/hid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ void IronHID::switchViSet(viset_evt_t v){
viset = std::make_unique<ViSet_TemperatureSetup>(_btn, _encdr);
break;
};
LOGI(T_HID, printf, "heap:%u\n", ESP.getFreeHeap()/1024);
}

void IronHID::init(){
Expand Down Expand Up @@ -236,14 +237,15 @@ ViSet_MainScreen::ViSet_MainScreen(GPIOButton<ESPEventPolicy> &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(){
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -605,6 +607,21 @@ void ViSet_MainMenu::_submenu_selector(size_t index){
// **************************************
// *** Temperature Control Menu ***

ViSet_TemperatureSetup::ViSet_TemperatureSetup(GPIOButton<ESPEventPolicy> &button, PseudoRotaryEncoder &encoder) : MuiMenu(button, encoder){
// load temperature values from NVS
nvs_blob_read(T_IRON, T_temperatures, static_cast<void*>(&_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<void*>(&_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));
Expand Down Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion SolderingPen_ESP32S2/hid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ESPEventPolicy> &button, PseudoRotaryEncoder &encoder) : MuiMenu(button, encoder) { _buildMenu(); }
ViSet_TemperatureSetup(GPIOButton<ESPEventPolicy> &button, PseudoRotaryEncoder &encoder);
// d-tor
~ViSet_TemperatureSetup();

};

Expand Down
18 changes: 17 additions & 1 deletion SolderingPen_ESP32S2/ironcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ void IronController::init(){
// load temperature values from NVS
nvs_blob_read(T_IRON, T_temperatures, static_cast<void*>(&_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",
Expand Down Expand Up @@ -218,13 +222,25 @@ void IronController::_evt_commands(esp_event_base_t base, int32_t id, void* data
int32_t t = *reinterpret_cast<int32_t*>(data);
if (t != _temp.working){
_temp.working = *reinterpret_cast<int32_t*>(data);
nvs_blob_write(T_IRON, T_temperatures, static_cast<void*>(&_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<void*>(&_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<void*>(&_temp), sizeof(Temperatures));

// if we are not saving working temp, then use default one instead
if (!_temp.savewrk)
_temp.working = _temp.deflt;
}
// some
}

Expand Down
4 changes: 3 additions & 1 deletion SolderingPen_ESP32S2/lang/i18n.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
18 changes: 12 additions & 6 deletions SolderingPen_ESP32S2/lang/lang_en_us.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<const char *, D_____SIZE> dictionary = {
static constexpr std::array<const char *, DICT___SIZE> 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,
Expand All @@ -99,7 +105,7 @@ static constexpr std::array<const char *, MENU_MAIN_CFG_SIZE> menu_MainConfigura

// Main Configuration menu items
static constexpr std::array<const char *, MENU_TEMPERATURE_CFG_SIZE> 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,
Expand Down
8 changes: 4 additions & 4 deletions SolderingPen_ESP32S2/nvs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ esp_err_t nvs_blob_read(const char* nvsspace, const char* key, void* blob, size_
std::unique_ptr<nvs::NVSHandle> 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;
}
Expand All @@ -37,13 +37,13 @@ esp_err_t nvs_blob_write(const char* nvsspace, const char* key, void* blob, size
std::unique_ptr<nvs::NVSHandle> 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;
}

0 comments on commit 79d38b2

Please sign in to comment.