Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ele350 by
soft_pwm.cpp@7:04e1552c7c95, 2016-10-18 (annotated)
- Committer:
- yyue
- Date:
- Tue Oct 18 17:28:08 2016 +0000
- Revision:
- 7:04e1552c7c95
- Parent:
- 5:e820e1348f98
created
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GGHHHH | 3:1ad50b4e51a6 | 1 | #include "soft_pwm.h" |
GGHHHH | 3:1ad50b4e51a6 | 2 | |
GGHHHH | 3:1ad50b4e51a6 | 3 | |
GGHHHH | 3:1ad50b4e51a6 | 4 | float SoftPwm::getPeriod() |
GGHHHH | 3:1ad50b4e51a6 | 5 | { |
GGHHHH | 3:1ad50b4e51a6 | 6 | return this->period; |
GGHHHH | 3:1ad50b4e51a6 | 7 | } |
GGHHHH | 3:1ad50b4e51a6 | 8 | |
GGHHHH | 3:1ad50b4e51a6 | 9 | float SoftPwm::getDutyCycle() |
GGHHHH | 3:1ad50b4e51a6 | 10 | { |
GGHHHH | 3:1ad50b4e51a6 | 11 | return this->dutyCycle; |
GGHHHH | 3:1ad50b4e51a6 | 12 | } |
GGHHHH | 3:1ad50b4e51a6 | 13 | |
GGHHHH | 3:1ad50b4e51a6 | 14 | void SoftPwm::setPeriod(float newPeriod) |
GGHHHH | 3:1ad50b4e51a6 | 15 | { |
GGHHHH | 3:1ad50b4e51a6 | 16 | this->period = newPeriod; |
GGHHHH | 3:1ad50b4e51a6 | 17 | } |
GGHHHH | 3:1ad50b4e51a6 | 18 | |
GGHHHH | 3:1ad50b4e51a6 | 19 | void SoftPwm::setDutyCycle(float newDutyCycle) |
GGHHHH | 3:1ad50b4e51a6 | 20 | { |
GGHHHH | 3:1ad50b4e51a6 | 21 | this->dutyCycle = newDutyCycle; |
GGHHHH | 3:1ad50b4e51a6 | 22 | } |
GGHHHH | 3:1ad50b4e51a6 | 23 | SoftPwm::SoftPwm(float initialPeriod, float initialDutyCycle) |
GGHHHH | 3:1ad50b4e51a6 | 24 | { |
GGHHHH | 3:1ad50b4e51a6 | 25 | |
GGHHHH | 3:1ad50b4e51a6 | 26 | this->period = initialPeriod; |
GGHHHH | 3:1ad50b4e51a6 | 27 | this->dutyCycle = initialDutyCycle; |
GGHHHH | 3:1ad50b4e51a6 | 28 | |
GGHHHH | 5:e820e1348f98 | 29 | |
GGHHHH | 3:1ad50b4e51a6 | 30 | this->timer.start(); |
GGHHHH | 5:e820e1348f98 | 31 | } |
GGHHHH | 3:1ad50b4e51a6 | 32 | bool SoftPwm::isOn() |
GGHHHH | 5:e820e1348f98 | 33 | { |
GGHHHH | 3:1ad50b4e51a6 | 34 | float onPhaseDuration = this->dutyCycle * this->period; |
GGHHHH | 5:e820e1348f98 | 35 | float currentTime = this->timer.read(); |
yyue | 7:04e1552c7c95 | 36 | if (currentTime > onPhaseDuration){ |
yyue | 7:04e1552c7c95 | 37 | this->timer.start(); |
yyue | 7:04e1552c7c95 | 38 | } |
GGHHHH | 3:1ad50b4e51a6 | 39 | if (currentTime < onPhaseDuration){ |
GGHHHH | 3:1ad50b4e51a6 | 40 | return true; |
GGHHHH | 3:1ad50b4e51a6 | 41 | }else { |
GGHHHH | 3:1ad50b4e51a6 | 42 | return false; |
GGHHHH | 3:1ad50b4e51a6 | 43 | } |
GGHHHH | 3:1ad50b4e51a6 | 44 | } |