From 1a14222d97197357f5f69e4aea44935fb4e07f98 Mon Sep 17 00:00:00 2001 From: Ardi Loot Date: Sat, 26 Nov 2022 17:56:34 +0200 Subject: [PATCH] refactoring --- usermods/pwm_outputs/usermod_pwm_outputs.h | 44 +++++++++------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/usermods/pwm_outputs/usermod_pwm_outputs.h b/usermods/pwm_outputs/usermod_pwm_outputs.h index 68722da4..9688b28f 100644 --- a/usermods/pwm_outputs/usermod_pwm_outputs.h +++ b/usermods/pwm_outputs/usermod_pwm_outputs.h @@ -14,8 +14,22 @@ class PwmOutput { } PwmOutput(int8_t pin, uint32_t freq) : pin_(pin), freq_(freq) { - DEBUG_PRINTF("pwm_output[%d]: setup at freq %d\n", pin_, freq_); - open(); + DEBUG_PRINTF("pwm_output[%d]: setup to freq %d\n", pin_, freq_); + if (pin_ < 0 || !pinManager.allocatePin(pin_, true, PinOwner::UM_PWM_OUTPUTS)) { + return; + } + + channel_ = pinManager.allocateLedc(1); + if (channel_ == 255) { + DEBUG_PRINTF("pwm_output[%d]: failed to quire ledc\n", pin_); + pinManager.deallocatePin(pin_, PinOwner::UM_PWM_OUTPUTS); + return; + } + + ledcSetup(channel_, freq_, bit_depth_); + ledcAttachPin(pin_, channel_); + DEBUG_PRINTF("pwm_output[%d]: init successful\n", pin_); + enabled_ = true; } void close() { @@ -62,29 +76,7 @@ class PwmOutput { uint8_t bit_depth_ = 12; uint8_t channel_ {255}; bool enabled_ {false}; - float duty_ {-1.0f}; // Unknown duty - - void open() { - DEBUG_PRINTF("pwm_output[%d]: open...\n", pin_); - if (enabled_) - return; - - if (pin_ < 0 || !pinManager.allocatePin(pin_, true, PinOwner::UM_PWM_OUTPUTS)) { - return; - } - - channel_ = pinManager.allocateLedc(1); - if (channel_ == 255) { - DEBUG_PRINTF("pwm_output[%d]: failed to quire ledc\n", pin_); - pinManager.deallocatePin(pin_, PinOwner::UM_PWM_OUTPUTS); - return; - } - - ledcSetup(channel_, freq_, bit_depth_); - ledcAttachPin(pin_, channel_); - DEBUG_PRINTF("pwm_output[%d]: open successful\n", pin_); - enabled_ = true; - } + float duty_ {0.0f}; }; @@ -139,7 +131,7 @@ class PwmOutputsUsermod : public Usermod { const PwmOutput& pwm = pwms_[i]; if (!pwm.isEnabled()) continue; - JsonArray data = user.createNestedArray("pwm_" + String(i)); + JsonArray data = user.createNestedArray("PWM pin " + String(pwm.getPin())); data.add(1e2f * pwm.getDuty()); data.add(F("%")); }