Une version perso de PwmOut
Une version à peine modifié de la librairie PwmOut
Revision 0:b2ab9e77dd7e, committed 2017-05-26
- Comitter:
- jmambroi
- Date:
- Fri May 26 14:15:14 2017 +0000
- Commit message:
- Ok
Changed in this revision
JMAPwmOut.cpp | Show annotated file Show diff for this revision Revisions of this file |
JMAPwmOut.hpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r b2ab9e77dd7e JMAPwmOut.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/JMAPwmOut.cpp Fri May 26 14:15:14 2017 +0000 @@ -0,0 +1,71 @@ +#include "mbed.h" +#include "InterruptIn.h" +#include "JMAPwmOut.hpp" + +JMAPwmOut::JMAPwmOut(PinName pin) : _outPin(pin) { // Constructor definition. Assign _out + + _interval = 0.1; // Default fequency + _width = 0; + start(); +} + +float JMAPwmOut::read() { + return _width / _interval; +} + +void JMAPwmOut::write(float duty) { + _width = _interval * duty; + if ( duty <= 0 ) _width = 0.0; + if ( duty > 1 ) _width = _interval; +} + +void JMAPwmOut::start() { + _ticker.attach(this,&JMAPwmOut::TickerInterrupt,_interval); +} + +void JMAPwmOut::stop() { + _ticker.detach(); + //wait(width); +} + +void JMAPwmOut::period(float period) { + _interval = period; + start(); +} + +void JMAPwmOut::period_ms(int period) { + JMAPwmOut::period((float)period / 1000); + start(); +} + +void JMAPwmOut::period_us(int period) { + JMAPwmOut::period((float)period / 1000000); + start(); +} + +void JMAPwmOut::pulsewidth(float width) { + _width = width; + if ( _width < 0) _width = 0.0; + if ( _width > _interval) _width = _interval; +} + +void JMAPwmOut::pulsewidth_ms(int width) { + JMAPwmOut::pulsewidth((float)width / 1000); +} + +void JMAPwmOut::pulsewidth_us(int width) { + JMAPwmOut::pulsewidth((float)width / 1000000); +} + +void JMAPwmOut::TickerInterrupt() { + _outPin = 0; + if ( _width <= 0 ) return; + _timeout.attach(this,&JMAPwmOut::end,_width); +} + +void JMAPwmOut::end() { + _outPin = 1; +// _timeout.detach(); +} +; +
diff -r 000000000000 -r b2ab9e77dd7e JMAPwmOut.hpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/JMAPwmOut.hpp Fri May 26 14:15:14 2017 +0000 @@ -0,0 +1,45 @@ +#ifndef _JMAPWMOUT_HPP_ +#define _JMAPWMOUT_HPP_ + +#define POSITIVE true +#define NEGATIVE false + +#include "mbed.h" + +class JMAPwmOut { + public : + JMAPwmOut(PinName); +// void attach_us(int); + void start(); + void write(float); + float read(); + void pulsewidth(float); + void pulsewidth_ms(int); + void pulsewidth_us(int); + void period(float); + void period_ms(int); + void period_us(int); + void stop(); + operator float() { + if ( _width <= 0 ) return 0.0; + if ( _width > 1 ) return 1.0; + return _width / _interval; + } + JMAPwmOut& operator=(float duty) { + _width = _interval * duty; + if ( duty <= 0 ) _width = 0.0; + if ( duty > 1 ) _width = _interval; + return *this; + } + + private : + Timeout _timeout; + Ticker _ticker; + void TickerInterrupt(); + void end(); + DigitalOut _outPin; + float _width; + float _interval; + +}; +#endif \ No newline at end of file