Pulse measurement to know signal occupancy

Dependents:   PwmReaderTest

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?

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 0:15aa9d3aeb2e 26
abouillot 0:15aa9d3aeb2e 27 #include "mbed.h"
abouillot 0:15aa9d3aeb2e 28
abouillot 0:15aa9d3aeb2e 29 class PwmReader
abouillot 0:15aa9d3aeb2e 30 {
abouillot 0:15aa9d3aeb2e 31 public:
abouillot 0:15aa9d3aeb2e 32 PwmReader(PinName pin) : _pin(pin) {
abouillot 0:15aa9d3aeb2e 33 init();
abouillot 0:15aa9d3aeb2e 34 };
abouillot 0:15aa9d3aeb2e 35 void start();
abouillot 0:15aa9d3aeb2e 36 void stop();
abouillot 0:15aa9d3aeb2e 37 float occupacyLow() {
abouillot 0:15aa9d3aeb2e 38 return 100.0 * _down / (_down + _high);
abouillot 0:15aa9d3aeb2e 39 };
abouillot 0:15aa9d3aeb2e 40 float occupacyHigh() {
abouillot 0:15aa9d3aeb2e 41 return 100.0 * _high / (_down + _high);
abouillot 0:15aa9d3aeb2e 42 };
abouillot 0:15aa9d3aeb2e 43 long down() {
abouillot 0:15aa9d3aeb2e 44 return _down;
abouillot 0:15aa9d3aeb2e 45 };
abouillot 0:15aa9d3aeb2e 46 float high() {
abouillot 0:15aa9d3aeb2e 47 return _high;
abouillot 0:15aa9d3aeb2e 48 };
abouillot 0:15aa9d3aeb2e 49 private:
abouillot 0:15aa9d3aeb2e 50 void init();
abouillot 0:15aa9d3aeb2e 51 void pressedInt();
abouillot 0:15aa9d3aeb2e 52 void releasedInt();
abouillot 0:15aa9d3aeb2e 53 InterruptIn _pin;
abouillot 0:15aa9d3aeb2e 54 long _high;
abouillot 0:15aa9d3aeb2e 55 long _down;
abouillot 0:15aa9d3aeb2e 56 Timer _timer;
abouillot 0:15aa9d3aeb2e 57 int _start_state;
abouillot 0:15aa9d3aeb2e 58 long _last_toggle;
abouillot 0:15aa9d3aeb2e 59 };