diff --git a/src/ConfigFile.cpp b/src/ConfigFile.cpp index 6ff02a3..fbd3f4f 100644 --- a/src/ConfigFile.cpp +++ b/src/ConfigFile.cpp @@ -6,7 +6,6 @@ #include "Utils.h" #include "ConfigSettings.h" #include "RollingCode.h" -#include "BoardProfile.h" static const char *TAG = "ConfigFile"; @@ -288,12 +287,7 @@ void ShadeConfigFile::readShade(SomfyShade *shade) { shade->gpioMy = rec.gpioMy; shade->gpioFlags = rec.gpioFlags; shade->roomId = rec.roomId; - if(shade->proto == radio_proto::GP_Relay || shade->proto == radio_proto::GP_Remote) { - if(!BoardProfile::usesPin(shade->gpioUp)) pinMode(shade->gpioUp, OUTPUT); - if(!BoardProfile::usesPin(shade->gpioDown)) pinMode(shade->gpioDown, OUTPUT); - } - if(shade->proto == radio_proto::GP_Remote && !BoardProfile::usesPin(shade->gpioMy)) - pinMode(shade->gpioMy, OUTPUT); + shade->initGPIOs(); } void ShadeConfigFile::readGroup(SomfyGroup *group) { group_record_t rec = groupRecord(group); diff --git a/src/ShadeGpio.cpp b/src/ShadeGpio.cpp index bd29617..7085f95 100644 --- a/src/ShadeGpio.cpp +++ b/src/ShadeGpio.cpp @@ -12,6 +12,17 @@ static bool usesBoardGPIOs(const SomfyShade &shade) { (shade.proto == radio_proto::GP_Remote && BoardProfile::usesPin(shade.gpioMy)); } +// Shared by saved/backup configuration loading and JSON updates. Refuse the +// entire assignment if any output belongs to integrated board hardware. +void SomfyShade::initGPIOs() { + if(usesBoardGPIOs(*this)) return; + if(this->proto == radio_proto::GP_Relay || this->proto == radio_proto::GP_Remote) { + pinMode(this->gpioUp, OUTPUT); + pinMode(this->gpioDown, OUTPUT); + } + if(this->proto == radio_proto::GP_Remote) pinMode(this->gpioMy, OUTPUT); +} + void SomfyShade::setGPIOs() { if(usesBoardGPIOs(*this)) return; if(this->proto == radio_proto::GP_Relay) { diff --git a/src/Somfy.h b/src/Somfy.h index d0de9fb..c1df738 100644 --- a/src/Somfy.h +++ b/src/Somfy.h @@ -152,6 +152,7 @@ class SomfyShade : public SomfyRemote, public ShadeSettings, private MotorLink, void commit(); void clear(); int8_t transformPosition(float fpos); + void initGPIOs(); void setGPIOs(); void triggerGPIOs(somfy_frame_t &frame); bool usesPin(uint8_t pin); diff --git a/src/SomfyJson.cpp b/src/SomfyJson.cpp index bac699a..4ead8a0 100644 --- a/src/SomfyJson.cpp +++ b/src/SomfyJson.cpp @@ -157,13 +157,11 @@ int8_t SomfyShade::fromJSON(JsonObject &obj) { if(this->proto == radio_proto::GP_Remote || this->proto == radio_proto::GP_Relay) { if(!obj["gpioUp"].isNull()) this->gpioUp = obj["gpioUp"]; if(!obj["gpioDown"].isNull()) this->gpioDown = obj["gpioDown"]; - if(!BoardProfile::usesPin(this->gpioUp)) pinMode(this->gpioUp, OUTPUT); - if(!BoardProfile::usesPin(this->gpioDown)) pinMode(this->gpioDown, OUTPUT); } if(this->proto == radio_proto::GP_Remote) { if(!obj["gpioMy"].isNull()) this->gpioMy = obj["gpioMy"]; - if(!BoardProfile::usesPin(this->gpioMy)) pinMode(this->gpioMy, OUTPUT); } + this->initGPIOs(); } return err; } diff --git a/tests/board-profile/shade-gpio-test.cpp b/tests/board-profile/shade-gpio-test.cpp index 6cb75a2..2c46ed8 100644 --- a/tests/board-profile/shade-gpio-test.cpp +++ b/tests/board-profile/shade-gpio-test.cpp @@ -21,6 +21,7 @@ struct SomfyShade : SomfyRemote, ShadeSettings { struct State { int8_t direction = -1, tiltDirection = 0; float currentPos = 0; } position; const State &state() const { return position; } bool isToggle() { return isToggleShade(shadeType); } + void initGPIOs(); void setGPIOs(); void triggerGPIOs(somfy_frame_t &frame); bool usesPin(uint8_t pin); @@ -30,9 +31,11 @@ struct SomfyShade : SomfyRemote, ShadeSettings { TestSerial Serial; TestESP ESP; static unsigned int writes = 0; +static unsigned int modes = 0; +static int pinModes[49] = {}; static int levels[49] = {}; int gpio_set_level(gpio_num_t pin, uint32_t value) { levels[pin] = value; return 0; } -void pinMode(uint8_t, uint8_t) {} +void pinMode(uint8_t pin, uint8_t mode) { modes++; pinModes[pin] = mode; } void digitalWrite(uint8_t pin, uint8_t value) { writes++; levels[pin] = value; } int digitalRead(uint8_t pin) { return levels[pin]; } unsigned long millis() { return 100; } @@ -45,6 +48,15 @@ void TestSPI::end() {} int main() { SomfyShade shade; + shade.initGPIOs(); + assert(modes == 2 && pinModes[16] == OUTPUT && pinModes[17] == OUTPUT); + shade.proto = radio_proto::GP_Remote; + shade.initGPIOs(); + assert(modes == 5 && pinModes[18] == OUTPUT); + shade.proto = radio_proto::RTS; + shade.initGPIOs(); + assert(modes == 5); + shade.proto = radio_proto::GP_Relay; shade.setGPIOs(); assert(writes == 2 && levels[16] == HIGH && levels[17] == LOW); somfy_frame_t frame; @@ -63,10 +75,15 @@ int main() { shade.gpioDown = slot == 1 ? pin : 17; shade.gpioMy = slot == 2 ? pin : 18; shade.proto = radio_proto::GP_Remote; + const unsigned int beforeModes = modes; + shade.initGPIOs(); + assert(modes == beforeModes); shade.triggerGPIOs(frame); shade.setGPIOs(); if(slot != 2) { shade.proto = radio_proto::GP_Relay; + shade.initGPIOs(); + assert(modes == beforeModes); shade.setGPIOs(); } assert(writes == before); diff --git a/tools/test-board-profile.sh b/tools/test-board-profile.sh index 5810bd7..cbeb336 100755 --- a/tools/test-board-profile.sh +++ b/tools/test-board-profile.sh @@ -11,9 +11,15 @@ sources=("$repo/tests/board-profile/board-profile-test.cpp" "$repo/src/BoardProf "$scratch/lilygo-test" "$compiler" "${common[@]}" "${sources[@]}" -o "$scratch/generic-test" "$scratch/generic-test" -"$compiler" "${common[@]}" -DESPSOMFY_LILYGO_T_EMBED_CC1101_PLUS -DCONFIG_IDF_TARGET_ESP32S3 \ - "$repo/tests/board-profile/shade-gpio-test.cpp" "$repo/src/BoardProfile.cpp" -o "$scratch/shade-gpio-test" -"$scratch/shade-gpio-test" +for profile in generic lilygo; do + flags=() + if [[ "$profile" == lilygo ]]; then + flags=(-DESPSOMFY_LILYGO_T_EMBED_CC1101_PLUS -DCONFIG_IDF_TARGET_ESP32S3) + fi + "$compiler" "${common[@]}" "${flags[@]}" \ + "$repo/tests/board-profile/shade-gpio-test.cpp" "$repo/src/BoardProfile.cpp" -o "$scratch/shade-gpio-$profile" + "$scratch/shade-gpio-$profile" +done if "$compiler" "${common[@]}" -DESPSOMFY_LILYGO_T_EMBED_CC1101_PLUS \ "${sources[@]}" -o "$scratch/wrong-target" > "$scratch/wrong-target.log" 2>&1; then echo 'LILYGO profile incorrectly accepted a non-S3 target' >&2