2020-09-19 15:44:10 -07:00
|
|
|
#include "pwm.h"
|
|
|
|
|
2022-07-16 19:42:30 -07:00
|
|
|
#include <rusefi/math.h>
|
|
|
|
|
2022-06-29 11:39:50 -07:00
|
|
|
Pwm::Pwm(PWMDriver& driver)
|
|
|
|
: m_driver(&driver)
|
|
|
|
, m_counterFrequency(0)
|
|
|
|
, m_counterPeriod(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-12-01 15:51:01 -08:00
|
|
|
void Pwm::Start(const PWMConfig& config)
|
2022-06-29 11:39:50 -07:00
|
|
|
{
|
2022-06-29 11:41:45 -07:00
|
|
|
m_counterFrequency = config.frequency;
|
|
|
|
m_counterPeriod = config.period;
|
2022-06-29 11:39:50 -07:00
|
|
|
|
2022-06-29 11:41:45 -07:00
|
|
|
pwmStart(m_driver, &config);
|
2022-06-29 11:39:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void Pwm::SetDuty(int channel, float duty) {
|
2022-01-26 10:56:58 -08:00
|
|
|
auto dutyFloat = clampF(0, duty, 1);
|
2022-08-29 17:19:30 -07:00
|
|
|
m_dutyFloat[channel] = dutyFloat;
|
2022-01-26 10:56:58 -08:00
|
|
|
pwmcnt_t highTime = m_counterPeriod * dutyFloat;
|
2020-12-15 14:58:39 -08:00
|
|
|
|
2022-08-29 17:19:30 -07:00
|
|
|
pwmEnableChannel(m_driver, channel, highTime);
|
2020-09-19 15:44:10 -07:00
|
|
|
}
|
2020-12-15 14:58:39 -08:00
|
|
|
|
2022-08-29 17:19:30 -07:00
|
|
|
float Pwm::GetLastDuty(int channel)
|
2020-12-15 14:58:39 -08:00
|
|
|
{
|
2022-08-29 17:19:30 -07:00
|
|
|
return m_dutyFloat[channel];
|
2020-12-15 14:58:39 -08:00
|
|
|
}
|