softPWM

Dependents:   2021_NHK_A

Committer:
THtakahiro702286
Date:
Wed Jan 29 09:20:03 2020 +0000
Revision:
0:9a7ca977803b
softPWM

Who changed what in which revision?

UserRevisionLine numberNew contents of line
THtakahiro702286 0:9a7ca977803b 1 #ifndef SoftPWM_H
THtakahiro702286 0:9a7ca977803b 2 #define SoftPWM_H
THtakahiro702286 0:9a7ca977803b 3 #define POSITIVE true
THtakahiro702286 0:9a7ca977803b 4 #define NEGATIVE false
THtakahiro702286 0:9a7ca977803b 5
THtakahiro702286 0:9a7ca977803b 6 #include "mbed.h"
THtakahiro702286 0:9a7ca977803b 7
THtakahiro702286 0:9a7ca977803b 8 class SoftPWM
THtakahiro702286 0:9a7ca977803b 9 {
THtakahiro702286 0:9a7ca977803b 10 private:
THtakahiro702286 0:9a7ca977803b 11 Timeout _timeout;
THtakahiro702286 0:9a7ca977803b 12 Ticker _ticker;
THtakahiro702286 0:9a7ca977803b 13 void end();
THtakahiro702286 0:9a7ca977803b 14 DigitalOut pulse;
THtakahiro702286 0:9a7ca977803b 15 bool positive;
THtakahiro702286 0:9a7ca977803b 16 void TickerInterrapt();
THtakahiro702286 0:9a7ca977803b 17 float width;
THtakahiro702286 0:9a7ca977803b 18 float interval;
THtakahiro702286 0:9a7ca977803b 19 public:
THtakahiro702286 0:9a7ca977803b 20 SoftPWM(PinName,bool mode=true);
THtakahiro702286 0:9a7ca977803b 21 // void attach_us(int);
THtakahiro702286 0:9a7ca977803b 22 void start();
THtakahiro702286 0:9a7ca977803b 23 void write(float);
THtakahiro702286 0:9a7ca977803b 24 float read();
THtakahiro702286 0:9a7ca977803b 25 void pulsewidth(float);
THtakahiro702286 0:9a7ca977803b 26 void pulsewidth_ms(int);
THtakahiro702286 0:9a7ca977803b 27 void pulsewidth_us(int);
THtakahiro702286 0:9a7ca977803b 28 void period(float);
THtakahiro702286 0:9a7ca977803b 29 void period_ms(int);
THtakahiro702286 0:9a7ca977803b 30 void period_us(int);
THtakahiro702286 0:9a7ca977803b 31 void stop();
THtakahiro702286 0:9a7ca977803b 32 operator float() {
THtakahiro702286 0:9a7ca977803b 33 if ( width <= 0.0 ) return 0.0;
THtakahiro702286 0:9a7ca977803b 34 if ( width > 1.0 ) return 1.0;
THtakahiro702286 0:9a7ca977803b 35 return width / interval;
THtakahiro702286 0:9a7ca977803b 36 }
THtakahiro702286 0:9a7ca977803b 37 SoftPWM& operator=(float duty) {
THtakahiro702286 0:9a7ca977803b 38 width = interval * duty;
THtakahiro702286 0:9a7ca977803b 39 if ( duty <= 0.0 ) width = 0.0;
THtakahiro702286 0:9a7ca977803b 40 if ( duty > 1.0 ) width = interval;
THtakahiro702286 0:9a7ca977803b 41 return *this;
THtakahiro702286 0:9a7ca977803b 42 }
THtakahiro702286 0:9a7ca977803b 43
THtakahiro702286 0:9a7ca977803b 44 };
THtakahiro702286 0:9a7ca977803b 45 #endif