Counts interrupt driven pulses (UP or DOWN) with min and max width

Fork of PulseCounter by Wim De Roeve

Committer:
Mehrad
Date:
Fri Jun 10 00:05:35 2016 +0000
Revision:
1:54e8663265c3
Parent:
0:d534558752b8
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wimpie 0:d534558752b8 1 /*
Wimpie 0:d534558752b8 2 PulseCounter
Wimpie 0:d534558752b8 3 interrrupt driven counter of edges (UP or DOWN)
Wimpie 0:d534558752b8 4 with min en max width
Wimpie 0:d534558752b8 5
Wimpie 0:d534558752b8 6 Copyright (c) 2010 Wim De Roeve
Wimpie 0:d534558752b8 7
Wimpie 0:d534558752b8 8 Permission is hereby granted, free of charge, to any person obtaining a copy
Wimpie 0:d534558752b8 9 of this software and associated documentation files (the "Software"), to deal
Wimpie 0:d534558752b8 10 in the Software without restriction, including without limitation the rights
Wimpie 0:d534558752b8 11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Wimpie 0:d534558752b8 12 copies of the Software, and to permit persons to whom the Software is
Wimpie 0:d534558752b8 13 furnished to do so, subject to the following conditions:
Wimpie 0:d534558752b8 14
Wimpie 0:d534558752b8 15 The above copyright notice and this permission notice shall be included in
Wimpie 0:d534558752b8 16 all copies or substantial portions of the Software.
Wimpie 0:d534558752b8 17
Wimpie 0:d534558752b8 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Wimpie 0:d534558752b8 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Wimpie 0:d534558752b8 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Wimpie 0:d534558752b8 21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Wimpie 0:d534558752b8 22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Wimpie 0:d534558752b8 23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Wimpie 0:d534558752b8 24 THE SOFTWARE.
Wimpie 0:d534558752b8 25 */
Wimpie 0:d534558752b8 26
Wimpie 0:d534558752b8 27 #ifndef PULSECOUNTER_H
Wimpie 0:d534558752b8 28 #define PULSECOUNTER_H
Wimpie 0:d534558752b8 29
Wimpie 0:d534558752b8 30 #include "mbed.h"
Wimpie 0:d534558752b8 31
Wimpie 0:d534558752b8 32 extern "C" DigitalOut led3;
Wimpie 0:d534558752b8 33 extern "C" DigitalOut led4;
Wimpie 0:d534558752b8 34
Wimpie 0:d534558752b8 35 enum PULSMODE { PULSE_DOWN=false, PULSE_UP = true};
Wimpie 0:d534558752b8 36
Wimpie 0:d534558752b8 37 class PulseCounter {
Wimpie 0:d534558752b8 38 public:
Wimpie 0:d534558752b8 39 PulseCounter(PinName pin, PinMode pull, int step, uint32_t minwidth, uint32_t maxwidth,
Wimpie 0:d534558752b8 40 bool up, char c):
Wimpie 0:d534558752b8 41 _interrupt(pin)
Wimpie 0:d534558752b8 42 {
Wimpie 0:d534558752b8 43
Wimpie 0:d534558752b8 44 if (up) { // pulse up
Wimpie 0:d534558752b8 45 _interrupt.rise(this, &PulseCounter::startpulse); // first rise then fall
Wimpie 0:d534558752b8 46 _interrupt.fall(this, &PulseCounter::endpulse);
Wimpie 0:d534558752b8 47
Wimpie 0:d534558752b8 48 } else { // pulse down
Wimpie 0:d534558752b8 49 _interrupt.fall(this, &PulseCounter::startpulse); // first fall then rise
Wimpie 0:d534558752b8 50 _interrupt.rise(this, &PulseCounter::endpulse);
Wimpie 0:d534558752b8 51 }
Wimpie 0:d534558752b8 52
Wimpie 0:d534558752b8 53 _interrupt.mode(pull);
Wimpie 0:d534558752b8 54
Wimpie 0:d534558752b8 55 _step=step;
Wimpie 0:d534558752b8 56 _count=0;
Wimpie 0:d534558752b8 57 _countall=0;
Wimpie 0:d534558752b8 58 _minwidth =minwidth;
Wimpie 0:d534558752b8 59 _maxwidth =maxwidth;
Wimpie 0:d534558752b8 60
Wimpie 0:d534558752b8 61 _useled3= (c=='E');
Wimpie 0:d534558752b8 62 _f=false;
Wimpie 0:d534558752b8 63 _t.stop();
Wimpie 0:d534558752b8 64 _t.reset();
Wimpie 0:d534558752b8 65 _up=up;
Wimpie 0:d534558752b8 66
Wimpie 0:d534558752b8 67 }
Wimpie 0:d534558752b8 68
Wimpie 0:d534558752b8 69 void startpulse() {
Wimpie 0:d534558752b8 70
Wimpie 0:d534558752b8 71 _diff_start=_t.read_ms();
Wimpie 0:d534558752b8 72
Wimpie 0:d534558752b8 73 _t.reset();
Wimpie 0:d534558752b8 74 _t.start();
Mehrad 1:54e8663265c3 75 /*
Wimpie 0:d534558752b8 76 if (_useled3)
Wimpie 0:d534558752b8 77 led3=1;
Wimpie 0:d534558752b8 78 else
Wimpie 0:d534558752b8 79 led4=1;
Mehrad 1:54e8663265c3 80 */
Wimpie 0:d534558752b8 81
Wimpie 0:d534558752b8 82 _f=true; // pulse is on
Wimpie 0:d534558752b8 83 }
Wimpie 0:d534558752b8 84
Wimpie 0:d534558752b8 85 void endpulse() {
Wimpie 0:d534558752b8 86 _diff_stop=_t.read_ms();
Wimpie 0:d534558752b8 87 _t.stop();
Mehrad 1:54e8663265c3 88
Mehrad 1:54e8663265c3 89 /*
Wimpie 0:d534558752b8 90 if (_useled3)
Wimpie 0:d534558752b8 91 led3=0;
Wimpie 0:d534558752b8 92 else
Wimpie 0:d534558752b8 93 led4=0;
Mehrad 1:54e8663265c3 94 */
Wimpie 0:d534558752b8 95
Wimpie 0:d534558752b8 96 if ((_diff_stop >= _minwidth) and (_diff_stop <= _maxwidth) and (_f)) {
Wimpie 0:d534558752b8 97
Wimpie 0:d534558752b8 98 _count=_count+_step;
Wimpie 0:d534558752b8 99 }
Wimpie 0:d534558752b8 100 _countall=_countall+_step;
Wimpie 0:d534558752b8 101 _f=false; // pulse is off
Wimpie 0:d534558752b8 102 }
Wimpie 0:d534558752b8 103
Wimpie 0:d534558752b8 104
Wimpie 0:d534558752b8 105 int read() {
Wimpie 0:d534558752b8 106 return _count;
Wimpie 0:d534558752b8 107 }
Wimpie 0:d534558752b8 108
Wimpie 0:d534558752b8 109 int readall() {
Wimpie 0:d534558752b8 110 return _countall;
Wimpie 0:d534558752b8 111 }
Wimpie 0:d534558752b8 112
Wimpie 0:d534558752b8 113
Wimpie 0:d534558752b8 114 void set(int value) {
Wimpie 0:d534558752b8 115 _count=value;
Wimpie 0:d534558752b8 116 }
Wimpie 0:d534558752b8 117
Wimpie 0:d534558752b8 118 void setall(int value) {
Wimpie 0:d534558752b8 119 _countall=value;
Wimpie 0:d534558752b8 120 }
Wimpie 0:d534558752b8 121
Wimpie 0:d534558752b8 122 private:
Wimpie 0:d534558752b8 123 InterruptIn _interrupt;
Wimpie 0:d534558752b8 124
Wimpie 0:d534558752b8 125 volatile int _count;
Wimpie 0:d534558752b8 126 volatile int _countall;
Wimpie 0:d534558752b8 127 volatile int _step;
Wimpie 0:d534558752b8 128
Wimpie 0:d534558752b8 129 Timer _t;
Wimpie 0:d534558752b8 130
Wimpie 0:d534558752b8 131 float _minwidth;
Wimpie 0:d534558752b8 132 float _maxwidth;
Wimpie 0:d534558752b8 133 bool _f;
Wimpie 0:d534558752b8 134 bool _up;
Wimpie 0:d534558752b8 135 bool _useled3;
Wimpie 0:d534558752b8 136 uint32_t _diff_start;
Wimpie 0:d534558752b8 137 uint32_t _diff_stop;
Wimpie 0:d534558752b8 138
Wimpie 0:d534558752b8 139 };
Wimpie 0:d534558752b8 140 #endif