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