Skip to content

Commit

Permalink
unassign the old addon pin after assigning the new one
Browse files Browse the repository at this point in the history
  • Loading branch information
bsstephan committed Mar 15, 2024
1 parent 55c1f3a commit a3339b8
Showing 1 changed file with 32 additions and 56 deletions.
88 changes: 32 additions & 56 deletions src/configs/webconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,33 +105,43 @@ static void __attribute__((noinline)) docToValue(T& value, const DynamicJsonDocu
}
}

// Don't inline this function, we do not want to consume stack space in the calling function
static void __attribute__((noinline)) cleanAddonGpioMappings(Pin_t& addonPin, Pin_t oldAddonPin)
{
GpioMappingInfo* gpioMappings = Storage::getInstance().getGpioMappings().pins;
ProfileOptions& profiles = Storage::getInstance().getProfileOptions();

// if the new addon pin value is valid, mark it assigned in GpioMappings
if (isValidPin(addonPin))
{
gpioMappings[addonPin].action = GpioAction::ASSIGNED_TO_ADDON;
profiles.gpioMappingsSets[0].pins[addonPin].action = GpioAction::ASSIGNED_TO_ADDON;
profiles.gpioMappingsSets[1].pins[addonPin].action = GpioAction::ASSIGNED_TO_ADDON;
profiles.gpioMappingsSets[2].pins[addonPin].action = GpioAction::ASSIGNED_TO_ADDON;
} else {
// -1 is our de facto value for "not assigned" in addons
addonPin = -1;
}

// either way now, the addon's pin config is set to its real value, if the
// old value is a real pin (and different), we should unset it
if (isValidPin(oldAddonPin) && oldAddonPin != addonPin)
{
gpioMappings[oldAddonPin].action = GpioAction::NONE;
profiles.gpioMappingsSets[0].pins[oldAddonPin].action = GpioAction::NONE;
profiles.gpioMappingsSets[1].pins[oldAddonPin].action = GpioAction::NONE;
profiles.gpioMappingsSets[2].pins[oldAddonPin].action = GpioAction::NONE;
}
}

// Don't inline this function, we do not want to consume stack space in the calling function
static void __attribute__((noinline)) docToPin(Pin_t& pin, const DynamicJsonDocument& doc, const char* key)
{
Pin_t oldPin = pin;
if (doc.containsKey(key))
{
pin = doc[key];
GpioMappingInfo* gpioMappings = Storage::getInstance().getGpioMappings().pins;
ProfileOptions& profiles = Storage::getInstance().getProfileOptions();
if (isValidPin(pin))
{
gpioMappings[pin].action = GpioAction::ASSIGNED_TO_ADDON;
profiles.gpioMappingsSets[0].pins[pin].action = GpioAction::ASSIGNED_TO_ADDON;
profiles.gpioMappingsSets[1].pins[pin].action = GpioAction::ASSIGNED_TO_ADDON;
profiles.gpioMappingsSets[2].pins[pin].action = GpioAction::ASSIGNED_TO_ADDON;
}
else
{
pin = -1;
if (isValidPin(oldPin))
{
gpioMappings[oldPin].action = GpioAction::NONE;
profiles.gpioMappingsSets[0].pins[oldPin].action = GpioAction::NONE;
profiles.gpioMappingsSets[1].pins[oldPin].action = GpioAction::NONE;
profiles.gpioMappingsSets[2].pins[oldPin].action = GpioAction::NONE;
}
}
cleanAddonGpioMappings(pin, oldPin);
}
}

Expand All @@ -142,24 +152,7 @@ static void __attribute__((noinline)) docToPin(Pin_t& pin, const DynamicJsonDocu
if (doc.containsKey(key0) && doc[key0].containsKey(key1))
{
pin = doc[key0][key1];
GpioMappingInfo* gpioMappings = Storage::getInstance().getGpioMappings().pins;
ProfileOptions& profiles = Storage::getInstance().getProfileOptions();
if (isValidPin(pin))
{
gpioMappings[pin].action = GpioAction::ASSIGNED_TO_ADDON;
profiles.gpioMappingsSets[0].pins[pin].action = GpioAction::ASSIGNED_TO_ADDON;
profiles.gpioMappingsSets[1].pins[pin].action = GpioAction::ASSIGNED_TO_ADDON;
profiles.gpioMappingsSets[2].pins[pin].action = GpioAction::ASSIGNED_TO_ADDON;
} else {
pin = -1;
if (isValidPin(oldPin))
{
gpioMappings[oldPin].action = GpioAction::NONE;
profiles.gpioMappingsSets[0].pins[oldPin].action = GpioAction::NONE;
profiles.gpioMappingsSets[1].pins[oldPin].action = GpioAction::NONE;
profiles.gpioMappingsSets[2].pins[oldPin].action = GpioAction::NONE;
}
}
cleanAddonGpioMappings(pin, oldPin);
}
}

Expand All @@ -170,24 +163,7 @@ static void __attribute__((noinline)) docToPin(Pin_t& pin, const DynamicJsonDocu
if (doc.containsKey(key0) && doc[key0].containsKey(key1) && doc[key0][key1].containsKey(key2))
{
pin = doc[key0][key1][key2];
GpioMappingInfo* gpioMappings = Storage::getInstance().getGpioMappings().pins;
ProfileOptions& profiles = Storage::getInstance().getProfileOptions();
if (isValidPin(pin))
{
gpioMappings[pin].action = GpioAction::ASSIGNED_TO_ADDON;
profiles.gpioMappingsSets[0].pins[pin].action = GpioAction::ASSIGNED_TO_ADDON;
profiles.gpioMappingsSets[1].pins[pin].action = GpioAction::ASSIGNED_TO_ADDON;
profiles.gpioMappingsSets[2].pins[pin].action = GpioAction::ASSIGNED_TO_ADDON;
} else {
pin = -1;
if (isValidPin(oldPin))
{
gpioMappings[oldPin].action = GpioAction::NONE;
profiles.gpioMappingsSets[0].pins[oldPin].action = GpioAction::NONE;
profiles.gpioMappingsSets[1].pins[oldPin].action = GpioAction::NONE;
profiles.gpioMappingsSets[2].pins[oldPin].action = GpioAction::NONE;
}
}
cleanAddonGpioMappings(pin, oldPin);
}
}

Expand Down

0 comments on commit a3339b8

Please sign in to comment.