softpwm only a little change
Fork of SoftPWM by
SoftPWM.h@1:6d557e90686e, 2017-12-15 (annotated)
- Committer:
- wanzq
- Date:
- Fri Dec 15 17:26:23 2017 +0000
- Revision:
- 1:6d557e90686e
- Parent:
- 0:7918ce37626c
softpwm changed a little
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
komaida424 | 0:7918ce37626c | 1 | #ifndef SoftPWM_H |
komaida424 | 0:7918ce37626c | 2 | #define SoftPWM_H |
komaida424 | 0:7918ce37626c | 3 | #define POSITIVE true |
komaida424 | 0:7918ce37626c | 4 | #define NEGATIVE false |
komaida424 | 0:7918ce37626c | 5 | |
komaida424 | 0:7918ce37626c | 6 | #include "mbed.h" |
komaida424 | 0:7918ce37626c | 7 | |
komaida424 | 0:7918ce37626c | 8 | class SoftPWM |
komaida424 | 0:7918ce37626c | 9 | { |
komaida424 | 0:7918ce37626c | 10 | private: |
komaida424 | 0:7918ce37626c | 11 | Timeout _timeout; |
komaida424 | 0:7918ce37626c | 12 | Ticker _ticker; |
komaida424 | 0:7918ce37626c | 13 | void end(); |
komaida424 | 0:7918ce37626c | 14 | DigitalOut pulse; |
komaida424 | 0:7918ce37626c | 15 | bool positive; |
komaida424 | 0:7918ce37626c | 16 | void TickerInterrapt(); |
komaida424 | 0:7918ce37626c | 17 | float width; |
komaida424 | 0:7918ce37626c | 18 | float interval; |
komaida424 | 0:7918ce37626c | 19 | public: |
komaida424 | 0:7918ce37626c | 20 | SoftPWM(PinName,bool mode=true); |
komaida424 | 0:7918ce37626c | 21 | // void attach_us(int); |
komaida424 | 0:7918ce37626c | 22 | void start(); |
komaida424 | 0:7918ce37626c | 23 | void write(float); |
komaida424 | 0:7918ce37626c | 24 | float read(); |
komaida424 | 0:7918ce37626c | 25 | void pulsewidth(float); |
komaida424 | 0:7918ce37626c | 26 | void pulsewidth_ms(int); |
komaida424 | 0:7918ce37626c | 27 | void pulsewidth_us(int); |
komaida424 | 0:7918ce37626c | 28 | void period(float); |
komaida424 | 0:7918ce37626c | 29 | void period_ms(int); |
komaida424 | 0:7918ce37626c | 30 | void period_us(int); |
komaida424 | 0:7918ce37626c | 31 | void stop(); |
komaida424 | 0:7918ce37626c | 32 | operator float() { |
komaida424 | 0:7918ce37626c | 33 | if ( width <= 0.0 ) return 0.0; |
komaida424 | 0:7918ce37626c | 34 | if ( width > 1.0 ) return 1.0; |
komaida424 | 0:7918ce37626c | 35 | return width / interval; |
komaida424 | 0:7918ce37626c | 36 | } |
komaida424 | 0:7918ce37626c | 37 | SoftPWM& operator=(float duty) { |
komaida424 | 0:7918ce37626c | 38 | width = interval * duty; |
komaida424 | 0:7918ce37626c | 39 | if ( duty <= 0.0 ) width = 0.0; |
komaida424 | 0:7918ce37626c | 40 | if ( duty > 1.0 ) width = interval; |
komaida424 | 0:7918ce37626c | 41 | return *this; |
komaida424 | 0:7918ce37626c | 42 | } |
komaida424 | 0:7918ce37626c | 43 | |
komaida424 | 0:7918ce37626c | 44 | }; |
komaida424 | 0:7918ce37626c | 45 | #endif |