Pulse measurement to know signal occupancy

Dependents:   PwmReaderTest

Committer:
abouillot
Date:
Thu Jan 26 08:26:22 2017 +0000
Revision:
1:ebc39fb22351
Parent:
0:15aa9d3aeb2e
Fixed few bugs and added inline documentation. It is now possible to retrieve the time spent in a state in us, ms and seconds.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
abouillot 0:15aa9d3aeb2e 1 /*!
abouillot 0:15aa9d3aeb2e 2 * PwmReader.h
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 1:ebc39fb22351 26
abouillot 1:ebc39fb22351 27 #include "mbed.h"
abouillot 1:ebc39fb22351 28
abouillot 1:ebc39fb22351 29 /*!
abouillot 1:ebc39fb22351 30 * Class PwmReader lmeasure the time spend in high and low state on a digital pin
abouillot 1:ebc39fb22351 31 * Uses useconds timer and IRQ
abouillot 1:ebc39fb22351 32 */
abouillot 0:15aa9d3aeb2e 33 class PwmReader
abouillot 0:15aa9d3aeb2e 34 {
abouillot 0:15aa9d3aeb2e 35 public:
abouillot 1:ebc39fb22351 36 /** create a PwmReader object
abouillot 1:ebc39fb22351 37 *
abouillot 1:ebc39fb22351 38 * @param pin Pin numer to be observed
abouillot 1:ebc39fb22351 39 */
abouillot 0:15aa9d3aeb2e 40 PwmReader(PinName pin) : _pin(pin) {
abouillot 0:15aa9d3aeb2e 41 init();
abouillot 0:15aa9d3aeb2e 42 };
abouillot 1:ebc39fb22351 43 /** Start the measurment
abouillot 1:ebc39fb22351 44 *
abouillot 1:ebc39fb22351 45 */
abouillot 0:15aa9d3aeb2e 46 void start();
abouillot 1:ebc39fb22351 47 /** Stop the measurment
abouillot 1:ebc39fb22351 48 *
abouillot 1:ebc39fb22351 49 */
abouillot 0:15aa9d3aeb2e 50 void stop();
abouillot 1:ebc39fb22351 51 /** return the time spend in low state
abouillot 1:ebc39fb22351 52 *
abouillot 1:ebc39fb22351 53 * @return the percentage of time spent in low state. value between 0.0 and 1.0
abouillot 1:ebc39fb22351 54 */
abouillot 0:15aa9d3aeb2e 55 float occupacyLow() {
abouillot 0:15aa9d3aeb2e 56 return 100.0 * _down / (_down + _high);
abouillot 0:15aa9d3aeb2e 57 };
abouillot 1:ebc39fb22351 58 /** return the time spend in high state
abouillot 1:ebc39fb22351 59 *
abouillot 1:ebc39fb22351 60 * @return the percentage of time spent in high state. value between 0.0 and 1.0
abouillot 1:ebc39fb22351 61 */
abouillot 0:15aa9d3aeb2e 62 float occupacyHigh() {
abouillot 0:15aa9d3aeb2e 63 return 100.0 * _high / (_down + _high);
abouillot 0:15aa9d3aeb2e 64 };
abouillot 1:ebc39fb22351 65 /** return the time spend in low state
abouillot 1:ebc39fb22351 66 *
abouillot 1:ebc39fb22351 67 * @return the percentage of time spent in low state. value in useconds
abouillot 1:ebc39fb22351 68 */
abouillot 1:ebc39fb22351 69 long down_us() {
abouillot 0:15aa9d3aeb2e 70 return _down;
abouillot 0:15aa9d3aeb2e 71 };
abouillot 1:ebc39fb22351 72 /** return the time spend in high state
abouillot 1:ebc39fb22351 73 *
abouillot 1:ebc39fb22351 74 * @return the percentage of time spent in high state. value in useconds
abouillot 1:ebc39fb22351 75 */
abouillot 1:ebc39fb22351 76 long high_us() {
abouillot 0:15aa9d3aeb2e 77 return _high;
abouillot 0:15aa9d3aeb2e 78 };
abouillot 1:ebc39fb22351 79 /** return the time spend in low state
abouillot 1:ebc39fb22351 80 * @param round set round to tue to round the value (closest integer) - false by default
abouillot 1:ebc39fb22351 81 * @return the time spent in low state. value in mseconds
abouillot 1:ebc39fb22351 82 */
abouillot 1:ebc39fb22351 83 long down_ms(bool round = false) {
abouillot 1:ebc39fb22351 84 return (_down + (round ? 500 : 0)) / 1000;
abouillot 1:ebc39fb22351 85 };
abouillot 1:ebc39fb22351 86 /** return the time spend in high state
abouillot 1:ebc39fb22351 87 * @param round set round to tue to round the value (closest integer) - false by default
abouillot 1:ebc39fb22351 88 * @return the time spent in high state. value in mseconds
abouillot 1:ebc39fb22351 89 */
abouillot 1:ebc39fb22351 90 long high_ms(bool round = false) {
abouillot 1:ebc39fb22351 91 return (_high + (round ? 500 : 0)) / 1000;
abouillot 1:ebc39fb22351 92 };
abouillot 1:ebc39fb22351 93 /** return the time spend in low state
abouillot 1:ebc39fb22351 94 * @param round set round to tue to round the value (closest integer) - false by default
abouillot 1:ebc39fb22351 95 * @return the time spent in low state. value in seconds
abouillot 1:ebc39fb22351 96 */
abouillot 1:ebc39fb22351 97 long down(bool round = false) {
abouillot 1:ebc39fb22351 98 return (_down + (round ? 500000 : 0)) / 1000000;
abouillot 1:ebc39fb22351 99 };
abouillot 1:ebc39fb22351 100 /** return the time spend in high state
abouillot 1:ebc39fb22351 101 * @param round set round to tue to round the value (closest integer) - false by default
abouillot 1:ebc39fb22351 102 * @return the time spent in high state. value in seconds
abouillot 1:ebc39fb22351 103 */
abouillot 1:ebc39fb22351 104 long high(bool round = false) {
abouillot 1:ebc39fb22351 105 return (_high + (round ? 500000 : 0)) / 1000000;
abouillot 1:ebc39fb22351 106 };
abouillot 0:15aa9d3aeb2e 107 private:
abouillot 0:15aa9d3aeb2e 108 void init();
abouillot 0:15aa9d3aeb2e 109 void pressedInt();
abouillot 0:15aa9d3aeb2e 110 void releasedInt();
abouillot 0:15aa9d3aeb2e 111 InterruptIn _pin;
abouillot 0:15aa9d3aeb2e 112 long _high;
abouillot 0:15aa9d3aeb2e 113 long _down;
abouillot 0:15aa9d3aeb2e 114 Timer _timer;
abouillot 0:15aa9d3aeb2e 115 int _start_state;
abouillot 0:15aa9d3aeb2e 116 long _last_toggle;
abouillot 0:15aa9d3aeb2e 117 };