
elec350
Fork of elec350 by
soft_pwm.cpp@10:021f19a9861f, 2015-10-21 (annotated)
- Committer:
- rmerrisonhort
- Date:
- Wed Oct 21 19:52:30 2015 +0000
- Revision:
- 10:021f19a9861f
- Child:
- 11:4685f33a2468
Added SoftPwm class.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rmerrisonhort | 10:021f19a9861f | 1 | #include "soft_pwm.h" |
rmerrisonhort | 10:021f19a9861f | 2 | |
rmerrisonhort | 10:021f19a9861f | 3 | SoftPwm::SoftPwm(float initialPeriod, float initialDutyCycle) |
rmerrisonhort | 10:021f19a9861f | 4 | { |
rmerrisonhort | 10:021f19a9861f | 5 | this->period = initialPeriod; |
rmerrisonhort | 10:021f19a9861f | 6 | this->dutyCycle = initialDutyCycle; |
rmerrisonhort | 10:021f19a9861f | 7 | this->timer.start(); |
rmerrisonhort | 10:021f19a9861f | 8 | } |
rmerrisonhort | 10:021f19a9861f | 9 | |
rmerrisonhort | 10:021f19a9861f | 10 | void SoftPwm::setPeriod(float newPeriod) |
rmerrisonhort | 10:021f19a9861f | 11 | { |
rmerrisonhort | 10:021f19a9861f | 12 | this->period = newPeriod; |
rmerrisonhort | 10:021f19a9861f | 13 | } |
rmerrisonhort | 10:021f19a9861f | 14 | |
rmerrisonhort | 10:021f19a9861f | 15 | void SoftPwm::setDutyCycle(float newDutyCycle) |
rmerrisonhort | 10:021f19a9861f | 16 | { |
rmerrisonhort | 10:021f19a9861f | 17 | this->dutyCycle = newDutyCycle; |
rmerrisonhort | 10:021f19a9861f | 18 | } |
rmerrisonhort | 10:021f19a9861f | 19 | |
rmerrisonhort | 10:021f19a9861f | 20 | bool SoftPwm::isOn() |
rmerrisonhort | 10:021f19a9861f | 21 | { |
rmerrisonhort | 10:021f19a9861f | 22 | float phase = this->timer.read() / this->period; |
rmerrisonhort | 10:021f19a9861f | 23 | if (phase > 1) { |
rmerrisonhort | 10:021f19a9861f | 24 | this->timer.reset(); |
rmerrisonhort | 10:021f19a9861f | 25 | } |
rmerrisonhort | 10:021f19a9861f | 26 | if (phase < this->dutyCycle) { |
rmerrisonhort | 10:021f19a9861f | 27 | return true; |
rmerrisonhort | 10:021f19a9861f | 28 | } else { |
rmerrisonhort | 10:021f19a9861f | 29 | return false; |
rmerrisonhort | 10:021f19a9861f | 30 | } |
rmerrisonhort | 10:021f19a9861f | 31 | } |
rmerrisonhort | 10:021f19a9861f | 32 |