diff --git a/src/core/analogsensor.cpp b/src/core/analogsensor.cpp index d62701ded..74532a876 100644 --- a/src/core/analogsensor.cpp +++ b/src/core/analogsensor.cpp @@ -576,7 +576,7 @@ bool AnalogSensor::update(uint8_t gpio, const char * org_name, double offset, do newSensor.is_system = is_system; settings.analogCustomizations.push_back(newSensor); // check the gpio again and add to used list - if (EMSESP::system_.add_gpio(gpio, "Analog Sensor")) { + if (EMSESP::system_.add_gpio(gpio, name)) { LOG_DEBUG("Adding customization for analog sensor GPIO %02d", gpio); return StateUpdateResult::CHANGED; // persist the change } else { diff --git a/src/core/system.cpp b/src/core/system.cpp index 8d528ba31..31976d7ba 100644 --- a/src/core/system.cpp +++ b/src/core/system.cpp @@ -105,8 +105,8 @@ uint32_t System::led_flash_duration_ = 0; bool System::led_flash_timer_ = false; // GPIOs -std::vector> System::valid_system_gpios_; -std::vector> System::used_gpios_; +std::vector> System::valid_system_gpios_; +std::vector> System::used_gpios_; // find the index of the language // 0 = EN, 1 = DE, etc... @@ -1216,8 +1216,8 @@ void System::show_system(uuid::console::Shell & shell) { // GPIOs shell.println(" GPIOs:"); shell.printf(" in use:"); - for (const auto & gpio : used_gpios_) { - shell.printf(" %d", gpio); + for (const auto & gpio_info : used_gpios_) { + shell.printf(" %d(%s)", gpio_info.pin, gpio_info.name.c_str()); } shell.printfln(" (total %d)", used_gpios_.size()); auto available = available_gpios(); @@ -2060,18 +2060,18 @@ bool System::command_info(const char * value, const int8_t id, JsonObject output // GPIO information std::string gpios_in_use_str; - for (const auto & gpio : EMSESP::system_.used_gpios_) { + for (const auto & gpio_info : EMSESP::system_.used_gpios_) { if (!gpios_in_use_str.empty()) { - gpios_in_use_str += ","; + gpios_in_use_str += ", "; } - gpios_in_use_str += Helpers::itoa(gpio); + gpios_in_use_str += Helpers::itoa(gpio_info.pin) + "(" + gpio_info.name + ")"; } node["gpios_in_use"] = gpios_in_use_str; std::string gpios_available_str; for (const auto & gpio : EMSESP::system_.available_gpios()) { if (!gpios_available_str.empty()) { - gpios_available_str += ","; + gpios_available_str += ", "; } gpios_available_str += Helpers::itoa(gpio); } @@ -2938,8 +2938,9 @@ bool System::add_gpio(uint8_t pin, const char * source_name) { // check if this is a valid user GPIO if (std::find(valid_system_gpios_.begin(), valid_system_gpios_.end(), pin) != valid_system_gpios_.end()) { // It's valid now check if it's already in the used list - if (std::find(used_gpios_.begin(), used_gpios_.end(), pin) != used_gpios_.end()) { - LOG_WARNING("GPIO %d for %s is already in use", pin, source_name); + auto it = std::find_if(used_gpios_.begin(), used_gpios_.end(), [pin, source_name](const GpioInfo & gpio_info) { return gpio_info.pin == pin && gpio_info.name != source_name; }); + if (it != used_gpios_.end()) { + LOG_WARNING("Can't add GPIO %d for %s, already in use by %s", pin, source_name, it->name.c_str()); return false; // Pin is already used } } else { @@ -2952,24 +2953,24 @@ bool System::add_gpio(uint8_t pin, const char * source_name) { remove_gpio(pin); LOG_DEBUG("Adding GPIO %d for %s to used gpio list", pin, source_name); - used_gpios_.push_back(pin); // add to used list + used_gpios_.push_back({pin, source_name}); // add to used list with name return true; } // remove a gpio from both valid and used lists void System::remove_gpio(uint8_t pin, bool also_system) { - auto it = std::find(used_gpios_.begin(), used_gpios_.end(), pin); + auto it = std::find_if(used_gpios_.begin(), used_gpios_.end(), [pin](const GpioInfo & gpio_info) { return gpio_info.pin == pin; }); if (it != used_gpios_.end()) { LOG_DEBUG("GPIO %d removed from used gpio list", pin); used_gpios_.erase(it); } if (also_system) { - it = std::find(valid_system_gpios_.begin(), valid_system_gpios_.end(), pin); - if (it != valid_system_gpios_.end()) { + auto it_sys = std::find(valid_system_gpios_.begin(), valid_system_gpios_.end(), pin); + if (it_sys != valid_system_gpios_.end()) { LOG_DEBUG("GPIO %d removed from valid gpio list", pin); - valid_system_gpios_.erase(it); + valid_system_gpios_.erase(it_sys); } } } @@ -2978,7 +2979,8 @@ void System::remove_gpio(uint8_t pin, bool also_system) { std::vector System::available_gpios() { std::vector gpios; for (const auto & gpio : valid_system_gpios_) { - if (std::find(used_gpios_.begin(), used_gpios_.end(), gpio) == used_gpios_.end()) { + auto it = std::find_if(used_gpios_.begin(), used_gpios_.end(), [gpio](const GpioInfo & gpio_info) { return gpio_info.pin == gpio; }); + if (it == used_gpios_.end()) { gpios.push_back(gpio); // didn't find it in used_gpios_, so it's available } } @@ -2987,8 +2989,8 @@ std::vector System::available_gpios() { // make a snapshot of the current GPIOs void System::make_snapshot_gpios(std::vector & u_gpios, std::vector & s_gpios) { - for (const auto & gpio : used_gpios_) { - u_gpios.push_back(gpio); + for (const auto & gpio_info : used_gpios_) { + u_gpios.push_back(gpio_info.pin); } for (const auto & gpio : valid_system_gpios_) { s_gpios.push_back(gpio); @@ -2999,7 +3001,7 @@ void System::make_snapshot_gpios(std::vector & u_gpios, std::vector & u_gpios, std::vector & s_gpios) { used_gpios_.clear(); for (const auto & gpio : u_gpios) { - used_gpios_.push_back(gpio); + used_gpios_.push_back({static_cast(gpio), "restored"}); } valid_system_gpios_.clear(); diff --git a/src/core/system.h b/src/core/system.h index f082bc273..7dfb5174e 100644 --- a/src/core/system.h +++ b/src/core/system.h @@ -160,6 +160,11 @@ class System { static bool saveSettings(const char * filename, const char * section, JsonObject input); // GPIOs + struct GpioInfo { + uint8_t pin; + std::string name; + }; + static bool add_gpio(uint8_t pin, const char * source_name); static std::vector available_gpios(); static bool load_board_profile(std::vector & data, const std::string & board_profile); @@ -437,8 +442,8 @@ class System { static std::vector> string_range_to_vector(const std::string & range, const std::string & exclude = ""); // GPIOs - static std::vector> valid_system_gpios_; // list of valid GPIOs for the ESP32 board that can be used - static std::vector> used_gpios_; // list of GPIOs used by the application + static std::vector> valid_system_gpios_; // list of valid GPIOs for the ESP32 board that can be used + static std::vector> used_gpios_; // list of GPIOs used by the application with their names int8_t wifi_quality(int8_t dBm); diff --git a/src/web/WebCustomizationService.cpp b/src/web/WebCustomizationService.cpp index 9490b8067..80e1108b9 100644 --- a/src/web/WebCustomizationService.cpp +++ b/src/web/WebCustomizationService.cpp @@ -120,10 +120,11 @@ StateUpdateResult WebCustomization::update(JsonObject root, WebCustomization & c for (const JsonObject analogJson : analogJsons) { // create each of the sensor, overwriting any previous settings // if the gpio is invalid skip the sensor - if (!EMSESP::system_.add_gpio(analogJson["gpio"].as(), "Analog Sensor")) { + auto analog_sensor_name = analogJson["name"].as(); + if (!EMSESP::system_.add_gpio(analogJson["gpio"].as(), analog_sensor_name)) { EMSESP::logger().warning("Analog sensor: Invalid GPIO %d for %s. Skipping.", analogJson["gpio"].as(), - analogJson["name"].as()); + analog_sensor_name); continue; } auto analog = AnalogCustomization();