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.
soft_pwm.cpp@21:53c2a598dda1, 2015-12-01 (annotated)
- Committer:
- Oschofield
- Date:
- Tue Dec 01 13:18:49 2015 +0000
- Revision:
- 21:53c2a598dda1
- Parent:
- 16:e9e1b134f498
added the App.h and app.cpp files to library;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Oschofield | 16:e9e1b134f498 | 1 | #include "soft_pwm.h" |
Oschofield | 16:e9e1b134f498 | 2 | |
Oschofield | 16:e9e1b134f498 | 3 | |
Oschofield | 16:e9e1b134f498 | 4 | |
Oschofield | 16:e9e1b134f498 | 5 | SoftPwm::SoftPwm(float initalPeriod, float initialDutyCycle) |
Oschofield | 16:e9e1b134f498 | 6 | { |
Oschofield | 16:e9e1b134f498 | 7 | this->period = initalPeriod; //set initial Period |
Oschofield | 16:e9e1b134f498 | 8 | this->dutyCycle = initialDutyCycle; //set initial Duty Cycle |
Oschofield | 16:e9e1b134f498 | 9 | this->timer.start(); //PWM timer start |
Oschofield | 16:e9e1b134f498 | 10 | } |
Oschofield | 16:e9e1b134f498 | 11 | |
Oschofield | 16:e9e1b134f498 | 12 | float SoftPwm::getPeriod(){ //return current period value |
Oschofield | 16:e9e1b134f498 | 13 | return this->period; |
Oschofield | 16:e9e1b134f498 | 14 | } |
Oschofield | 16:e9e1b134f498 | 15 | |
Oschofield | 16:e9e1b134f498 | 16 | float SoftPwm::getDutyCycle(){ //returns current Duty Cycle value |
Oschofield | 16:e9e1b134f498 | 17 | return this->dutyCycle; |
Oschofield | 16:e9e1b134f498 | 18 | } |
Oschofield | 16:e9e1b134f498 | 19 | |
Oschofield | 16:e9e1b134f498 | 20 | void SoftPwm::setPeriod(float newPeriod){ //updates the period to a specified value |
Oschofield | 16:e9e1b134f498 | 21 | this->period = newPeriod; |
Oschofield | 16:e9e1b134f498 | 22 | return; |
Oschofield | 16:e9e1b134f498 | 23 | } |
Oschofield | 16:e9e1b134f498 | 24 | |
Oschofield | 16:e9e1b134f498 | 25 | void SoftPwm::setDutyCycle(float newDutyCycle){ //updates the Duty cycle to the specified value |
Oschofield | 16:e9e1b134f498 | 26 | this->dutyCycle = newDutyCycle; |
Oschofield | 16:e9e1b134f498 | 27 | } |
Oschofield | 16:e9e1b134f498 | 28 | |
Oschofield | 16:e9e1b134f498 | 29 | bool SoftPwm::isOn(){ |
Oschofield | 16:e9e1b134f498 | 30 | float onPhaseDuration = this->dutyCycle * this->period; |
Oschofield | 16:e9e1b134f498 | 31 | |
Oschofield | 16:e9e1b134f498 | 32 | float currentTime = this->timer.read(); |
Oschofield | 16:e9e1b134f498 | 33 | |
Oschofield | 16:e9e1b134f498 | 34 | if (currentTime > period) this->timer.reset(); |
Oschofield | 16:e9e1b134f498 | 35 | |
Oschofield | 16:e9e1b134f498 | 36 | if (currentTime < onPhaseDuration){ |
Oschofield | 16:e9e1b134f498 | 37 | return true; |
Oschofield | 16:e9e1b134f498 | 38 | } else { |
Oschofield | 16:e9e1b134f498 | 39 | return false; |
Oschofield | 16:e9e1b134f498 | 40 | } |
Oschofield | 16:e9e1b134f498 | 41 | |
Oschofield | 16:e9e1b134f498 | 42 | } |