
elec350
Fork of elec350 by
soft_pwm.cpp@11:4685f33a2468, 2015-10-21 (annotated)
- Committer:
- rmerrisonhort
- Date:
- Wed Oct 21 20:01:11 2015 +0000
- Revision:
- 11:4685f33a2468
- Parent:
- 10:021f19a9861f
- Child:
- 12:ae626e46b996
Added get* to SoftPwm.
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 | 10:021f19a9861f | 32 | float phase = this->timer.read() / this->period; |
rmerrisonhort | 10:021f19a9861f | 33 | if (phase > 1) { |
rmerrisonhort | 10:021f19a9861f | 34 | this->timer.reset(); |
rmerrisonhort | 10:021f19a9861f | 35 | } |
rmerrisonhort | 10:021f19a9861f | 36 | if (phase < this->dutyCycle) { |
rmerrisonhort | 10:021f19a9861f | 37 | return true; |
rmerrisonhort | 10:021f19a9861f | 38 | } else { |
rmerrisonhort | 10:021f19a9861f | 39 | return false; |
rmerrisonhort | 10:021f19a9861f | 40 | } |
rmerrisonhort | 10:021f19a9861f | 41 | } |
rmerrisonhort | 10:021f19a9861f | 42 |