Pulse measurement to know signal occupancy
Revision 1:ebc39fb22351, committed 2017-01-26
- Comitter:
- abouillot
- Date:
- Thu Jan 26 08:26:22 2017 +0000
- Parent:
- 0:15aa9d3aeb2e
- Commit message:
- Fixed few bugs and added inline documentation. It is now possible to retrieve the time spent in a state in us, ms and seconds.
Changed in this revision
PwmReader.cpp | Show annotated file Show diff for this revision Revisions of this file |
PwmReader.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 15aa9d3aeb2e -r ebc39fb22351 PwmReader.cpp --- a/PwmReader.cpp Wed Jan 25 08:52:00 2017 +0000 +++ b/PwmReader.cpp Thu Jan 26 08:26:22 2017 +0000 @@ -23,51 +23,89 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ - + #include "PwmReader.h" +/// #define TRACE to enable TRACEs using printf #undef to disable +#undef TRACE + void PwmReader::pressedInt() { + long now = _timer.read_us(); + // signal moved from high to low, record the time spent in this state _high += _timer.read_us() - _last_toggle; + // record the new timestamp _last_toggle = _timer.read_us(); -// _timer.reset(); -// printf("p %ld ", _high); +#ifdef TRACE + printf("p %ld ", _high); +#endif // sleep(); } void PwmReader::releasedInt() { - _down += _timer.read_us() - _last_toggle; - _last_toggle = _timer.read_us(); -// _timer.reset(); -// printf("r %ld", _down); + long now = _timer.read_us(); + // signal moved from down to high, record the time spent in this state + _down += now - _last_toggle; + // record the new timestamp + _last_toggle = now; +#ifdef TRACE + printf("r %ld", _down); +#endif // sleep(); } void PwmReader::init() { + // register the interupt attached with the levels changes _pin.fall(callback(this, &PwmReader::pressedInt)); _pin.rise(callback(this, &PwmReader::releasedInt)); } void PwmReader::start() { + // reset the storing variable _high = 0; _down = 0; + _last_toggle = 0; + + // store the inital state _start_state = _pin.read(); + + // start the timer at 0 + _timer.reset(); + _timer.start(); + + // enable the IRQ requests _pin.enable_irq(); - _timer.start(); + +#ifdef TRACE + if (_pin.read() != _start_state) { + printf("toggle "); + } + if (_pin.read() == 0) { + printf("low "); + } + if (_pin.read() == 1) { + printf("high "); + } +#endif } void PwmReader::stop() { + // disable the IRQ request, to avoid code call _pin.disable_irq(); + // no time has been recorded. The signal stayed at the same level from the start of measurment if (_high == 0 && _down == 0) - if (_start_state == 0) + // retrieve the duration of the measurment to assign it to the right state + if (_start_state == 1) _high = _timer.read_us(); - else if (_start_state == 1) + else if (_start_state == 0) _down = _timer.read_us(); + + // stop the running timer _timer.stop(); } \ No newline at end of file
diff -r 15aa9d3aeb2e -r ebc39fb22351 PwmReader.h --- a/PwmReader.h Wed Jan 25 08:52:00 2017 +0000 +++ b/PwmReader.h Thu Jan 26 08:26:22 2017 +0000 @@ -23,29 +23,87 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ - - #include "mbed.h" - + +#include "mbed.h" + +/*! + * Class PwmReader lmeasure the time spend in high and low state on a digital pin + * Uses useconds timer and IRQ + */ class PwmReader { public: + /** create a PwmReader object + * + * @param pin Pin numer to be observed + */ PwmReader(PinName pin) : _pin(pin) { init(); }; + /** Start the measurment + * + */ void start(); + /** Stop the measurment + * + */ void stop(); + /** return the time spend in low state + * + * @return the percentage of time spent in low state. value between 0.0 and 1.0 + */ float occupacyLow() { return 100.0 * _down / (_down + _high); }; + /** return the time spend in high state + * + * @return the percentage of time spent in high state. value between 0.0 and 1.0 + */ float occupacyHigh() { return 100.0 * _high / (_down + _high); }; - long down() { + /** return the time spend in low state + * + * @return the percentage of time spent in low state. value in useconds + */ + long down_us() { return _down; }; - float high() { + /** return the time spend in high state + * + * @return the percentage of time spent in high state. value in useconds + */ + long high_us() { return _high; }; + /** return the time spend in low state + * @param round set round to tue to round the value (closest integer) - false by default + * @return the time spent in low state. value in mseconds + */ + long down_ms(bool round = false) { + return (_down + (round ? 500 : 0)) / 1000; + }; + /** return the time spend in high state + * @param round set round to tue to round the value (closest integer) - false by default + * @return the time spent in high state. value in mseconds + */ + long high_ms(bool round = false) { + return (_high + (round ? 500 : 0)) / 1000; + }; + /** return the time spend in low state + * @param round set round to tue to round the value (closest integer) - false by default + * @return the time spent in low state. value in seconds + */ + long down(bool round = false) { + return (_down + (round ? 500000 : 0)) / 1000000; + }; + /** return the time spend in high state + * @param round set round to tue to round the value (closest integer) - false by default + * @return the time spent in high state. value in seconds + */ + long high(bool round = false) { + return (_high + (round ? 500000 : 0)) / 1000000; + }; private: void init(); void pressedInt();