refactor: centralize shade GPIO initialization and board guards

Use one initialization method for saved configuration and JSON updates. Reject the complete output assignment when any pin belongs to integrated board hardware, retaining separate UI validation and runtime output guards. Exercise production GPIO initialization on generic and Plus profiles.
This commit is contained in:
R. Sid Shaytay 2026-11-06 12:51:36 -08:00
parent a5afcfacea
commit d4c13d5134
6 changed files with 41 additions and 14 deletions

View file

@ -6,7 +6,6 @@
#include "Utils.h" #include "Utils.h"
#include "ConfigSettings.h" #include "ConfigSettings.h"
#include "RollingCode.h" #include "RollingCode.h"
#include "BoardProfile.h"
static const char *TAG = "ConfigFile"; static const char *TAG = "ConfigFile";
@ -288,12 +287,7 @@ void ShadeConfigFile::readShade(SomfyShade *shade) {
shade->gpioMy = rec.gpioMy; shade->gpioMy = rec.gpioMy;
shade->gpioFlags = rec.gpioFlags; shade->gpioFlags = rec.gpioFlags;
shade->roomId = rec.roomId; shade->roomId = rec.roomId;
if(shade->proto == radio_proto::GP_Relay || shade->proto == radio_proto::GP_Remote) { shade->initGPIOs();
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);
} }
void ShadeConfigFile::readGroup(SomfyGroup *group) { void ShadeConfigFile::readGroup(SomfyGroup *group) {
group_record_t rec = groupRecord(group); group_record_t rec = groupRecord(group);

View file

@ -12,6 +12,17 @@ static bool usesBoardGPIOs(const SomfyShade &shade) {
(shade.proto == radio_proto::GP_Remote && BoardProfile::usesPin(shade.gpioMy)); (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() { void SomfyShade::setGPIOs() {
if(usesBoardGPIOs(*this)) return; if(usesBoardGPIOs(*this)) return;
if(this->proto == radio_proto::GP_Relay) { if(this->proto == radio_proto::GP_Relay) {

View file

@ -152,6 +152,7 @@ class SomfyShade : public SomfyRemote, public ShadeSettings, private MotorLink,
void commit(); void commit();
void clear(); void clear();
int8_t transformPosition(float fpos); int8_t transformPosition(float fpos);
void initGPIOs();
void setGPIOs(); void setGPIOs();
void triggerGPIOs(somfy_frame_t &frame); void triggerGPIOs(somfy_frame_t &frame);
bool usesPin(uint8_t pin); bool usesPin(uint8_t pin);

View file

@ -157,13 +157,11 @@ int8_t SomfyShade::fromJSON(JsonObject &obj) {
if(this->proto == radio_proto::GP_Remote || this->proto == radio_proto::GP_Relay) { if(this->proto == radio_proto::GP_Remote || this->proto == radio_proto::GP_Relay) {
if(!obj["gpioUp"].isNull()) this->gpioUp = obj["gpioUp"]; if(!obj["gpioUp"].isNull()) this->gpioUp = obj["gpioUp"];
if(!obj["gpioDown"].isNull()) this->gpioDown = obj["gpioDown"]; 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(this->proto == radio_proto::GP_Remote) {
if(!obj["gpioMy"].isNull()) this->gpioMy = obj["gpioMy"]; if(!obj["gpioMy"].isNull()) this->gpioMy = obj["gpioMy"];
if(!BoardProfile::usesPin(this->gpioMy)) pinMode(this->gpioMy, OUTPUT);
} }
this->initGPIOs();
} }
return err; return err;
} }

View file

@ -21,6 +21,7 @@ struct SomfyShade : SomfyRemote, ShadeSettings {
struct State { int8_t direction = -1, tiltDirection = 0; float currentPos = 0; } position; struct State { int8_t direction = -1, tiltDirection = 0; float currentPos = 0; } position;
const State &state() const { return position; } const State &state() const { return position; }
bool isToggle() { return isToggleShade(shadeType); } bool isToggle() { return isToggleShade(shadeType); }
void initGPIOs();
void setGPIOs(); void setGPIOs();
void triggerGPIOs(somfy_frame_t &frame); void triggerGPIOs(somfy_frame_t &frame);
bool usesPin(uint8_t pin); bool usesPin(uint8_t pin);
@ -30,9 +31,11 @@ struct SomfyShade : SomfyRemote, ShadeSettings {
TestSerial Serial; TestSerial Serial;
TestESP ESP; TestESP ESP;
static unsigned int writes = 0; static unsigned int writes = 0;
static unsigned int modes = 0;
static int pinModes[49] = {};
static int levels[49] = {}; static int levels[49] = {};
int gpio_set_level(gpio_num_t pin, uint32_t value) { levels[pin] = value; return 0; } 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; } void digitalWrite(uint8_t pin, uint8_t value) { writes++; levels[pin] = value; }
int digitalRead(uint8_t pin) { return levels[pin]; } int digitalRead(uint8_t pin) { return levels[pin]; }
unsigned long millis() { return 100; } unsigned long millis() { return 100; }
@ -45,6 +48,15 @@ void TestSPI::end() {}
int main() { int main() {
SomfyShade shade; 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(); shade.setGPIOs();
assert(writes == 2 && levels[16] == HIGH && levels[17] == LOW); assert(writes == 2 && levels[16] == HIGH && levels[17] == LOW);
somfy_frame_t frame; somfy_frame_t frame;
@ -63,10 +75,15 @@ int main() {
shade.gpioDown = slot == 1 ? pin : 17; shade.gpioDown = slot == 1 ? pin : 17;
shade.gpioMy = slot == 2 ? pin : 18; shade.gpioMy = slot == 2 ? pin : 18;
shade.proto = radio_proto::GP_Remote; shade.proto = radio_proto::GP_Remote;
const unsigned int beforeModes = modes;
shade.initGPIOs();
assert(modes == beforeModes);
shade.triggerGPIOs(frame); shade.triggerGPIOs(frame);
shade.setGPIOs(); shade.setGPIOs();
if(slot != 2) { if(slot != 2) {
shade.proto = radio_proto::GP_Relay; shade.proto = radio_proto::GP_Relay;
shade.initGPIOs();
assert(modes == beforeModes);
shade.setGPIOs(); shade.setGPIOs();
} }
assert(writes == before); assert(writes == before);

View file

@ -11,9 +11,15 @@ sources=("$repo/tests/board-profile/board-profile-test.cpp" "$repo/src/BoardProf
"$scratch/lilygo-test" "$scratch/lilygo-test"
"$compiler" "${common[@]}" "${sources[@]}" -o "$scratch/generic-test" "$compiler" "${common[@]}" "${sources[@]}" -o "$scratch/generic-test"
"$scratch/generic-test" "$scratch/generic-test"
"$compiler" "${common[@]}" -DESPSOMFY_LILYGO_T_EMBED_CC1101_PLUS -DCONFIG_IDF_TARGET_ESP32S3 \ for profile in generic lilygo; do
"$repo/tests/board-profile/shade-gpio-test.cpp" "$repo/src/BoardProfile.cpp" -o "$scratch/shade-gpio-test" flags=()
"$scratch/shade-gpio-test" 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 \ if "$compiler" "${common[@]}" -DESPSOMFY_LILYGO_T_EMBED_CC1101_PLUS \
"${sources[@]}" -o "$scratch/wrong-target" > "$scratch/wrong-target.log" 2>&1; then "${sources[@]}" -o "$scratch/wrong-target" > "$scratch/wrong-target.log" 2>&1; then
echo 'LILYGO profile incorrectly accepted a non-S3 target' >&2 echo 'LILYGO profile incorrectly accepted a non-S3 target' >&2