mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2026-01-31 02:59:10 +03:00
add name to gpios
This commit is contained in:
@@ -576,7 +576,7 @@ bool AnalogSensor::update(uint8_t gpio, const char * org_name, double offset, do
|
|||||||
newSensor.is_system = is_system;
|
newSensor.is_system = is_system;
|
||||||
settings.analogCustomizations.push_back(newSensor);
|
settings.analogCustomizations.push_back(newSensor);
|
||||||
// check the gpio again and add to used list
|
// 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);
|
LOG_DEBUG("Adding customization for analog sensor GPIO %02d", gpio);
|
||||||
return StateUpdateResult::CHANGED; // persist the change
|
return StateUpdateResult::CHANGED; // persist the change
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -105,8 +105,8 @@ uint32_t System::led_flash_duration_ = 0;
|
|||||||
bool System::led_flash_timer_ = false;
|
bool System::led_flash_timer_ = false;
|
||||||
|
|
||||||
// GPIOs
|
// GPIOs
|
||||||
std::vector<uint8_t, AllocatorPSRAM<uint8_t>> System::valid_system_gpios_;
|
std::vector<uint8_t, AllocatorPSRAM<uint8_t>> System::valid_system_gpios_;
|
||||||
std::vector<uint8_t, AllocatorPSRAM<uint8_t>> System::used_gpios_;
|
std::vector<System::GpioInfo, AllocatorPSRAM<System::GpioInfo>> System::used_gpios_;
|
||||||
|
|
||||||
// find the index of the language
|
// find the index of the language
|
||||||
// 0 = EN, 1 = DE, etc...
|
// 0 = EN, 1 = DE, etc...
|
||||||
@@ -1216,8 +1216,8 @@ void System::show_system(uuid::console::Shell & shell) {
|
|||||||
// GPIOs
|
// GPIOs
|
||||||
shell.println(" GPIOs:");
|
shell.println(" GPIOs:");
|
||||||
shell.printf(" in use:");
|
shell.printf(" in use:");
|
||||||
for (const auto & gpio : used_gpios_) {
|
for (const auto & gpio_info : used_gpios_) {
|
||||||
shell.printf(" %d", gpio);
|
shell.printf(" %d(%s)", gpio_info.pin, gpio_info.name.c_str());
|
||||||
}
|
}
|
||||||
shell.printfln(" (total %d)", used_gpios_.size());
|
shell.printfln(" (total %d)", used_gpios_.size());
|
||||||
auto available = available_gpios();
|
auto available = available_gpios();
|
||||||
@@ -2060,18 +2060,18 @@ bool System::command_info(const char * value, const int8_t id, JsonObject output
|
|||||||
|
|
||||||
// GPIO information
|
// GPIO information
|
||||||
std::string gpios_in_use_str;
|
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()) {
|
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;
|
node["gpios_in_use"] = gpios_in_use_str;
|
||||||
|
|
||||||
std::string gpios_available_str;
|
std::string gpios_available_str;
|
||||||
for (const auto & gpio : EMSESP::system_.available_gpios()) {
|
for (const auto & gpio : EMSESP::system_.available_gpios()) {
|
||||||
if (!gpios_available_str.empty()) {
|
if (!gpios_available_str.empty()) {
|
||||||
gpios_available_str += ",";
|
gpios_available_str += ", ";
|
||||||
}
|
}
|
||||||
gpios_available_str += Helpers::itoa(gpio);
|
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
|
// check if this is a valid user GPIO
|
||||||
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()) {
|
||||||
// It's valid now check if it's already in the used list
|
// 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()) {
|
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; });
|
||||||
LOG_WARNING("GPIO %d for %s is already in use", pin, 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
|
return false; // Pin is already used
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -2952,24 +2953,24 @@ bool System::add_gpio(uint8_t pin, const char * source_name) {
|
|||||||
remove_gpio(pin);
|
remove_gpio(pin);
|
||||||
|
|
||||||
LOG_DEBUG("Adding GPIO %d for %s to used gpio list", pin, source_name);
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove a gpio from both valid and used lists
|
// remove a gpio from both valid and used lists
|
||||||
void System::remove_gpio(uint8_t pin, bool also_system) {
|
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()) {
|
if (it != used_gpios_.end()) {
|
||||||
LOG_DEBUG("GPIO %d removed from used gpio list", pin);
|
LOG_DEBUG("GPIO %d removed from used gpio list", pin);
|
||||||
used_gpios_.erase(it);
|
used_gpios_.erase(it);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (also_system) {
|
if (also_system) {
|
||||||
it = std::find(valid_system_gpios_.begin(), valid_system_gpios_.end(), pin);
|
auto it_sys = std::find(valid_system_gpios_.begin(), valid_system_gpios_.end(), pin);
|
||||||
if (it != valid_system_gpios_.end()) {
|
if (it_sys != valid_system_gpios_.end()) {
|
||||||
LOG_DEBUG("GPIO %d removed from valid gpio list", pin);
|
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<uint8_t> System::available_gpios() {
|
std::vector<uint8_t> System::available_gpios() {
|
||||||
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()) {
|
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
|
gpios.push_back(gpio); // didn't find it in used_gpios_, so it's available
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2987,8 +2989,8 @@ std::vector<uint8_t> System::available_gpios() {
|
|||||||
|
|
||||||
// make a snapshot of the current GPIOs
|
// make a snapshot of the current GPIOs
|
||||||
void System::make_snapshot_gpios(std::vector<int8_t> & u_gpios, std::vector<int8_t> & s_gpios) {
|
void System::make_snapshot_gpios(std::vector<int8_t> & u_gpios, std::vector<int8_t> & s_gpios) {
|
||||||
for (const auto & gpio : used_gpios_) {
|
for (const auto & gpio_info : used_gpios_) {
|
||||||
u_gpios.push_back(gpio);
|
u_gpios.push_back(gpio_info.pin);
|
||||||
}
|
}
|
||||||
for (const auto & gpio : valid_system_gpios_) {
|
for (const auto & gpio : valid_system_gpios_) {
|
||||||
s_gpios.push_back(gpio);
|
s_gpios.push_back(gpio);
|
||||||
@@ -2999,7 +3001,7 @@ void System::make_snapshot_gpios(std::vector<int8_t> & u_gpios, std::vector<int8
|
|||||||
void System::restore_snapshot_gpios(std::vector<int8_t> & u_gpios, std::vector<int8_t> & s_gpios) {
|
void System::restore_snapshot_gpios(std::vector<int8_t> & u_gpios, std::vector<int8_t> & s_gpios) {
|
||||||
used_gpios_.clear();
|
used_gpios_.clear();
|
||||||
for (const auto & gpio : u_gpios) {
|
for (const auto & gpio : u_gpios) {
|
||||||
used_gpios_.push_back(gpio);
|
used_gpios_.push_back({static_cast<uint8_t>(gpio), "restored"});
|
||||||
}
|
}
|
||||||
|
|
||||||
valid_system_gpios_.clear();
|
valid_system_gpios_.clear();
|
||||||
|
|||||||
@@ -160,6 +160,11 @@ class System {
|
|||||||
static bool saveSettings(const char * filename, const char * section, JsonObject input);
|
static bool saveSettings(const char * filename, const char * section, JsonObject input);
|
||||||
|
|
||||||
// GPIOs
|
// GPIOs
|
||||||
|
struct GpioInfo {
|
||||||
|
uint8_t pin;
|
||||||
|
std::string name;
|
||||||
|
};
|
||||||
|
|
||||||
static bool add_gpio(uint8_t pin, const char * source_name);
|
static bool add_gpio(uint8_t pin, const char * source_name);
|
||||||
static std::vector<uint8_t> available_gpios();
|
static std::vector<uint8_t> available_gpios();
|
||||||
static bool load_board_profile(std::vector<int8_t> & data, const std::string & board_profile);
|
static bool load_board_profile(std::vector<int8_t> & data, const std::string & board_profile);
|
||||||
@@ -437,8 +442,8 @@ class System {
|
|||||||
static std::vector<uint8_t, AllocatorPSRAM<uint8_t>> string_range_to_vector(const std::string & range, const std::string & exclude = "");
|
static std::vector<uint8_t, AllocatorPSRAM<uint8_t>> string_range_to_vector(const std::string & range, const std::string & exclude = "");
|
||||||
|
|
||||||
// GPIOs
|
// GPIOs
|
||||||
static std::vector<uint8_t, AllocatorPSRAM<uint8_t>> valid_system_gpios_; // list of valid GPIOs for the ESP32 board that can be used
|
static std::vector<uint8_t, AllocatorPSRAM<uint8_t>> valid_system_gpios_; // list of valid GPIOs for the ESP32 board that can be used
|
||||||
static std::vector<uint8_t, AllocatorPSRAM<uint8_t>> used_gpios_; // list of GPIOs used by the application
|
static std::vector<GpioInfo, AllocatorPSRAM<GpioInfo>> used_gpios_; // list of GPIOs used by the application with their names
|
||||||
|
|
||||||
int8_t wifi_quality(int8_t dBm);
|
int8_t wifi_quality(int8_t dBm);
|
||||||
|
|
||||||
|
|||||||
@@ -120,10 +120,11 @@ StateUpdateResult WebCustomization::update(JsonObject root, WebCustomization & c
|
|||||||
for (const JsonObject analogJson : analogJsons) {
|
for (const JsonObject analogJson : analogJsons) {
|
||||||
// create each of the sensor, overwriting any previous settings
|
// create each of the sensor, overwriting any previous settings
|
||||||
// if the gpio is invalid skip the sensor
|
// if the gpio is invalid skip the sensor
|
||||||
if (!EMSESP::system_.add_gpio(analogJson["gpio"].as<uint8_t>(), "Analog Sensor")) {
|
auto analog_sensor_name = analogJson["name"].as<const char *>();
|
||||||
|
if (!EMSESP::system_.add_gpio(analogJson["gpio"].as<uint8_t>(), analog_sensor_name)) {
|
||||||
EMSESP::logger().warning("Analog sensor: Invalid GPIO %d for %s. Skipping.",
|
EMSESP::logger().warning("Analog sensor: Invalid GPIO %d for %s. Skipping.",
|
||||||
analogJson["gpio"].as<uint8_t>(),
|
analogJson["gpio"].as<uint8_t>(),
|
||||||
analogJson["name"].as<const char *>());
|
analog_sensor_name);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
auto analog = AnalogCustomization();
|
auto analog = AnalogCustomization();
|
||||||
|
|||||||
Reference in New Issue
Block a user