From 70856151b7fee72f2a9c1a6013801ab14c2bffdb Mon Sep 17 00:00:00 2001 From: Andrey Gusakov Date: Tue, 3 May 2022 17:18:20 +0300 Subject: [PATCH] pwm.cpp: allow external config --- firmware/pwm.cpp | 18 ++++++++++++++++-- firmware/pwm.h | 9 +++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/firmware/pwm.cpp b/firmware/pwm.cpp index 72e44b8..c2f5f63 100644 --- a/firmware/pwm.cpp +++ b/firmware/pwm.cpp @@ -1,7 +1,5 @@ #include "pwm.h" -#include "hal.h" - Pwm::Pwm(PWMDriver& driver, uint8_t channel, uint32_t counterFrequency, uint32_t counterPeriod) : m_driver(&driver) , m_channel(channel) @@ -10,6 +8,14 @@ Pwm::Pwm(PWMDriver& driver, uint8_t channel, uint32_t counterFrequency, uint32_t { } +Pwm::Pwm(PWMDriver& driver) + : m_driver(&driver) + , m_channel(0) + , m_counterFrequency(0) + , m_counterPeriod(0) +{ +} + void Pwm::Start() { PWMConfig config = { @@ -32,6 +38,14 @@ void Pwm::Start() pwmStart(m_driver, &config); } +void Pwm::Start(PWMConfig *config) +{ + m_counterFrequency = config->frequency; + m_counterPeriod = config->period; + + pwmStart(m_driver, config); +} + float maxF(float i1, float i2) { return i1 > i2 ? i1 : i2; } diff --git a/firmware/pwm.h b/firmware/pwm.h index 834d86f..c878006 100644 --- a/firmware/pwm.h +++ b/firmware/pwm.h @@ -2,14 +2,19 @@ #include +/* for PWMConfig */ +#include "hal.h" + struct PWMDriver; class Pwm { public: Pwm(PWMDriver& driver, uint8_t channel, uint32_t counterFrequency, uint32_t counterPeriod); + Pwm(PWMDriver& driver); void Start(); + void Start(PWMConfig *config); void SetDuty(float duty); void SetDuty(int channel, float duty); float GetLastDuty() const; @@ -17,7 +22,7 @@ public: private: PWMDriver* const m_driver; const uint8_t m_channel; - const uint32_t m_counterFrequency; - const uint16_t m_counterPeriod; + /* const */ uint32_t m_counterFrequency; + /* const */ uint16_t m_counterPeriod; float m_dutyFloat; };