Detach freed PWM channels in attachMeters()
This commit is contained in:
+21
-4
@@ -138,12 +138,29 @@ static void saveConfig() {
|
|||||||
// PWM helpers
|
// PWM helpers
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Tracks the GPIO currently bound to each PWM channel so channels that get
|
||||||
|
// freed (meter count shrinks, or a pin is changed/cleared) are detached
|
||||||
|
// instead of being left driving PWM on their old pin.
|
||||||
|
static int8_t attachedPin[8] = { -1, -1, -1, -1, -1, -1, -1, -1 };
|
||||||
|
|
||||||
static void attachMeters() {
|
static void attachMeters() {
|
||||||
for (int i = 0; i < meterCount && i < 8; i++) {
|
for (int i = 0; i < 8; i++) {
|
||||||
if (meters[i].pin > 0) {
|
int pin = (i < meterCount && meters[i].pin > 0) ? meters[i].pin : -1;
|
||||||
|
|
||||||
|
// Release the channel if it's no longer used or its pin changed.
|
||||||
|
if (attachedPin[i] != -1 && attachedPin[i] != pin) {
|
||||||
|
ledcDetachPin(attachedPin[i]);
|
||||||
|
pinMode(attachedPin[i], OUTPUT);
|
||||||
|
digitalWrite(attachedPin[i], LOW); // drive freed pin low so the meter reads zero
|
||||||
|
Serial.printf("[PWM] detach ch%d pin%d\n", i, attachedPin[i]);
|
||||||
|
attachedPin[i] = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pin > 0 && attachedPin[i] != pin) {
|
||||||
ledcSetup(i, PWM_FREQ, PWM_RES);
|
ledcSetup(i, PWM_FREQ, PWM_RES);
|
||||||
ledcAttachPin(meters[i].pin, i);
|
ledcAttachPin(pin, i);
|
||||||
Serial.printf("[PWM] attach ch%d pin%d\n", i, meters[i].pin);
|
attachedPin[i] = pin;
|
||||||
|
Serial.printf("[PWM] attach ch%d pin%d\n", i, pin);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user