pwm.cpp: allow external config

This commit is contained in:
Andrey Gusakov 2022-05-03 17:18:20 +03:00
parent 27995893ee
commit 70856151b7
2 changed files with 23 additions and 4 deletions

View File

@ -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;
}

View File

@ -2,14 +2,19 @@
#include <cstdint>
/* 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;
};