fix pin deallocation issue
This commit is contained in:
parent
4b3c2fc873
commit
7f5a5cfa8e
@ -9,15 +9,25 @@
|
|||||||
class PwmOutput {
|
class PwmOutput {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
PwmOutput() { }
|
PwmOutput() {
|
||||||
|
DEBUG_PRINTLN("pwm_output[-1]: setup disabled output");
|
||||||
|
}
|
||||||
|
|
||||||
PwmOutput(int8_t pin, uint32_t freq) : pin_(pin), freq_(freq) {
|
PwmOutput(int8_t pin, uint32_t freq) : pin_(pin), freq_(freq) {
|
||||||
DEBUG_PRINTF("pwm_output[%d]: setup at freq %d\n", pin_, freq_);
|
DEBUG_PRINTF("pwm_output[%d]: setup at freq %d\n", pin_, freq_);
|
||||||
open();
|
open();
|
||||||
}
|
}
|
||||||
|
|
||||||
~PwmOutput() {
|
void close() {
|
||||||
close();
|
DEBUG_PRINTF("pwm_output[%d]: close\n", pin_);
|
||||||
|
if (!enabled_)
|
||||||
|
return;
|
||||||
|
pinManager.deallocatePin(pin_, PinOwner::UM_PWM_OUTPUTS);
|
||||||
|
if (channel_ != 255)
|
||||||
|
pinManager.deallocateLedc(channel_, 1);
|
||||||
|
channel_ = 255;
|
||||||
|
enabled_ = false;
|
||||||
|
duty_ = -1.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setDuty(const float duty) {
|
void setDuty(const float duty) {
|
||||||
@ -30,6 +40,10 @@ class PwmOutput {
|
|||||||
ledcWrite(channel_, value);
|
ledcWrite(channel_, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool isEnabled() const {
|
||||||
|
return enabled_;
|
||||||
|
}
|
||||||
|
|
||||||
int8_t getPin() const {
|
int8_t getPin() const {
|
||||||
return pin_;
|
return pin_;
|
||||||
}
|
}
|
||||||
@ -71,19 +85,6 @@ class PwmOutput {
|
|||||||
DEBUG_PRINTF("pwm_output[%d]: open successful\n", pin_);
|
DEBUG_PRINTF("pwm_output[%d]: open successful\n", pin_);
|
||||||
enabled_ = true;
|
enabled_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void close() {
|
|
||||||
DEBUG_PRINTF("pwm_output[%d]: close\n", pin_);
|
|
||||||
if (!enabled_)
|
|
||||||
return;
|
|
||||||
pinManager.deallocatePin(pin_, PinOwner::UM_PWM_OUTPUTS);
|
|
||||||
if (channel_ != 255)
|
|
||||||
pinManager.deallocateLedc(channel_, 1);
|
|
||||||
channel_ = 255;
|
|
||||||
enabled_ = false;
|
|
||||||
duty_ = -1.0f;
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -108,7 +109,9 @@ class PwmOutputsUsermod : public Usermod {
|
|||||||
void addToJsonState(JsonObject& root) {
|
void addToJsonState(JsonObject& root) {
|
||||||
DEBUG_PRINTLN("PwmOutputs: addToJsonState");
|
DEBUG_PRINTLN("PwmOutputs: addToJsonState");
|
||||||
for (int i = 0; i < USERMOD_PWM_OUTPUT_PINS; i++) {
|
for (int i = 0; i < USERMOD_PWM_OUTPUT_PINS; i++) {
|
||||||
const PwmOutput& pwm = pwms[i];
|
const PwmOutput& pwm = pwms_[i];
|
||||||
|
if (!pwm.isEnabled())
|
||||||
|
continue;
|
||||||
root["pwm_" + String(i)] = pwm.getDuty();
|
root["pwm_" + String(i)] = pwm.getDuty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -116,7 +119,9 @@ class PwmOutputsUsermod : public Usermod {
|
|||||||
void readFromJsonState(JsonObject& root) {
|
void readFromJsonState(JsonObject& root) {
|
||||||
DEBUG_PRINTLN("PwmOutputs: readFromJsonState");
|
DEBUG_PRINTLN("PwmOutputs: readFromJsonState");
|
||||||
for (int i = 0; i < USERMOD_PWM_OUTPUT_PINS; i++) {
|
for (int i = 0; i < USERMOD_PWM_OUTPUT_PINS; i++) {
|
||||||
PwmOutput& pwm = pwms[i];
|
PwmOutput& pwm = pwms_[i];
|
||||||
|
if (!pwm.isEnabled())
|
||||||
|
continue;
|
||||||
float duty = 0.0f;
|
float duty = 0.0f;
|
||||||
if (getJsonValue(root["pwm_" + String(i)], duty)) {
|
if (getJsonValue(root["pwm_" + String(i)], duty)) {
|
||||||
pwm.setDuty(duty);
|
pwm.setDuty(duty);
|
||||||
@ -131,7 +136,9 @@ class PwmOutputsUsermod : public Usermod {
|
|||||||
user = root.createNestedObject("u");
|
user = root.createNestedObject("u");
|
||||||
|
|
||||||
for (int i = 0; i < USERMOD_PWM_OUTPUT_PINS; i++) {
|
for (int i = 0; i < USERMOD_PWM_OUTPUT_PINS; i++) {
|
||||||
const PwmOutput& pwm = pwms[i];
|
const PwmOutput& pwm = pwms_[i];
|
||||||
|
if (!pwm.isEnabled())
|
||||||
|
continue;
|
||||||
JsonArray data = user.createNestedArray("pwm_" + String(i));
|
JsonArray data = user.createNestedArray("pwm_" + String(i));
|
||||||
data.add(1e2f * pwm.getDuty());
|
data.add(1e2f * pwm.getDuty());
|
||||||
data.add(F("%"));
|
data.add(F("%"));
|
||||||
@ -142,7 +149,7 @@ class PwmOutputsUsermod : public Usermod {
|
|||||||
DEBUG_PRINTLN("PwmOutputs: addToConfig");
|
DEBUG_PRINTLN("PwmOutputs: addToConfig");
|
||||||
JsonObject top = root.createNestedObject(USERMOD_NAME);
|
JsonObject top = root.createNestedObject(USERMOD_NAME);
|
||||||
for (int i = 0; i < USERMOD_PWM_OUTPUT_PINS; i++) {
|
for (int i = 0; i < USERMOD_PWM_OUTPUT_PINS; i++) {
|
||||||
const PwmOutput& pwm = pwms[i];
|
const PwmOutput& pwm = pwms_[i];
|
||||||
top["pin_" + String(i)] = pwm.getPin();
|
top["pin_" + String(i)] = pwm.getPin();
|
||||||
top["freq_" + String(i)] = pwm.getFreq();
|
top["freq_" + String(i)] = pwm.getFreq();
|
||||||
}
|
}
|
||||||
@ -153,7 +160,7 @@ class PwmOutputsUsermod : public Usermod {
|
|||||||
JsonObject top = root[USERMOD_NAME];
|
JsonObject top = root[USERMOD_NAME];
|
||||||
bool configComplete = !top.isNull();
|
bool configComplete = !top.isNull();
|
||||||
for (int i = 0; i < USERMOD_PWM_OUTPUT_PINS; i++) {
|
for (int i = 0; i < USERMOD_PWM_OUTPUT_PINS; i++) {
|
||||||
PwmOutput& pwm = pwms[i];
|
PwmOutput& pwm = pwms_[i];
|
||||||
|
|
||||||
int8_t newPin = pwm.getPin();
|
int8_t newPin = pwm.getPin();
|
||||||
uint32_t newFreq = pwm.getFreq();
|
uint32_t newFreq = pwm.getFreq();
|
||||||
@ -162,6 +169,7 @@ class PwmOutputsUsermod : public Usermod {
|
|||||||
|
|
||||||
// Recreate output if config has changed
|
// Recreate output if config has changed
|
||||||
if (newPin != pwm.getPin() || newFreq != pwm.getFreq()) {
|
if (newPin != pwm.getPin() || newFreq != pwm.getFreq()) {
|
||||||
|
pwm.close();
|
||||||
pwm = PwmOutput(newPin, newFreq);
|
pwm = PwmOutput(newPin, newFreq);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -174,7 +182,7 @@ class PwmOutputsUsermod : public Usermod {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned long lastUpdate_ = 0;
|
unsigned long lastUpdate_ = 0;
|
||||||
PwmOutput pwms[USERMOD_PWM_OUTPUT_PINS];
|
PwmOutput pwms_[USERMOD_PWM_OUTPUT_PINS];
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user