
elec350
Fork of elec350 by
soft_pwm.cpp@13:95223a7ce05b, 2015-10-21 (annotated)
- Committer:
- rmerrisonhort
- Date:
- Wed Oct 21 22:43:49 2015 +0000
- Revision:
- 13:95223a7ce05b
- Parent:
- 12:ae626e46b996
Bug in soft_pwm.
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 | 11:4685f33a2468 | 20 | float SoftPwm::getDutyCycle() |
rmerrisonhort | 11:4685f33a2468 | 21 | { |
rmerrisonhort | 11:4685f33a2468 | 22 | return this->dutyCycle; |
rmerrisonhort | 11:4685f33a2468 | 23 | } |
rmerrisonhort | 11:4685f33a2468 | 24 | |
rmerrisonhort | 11:4685f33a2468 | 25 | float SoftPwm::getPeriod() |
rmerrisonhort | 11:4685f33a2468 | 26 | { |
rmerrisonhort | 11:4685f33a2468 | 27 | return this->period; |
rmerrisonhort | 11:4685f33a2468 | 28 | } |
rmerrisonhort | 11:4685f33a2468 | 29 | |
rmerrisonhort | 10:021f19a9861f | 30 | bool SoftPwm::isOn() |
rmerrisonhort | 10:021f19a9861f | 31 | { |
rmerrisonhort | 12:ae626e46b996 | 32 | |
rmerrisonhort | 12:ae626e46b996 | 33 | // Calculate the length of the on-phase. |
rmerrisonhort | 12:ae626e46b996 | 34 | float onPhaseDuration = this->dutyCycle * this->period; |
rmerrisonhort | 12:ae626e46b996 | 35 | // Store the current time value in a local variable. |
rmerrisonhort | 12:ae626e46b996 | 36 | float currentTime = this->timer.read(); |
rmerrisonhort | 12:ae626e46b996 | 37 | |
rmerrisonhort | 13:95223a7ce05b | 38 | if (currentTime > this->period) { |
rmerrisonhort | 10:021f19a9861f | 39 | this->timer.reset(); |
rmerrisonhort | 10:021f19a9861f | 40 | } |
rmerrisonhort | 12:ae626e46b996 | 41 | |
rmerrisonhort | 12:ae626e46b996 | 42 | if (currentTime < onPhaseDuration) { |
rmerrisonhort | 10:021f19a9861f | 43 | return true; |
rmerrisonhort | 10:021f19a9861f | 44 | } else { |
rmerrisonhort | 10:021f19a9861f | 45 | return false; |
rmerrisonhort | 10:021f19a9861f | 46 | } |
rmerrisonhort | 10:021f19a9861f | 47 | } |
rmerrisonhort | 10:021f19a9861f | 48 |