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.
Revision 0:813ee8141cdd, committed 2015-09-03
- Comitter:
- gagarcr
- Date:
- Thu Sep 03 08:15:34 2015 +0000
- Commit message:
- Intial version based on SoftPWM
Changed in this revision
| SoftPwmOut.cpp | Show annotated file Show diff for this revision Revisions of this file |
| SoftPwmOut.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/SoftPwmOut.cpp Thu Sep 03 08:15:34 2015 +0000
@@ -0,0 +1,83 @@
+#include "mbed.h"
+#include "InterruptIn.h"
+#include "SoftPwmOut.h"
+
+SoftPwmOut::SoftPwmOut(PinName pin) : _outPin(pin) // Constructor definition. Assign _out
+{
+ _interval = 0.1; // Default fequency
+ _width = 0;
+ start();
+}
+
+float SoftPwmOut::read()
+{
+ return _width / _interval;
+}
+
+void SoftPwmOut::write(float duty)
+{
+ _width = _interval * duty;
+ if ( duty <= 0.0 ) _width = 0.0;
+ if ( duty > 1.0 ) _width = _interval;
+}
+
+void SoftPwmOut::start()
+{
+ _ticker.attach(this,&SoftPwmOut::TickerInterrupt,_interval);
+}
+
+void SoftPwmOut::stop()
+{
+ _ticker.detach();
+ //wait(width);
+}
+
+void SoftPwmOut::period(float period)
+{
+ _interval = period;
+ start();
+}
+
+void SoftPwmOut::period_ms(int period)
+{
+ SoftPwmOut::period((float)period / 1000);
+ start();
+}
+
+void SoftPwmOut::period_us(int period)
+{
+ SoftPwmOut::period((float)period / 1000000);
+ start();
+}
+
+void SoftPwmOut::pulsewidth(float width)
+{
+ _width = width;
+ if ( _width < 0.0 ) _width = 0.0;
+ if ( _width > _interval) _width = _interval;
+}
+
+void SoftPwmOut::pulsewidth_ms(int width)
+{
+ SoftPwmOut::pulsewidth((float)width / 1000);
+}
+
+void SoftPwmOut::pulsewidth_us(int width)
+{
+ SoftPwmOut::pulsewidth((float)width / 1000000);
+}
+
+void SoftPwmOut::TickerInterrupt()
+{
+ _outPin = 0;
+ if ( _width <= 0 ) return;
+ _timeout.attach(this,&SoftPwmOut::end,_width);
+}
+
+void SoftPwmOut::end()
+{
+ _outPin = 1;
+// _timeout.detach();
+}
+;
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/SoftPwmOut.h Thu Sep 03 08:15:34 2015 +0000
@@ -0,0 +1,45 @@
+#ifndef SoftPWM_H
+#define SoftPWM_H
+#define POSITIVE true
+#define NEGATIVE false
+
+#include "mbed.h"
+
+class SoftPwmOut
+{
+public:
+ SoftPwmOut(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.0 ) return 0.0;
+ if ( _width > 1.0 ) return 1.0;
+ return _width / _interval;
+ }
+ SoftPwmOut& operator=(float duty) {
+ _width = _interval * duty;
+ if ( duty <= 0.0 ) _width = 0.0;
+ if ( duty > 1.0 ) _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