semf
pwmout.cpp
Go to the documentation of this file.
1
10#include <semf/output/pwmout.h>
12
13namespace semf
14{
16: m_pwm(pwm)
17{
18}
19
20void PwmOut::enable(bool enable)
21{
22 SEMF_INFO("set to: %d", enable);
23 if (enable && !isEnabled())
24 {
25 SEMF_INFO("start pwm");
26 m_pwm.start();
27 }
28 else if (!enable && isEnabled())
29 {
30 SEMF_INFO("stop pwm");
31 m_pwm.stop();
32 }
33}
34
35void PwmOut::disable(bool disable)
36{
38}
39
41{
42 return m_pwm.isEnabled();
43}
44
46{
47 enable();
48}
49
51{
52 disable();
53}
54
55void PwmOut::setValue(uint32_t pwm)
56{
57 if (pwm > m_pwm.maxValue())
58 {
59 SEMF_INFO("reduced pwm %u to max %u", pwm, m_pwm.maxValue());
60 pwm = m_pwm.maxValue();
61 }
62 SEMF_INFO("set to %u", pwm);
63 m_pwm.set(pwm, m_inverted);
64}
65
66uint32_t PwmOut::value() const
67{
68 return m_pwm.value();
69}
70
71void PwmOut::setPermille(uint16_t pwm)
72{
73 if (pwm > 1000)
74 {
75 SEMF_INFO("reduced pwm %u to max 1000", pwm);
76 pwm = 1000;
77 }
78 setValue(static_cast<uint32_t>(static_cast<uint64_t>(m_pwm.maxValue()) * pwm / 1000));
79}
80
81uint16_t PwmOut::permille() const
82{
83 return static_cast<uint16_t>(m_pwm.value() * 1000ULL / m_pwm.maxValue());
84}
85
86void PwmOut::setMaxValue(uint32_t pwm)
87{
88 // min 1 to prevent divide by zero
89 if (pwm == 0)
90 {
91 SEMF_INFO("set max value to 1 instead of 0");
92 m_pwm.setMaxValue(1);
93 }
94 else
95 {
96 SEMF_INFO("set max value to %u", pwm);
97 m_pwm.setMaxValue(pwm);
98 }
99}
100
101uint32_t PwmOut::maxValue() const
102{
103 return m_pwm.maxValue();
104}
105
106void PwmOut::setInverted(bool inverted)
107{
108 SEMF_INFO("set inverted to %d", inverted);
109 m_inverted = inverted;
110}
111} /* namespace semf */
Class to interface pwm's hardware module of the microcontroller.
Definition: pwm.h:23
virtual void start()=0
virtual unsigned int value() const =0
Returns the current PWM value.
virtual void set(unsigned int value, bool inverted=false)=0
Set a PWM value. Duty cycle is value / maxValue.
virtual bool isEnabled() const =0
Returns the current status of PWM module.
virtual void stop()=0
virtual void setMaxValue(unsigned int maxValue)=0
Set the maximum possible PWM value.
virtual unsigned int maxValue() const =0
Returns the maximum possible PWM value.
void enable(bool enable=true) override
Enables / disable the PWM output.
Definition: pwmout.cpp:20
bool isEnabled() const override
Current status of the PWM module.
Definition: pwmout.cpp:40
uint32_t value() const override
Returns the current PWM value.
Definition: pwmout.cpp:66
void stop() override
Disable the PWM.
Definition: pwmout.cpp:50
uint16_t permille() const override
Returns the current PWM value in permille. The PWM value is calculated based on the set maximum value...
Definition: pwmout.cpp:81
void disable(bool disable=true) override
Enables / disable the PWM output.
Definition: pwmout.cpp:35
void start() override
Enables the PWM.
Definition: pwmout.cpp:45
void setPermille(uint16_t pwm) override
Set the PWM value in permille. The pwm value is calculated based on the set maximum value.
Definition: pwmout.cpp:71
void setMaxValue(uint32_t pwm) override
Set the maximum value for the PWM.
Definition: pwmout.cpp:86
PwmOut(Pwm &pwm)
Constructor.
Definition: pwmout.cpp:15
uint32_t maxValue() const override
Returns the maximum possible PWM value.
Definition: pwmout.cpp:101
void setInverted(bool inverted) override
To configure whether the PWM should be inverted. This configuration does not become active until the ...
Definition: pwmout.cpp:106
void setValue(uint32_t pwm) override
Change the PWM value.
Definition: pwmout.cpp:55
#define SEMF_INFO(...)
Definition: debug.h:41