semf
softpwm.cpp
Go to the documentation of this file.
1
10#include <semf/output/softpwm.h>
12
13namespace semf
14{
15SoftPwm::SoftPwm(Gpio& output, app::TimeBase& timebase, bool inverted)
16: m_out(output, inverted),
17 m_pwmCycleTimer(timebase, 1, false)
18{
19}
20
21void SoftPwm::setMaxValue(unsigned int maxValue)
22{
23 SEMF_INFO("set max value to %u", maxValue);
24 m_maxValue = maxValue;
25}
26
27unsigned int SoftPwm::maxValue() const
28{
29 return m_maxValue;
30}
31
32void SoftPwm::set(unsigned int value, bool inverted)
33{
34 SEMF_INFO("set value to %u and inverted to %d", value, inverted);
35 m_pwmValue = value;
36 m_out.setInverted(inverted);
37 if (!m_pwmCycleTimer.isRunning())
38 {
39 startOnTime();
40 }
41}
42
43unsigned int SoftPwm::value() const
44{
45 return m_pwmValue;
46}
47
49{
50 SEMF_INFO("start");
51 startOnTime();
52}
53
55{
56 SEMF_INFO("stop");
57 m_pwmCycleTimer.stop();
58 m_out.reset();
59}
60
62{
63 return m_pwmCycleTimer.isRunning();
64}
65
66void SoftPwm::startOnTime()
67{
68 if (checkForConstantSetting())
69 return;
70
71 m_pwmCycleTimer.timeout.clear();
72 m_pwmCycleTimer.timeout.connect(m_startOffTimeSlot);
73 m_pwmCycleTimer.setInterval(m_pwmValue);
74 m_out.set();
75 m_pwmCycleTimer.start();
76}
77
78void SoftPwm::startOffTime()
79{
80 if (checkForConstantSetting())
81 return;
82
83 m_pwmCycleTimer.timeout.clear();
84 m_pwmCycleTimer.timeout.connect(m_startOnTimeSlot);
85 m_pwmCycleTimer.setInterval(m_maxValue - m_pwmValue);
86 m_out.reset();
87 m_pwmCycleTimer.start();
88}
89
90bool SoftPwm::checkForConstantSetting()
91{
92 bool constant = false;
93 // Constant off
94 if (m_pwmValue == 0)
95 {
96 m_pwmCycleTimer.stop();
97 m_out.reset();
98 constant = true;
99 }
100 // Constant on
101 else if (m_pwmValue == m_maxValue)
102 {
103 m_pwmCycleTimer.stop();
104 m_out.set();
105 constant = true;
106 }
107 return constant;
108}
109} /* namespace semf */
void set(State state=High) override
Sets the output level of a GPIO pin.
Definition: digitalout.cpp:27
void reset() override
Definition: digitalout.cpp:57
void setInverted(bool inverted) override
Configures the inversion of the output.
Definition: digitalout.cpp:21
Interface class for using a GPIO pin of the microcontroller.
Definition: gpio.h:23
void clear()
Disconnect all functions or methods.
Definition: signal.h:81
void connect(SlotBase< Arguments... > &slot)
Connect a method to the signal.
Definition: signal.h:94
unsigned int value() const override
Returns the current PWM value.
Definition: softpwm.cpp:43
void stop() override
Definition: softpwm.cpp:54
unsigned int maxValue() const override
Returns the maximum possible PWM value.
Definition: softpwm.cpp:27
void setMaxValue(unsigned int maxValue) override
Set the maximum possible PWM value.
Definition: softpwm.cpp:21
void set(unsigned int value, bool inverted=false) override
Set a PWM value. Duty cycle is value / maxValue.
Definition: softpwm.cpp:32
bool isEnabled() const override
Returns the current status of PWM module.
Definition: softpwm.cpp:61
SoftPwm(Gpio &output, app::TimeBase &timebase, bool inverted=false)
Constructor.
Definition: softpwm.cpp:15
void start() override
Definition: softpwm.cpp:48
bool isRunning() const
Check if the timer is running.
void setInterval(uint32_t interval)
Set the timer interval.
void start() override
Start time counting.
void stop() override
A TimeBase is the bridge between e.g. a hardware timer (interrupt service routine) and TickReceiver o...
Definition: timebase.h:36
Signal timeout
Definition: timer.h:36
#define SEMF_INFO(...)
Definition: debug.h:41