syouichi imamori
/
MulticopterQuadX
Quad X Type Multicopter
SoftPWM/SoftPWM.cpp@8:1db19b529b22, 2021-02-21 (annotated)
- Committer:
- komaida424
- Date:
- Sun Feb 21 05:14:57 2021 +0000
- Revision:
- 8:1db19b529b22
- Parent:
- 4:4060309b9cc0
rev 020
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
komaida424 | 2:59ac9df97701 | 1 | #include "mbed.h" |
komaida424 | 2:59ac9df97701 | 2 | #include "InterruptIn.h" |
komaida424 | 2:59ac9df97701 | 3 | #include "SoftPWM.h" |
komaida424 | 8:1db19b529b22 | 4 | void wait(float); |
komaida424 | 2:59ac9df97701 | 5 | |
komaida424 | 2:59ac9df97701 | 6 | SoftPWM::SoftPWM(PinName _outpin,bool _positive) : pulse(_outpin) //constructa |
komaida424 | 2:59ac9df97701 | 7 | { |
komaida424 | 2:59ac9df97701 | 8 | if ( _positive ) |
komaida424 | 2:59ac9df97701 | 9 | pulse = 0; |
komaida424 | 2:59ac9df97701 | 10 | else |
komaida424 | 2:59ac9df97701 | 11 | pulse = 1; |
komaida424 | 2:59ac9df97701 | 12 | positive = _positive; |
komaida424 | 2:59ac9df97701 | 13 | interval = 0.02; |
komaida424 | 2:59ac9df97701 | 14 | width = 0; |
komaida424 | 2:59ac9df97701 | 15 | start(); |
komaida424 | 2:59ac9df97701 | 16 | } |
komaida424 | 2:59ac9df97701 | 17 | |
komaida424 | 2:59ac9df97701 | 18 | float SoftPWM::read() |
komaida424 | 2:59ac9df97701 | 19 | { |
komaida424 | 4:4060309b9cc0 | 20 | if ( width <= 0.0f ) return 0.0; |
komaida424 | 4:4060309b9cc0 | 21 | if ( width > 1.0f ) return 1.0; |
komaida424 | 2:59ac9df97701 | 22 | return width / interval; |
komaida424 | 2:59ac9df97701 | 23 | } |
komaida424 | 2:59ac9df97701 | 24 | |
komaida424 | 2:59ac9df97701 | 25 | void SoftPWM::write(float duty) |
komaida424 | 2:59ac9df97701 | 26 | { |
komaida424 | 2:59ac9df97701 | 27 | width = interval * duty; |
komaida424 | 4:4060309b9cc0 | 28 | if ( duty <= 0.0f ) width = 0.0; |
komaida424 | 4:4060309b9cc0 | 29 | if ( duty > 1.0f ) width = interval; |
komaida424 | 2:59ac9df97701 | 30 | } |
komaida424 | 2:59ac9df97701 | 31 | |
komaida424 | 2:59ac9df97701 | 32 | void SoftPWM::start() |
komaida424 | 2:59ac9df97701 | 33 | { |
komaida424 | 8:1db19b529b22 | 34 | uint32_t itv = interval; |
komaida424 | 8:1db19b529b22 | 35 | _ticker.attach(callback(this,&SoftPWM::TickerInterrapt),itv); |
komaida424 | 2:59ac9df97701 | 36 | } |
komaida424 | 2:59ac9df97701 | 37 | |
komaida424 | 2:59ac9df97701 | 38 | void SoftPWM::stop() |
komaida424 | 2:59ac9df97701 | 39 | { |
komaida424 | 2:59ac9df97701 | 40 | _ticker.detach(); |
komaida424 | 2:59ac9df97701 | 41 | if ( positive ) |
komaida424 | 2:59ac9df97701 | 42 | pulse = 0; |
komaida424 | 2:59ac9df97701 | 43 | else |
komaida424 | 2:59ac9df97701 | 44 | pulse = 1; |
komaida424 | 2:59ac9df97701 | 45 | wait(width); |
komaida424 | 2:59ac9df97701 | 46 | } |
komaida424 | 2:59ac9df97701 | 47 | |
komaida424 | 2:59ac9df97701 | 48 | void SoftPWM::period(float _period) |
komaida424 | 2:59ac9df97701 | 49 | { |
komaida424 | 2:59ac9df97701 | 50 | interval = _period; |
komaida424 | 2:59ac9df97701 | 51 | start(); |
komaida424 | 2:59ac9df97701 | 52 | } |
komaida424 | 2:59ac9df97701 | 53 | |
komaida424 | 2:59ac9df97701 | 54 | void SoftPWM::period_ms(int _period) |
komaida424 | 2:59ac9df97701 | 55 | { |
komaida424 | 2:59ac9df97701 | 56 | period((float)_period / 1000); |
komaida424 | 2:59ac9df97701 | 57 | start(); |
komaida424 | 2:59ac9df97701 | 58 | } |
komaida424 | 2:59ac9df97701 | 59 | |
komaida424 | 2:59ac9df97701 | 60 | void SoftPWM::period_us(int _period) |
komaida424 | 2:59ac9df97701 | 61 | { |
komaida424 | 2:59ac9df97701 | 62 | period((float)_period / 1000000); |
komaida424 | 2:59ac9df97701 | 63 | start(); |
komaida424 | 2:59ac9df97701 | 64 | } |
komaida424 | 2:59ac9df97701 | 65 | |
komaida424 | 2:59ac9df97701 | 66 | void SoftPWM::pulsewidth(float _width) |
komaida424 | 2:59ac9df97701 | 67 | { |
komaida424 | 2:59ac9df97701 | 68 | width = _width; |
komaida424 | 4:4060309b9cc0 | 69 | if ( width < 0.0f ) width = 0.0; |
komaida424 | 2:59ac9df97701 | 70 | } |
komaida424 | 2:59ac9df97701 | 71 | |
komaida424 | 2:59ac9df97701 | 72 | void SoftPWM::pulsewidth_ms(int _width) |
komaida424 | 2:59ac9df97701 | 73 | { |
komaida424 | 2:59ac9df97701 | 74 | pulsewidth((float)_width / 1000); |
komaida424 | 2:59ac9df97701 | 75 | } |
komaida424 | 2:59ac9df97701 | 76 | |
komaida424 | 2:59ac9df97701 | 77 | void SoftPWM::pulsewidth_us(int _width) |
komaida424 | 2:59ac9df97701 | 78 | { |
komaida424 | 2:59ac9df97701 | 79 | pulsewidth((float)_width / 1000000); |
komaida424 | 2:59ac9df97701 | 80 | } |
komaida424 | 2:59ac9df97701 | 81 | |
komaida424 | 2:59ac9df97701 | 82 | void SoftPWM::TickerInterrapt() |
komaida424 | 2:59ac9df97701 | 83 | { |
komaida424 | 2:59ac9df97701 | 84 | if ( width <= 0 ) return; |
komaida424 | 8:1db19b529b22 | 85 | _timeout.attach(callback(this,&SoftPWM::end),width); |
komaida424 | 2:59ac9df97701 | 86 | if ( positive ) |
komaida424 | 2:59ac9df97701 | 87 | pulse = 1; |
komaida424 | 2:59ac9df97701 | 88 | else |
komaida424 | 2:59ac9df97701 | 89 | pulse = 0; |
komaida424 | 2:59ac9df97701 | 90 | } |
komaida424 | 2:59ac9df97701 | 91 | |
komaida424 | 2:59ac9df97701 | 92 | void SoftPWM::end() |
komaida424 | 2:59ac9df97701 | 93 | { |
komaida424 | 2:59ac9df97701 | 94 | if ( positive ) |
komaida424 | 2:59ac9df97701 | 95 | pulse = 0; |
komaida424 | 2:59ac9df97701 | 96 | else |
komaida424 | 2:59ac9df97701 | 97 | pulse = 1; |
komaida424 | 2:59ac9df97701 | 98 | // _timeout.detach(); |
komaida424 | 2:59ac9df97701 | 99 | } |
komaida424 | 2:59ac9df97701 | 100 | ; |
komaida424 | 2:59ac9df97701 | 101 |