The PWM output by software. Can be output to any pin. It can be used to replace the Pwmout.
Fork of SoftPWM by
Revision 1:9aba3dc9cd97, committed 2018-06-05
- Comitter:
- haarkon
- Date:
- Tue Jun 05 12:21:14 2018 +0000
- Parent:
- 0:7918ce37626c
- Commit message:
- Lightly modified to correct multiple dumb warning
Changed in this revision
SoftPWM.cpp | Show annotated file Show diff for this revision Revisions of this file |
SoftPWM.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 7918ce37626c -r 9aba3dc9cd97 SoftPWM.cpp --- a/SoftPWM.cpp Wed Oct 23 19:31:14 2013 +0000 +++ b/SoftPWM.cpp Tue Jun 05 12:21:14 2018 +0000 @@ -14,14 +14,14 @@ start(); } -float SoftPWM::read() +double SoftPWM::read() { if ( width <= 0.0 ) return 0.0; if ( width > 1.0 ) return 1.0; return width / interval; } -void SoftPWM::write(float duty) +void SoftPWM::write(double duty) { width = interval * duty; if ( duty <= 0.0 ) width = 0.0; @@ -30,7 +30,7 @@ void SoftPWM::start() { - _ticker.attach(this,&SoftPWM::TickerInterrapt,interval); + _ticker.attach(callback(this,&SoftPWM::TickerInterrapt),interval); } void SoftPWM::stop() @@ -43,7 +43,7 @@ wait(width); } -void SoftPWM::period(float _period) +void SoftPWM::period(double _period) { interval = _period; start(); @@ -51,17 +51,17 @@ void SoftPWM::period_ms(int _period) { - period((float)_period / 1000); + period((double)_period / 1000); start(); } void SoftPWM::period_us(int _period) { - period((float)_period / 1000000); + period((double)_period / 1000000); start(); } -void SoftPWM::pulsewidth(float _width) +void SoftPWM::pulsewidth(double _width) { width = _width; if ( width < 0.0 ) width = 0.0; @@ -69,18 +69,18 @@ void SoftPWM::pulsewidth_ms(int _width) { - pulsewidth((float)_width / 1000); + pulsewidth((double)_width / 1000); } void SoftPWM::pulsewidth_us(int _width) { - pulsewidth((float)_width / 1000000); + pulsewidth((double)_width / 1000000); } void SoftPWM::TickerInterrapt() { if ( width <= 0 ) return; - _timeout.attach(this,&SoftPWM::end,width); + _timeout.attach(callback(this,&SoftPWM::end),width); if ( positive ) pulse = 1; else
diff -r 7918ce37626c -r 9aba3dc9cd97 SoftPWM.h --- a/SoftPWM.h Wed Oct 23 19:31:14 2013 +0000 +++ b/SoftPWM.h Tue Jun 05 12:21:14 2018 +0000 @@ -14,29 +14,29 @@ DigitalOut pulse; bool positive; void TickerInterrapt(); - float width; - float interval; + double width; + double interval; public: SoftPWM(PinName,bool mode=true); // void attach_us(int); void start(); - void write(float); - float read(); - void pulsewidth(float); + void write(double); + double read(); + void pulsewidth(double); void pulsewidth_ms(int); void pulsewidth_us(int); - void period(float); + void period(double); void period_ms(int); void period_us(int); void stop(); - operator float() { + operator double() { if ( width <= 0.0 ) return 0.0; if ( width > 1.0 ) return 1.0; return width / interval; } - SoftPWM& operator=(float duty) { + SoftPWM& operator=(double duty) { width = interval * duty; - if ( duty <= 0.0 ) width = 0.0; + if ( duty <= 0.0 ) width = 0.0f; if ( duty > 1.0 ) width = interval; return *this; }