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.
Diff: SlowPWM.cpp
- Revision:
- 0:76861123625b
- Child:
- 1:386d04fe1e37
diff -r 000000000000 -r 76861123625b SlowPWM.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/SlowPWM.cpp Tue Jun 18 10:52:51 2019 +0000
@@ -0,0 +1,59 @@
+#include "slowPWM.h"
+
+slowPWM::slowPWM(const PinName pin ):DigitalOut(pin)
+{
+ _repeatTime=1;
+ _timeOn=0.5f;
+}
+
+slowPWM::slowPWM( float period, float highTime, const PinName pin ):DigitalOut(pin)
+{
+ setPeriod(period);
+ setHighTime(highTime);
+ start();
+}
+
+void slowPWM::setPeriod(float period)
+{
+ _repeatTime = period;
+ if (_repeatTime <= 0) // check it's not 0 or negative
+ _repeatTime = 1;
+ setHighTime(_timeOn); // perform sanity check on high time
+}
+
+void slowPWM::setHighTime(float highTime)
+{
+ _timeOn = highTime;
+ if (_timeOn >= _repeatTime) // check it's not more than the cycle time.
+ _timeOn = _repeatTime/2; // set to 50% if invalid
+}
+
+void slowPWM::setDutyCycle(float cycle) {
+ if ((cycle >=0) && (cycle <=1)) {
+ _timeOn = _repeatTime*cycle;
+ } else
+ _timeOn = _repeatTime/2; // set to 50% if invalid
+}
+
+void slowPWM::stop()
+{
+ cycleTimer.detach();
+}
+
+// start things.
+void slowPWM::start()
+{
+ cycleTimer.attach(callback(this,&slowPWM::onCycleStart),_repeatTime);
+ onCycleStart();
+}
+
+void slowPWM::onTurnOff(void)
+{
+ write(0);
+}
+
+void slowPWM::onCycleStart(void)
+{
+ offTimer.attach(callback(this,&slowPWM::onTurnOff),_timeOn);
+ write(1);
+}