Andy A / SlowPWM
Revision:
2:c90e2d2f52aa
Parent:
1:386d04fe1e37
Child:
3:3f7eb3ad23d4
diff -r 386d04fe1e37 -r c90e2d2f52aa SlowPWM.cpp
--- a/SlowPWM.cpp	Tue Jun 18 16:22:56 2019 +0000
+++ b/SlowPWM.cpp	Fri Jun 21 09:33:26 2019 +0000
@@ -6,14 +6,14 @@
     _timeOn=0.5f;
 }
  
-SlowPWM::SlowPWM( float period, float highTime, const PinName pin ):DigitalOut(pin)
+SlowPWM::SlowPWM( const PinName pin, const float period, const float highTime ):DigitalOut(pin)
 {
     setPeriod(period);
     setHighTime(highTime);
     start();
 }
  
-void SlowPWM::setPeriod(float period)
+void SlowPWM::setPeriod(const float period)
 {
     _repeatTime = period;
     if (_repeatTime <= 0)   // check it's not 0 or negative
@@ -21,22 +21,30 @@
     setHighTime(_timeOn);  // perform sanity check on high time
 }
  
-void SlowPWM::setHighTime(float highTime)
+void SlowPWM::setHighTime(const 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)) {
+void SlowPWM::setDutyCycle(const float cycle) {
+        if (cycle == 1) {
+          stop();
+          write(1);
+          } else if (cycle == 0) {
+              stop();
+              write(0);
+        } else if ((cycle >0) && (cycle <1)) {
         _timeOn = _repeatTime*cycle; 
+        start();
         } else
         _timeOn = _repeatTime/2; // set to 50% if invalid 
 }
  
 void SlowPWM::stop()
 {
+    offTimer.detach();
     cycleTimer.detach();
 }