This commit is contained in:
proddy
2025-11-20 23:03:06 +01:00
parent 23a660aabb
commit eeba7a3a6b

View File

@@ -452,10 +452,10 @@ void System::get_settings() {
locale_ = settings.locale; locale_ = settings.locale;
developer_mode_ = settings.developer_mode; developer_mode_ = settings.developer_mode;
}); });
}
// Starts up the UART Serial bridge
// Starts up the UART Serial bridge void System::start() {
void System::start() {
#ifndef EMSESP_STANDALONE #ifndef EMSESP_STANDALONE
// disable bluetooth module // disable bluetooth module
// periph_module_disable(PERIPH_BT_MODULE); // periph_module_disable(PERIPH_BT_MODULE);
@@ -499,10 +499,10 @@ void System::get_settings() {
network_init(); // network network_init(); // network
uart_init(); // start UART uart_init(); // start UART
syslog_init(); // start syslog syslog_init(); // start syslog
} }
// button single click // button single click
void System::button_OnClick(PButton & b) { void System::button_OnClick(PButton & b) {
LOG_NOTICE("Button pressed - single click"); LOG_NOTICE("Button pressed - single click");
#if defined(EMSESP_TEST) #if defined(EMSESP_TEST)
@@ -511,10 +511,10 @@ void System::get_settings() {
Test::listDir(LittleFS, "/", 3); Test::listDir(LittleFS, "/", 3);
#endif #endif
#endif #endif
} }
// button double click // button double click
void System::button_OnDblClick(PButton & b) { void System::button_OnDblClick(PButton & b) {
LOG_NOTICE("Button pressed - double click - wifi reconnect to AP"); LOG_NOTICE("Button pressed - double click - wifi reconnect to AP");
// set AP mode to always so will join AP if wifi ssid fails to connect // set AP mode to always so will join AP if wifi ssid fails to connect
EMSESP::esp32React.getAPSettingsService()->update([&](APSettings & apSettings) { EMSESP::esp32React.getAPSettingsService()->update([&](APSettings & apSettings) {
@@ -527,24 +527,24 @@ void System::get_settings() {
return StateUpdateResult::CHANGED; return StateUpdateResult::CHANGED;
}); });
EMSESP::esp32React.getNetworkSettingsService()->callUpdateHandlers(); // in case we've changed ssid or password EMSESP::esp32React.getNetworkSettingsService()->callUpdateHandlers(); // in case we've changed ssid or password
} }
// button long press // button long press
void System::button_OnLongPress(PButton & b) { void System::button_OnLongPress(PButton & b) {
LOG_NOTICE("Button pressed - long press - perform factory reset"); LOG_NOTICE("Button pressed - long press - perform factory reset");
#ifndef EMSESP_STANDALONE #ifndef EMSESP_STANDALONE
System::command_format(nullptr, 0); System::command_format(nullptr, 0);
#endif #endif
} }
// button indefinite press - do nothing for now // button indefinite press - do nothing for now
void System::button_OnVLongPress(PButton & b) { void System::button_OnVLongPress(PButton & b) {
LOG_NOTICE("Button pressed - very long press - restart from factory/boot partition"); LOG_NOTICE("Button pressed - very long press - restart from factory/boot partition");
EMSESP::system_.system_restart("boot"); EMSESP::system_.system_restart("boot");
} }
// push button // push button
void System::button_init() { void System::button_init() {
#ifndef EMSESP_STANDALONE #ifndef EMSESP_STANDALONE
if (!myPButton_.init(pbutton_gpio_, HIGH)) { if (!myPButton_.init(pbutton_gpio_, HIGH)) {
LOG_WARNING("Multi-functional button not detected"); LOG_WARNING("Multi-functional button not detected");
@@ -557,10 +557,10 @@ void System::get_settings() {
myPButton_.onLongPress(BUTTON_LongPressDelay, button_OnLongPress); myPButton_.onLongPress(BUTTON_LongPressDelay, button_OnLongPress);
myPButton_.onVLongPress(BUTTON_VLongPressDelay, button_OnVLongPress); myPButton_.onVLongPress(BUTTON_VLongPressDelay, button_OnVLongPress);
#endif #endif
} }
// set the LED to on or off when in normal operating mode // set the LED to on or off when in normal operating mode
void System::led_init() { void System::led_init() {
// disabled old led port before setting new one // disabled old led port before setting new one
#if ESP_ARDUINO_VERSION_MAJOR < 3 #if ESP_ARDUINO_VERSION_MAJOR < 3
led_type_ ? neopixelWrite(led_gpio_, 0, 0, 0) : digitalWrite(led_gpio_, !LED_ON); led_type_ ? neopixelWrite(led_gpio_, 0, 0, 0) : digitalWrite(led_gpio_, !LED_ON);
@@ -583,19 +583,19 @@ void System::get_settings() {
} else { } else {
LOG_INFO("LED disabled"); LOG_INFO("LED disabled");
} }
} }
void System::uart_init() { void System::uart_init() {
EMSuart::stop(); EMSuart::stop();
// start UART, GPIOs have already been checked // start UART, GPIOs have already been checked
EMSuart::start(tx_mode_, rx_gpio_, tx_gpio_); EMSuart::start(tx_mode_, rx_gpio_, tx_gpio_);
EMSESP::txservice_.start(); // reset counters and send devices request EMSESP::txservice_.start(); // reset counters and send devices request
} }
// checks system health and handles LED flashing wizardry // checks system health and handles LED flashing wizardry
void System::loop() { void System::loop() {
// check if we're supposed to do a reset/restart // check if we're supposed to do a reset/restart
if (systemStatus() == SYSTEM_STATUS::SYSTEM_STATUS_RESTART_REQUESTED) { if (systemStatus() == SYSTEM_STATUS::SYSTEM_STATUS_RESTART_REQUESTED) {
system_restart(); system_restart();
@@ -613,11 +613,11 @@ void System::get_settings() {
system_check(); // check system health system_check(); // check system health
send_info_mqtt(); send_info_mqtt();
#endif #endif
} }
// send MQTT info topic appended with the version information as JSON, as a retained flag // send MQTT info topic appended with the version information as JSON, as a retained flag
// this is only done once when the connection is established // this is only done once when the connection is established
void System::send_info_mqtt() { void System::send_info_mqtt() {
static uint8_t _connection = 0; static uint8_t _connection = 0;
uint8_t connection = (ethernet_connected() ? 1 : 0) + ((WiFi.status() == WL_CONNECTED) ? 2 : 0) + (ntp_connected_ ? 4 : 0) + (has_ipv6_ ? 8 : 0); uint8_t connection = (ethernet_connected() ? 1 : 0) + ((WiFi.status() == WL_CONNECTED) ? 2 : 0) + (ntp_connected_ ? 4 : 0) + (has_ipv6_ ? 8 : 0);
// check if connection status has changed // check if connection status has changed
@@ -675,10 +675,10 @@ void System::get_settings() {
} }
#endif #endif
Mqtt::queue_publish_retain(F_(info), doc.as<JsonObject>()); // topic called "info" and it's Retained Mqtt::queue_publish_retain(F_(info), doc.as<JsonObject>()); // topic called "info" and it's Retained
} }
// create the json for heartbeat // create the json for heartbeat
void System::heartbeat_json(JsonObject output) { void System::heartbeat_json(JsonObject output) {
switch (EMSESP::bus_status()) { switch (EMSESP::bus_status()) {
case EMSESP::BUS_STATUS_OFFLINE: case EMSESP::BUS_STATUS_OFFLINE:
output["bus_status"] = "connecting"; // EMS-ESP is booting... output["bus_status"] = "connecting"; // EMS-ESP is booting...
@@ -732,10 +732,10 @@ void System::get_settings() {
output["wifireconnects"] = EMSESP::esp32React.getWifiReconnects(); output["wifireconnects"] = EMSESP::esp32React.getWifiReconnects();
} }
#endif #endif
} }
// send periodic MQTT message with system information // send periodic MQTT message with system information
void System::send_heartbeat() { void System::send_heartbeat() {
// don't send heartbeat if WiFi or MQTT is not connected // don't send heartbeat if WiFi or MQTT is not connected
if (!Mqtt::connected()) { if (!Mqtt::connected()) {
return; return;
@@ -748,10 +748,10 @@ void System::get_settings() {
heartbeat_json(json); heartbeat_json(json);
Mqtt::queue_publish(F_(heartbeat), json); // send to MQTT with retain off. This will add to MQTT queue. Mqtt::queue_publish(F_(heartbeat), json); // send to MQTT with retain off. This will add to MQTT queue.
} }
// initializes network // initializes network
void System::network_init() { void System::network_init() {
last_system_check_ = 0; // force the LED to go from fast flash to pulse last_system_check_ = 0; // force the LED to go from fast flash to pulse
#if CONFIG_IDF_TARGET_ESP32 #if CONFIG_IDF_TARGET_ESP32
@@ -790,10 +790,10 @@ void System::get_settings() {
eth_present_ = ETH.begin(type, phy_addr, mdc, mdio, power, clock_mode); eth_present_ = ETH.begin(type, phy_addr, mdc, mdio, power, clock_mode);
#endif #endif
#endif #endif
} }
// check health of system, done every 5 seconds // check health of system, done every 5 seconds
void System::system_check() { void System::system_check() {
if (!last_system_check_ || ((uint32_t)(uuid::get_uptime() - last_system_check_) >= SYSTEM_CHECK_FREQUENCY)) { if (!last_system_check_ || ((uint32_t)(uuid::get_uptime() - last_system_check_) >= SYSTEM_CHECK_FREQUENCY)) {
last_system_check_ = uuid::get_uptime(); last_system_check_ = uuid::get_uptime();
@@ -855,11 +855,11 @@ void System::get_settings() {
} }
} }
} }
} }
// commands - takes static function pointers // commands - takes static function pointers
// can be called via Console using 'call system <cmd>' // can be called via Console using 'call system <cmd>'
void System::commands_init() { void System::commands_init() {
Command::add(EMSdevice::DeviceType::SYSTEM, F_(read), System::command_read, FL_(read_cmd), CommandFlag::ADMIN_ONLY); Command::add(EMSdevice::DeviceType::SYSTEM, F_(read), System::command_read, FL_(read_cmd), CommandFlag::ADMIN_ONLY);
Command::add(EMSdevice::DeviceType::SYSTEM, F_(send), System::command_send, FL_(send_cmd), CommandFlag::ADMIN_ONLY); Command::add(EMSdevice::DeviceType::SYSTEM, F_(send), System::command_send, FL_(send_cmd), CommandFlag::ADMIN_ONLY);
Command::add(EMSdevice::DeviceType::SYSTEM, F_(fetch), System::command_fetch, FL_(fetch_cmd), CommandFlag::ADMIN_ONLY); Command::add(EMSdevice::DeviceType::SYSTEM, F_(fetch), System::command_fetch, FL_(fetch_cmd), CommandFlag::ADMIN_ONLY);
@@ -876,13 +876,13 @@ void System::get_settings() {
// MQTT subscribe "ems-esp/system/#" // MQTT subscribe "ems-esp/system/#"
Mqtt::subscribe(EMSdevice::DeviceType::SYSTEM, "system/#", nullptr); // use empty function callback Mqtt::subscribe(EMSdevice::DeviceType::SYSTEM, "system/#", nullptr); // use empty function callback
} }
// uses LED to show system health // uses LED to show system health
// 1 x flash = the EMS bus is not connected // 1 x flash = the EMS bus is not connected
// 2 x flash = the network (wifi or ethernet) is not connected // 2 x flash = the network (wifi or ethernet) is not connected
// 3 x flash = both EMS bus and network are failing. This is a critical error! // 3 x flash = both EMS bus and network are failing. This is a critical error!
void System::led_monitor() { void System::led_monitor() {
// we only need to run the LED healthcheck if there are errors // we only need to run the LED healthcheck if there are errors
if (!healthcheck_ || !led_gpio_) { if (!healthcheck_ || !led_gpio_) {
return; // all good return; // all good
@@ -982,14 +982,14 @@ void System::get_settings() {
} }
} }
} }
} }
// Return the quality (Received Signal Strength Indicator) of the WiFi network as a % // Return the quality (Received Signal Strength Indicator) of the WiFi network as a %
// High quality: 90% ~= -55dBm // High quality: 90% ~= -55dBm
// Medium quality: 50% ~= -75dBm // Medium quality: 50% ~= -75dBm
// Low quality: 30% ~= -85dBm // Low quality: 30% ~= -85dBm
// Unusable quality: 8% ~= -96dBm // Unusable quality: 8% ~= -96dBm
int8_t System::wifi_quality(int8_t dBm) { int8_t System::wifi_quality(int8_t dBm) {
if (dBm <= -100) { if (dBm <= -100) {
return 0; return 0;
} }
@@ -998,10 +998,10 @@ void System::get_settings() {
return 100; return 100;
} }
return 2 * (dBm + 100); return 2 * (dBm + 100);
} }
// print users to console // print users to console
void System::show_users(uuid::console::Shell & shell) { void System::show_users(uuid::console::Shell & shell) {
shell.printfln("Users:"); shell.printfln("Users:");
#ifndef EMSESP_STANDALONE #ifndef EMSESP_STANDALONE
@@ -1013,10 +1013,10 @@ void System::get_settings() {
#endif #endif
shell.println(); shell.println();
} }
// shell command 'show system' // shell command 'show system'
void System::show_system(uuid::console::Shell & shell) { void System::show_system(uuid::console::Shell & shell) {
refreshHeapMem(); // refresh free heap and max alloc heap refreshHeapMem(); // refresh free heap and max alloc heap
shell.println(); shell.println();
@@ -1153,11 +1153,11 @@ void System::get_settings() {
shell.println(); shell.println();
#endif #endif
} }
// see if there is a restore of an older settings file that needs to be applied // see if there is a restore of an older settings file that needs to be applied
// note there can be only one file at a time // note there can be only one file at a time
bool System::check_restore() { bool System::check_restore() {
bool reboot_required = false; // true if we need to reboot bool reboot_required = false; // true if we need to reboot
#ifndef EMSESP_STANDALONE #ifndef EMSESP_STANDALONE
@@ -1209,12 +1209,12 @@ void System::get_settings() {
#endif #endif
return reboot_required; return reboot_required;
} }
// handle upgrades from previous versions // handle upgrades from previous versions
// this function will not be called on a clean install, with no settings files yet created // this function will not be called on a clean install, with no settings files yet created
// returns true if we need a reboot // returns true if we need a reboot
bool System::check_upgrade(bool factory_settings) { bool System::check_upgrade(bool factory_settings) {
bool missing_version = true; bool missing_version = true;
std::string settingsVersion; std::string settingsVersion;
@@ -1334,10 +1334,10 @@ void System::get_settings() {
} }
return false; return false;
} }
// convert settings file into json object // convert settings file into json object
void System::extractSettings(const char * filename, const char * section, JsonObject output) { void System::extractSettings(const char * filename, const char * section, JsonObject output) {
#ifndef EMSESP_STANDALONE #ifndef EMSESP_STANDALONE
File settingsFile = LittleFS.open(filename); File settingsFile = LittleFS.open(filename);
if (settingsFile) { if (settingsFile) {
@@ -1353,10 +1353,10 @@ void System::get_settings() {
} }
settingsFile.close(); settingsFile.close();
#endif #endif
} }
// save settings file using input from a json object // save settings file using input from a json object
bool System::saveSettings(const char * filename, const char * section, JsonObject input) { bool System::saveSettings(const char * filename, const char * section, JsonObject input) {
#ifndef EMSESP_STANDALONE #ifndef EMSESP_STANDALONE
JsonObject section_json = input[section]; JsonObject section_json = input[section];
if (section_json) { if (section_json) {
@@ -1370,10 +1370,10 @@ void System::get_settings() {
} }
#endif #endif
return false; // not found return false; // not found
} }
// set a entity of services 'network', 'settings', 'mqtt', etc. // set a entity of services 'network', 'settings', 'mqtt', etc.
bool System::command_service(const char * cmd, const char * value) { bool System::command_service(const char * cmd, const char * value) {
bool ok = false; bool ok = false;
bool b; bool b;
if (Helpers::value2bool(value, b)) { if (Helpers::value2bool(value, b)) {
@@ -1456,10 +1456,10 @@ void System::get_settings() {
LOG_INFO("System command '%s' with value '%s'", cmd, value); LOG_INFO("System command '%s' with value '%s'", cmd, value);
} }
return ok; return ok;
} }
// return back a system value // return back a system value
bool System::get_value_info(JsonObject output, const char * cmd) { bool System::get_value_info(JsonObject output, const char * cmd) {
if (cmd == nullptr || strlen(cmd) == 0) { if (cmd == nullptr || strlen(cmd) == 0) {
LOG_ERROR("empty system command"); LOG_ERROR("empty system command");
return false; return false;
@@ -1531,9 +1531,9 @@ void System::get_settings() {
} }
} }
return false; return false;
} }
void System::get_value_json(JsonObject output, const std::string & circuit, const std::string & name, JsonVariant val) { void System::get_value_json(JsonObject output, const std::string & circuit, const std::string & name, JsonVariant val) {
output["name"] = name; output["name"] = name;
if (circuit.length()) { if (circuit.length()) {
output["circuit"] = circuit; output["circuit"] = circuit;
@@ -1551,11 +1551,11 @@ void System::get_settings() {
output["value"] = val.as<std::string>(); output["value"] = val.as<std::string>();
output["type"] = "string"; output["type"] = "string";
} }
} }
// export status information including the device information // export status information including the device information
// http://ems-esp/api/system/info // http://ems-esp/api/system/info
bool System::command_info(const char * value, const int8_t id, JsonObject output) { bool System::command_info(const char * value, const int8_t id, JsonObject output) {
JsonObject node; JsonObject node;
// System // System
@@ -1889,30 +1889,30 @@ void System::get_settings() {
} }
return true; // this function always returns true! return true; // this function always returns true!
} }
#if defined(EMSESP_TEST) #if defined(EMSESP_TEST)
// run a test, e.g. http://ems-esp/api?device=system&cmd=test&data=boiler // run a test, e.g. http://ems-esp/api?device=system&cmd=test&data=boiler
bool System::command_test(const char * value, const int8_t id) { bool System::command_test(const char * value, const int8_t id) {
if (value) { if (value) {
return Test::test(value, id); return Test::test(value, id);
} else { } else {
return false; return false;
} }
} }
#endif #endif
// takes a board profile and populates a data array with GPIO configurations // takes a board profile and populates a data array with GPIO configurations
// returns false if profile is unknown // returns false if profile is unknown
// //
// data = led, dallas, rx, tx, button, phy_type, eth_power, eth_phy_addr, eth_clock_mode, led_type // data = led, dallas, rx, tx, button, phy_type, eth_power, eth_phy_addr, eth_clock_mode, led_type
// //
// clock modes: // clock modes:
// 0 = RMII clock input to GPIO0 // 0 = RMII clock input to GPIO0
// 1 = RMII clock output from GPIO0 // 1 = RMII clock output from GPIO0
// 2 = RMII clock output from GPIO16 // 2 = RMII clock output from GPIO16
// 3 = RMII clock output from GPIO17, for 50hz inverted clock // 3 = RMII clock output from GPIO17, for 50hz inverted clock
bool System::load_board_profile(std::vector<int8_t> & data, const std::string & board_profile) { bool System::load_board_profile(std::vector<int8_t> & data, const std::string & board_profile) {
if (board_profile == "S32") { if (board_profile == "S32") {
data = {2, 18, 23, 5, 0, PHY_type::PHY_TYPE_NONE, 0, 0, 0, 0}; // BBQKees Gateway S32 data = {2, 18, 23, 5, 0, PHY_type::PHY_TYPE_NONE, 0, 0, 0, 0}; // BBQKees Gateway S32
} else if (board_profile == "E32") { } else if (board_profile == "E32") {
@@ -1962,10 +1962,10 @@ void System::get_settings() {
// LOG_DEBUG("Found data for board profile %s", board_profile.c_str()); // LOG_DEBUG("Found data for board profile %s", board_profile.c_str());
return true; return true;
} }
// format command - factory reset, removing all config files // format command - factory reset, removing all config files
bool System::command_format(const char * value, const int8_t id) { bool System::command_format(const char * value, const int8_t id) {
LOG_INFO("Formatting FS, removing all config files"); LOG_INFO("Formatting FS, removing all config files");
#ifndef EMSESP_STANDALONE #ifndef EMSESP_STANDALONE
if (LittleFS.format()) { if (LittleFS.format()) {
@@ -1978,10 +1978,10 @@ void System::get_settings() {
// restart will be handled by the main loop // restart will be handled by the main loop
EMSESP::system_.systemStatus(SYSTEM_STATUS::SYSTEM_STATUS_RESTART_REQUESTED); EMSESP::system_.systemStatus(SYSTEM_STATUS::SYSTEM_STATUS_RESTART_REQUESTED);
return true; return true;
} }
// restart command - perform a hard reset (system reboot) // restart command - perform a hard reset (system reboot)
bool System::command_restart(const char * value, const int8_t id) { bool System::command_restart(const char * value, const int8_t id) {
if (id == 0) { if (id == 0) {
// if it has an id then it's a web call and we need to queue the restart // if it has an id then it's a web call and we need to queue the restart
// default id is -1 when calling /api/system/restart directly for example // default id is -1 when calling /api/system/restart directly for example
@@ -1994,12 +1994,12 @@ void System::get_settings() {
// restart will be handled by the main loop // restart will be handled by the main loop
EMSESP::system_.systemStatus(SYSTEM_STATUS::SYSTEM_STATUS_RESTART_REQUESTED); EMSESP::system_.systemStatus(SYSTEM_STATUS::SYSTEM_STATUS_RESTART_REQUESTED);
return true; return true;
} }
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch" #pragma GCC diagnostic ignored "-Wswitch"
std::string System::reset_reason(uint8_t cpu) const { std::string System::reset_reason(uint8_t cpu) const {
#ifndef EMSESP_STANDALONE #ifndef EMSESP_STANDALONE
switch (rtc_get_reset_reason(cpu)) { switch (rtc_get_reset_reason(cpu)) {
case 1: case 1:
@@ -2038,11 +2038,11 @@ void System::get_settings() {
} }
#endif #endif
return "Unknown"; return "Unknown";
} }
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
// set NTP status // set NTP status
void System::ntp_connected(bool b) { void System::ntp_connected(bool b) {
if (b != ntp_connected_) { if (b != ntp_connected_) {
if (b) { if (b) {
LOG_INFO("NTP connected"); LOG_INFO("NTP connected");
@@ -2053,20 +2053,20 @@ void System::get_settings() {
ntp_connected_ = b; ntp_connected_ = b;
ntp_last_check_ = b ? uuid::get_uptime_sec() : 0; ntp_last_check_ = b ? uuid::get_uptime_sec() : 0;
} }
// get NTP status // get NTP status
bool System::ntp_connected() { bool System::ntp_connected() {
// timeout 2 hours, ntp sync is normally every hour. // timeout 2 hours, ntp sync is normally every hour.
if ((uuid::get_uptime_sec() - ntp_last_check_ > 7201) && ntp_connected_) { if ((uuid::get_uptime_sec() - ntp_last_check_ > 7201) && ntp_connected_) {
ntp_connected(false); ntp_connected(false);
} }
return ntp_connected_; return ntp_connected_;
} }
// see if its a BBQKees Gateway by checking the nvs values // see if its a BBQKees Gateway by checking the nvs values
String System::getBBQKeesGatewayDetails(uint8_t detail) { String System::getBBQKeesGatewayDetails(uint8_t detail) {
#ifndef EMSESP_STANDALONE #ifndef EMSESP_STANDALONE
/* /*
if (!EMSESP::nvs_.isKey("mfg")) { if (!EMSESP::nvs_.isKey("mfg")) {
@@ -2128,13 +2128,13 @@ void System::get_settings() {
#else #else
return ""; return "";
#endif #endif
} }
// Stream from an URL and send straight to OTA uploader service. // Stream from an URL and send straight to OTA uploader service.
// //
// This function needs to be called twice, 1st pass once with a url to persist it, 2nd pass with no arguments to start the upload // This function needs to be called twice, 1st pass once with a url to persist it, 2nd pass with no arguments to start the upload
// This is to avoid timeouts in callback functions, like calling from a web hook. // This is to avoid timeouts in callback functions, like calling from a web hook.
bool System::uploadFirmwareURL(const char * url) { bool System::uploadFirmwareURL(const char * url) {
#ifndef EMSESP_STANDALONE #ifndef EMSESP_STANDALONE
static String saved_url; static String saved_url;
@@ -2219,11 +2219,11 @@ void System::get_settings() {
#endif #endif
return true; // OK return true; // OK
} }
// read command, e.g. read <deviceID> <type ID> [offset] [length] from console or API // read command, e.g. read <deviceID> <type ID> [offset] [length] from console or API
// from Console use quotes so: call system read "<deviceID> <type ID> [offset] [length]" // from Console use quotes so: call system read "<deviceID> <type ID> [offset] [length]"
bool System::readCommand(const char * data) { bool System::readCommand(const char * data) {
if (!data) { if (!data) {
return false; return false;
} }
@@ -2275,25 +2275,25 @@ void System::get_settings() {
EMSESP::set_read_id(type_id); EMSESP::set_read_id(type_id);
return true; return true;
} }
// system read command // system read command
bool System::command_read(const char * value, const int8_t id) { bool System::command_read(const char * value, const int8_t id) {
return readCommand(value); return readCommand(value);
} }
// set the system status code - SYSTEM_STATUS in system.h // set the system status code - SYSTEM_STATUS in system.h
void System::systemStatus(uint8_t status_code) { void System::systemStatus(uint8_t status_code) {
systemStatus_ = status_code; systemStatus_ = status_code;
// LOG_DEBUG("Setting System status code %d", status_code); // LOG_DEBUG("Setting System status code %d", status_code);
} }
uint8_t System::systemStatus() { uint8_t System::systemStatus() {
return systemStatus_; return systemStatus_;
} }
// takes a string range like "6-11, 1, 23, 24-48" which has optional ranges and single values and converts to a vector of ints // takes a string range like "6-11, 1, 23, 24-48" which has optional ranges and single values and converts to a vector of ints
std::vector<uint8_t> System::string_range_to_vector(const std::string & range) { std::vector<uint8_t> System::string_range_to_vector(const std::string & range) {
std::vector<uint8_t> gpios; std::vector<uint8_t> gpios;
std::string::size_type pos = 0; std::string::size_type pos = 0;
std::string::size_type prev = 0; std::string::size_type prev = 0;
@@ -2326,14 +2326,14 @@ void System::get_settings() {
process_part(range.substr(prev)); process_part(range.substr(prev));
return gpios; return gpios;
} }
// initialize a list of valid GPIOs based on the ESP32 board // initialize a list of valid GPIOs based on the ESP32 board
// notes: // notes:
// - we allow 0, which is used on some board for the button // - we allow 0, which is used on some board for the button
// - we also allow input only pins are accepted (34-39) on some boards // - we also allow input only pins are accepted (34-39) on some boards
// - and allow pins 33-38 for octal SPI for 32M vchip version on some boards // - and allow pins 33-38 for octal SPI for 32M vchip version on some boards
void System::set_valid_system_gpios(bool exclude_used) { void System::set_valid_system_gpios(bool exclude_used) {
valid_system_gpios_.clear(); // reset system list valid_system_gpios_.clear(); // reset system list
used_gpios_.clear(); // reset used list used_gpios_.clear(); // reset used list
@@ -2357,10 +2357,10 @@ void System::get_settings() {
valid_system_gpios_.erase(std::remove(valid_system_gpios_.begin(), valid_system_gpios_.end(), 17), valid_system_gpios_.end()); valid_system_gpios_.erase(std::remove(valid_system_gpios_.begin(), valid_system_gpios_.end(), 17), valid_system_gpios_.end());
} }
#endif #endif
} }
// check if a pin is valid ESP32 pin and if not already used, add to the used gpio list // check if a pin is valid ESP32 pin and if not already used, add to the used gpio list
bool System::check_valid_gpio(uint8_t pin, const char * source_name) { bool System::check_valid_gpio(uint8_t pin, const char * source_name) {
bool ok = false; bool ok = false;
// check if we're allowed to use this pin // check if we're allowed to use this pin
if (std::find(valid_system_gpios_.begin(), valid_system_gpios_.end(), pin) != valid_system_gpios_.end()) { if (std::find(valid_system_gpios_.begin(), valid_system_gpios_.end(), pin) != valid_system_gpios_.end()) {
@@ -2380,10 +2380,10 @@ void System::get_settings() {
LOG_DEBUG("GPIO %d for %s is not valid", pin, source_name); LOG_DEBUG("GPIO %d for %s is not valid", pin, source_name);
} }
return ok; return ok;
} }
// return a list of valid and unused GPIOs still available for use // return a list of valid and unused GPIOs still available for use
std::vector<uint8_t> System::valid_gpio_list() { std::vector<uint8_t> System::valid_gpio_list() {
std::vector<uint8_t> gpios; std::vector<uint8_t> gpios;
for (const auto & gpio : valid_system_gpios_) { for (const auto & gpio : valid_system_gpios_) {
if (std::find(used_gpios_.begin(), used_gpios_.end(), gpio) == used_gpios_.end()) { if (std::find(used_gpios_.begin(), used_gpios_.end(), gpio) == used_gpios_.end()) {
@@ -2391,7 +2391,7 @@ void System::get_settings() {
} }
} }
return gpios; return gpios;
} }
} // namespace emsesp } // namespace emsesp