This commit is contained in:
proddy
2025-11-17 23:00:45 +01:00
parent 24421a0224
commit 80dd16740d
8 changed files with 75 additions and 38 deletions

View File

@@ -115,6 +115,23 @@ const SensorsAnalogDialog = ({
[] []
); );
const analogGPIOMenuItems = () =>
// add selectedItem.g to the list
[
...(analogGPIOList?.includes(selectedItem.g) || selectedItem.g === undefined
? analogGPIOList
: [selectedItem.g, ...analogGPIOList])
]
.filter((gpio, idx, arr) => arr.indexOf(gpio) === idx)
.sort((a, b) => a - b)
.map((gpio: number) => {
return (
<MenuItem key={gpio} value={gpio}>
{gpio}
</MenuItem>
);
});
// Reset form when dialog opens or selectedItem changes // Reset form when dialog opens or selectedItem changes
useEffect(() => { useEffect(() => {
if (open) { if (open) {
@@ -162,15 +179,11 @@ const SensorsAnalogDialog = ({
label="GPIO" label="GPIO"
value={editItem.g} value={editItem.g}
sx={{ width: '9ch' }} sx={{ width: '9ch' }}
disabled={editItem.s} disabled={editItem.s || !creating}
select select
onChange={updateFormValue} onChange={updateFormValue}
> >
{analogGPIOList?.map((gpio: number) => ( {analogGPIOMenuItems()}
<MenuItem key={gpio} value={gpio}>
{gpio}
</MenuItem>
))}
</ValidatedTextField> </ValidatedTextField>
<Grid> <Grid>
<ValidatedTextField <ValidatedTextField

View File

@@ -201,7 +201,7 @@ export enum DeviceValueUOM {
export const DeviceValueUOM_s = [ export const DeviceValueUOM_s = [
'', '',
'°C', '°C',
'°C', '°C Rel',
'%', '%',
'l/min', 'l/min',
'kWh', 'kWh',

View File

@@ -25,11 +25,22 @@ const ValidatedTextField: FC<ValidatedTextFieldProps> = ({
error={!!errors} error={!!errors}
{...rest} {...rest}
aria-label="Error" aria-label="Error"
slotProps={{ sx={{
inputLabel: { '& .MuiInputBase-input.Mui-disabled': {
style: rest.disabled ? { color: 'grey' } : undefined WebkitTextFillColor: 'grey'
} }
}} }}
{...(rest.disabled && {
slotProps: {
select: {
IconComponent: () => null
},
inputLabel: {
style: { color: 'grey' }
}
}
})}
color={rest.disabled ? 'secondary' : 'primary'}
/> />
{errors?.map((e) => ( {errors?.map((e) => (
<FormHelperText key={e.message} sx={{ color: 'rgb(250, 95, 84)' }}> <FormHelperText key={e.message} sx={{ color: 'rgb(250, 95, 84)' }}>

View File

@@ -472,8 +472,9 @@ void AnalogSensor::loop() {
measure(); // take the measurements measure(); // take the measurements
} }
// update analog information name and offset // update analog information name, offset, factor, uom, type, deleted, is_system
// a type value of -1 is used to delete the sensor // a type value of -1 is used to delete the sensor
// the gpio is the key
bool AnalogSensor::update(uint8_t gpio, std::string & name, double offset, double factor, uint8_t uom, int8_t type, bool deleted, bool is_system) { bool AnalogSensor::update(uint8_t gpio, std::string & name, double offset, double factor, uint8_t uom, int8_t type, bool deleted, bool is_system) {
// first see if we can find the sensor in our customization list // first see if we can find the sensor in our customization list
bool found_sensor = false; bool found_sensor = false;
@@ -521,7 +522,7 @@ bool AnalogSensor::update(uint8_t gpio, std::string & name, double offset, doubl
} }
// we didn't find it, it's new, so create and store it in the customization list // we didn't find it, it's new, so create and store it in the customization list
// gpio is already checked in web interface, should never trigger. // gpio is already checked in web interface
if (!found_sensor && EMSESP::system_.is_valid_gpio(gpio)) { if (!found_sensor && EMSESP::system_.is_valid_gpio(gpio)) {
found_sensor = true; found_sensor = true;
EMSESP::webCustomizationService.update([&](WebCustomization & settings) { EMSESP::webCustomizationService.update([&](WebCustomization & settings) {
@@ -534,7 +535,7 @@ bool AnalogSensor::update(uint8_t gpio, std::string & name, double offset, doubl
newSensor.type = type; newSensor.type = type;
newSensor.is_system = is_system; newSensor.is_system = is_system;
settings.analogCustomizations.push_back(newSensor); settings.analogCustomizations.push_back(newSensor);
LOG_DEBUG("Adding new 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
}); });
} }

View File

@@ -415,8 +415,8 @@ void System::reload_settings() {
EMSESP::webSettingsService.read([&](WebSettings & settings) { EMSESP::webSettingsService.read([&](WebSettings & settings) {
version_ = settings.version; version_ = settings.version;
// first check gpios, priority to rx and tx // first check gpios, priority to rx and tx
rx_gpio_ = is_valid_gpio(settings.rx_gpio) ? settings.rx_gpio : 0; rx_gpio_ = is_valid_gpio(settings.rx_gpio);
tx_gpio_ = is_valid_gpio(settings.tx_gpio) ? settings.tx_gpio : 0; tx_gpio_ = is_valid_gpio(settings.tx_gpio);
pbutton_gpio_ = is_valid_gpio(settings.pbutton_gpio) ? settings.pbutton_gpio : 0; pbutton_gpio_ = is_valid_gpio(settings.pbutton_gpio) ? settings.pbutton_gpio : 0;
dallas_gpio_ = is_valid_gpio(settings.dallas_gpio) ? settings.dallas_gpio : 0; dallas_gpio_ = is_valid_gpio(settings.dallas_gpio) ? settings.dallas_gpio : 0;
led_gpio_ = is_valid_gpio(settings.led_gpio) ? settings.led_gpio : 0; led_gpio_ = is_valid_gpio(settings.led_gpio) ? settings.led_gpio : 0;
@@ -456,9 +456,9 @@ void System::reload_settings() {
}); });
} }
// check for valid ESP32 pins // check if a pin is valid ESP32 pin and not used by application settings
bool System::is_valid_gpio(uint8_t pin) { bool System::is_valid_gpio(uint8_t pin, bool exclude_used) {
auto valid_gpios = valid_gpio_list(); auto valid_gpios = valid_gpio_list(exclude_used);
return std::find(valid_gpios.begin(), valid_gpios.end(), pin) != valid_gpios.end(); return std::find(valid_gpios.begin(), valid_gpios.end(), pin) != valid_gpios.end();
} }
@@ -2356,12 +2356,12 @@ std::vector<uint8_t> System::string_range_to_vector(const std::string & range) {
return valid_gpios; return valid_gpios;
} }
// return a list of valid GPIOs for the ESP32 board // return a list of valid GPIOs for the ESP32 board that can be used
// notes: // notes:
// - we allow 0, which is used sometimes to indicate a disabled pin // - we allow 0, which is used sometimes to indicate a disabled pin (e.g. button, led)
// - 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, excluding 39
// - 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
std::vector<uint8_t> System::valid_gpio_list() { std::vector<uint8_t> System::valid_gpio_list(bool exclude_used) {
// get free gpios based on board/platform type // get free gpios based on board/platform type
#if CONFIG_IDF_TARGET_ESP32C3 #if CONFIG_IDF_TARGET_ESP32C3
// https://www.wemos.cc/en/latest/c3/c3_mini.html // https://www.wemos.cc/en/latest/c3/c3_mini.html
@@ -2390,21 +2390,25 @@ std::vector<uint8_t> System::valid_gpio_list() {
valid_gpios.erase(std::remove(valid_gpios.begin(), valid_gpios.end(), 22), valid_gpios.end()); valid_gpios.erase(std::remove(valid_gpios.begin(), valid_gpios.end(), 22), valid_gpios.end());
} }
// filter out GPIOs already used in application settings, gpio 0 means disabled, except for pbutton // filter out GPIOs already used in application settings and analog sensors, if enabled
for (const auto & gpio : valid_gpios) { if (exclude_used) {
if (gpio == EMSESP::system_.pbutton_gpio_ // application settings
|| (gpio for (const auto & gpio : valid_gpios) {
&& (gpio == EMSESP::system_.led_gpio_ || gpio == EMSESP::system_.dallas_gpio_ || gpio == EMSESP::system_.rx_gpio_ if (gpio == EMSESP::system_.pbutton_gpio_
|| gpio == EMSESP::system_.tx_gpio_))) { || (gpio
valid_gpios.erase(std::remove(valid_gpios.begin(), valid_gpios.end(), gpio), valid_gpios.end()); && (gpio == EMSESP::system_.led_gpio_ || gpio == EMSESP::system_.dallas_gpio_ || gpio == EMSESP::system_.rx_gpio_
|| gpio == EMSESP::system_.tx_gpio_))) {
valid_gpios.erase(std::remove(valid_gpios.begin(), valid_gpios.end(), gpio), valid_gpios.end());
}
} }
}
// filter out GPIOs already used in analog sensors, if enabled // analog sensors
if (EMSESP::system_.analog_enabled_) { if (EMSESP::system_.analog_enabled_) {
for (const auto & sensor : EMSESP::analogsensor_.sensors()) { // TODO: check if core_voltage and supply_voltage are already used
if (std::find(valid_gpios.begin(), valid_gpios.end(), sensor.gpio()) != valid_gpios.end()) { for (const auto & sensor : EMSESP::analogsensor_.sensors()) {
valid_gpios.erase(std::find(valid_gpios.begin(), valid_gpios.end(), sensor.gpio())); if (std::find(valid_gpios.begin(), valid_gpios.end(), sensor.gpio()) != valid_gpios.end()) {
valid_gpios.erase(std::find(valid_gpios.begin(), valid_gpios.end(), sensor.gpio()));
}
} }
} }
} }

View File

@@ -141,7 +141,7 @@ class System {
static void extractSettings(const char * filename, const char * section, JsonObject output); static void extractSettings(const char * filename, const char * section, JsonObject output);
static bool saveSettings(const char * filename, const char * section, JsonObject input); static bool saveSettings(const char * filename, const char * section, JsonObject input);
bool is_valid_gpio(uint8_t pin); bool is_valid_gpio(uint8_t pin, bool exclude_used = false);
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);
static bool readCommand(const char * data); static bool readCommand(const char * data);
@@ -342,7 +342,7 @@ class System {
test_set_all_active_ = n; test_set_all_active_ = n;
} }
static std::vector<uint8_t> valid_gpio_list(); static std::vector<uint8_t> valid_gpio_list(bool exclude_used = false);
#if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S2 #if CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S2
float temperature() { float temperature() {

View File

@@ -117,6 +117,13 @@ StateUpdateResult WebCustomization::update(JsonObject root, WebCustomization & c
auto analogJsons = root["as"].as<JsonArray>(); auto analogJsons = root["as"].as<JsonArray>();
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 (!EMSESP::system_.is_valid_gpio(analogJson["gpio"].as<uint8_t>(), true)) {
EMSESP::logger().warning("Invalid GPIO %d for Sensor %s. Skipping.",
analogJson["gpio"].as<uint8_t>(),
analogJson["name"].as<std::string>().c_str());
continue;
}
auto analog = AnalogCustomization(); auto analog = AnalogCustomization();
analog.gpio = analogJson["gpio"]; analog.gpio = analogJson["gpio"];
analog.name = analogJson["name"].as<std::string>(); analog.name = analogJson["name"].as<std::string>();

View File

@@ -157,8 +157,9 @@ void WebDataService::sensor_data(AsyncWebServerRequest * request) {
root["analog_enabled"] = EMSESP::analog_enabled(); root["analog_enabled"] = EMSESP::analog_enabled();
root["platform"] = EMSESP_PLATFORM; root["platform"] = EMSESP_PLATFORM;
// send back a list of valid GPIOs that can be used for analog sensors, excluding those already used
JsonArray valid_gpio_list = root["valid_gpio_list"].to<JsonArray>(); JsonArray valid_gpio_list = root["valid_gpio_list"].to<JsonArray>();
for (const auto & gpio : EMSESP::system_.valid_gpio_list()) { for (const auto & gpio : EMSESP::system_.valid_gpio_list(true)) {
valid_gpio_list.add(gpio); valid_gpio_list.add(gpio);
} }