Pulse measurement to know signal occupancy
PwmReader.cpp@0:15aa9d3aeb2e, 2017-01-25 (annotated)
- Committer:
- abouillot
- Date:
- Wed Jan 25 08:52:00 2017 +0000
- Revision:
- 0:15aa9d3aeb2e
- Child:
- 1:ebc39fb22351
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
abouillot | 0:15aa9d3aeb2e | 1 | /*! |
abouillot | 0:15aa9d3aeb2e | 2 | * PwmReader.cpp |
abouillot | 0:15aa9d3aeb2e | 3 | * |
abouillot | 0:15aa9d3aeb2e | 4 | * Read signal occupacy on a pin in a non blocking way using timer and interrupt |
abouillot | 0:15aa9d3aeb2e | 5 | * |
abouillot | 0:15aa9d3aeb2e | 6 | * Copyright (c) 2017 - Alexandre Bouillot github.com/abouillot |
abouillot | 0:15aa9d3aeb2e | 7 | * |
abouillot | 0:15aa9d3aeb2e | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
abouillot | 0:15aa9d3aeb2e | 9 | * of this software and associated documnetation files (the "Software"), to deal |
abouillot | 0:15aa9d3aeb2e | 10 | * in the Software without restriction, including without limitation the rights |
abouillot | 0:15aa9d3aeb2e | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
abouillot | 0:15aa9d3aeb2e | 12 | * copies of the Software, and to permit persons to whom the Software is |
abouillot | 0:15aa9d3aeb2e | 13 | * furished to do so, subject to the following conditions: |
abouillot | 0:15aa9d3aeb2e | 14 | * |
abouillot | 0:15aa9d3aeb2e | 15 | * The above copyright notice and this permission notice shall be included in |
abouillot | 0:15aa9d3aeb2e | 16 | * all copies or substantial portions of the Software. |
abouillot | 0:15aa9d3aeb2e | 17 | * |
abouillot | 0:15aa9d3aeb2e | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
abouillot | 0:15aa9d3aeb2e | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
abouillot | 0:15aa9d3aeb2e | 20 | * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
abouillot | 0:15aa9d3aeb2e | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
abouillot | 0:15aa9d3aeb2e | 22 | * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
abouillot | 0:15aa9d3aeb2e | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
abouillot | 0:15aa9d3aeb2e | 24 | * THE SOFTWARE. |
abouillot | 0:15aa9d3aeb2e | 25 | */ |
abouillot | 0:15aa9d3aeb2e | 26 | |
abouillot | 0:15aa9d3aeb2e | 27 | #include "PwmReader.h" |
abouillot | 0:15aa9d3aeb2e | 28 | |
abouillot | 0:15aa9d3aeb2e | 29 | |
abouillot | 0:15aa9d3aeb2e | 30 | void PwmReader::pressedInt() |
abouillot | 0:15aa9d3aeb2e | 31 | { |
abouillot | 0:15aa9d3aeb2e | 32 | _high += _timer.read_us() - _last_toggle; |
abouillot | 0:15aa9d3aeb2e | 33 | _last_toggle = _timer.read_us(); |
abouillot | 0:15aa9d3aeb2e | 34 | // _timer.reset(); |
abouillot | 0:15aa9d3aeb2e | 35 | // printf("p %ld ", _high); |
abouillot | 0:15aa9d3aeb2e | 36 | // sleep(); |
abouillot | 0:15aa9d3aeb2e | 37 | } |
abouillot | 0:15aa9d3aeb2e | 38 | |
abouillot | 0:15aa9d3aeb2e | 39 | void PwmReader::releasedInt() |
abouillot | 0:15aa9d3aeb2e | 40 | { |
abouillot | 0:15aa9d3aeb2e | 41 | _down += _timer.read_us() - _last_toggle; |
abouillot | 0:15aa9d3aeb2e | 42 | _last_toggle = _timer.read_us(); |
abouillot | 0:15aa9d3aeb2e | 43 | // _timer.reset(); |
abouillot | 0:15aa9d3aeb2e | 44 | // printf("r %ld", _down); |
abouillot | 0:15aa9d3aeb2e | 45 | // sleep(); |
abouillot | 0:15aa9d3aeb2e | 46 | } |
abouillot | 0:15aa9d3aeb2e | 47 | |
abouillot | 0:15aa9d3aeb2e | 48 | void PwmReader::init() |
abouillot | 0:15aa9d3aeb2e | 49 | { |
abouillot | 0:15aa9d3aeb2e | 50 | _pin.fall(callback(this, &PwmReader::pressedInt)); |
abouillot | 0:15aa9d3aeb2e | 51 | _pin.rise(callback(this, &PwmReader::releasedInt)); |
abouillot | 0:15aa9d3aeb2e | 52 | } |
abouillot | 0:15aa9d3aeb2e | 53 | |
abouillot | 0:15aa9d3aeb2e | 54 | void PwmReader::start() |
abouillot | 0:15aa9d3aeb2e | 55 | { |
abouillot | 0:15aa9d3aeb2e | 56 | _high = 0; |
abouillot | 0:15aa9d3aeb2e | 57 | _down = 0; |
abouillot | 0:15aa9d3aeb2e | 58 | _start_state = _pin.read(); |
abouillot | 0:15aa9d3aeb2e | 59 | _pin.enable_irq(); |
abouillot | 0:15aa9d3aeb2e | 60 | _timer.start(); |
abouillot | 0:15aa9d3aeb2e | 61 | } |
abouillot | 0:15aa9d3aeb2e | 62 | |
abouillot | 0:15aa9d3aeb2e | 63 | void PwmReader::stop() |
abouillot | 0:15aa9d3aeb2e | 64 | { |
abouillot | 0:15aa9d3aeb2e | 65 | _pin.disable_irq(); |
abouillot | 0:15aa9d3aeb2e | 66 | |
abouillot | 0:15aa9d3aeb2e | 67 | if (_high == 0 && _down == 0) |
abouillot | 0:15aa9d3aeb2e | 68 | if (_start_state == 0) |
abouillot | 0:15aa9d3aeb2e | 69 | _high = _timer.read_us(); |
abouillot | 0:15aa9d3aeb2e | 70 | else if (_start_state == 1) |
abouillot | 0:15aa9d3aeb2e | 71 | _down = _timer.read_us(); |
abouillot | 0:15aa9d3aeb2e | 72 | _timer.stop(); |
abouillot | 0:15aa9d3aeb2e | 73 | } |