Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterfacePlusHostname NTPClient Onewire RdWebServer SDFileSystem-RTOS mbed-rtos mbed-src
PulsePin.cpp@22:14b4060dd027, 2015-10-16 (annotated)
- Committer:
- Bobty
- Date:
- Fri Oct 16 08:42:13 2015 +0000
- Revision:
- 22:14b4060dd027
- Parent:
- 10:72eb217def1f
Re-enabled web folder view on server
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Bobty | 2:6bfef0839102 | 1 | // Handles a pin that has a slow pulse applied |
| Bobty | 2:6bfef0839102 | 2 | // Written for a gas meter monitor |
| Bobty | 10:72eb217def1f | 3 | // Rob Dobson, 2015 |
| Bobty | 2:6bfef0839102 | 4 | |
| Bobty | 2:6bfef0839102 | 5 | #include "PulsePin.h" |
| Bobty | 2:6bfef0839102 | 6 | |
| Bobty | 4:0d3a207680b0 | 7 | PulsePin::PulsePin(DigitalIn& pin, bool detectRisingEdge, int waitForPinStabilisationMs) : |
| Bobty | 2:6bfef0839102 | 8 | _pin(pin) |
| Bobty | 2:6bfef0839102 | 9 | { |
| Bobty | 4:0d3a207680b0 | 10 | _detectRisingEdge = detectRisingEdge; |
| Bobty | 2:6bfef0839102 | 11 | _waitForPinStabilisationMs = waitForPinStabilisationMs; |
| Bobty | 2:6bfef0839102 | 12 | _pinTimer.start(); |
| Bobty | 2:6bfef0839102 | 13 | _curPinState = _pin; |
| Bobty | 2:6bfef0839102 | 14 | _lastStableTimeMs = _pinTimer.read_ms(); |
| Bobty | 2:6bfef0839102 | 15 | _firstEdgeDetected = false; |
| Bobty | 2:6bfef0839102 | 16 | _timeBetweenEdgesMs = 0; |
| Bobty | 5:5bccf48799d4 | 17 | _pulseCount = 0; |
| Bobty | 5:5bccf48799d4 | 18 | _pinTimerMinutes = 0; |
| Bobty | 2:6bfef0839102 | 19 | } |
| Bobty | 2:6bfef0839102 | 20 | |
| Bobty | 2:6bfef0839102 | 21 | bool PulsePin::Service() |
| Bobty | 2:6bfef0839102 | 22 | { |
| Bobty | 2:6bfef0839102 | 23 | // Check time since last edge - looking for stability |
| Bobty | 2:6bfef0839102 | 24 | int timeNowMs = _pinTimer.read_ms(); |
| Bobty | 5:5bccf48799d4 | 25 | |
| Bobty | 5:5bccf48799d4 | 26 | // Check if over 1 minute (as the timer wraps around after 30 mins) |
| Bobty | 5:5bccf48799d4 | 27 | if (timeNowMs > 60000) |
| Bobty | 5:5bccf48799d4 | 28 | { |
| Bobty | 5:5bccf48799d4 | 29 | _pinTimerMinutes++; |
| Bobty | 5:5bccf48799d4 | 30 | _pinTimer.reset(); |
| Bobty | 5:5bccf48799d4 | 31 | timeNowMs -= 60000; |
| Bobty | 5:5bccf48799d4 | 32 | } |
| Bobty | 5:5bccf48799d4 | 33 | |
| Bobty | 5:5bccf48799d4 | 34 | // Get the real time elapsed (still wraps but now only after about 500 hours) |
| Bobty | 5:5bccf48799d4 | 35 | timeNowMs = _pinTimerMinutes*60000 + timeNowMs; |
| Bobty | 5:5bccf48799d4 | 36 | |
| Bobty | 5:5bccf48799d4 | 37 | // Check for pin stabilization |
| Bobty | 2:6bfef0839102 | 38 | if (timeNowMs < _lastStableTimeMs + _waitForPinStabilisationMs) |
| Bobty | 2:6bfef0839102 | 39 | return false; |
| Bobty | 2:6bfef0839102 | 40 | |
| Bobty | 2:6bfef0839102 | 41 | // Check for a change of state |
| Bobty | 2:6bfef0839102 | 42 | bool pinState = _pin; |
| Bobty | 2:6bfef0839102 | 43 | if (pinState == _curPinState) |
| Bobty | 2:6bfef0839102 | 44 | return false; |
| Bobty | 2:6bfef0839102 | 45 | |
| Bobty | 2:6bfef0839102 | 46 | _curPinState = pinState; |
| Bobty | 2:6bfef0839102 | 47 | _lastStableTimeMs = timeNowMs; |
| Bobty | 2:6bfef0839102 | 48 | |
| Bobty | 2:6bfef0839102 | 49 | // Check if this is the direction of edge we're looking for |
| Bobty | 4:0d3a207680b0 | 50 | if (pinState != _detectRisingEdge) |
| Bobty | 2:6bfef0839102 | 51 | return false; |
| Bobty | 2:6bfef0839102 | 52 | |
| Bobty | 2:6bfef0839102 | 53 | // Reset the timer to avoid wrap around problems |
| Bobty | 2:6bfef0839102 | 54 | bool firstEdgeDetected = _firstEdgeDetected; |
| Bobty | 5:5bccf48799d4 | 55 | _pinTimerMinutes = 0; |
| Bobty | 2:6bfef0839102 | 56 | _pinTimer.reset(); |
| Bobty | 2:6bfef0839102 | 57 | _firstEdgeDetected = true; |
| Bobty | 2:6bfef0839102 | 58 | _lastStableTimeMs = 0; |
| Bobty | 2:6bfef0839102 | 59 | |
| Bobty | 2:6bfef0839102 | 60 | // Check if this should be returned |
| Bobty | 2:6bfef0839102 | 61 | if (!firstEdgeDetected) |
| Bobty | 2:6bfef0839102 | 62 | return false; |
| Bobty | 2:6bfef0839102 | 63 | _timeBetweenEdgesMs = timeNowMs; |
| Bobty | 5:5bccf48799d4 | 64 | _pulseCount++; |
| Bobty | 2:6bfef0839102 | 65 | return true; |
| Bobty | 2:6bfef0839102 | 66 | } |
| Bobty | 2:6bfef0839102 | 67 | |
| Bobty | 5:5bccf48799d4 | 68 | int PulsePin::GetPulseRateMs() |
| Bobty | 2:6bfef0839102 | 69 | { |
| Bobty | 2:6bfef0839102 | 70 | return _timeBetweenEdgesMs; |
| Bobty | 2:6bfef0839102 | 71 | } |
| Bobty | 5:5bccf48799d4 | 72 | |
| Bobty | 5:5bccf48799d4 | 73 | int PulsePin::GetPulseCount() |
| Bobty | 5:5bccf48799d4 | 74 | { |
| Bobty | 5:5bccf48799d4 | 75 | return _pulseCount; |
| Bobty | 5:5bccf48799d4 | 76 | } |
| Bobty | 5:5bccf48799d4 | 77 | |
| Bobty | 5:5bccf48799d4 | 78 | void PulsePin::SetPulseCount(int pulseCount) |
| Bobty | 5:5bccf48799d4 | 79 | { |
| Bobty | 5:5bccf48799d4 | 80 | _pulseCount = pulseCount; |
| Bobty | 5:5bccf48799d4 | 81 | } |