From 939debdc0b0ec472d67aecdf2b9adbc5b9b74439 Mon Sep 17 00:00:00 2001 From: proddy Date: Tue, 4 Aug 2020 13:34:16 +0200 Subject: [PATCH] minor changes, added gpio command - #445 --- src/devices/boiler.cpp | 16 ++++++++-------- src/devices/thermostat.cpp | 18 +++++++++--------- src/helpers.cpp | 18 ++++++++++-------- src/helpers.h | 6 +++--- src/locale_EN.h | 6 +++++- src/system.cpp | 16 +++++++++++++++- src/telegram.h | 3 +-- src/test/test.cpp | 15 ++++++++++++++- 8 files changed, 65 insertions(+), 33 deletions(-) diff --git a/src/devices/boiler.cpp b/src/devices/boiler.cpp index 490d77c44..f35769fcc 100644 --- a/src/devices/boiler.cpp +++ b/src/devices/boiler.cpp @@ -666,7 +666,7 @@ void Boiler::process_UBAMaintenanceData(std::shared_ptr telegram // Set the warm water temperature 0x33 void Boiler::set_warmwater_temp(const char * value, const int8_t id) { - uint8_t v = 0; + int v = 0; if (!Helpers::value2number(value, v)) { return; } @@ -678,7 +678,7 @@ void Boiler::set_warmwater_temp(const char * value, const int8_t id) { // flow temp void Boiler::set_flow_temp(const char * value, const int8_t id) { - uint8_t v = 0; + int v = 0; if (!Helpers::value2number(value, v)) { return; } @@ -689,7 +689,7 @@ void Boiler::set_flow_temp(const char * value, const int8_t id) { // set min boiler output void Boiler::set_min_power(const char * value, const int8_t id) { - uint8_t v = 0; + int v = 0; if (!Helpers::value2number(value, v)) { return; } @@ -699,7 +699,7 @@ void Boiler::set_min_power(const char * value, const int8_t id) { // set max temp void Boiler::set_max_power(const char * value, const int8_t id) { - uint8_t v = 0; + int v = 0; if (!Helpers::value2number(value, v)) { return; } @@ -710,7 +710,7 @@ void Boiler::set_max_power(const char * value, const int8_t id) { // set oiler on hysteresis void Boiler::set_hyst_on(const char * value, const int8_t id) { - uint8_t v = 0; + int v = 0; if (!Helpers::value2number(value, v)) { return; } @@ -721,7 +721,7 @@ void Boiler::set_hyst_on(const char * value, const int8_t id) { // set boiler off hysteresis void Boiler::set_hyst_off(const char * value, const int8_t id) { - uint8_t v = 0; + int v = 0; if (!Helpers::value2number(value, v)) { return; } @@ -732,7 +732,7 @@ void Boiler::set_hyst_off(const char * value, const int8_t id) { // set min burner period void Boiler::set_burn_period(const char * value, const int8_t id) { - uint8_t v = 0; + int v = 0; if (!Helpers::value2number(value, v)) { return; } @@ -743,7 +743,7 @@ void Boiler::set_burn_period(const char * value, const int8_t id) { // set pump delay void Boiler::set_pump_delay(const char * value, const int8_t id) { - uint8_t v = 0; + int v = 0; if (!Helpers::value2number(value, v)) { return; } diff --git a/src/devices/thermostat.cpp b/src/devices/thermostat.cpp index eedcf4568..3ee250c79 100644 --- a/src/devices/thermostat.cpp +++ b/src/devices/thermostat.cpp @@ -1154,7 +1154,7 @@ void Thermostat::console_commands(Shell & shell, unsigned int context) { // 0xA5 - Set minimum external temperature void Thermostat::set_settings_minexttemp(const char * value, const int8_t id) { - int8_t mt = 0; + int mt = 0; if (!Helpers::value2number(value, mt)) { return; } @@ -1166,7 +1166,7 @@ void Thermostat::set_settings_minexttemp(const char * value, const int8_t id) { // 0xA5 - Clock offset void Thermostat::set_settings_clockoffset(const char * value, const int8_t id) { - int8_t co = 0; + int co = 0; if (!Helpers::value2number(value, co)) { return; } @@ -1178,7 +1178,7 @@ void Thermostat::set_settings_clockoffset(const char * value, const int8_t id) { // 0xA5 - Calibrate internal temperature void Thermostat::set_settings_calinttemp(const char * value, const int8_t id) { - int8_t ct = 0; + int ct = 0; if (!Helpers::value2number(value, ct)) { return; } @@ -1192,7 +1192,7 @@ void Thermostat::set_settings_calinttemp(const char * value, const int8_t id) { // 0xA5 - Set the display settings void Thermostat::set_settings_display(const char * value, const int8_t id) { - uint8_t ds = 0; + int ds = 0; if (!Helpers::value2number(value, ds)) { return; } @@ -1243,7 +1243,7 @@ void Thermostat::set_settings_building(const char * value, const int8_t id) { // 0xA5 Set the language settings void Thermostat::set_settings_language(const char * value, const int8_t id) { - uint8_t lg = 0; + int lg = 0; if (!Helpers::value2number(value, lg)) { return; } @@ -1255,7 +1255,7 @@ void Thermostat::set_settings_language(const char * value, const int8_t id) { // Set the control-mode for hc 0-off, 1-RC20, 2-RC3x void Thermostat::set_control(const char * value, const int8_t id) { - uint8_t ctrl = 0; + int ctrl = 0; if (!Helpers::value2number(value, ctrl)) { return; } @@ -1335,7 +1335,7 @@ void Thermostat::set_holiday(const char * value, const int8_t id) { // set pause in hours void Thermostat::set_pause(const char * value, const int8_t id) { - uint8_t hrs = 0; + int hrs = 0; if (!Helpers::value2number(value, hrs)) { return; } @@ -1356,7 +1356,7 @@ void Thermostat::set_pause(const char * value, const int8_t id) { // set partymode in hours void Thermostat::set_party(const char * value, const int8_t id) { - uint8_t hrs = 0; + int hrs = 0; if (!Helpers::value2number(value, hrs)) { return; } @@ -1747,7 +1747,7 @@ void Thermostat::thermostat_cmd_mode(const char * message) { } void Thermostat::set_temperature_value(const char * value, const uint8_t id, const uint8_t mode) { - float f = 0; + float f = 0; uint8_t hc_num = (id == -1) ? DEFAULT_HEATING_CIRCUIT : id; if (Helpers::value2float(value, f)) { set_temperature(f, mode, hc_num); diff --git a/src/helpers.cpp b/src/helpers.cpp index 4c145dab0..1319bc0a5 100644 --- a/src/helpers.cpp +++ b/src/helpers.cpp @@ -354,7 +354,7 @@ bool Helpers::hasValue(const uint32_t v) { } // checks if we can convert a char string to an int value -bool Helpers::value2number(const char * v, int value) { +bool Helpers::value2number(const char * v, int & value) { if ((v == nullptr) || (strlen(v) == 0)) { value = 0; return false; @@ -364,7 +364,7 @@ bool Helpers::value2number(const char * v, int value) { } // checks if we can convert a char string to a float value -bool Helpers::value2float(const char * v, float value) { +bool Helpers::value2float(const char * v, float & value) { if ((v == nullptr) || (strlen(v) == 0)) { value = 0; return false; @@ -392,22 +392,24 @@ bool Helpers::value2string(const char * v, std::string & value) { // checks to see if a string (usually a command or payload cmd) looks like a boolean // on, off, true, false, 1, 0 -bool Helpers::value2bool(const char * v, uint8_t value) { +bool Helpers::value2bool(const char * v, bool & value) { if ((v == nullptr) || (strlen(v) == 0)) { return false; } - if ((strncmp(v, "on", 2) == 0) || (strncmp(v, "1", 1) == 0) || (strncmp(v, "true", 4) == 0)) { + std::string bool_str = toLower(v); // convert to lower case + + if ((bool_str == "on") || (bool_str == "1") or (bool_str == "true")) { value = true; - return true; + return true; // is a bool } - if ((strncmp(v, "off", 3) == 0) || (strncmp(v, "0", 1) == 0) || (strncmp(v, "false", 5) == 0)) { + if ((bool_str == "off") || (bool_str == "0") or (bool_str == "false")) { value = false; - return true; + return true; // is a bool } - return false; + return false; // not a bool } } // namespace emsesp diff --git a/src/helpers.h b/src/helpers.h index 7ef33be3f..963c2735f 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -58,9 +58,9 @@ class Helpers { static std::string toLower(std::string const & s); - static bool value2number(const char * v, int value); - static bool value2float(const char * v, float value); - static bool value2bool(const char * v, uint8_t value); + static bool value2number(const char * v, int & value); + static bool value2float(const char * v, float & value); + static bool value2bool(const char * v, bool & value); static bool value2string(const char * v, std::string & value); }; diff --git a/src/locale_EN.h b/src/locale_EN.h index f75454798..13b29c833 100644 --- a/src/locale_EN.h +++ b/src/locale_EN.h @@ -62,6 +62,9 @@ MAKE_PSTR_WORD(heartbeat) MAKE_PSTR_WORD(users) MAKE_PSTR_WORD(master) MAKE_PSTR_WORD(test) +MAKE_PSTR_WORD(gpio) + +// for commands MAKE_PSTR_WORD(call) // devices @@ -81,7 +84,7 @@ MAKE_PSTR_WORD(sensors) MAKE_PSTR(kwh, "kWh") MAKE_PSTR(wh, "Wh") MAKE_PSTR(hc_optional, "[heating circuit]") -MAKE_PSTR(master_thermostat_fmt, "Master Thermostat device ID = %s") +MAKE_PSTR(master_thermostat_fmt, "Master Thermostat Device ID = %s") MAKE_PSTR(host_fmt, "Host = %s") MAKE_PSTR(hostname_fmt, "WiFi Hostname = %s") MAKE_PSTR(mark_interval_fmt, "Mark interval = %lus"); @@ -101,6 +104,7 @@ MAKE_PSTR(degrees, "°C") MAKE_PSTR(asterisks, "********") MAKE_PSTR(n_mandatory, "") MAKE_PSTR(n_optional, "[n]") +MAKE_PSTR(pin_mandatory, "") MAKE_PSTR(data_optional, "[data]") MAKE_PSTR(typeid_mandatory, "") MAKE_PSTR(deviceid_mandatory, "") diff --git a/src/system.cpp b/src/system.cpp index a99188a07..efeb4a478 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -42,7 +42,7 @@ void System::mqtt_command_gpio(const char * value, const int8_t id) { #elif defined(ESP32) const uint8_t pins[] = {26, 22, 21, 17}; #else - const uint8_t pins[] = {0, 1, 2, 3}; + const uint8_t pins[] = {11, 12, 13, 14}; #endif bool v = false; if (Helpers::value2bool(value, v)) { @@ -557,6 +557,20 @@ void System::console_commands(Shell & shell, unsigned int context) { flash_string_vector{F_(show), F_(users)}, [](Shell & shell, const std::vector & arguments __attribute__((unused))) { System::show_users(shell); }); + EMSESPShell::commands->add_command(ShellContext::SYSTEM, + CommandFlags::ADMIN, + flash_string_vector{F_(gpio)}, + flash_string_vector{F_(pin_mandatory), F_(data_optional)}, + [](Shell & shell, const std::vector & arguments) { + if (arguments.size() == 1) { + shell.printfln(F("use on/off, 1/0 or true/false")); + return; + } + int pin = 0; + if (Helpers::value2number(arguments[0].c_str(), pin)) { + System::mqtt_command_gpio(arguments[1].c_str(), pin); + } + }); // enter the context Console::enter_custom_context(shell, context); diff --git a/src/telegram.h b/src/telegram.h index aaf1580db..40fd136e5 100644 --- a/src/telegram.h +++ b/src/telegram.h @@ -112,8 +112,7 @@ class Telegram { value = 0; for (uint8_t i = 0; i < size; i++) { - // shift - value = (value << 8) + message_data[abs_index + i]; + value = (value << 8) + message_data[abs_index + i]; // shift } } diff --git a/src/test/test.cpp b/src/test/test.cpp index f12793f58..c5ad790ed 100644 --- a/src/test/test.cpp +++ b/src/test/test.cpp @@ -28,7 +28,7 @@ namespace emsesp { // used with the 'test' command, under su/admin void Test::run_test(uuid::console::Shell & shell, const std::string & command) { if (command == "default") { - run_test(shell, "mqtt"); // add the default test case here + run_test(shell, "gpio"); // add the default test case here } if (command.empty()) { @@ -480,6 +480,19 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & command) { EMSESP::txservice_.flush_tx_queue(); } + if (command == "gpio") { + shell.printfln(F("Testing gpio...")); + + EMSESP::add_context_menus(); // need to add this as it happens later in the code + shell.invoke_command("su"); + shell.invoke_command("system"); + shell.invoke_command("help"); + shell.invoke_command("gpio"); + shell.invoke_command("gpio 1 true"); + + shell.loop_all(); + } + if (command == "mqtt") { shell.printfln(F("Testing MQTT..."));