Monitor for central heating system (e.g. 2zones+hw) Supports up to 15 temp probes (DS18B20/DS18S20) 3 valve monitors Gas pulse meter recording Use stand-alone or with nodeEnergyServer See http://robdobson.com/2015/09/central-heating-monitor
Dependencies: EthernetInterfacePlusHostname NTPClient Onewire RdWebServer SDFileSystem-RTOS mbed-rtos mbed-src
PulsePin.cpp@5:5bccf48799d4, 2015-02-17 (annotated)
- Committer:
- Bobty
- Date:
- Tue Feb 17 21:33:39 2015 +0000
- Revision:
- 5:5bccf48799d4
- Parent:
- 4:0d3a207680b0
- Child:
- 10:72eb217def1f
Tidied up - but is unstable - web server crashes after some time
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 | 2:6bfef0839102 | 3 | |
Bobty | 2:6bfef0839102 | 4 | #include "PulsePin.h" |
Bobty | 2:6bfef0839102 | 5 | |
Bobty | 4:0d3a207680b0 | 6 | PulsePin::PulsePin(DigitalIn& pin, bool detectRisingEdge, int waitForPinStabilisationMs) : |
Bobty | 2:6bfef0839102 | 7 | _pin(pin) |
Bobty | 2:6bfef0839102 | 8 | { |
Bobty | 4:0d3a207680b0 | 9 | _detectRisingEdge = detectRisingEdge; |
Bobty | 2:6bfef0839102 | 10 | _waitForPinStabilisationMs = waitForPinStabilisationMs; |
Bobty | 2:6bfef0839102 | 11 | _pinTimer.start(); |
Bobty | 2:6bfef0839102 | 12 | _curPinState = _pin; |
Bobty | 2:6bfef0839102 | 13 | _lastStableTimeMs = _pinTimer.read_ms(); |
Bobty | 2:6bfef0839102 | 14 | _firstEdgeDetected = false; |
Bobty | 2:6bfef0839102 | 15 | _timeBetweenEdgesMs = 0; |
Bobty | 5:5bccf48799d4 | 16 | _pulseCount = 0; |
Bobty | 5:5bccf48799d4 | 17 | _pinTimerMinutes = 0; |
Bobty | 2:6bfef0839102 | 18 | } |
Bobty | 2:6bfef0839102 | 19 | |
Bobty | 2:6bfef0839102 | 20 | bool PulsePin::Service() |
Bobty | 2:6bfef0839102 | 21 | { |
Bobty | 2:6bfef0839102 | 22 | // Check time since last edge - looking for stability |
Bobty | 2:6bfef0839102 | 23 | int timeNowMs = _pinTimer.read_ms(); |
Bobty | 5:5bccf48799d4 | 24 | |
Bobty | 5:5bccf48799d4 | 25 | // Check if over 1 minute (as the timer wraps around after 30 mins) |
Bobty | 5:5bccf48799d4 | 26 | if (timeNowMs > 60000) |
Bobty | 5:5bccf48799d4 | 27 | { |
Bobty | 5:5bccf48799d4 | 28 | _pinTimerMinutes++; |
Bobty | 5:5bccf48799d4 | 29 | _pinTimer.reset(); |
Bobty | 5:5bccf48799d4 | 30 | timeNowMs -= 60000; |
Bobty | 5:5bccf48799d4 | 31 | } |
Bobty | 5:5bccf48799d4 | 32 | |
Bobty | 5:5bccf48799d4 | 33 | // Get the real time elapsed (still wraps but now only after about 500 hours) |
Bobty | 5:5bccf48799d4 | 34 | timeNowMs = _pinTimerMinutes*60000 + timeNowMs; |
Bobty | 5:5bccf48799d4 | 35 | |
Bobty | 5:5bccf48799d4 | 36 | // Check for pin stabilization |
Bobty | 2:6bfef0839102 | 37 | if (timeNowMs < _lastStableTimeMs + _waitForPinStabilisationMs) |
Bobty | 2:6bfef0839102 | 38 | return false; |
Bobty | 2:6bfef0839102 | 39 | |
Bobty | 2:6bfef0839102 | 40 | // Check for a change of state |
Bobty | 2:6bfef0839102 | 41 | bool pinState = _pin; |
Bobty | 2:6bfef0839102 | 42 | if (pinState == _curPinState) |
Bobty | 2:6bfef0839102 | 43 | return false; |
Bobty | 2:6bfef0839102 | 44 | |
Bobty | 2:6bfef0839102 | 45 | _curPinState = pinState; |
Bobty | 2:6bfef0839102 | 46 | _lastStableTimeMs = timeNowMs; |
Bobty | 2:6bfef0839102 | 47 | |
Bobty | 2:6bfef0839102 | 48 | // Check if this is the direction of edge we're looking for |
Bobty | 4:0d3a207680b0 | 49 | if (pinState != _detectRisingEdge) |
Bobty | 2:6bfef0839102 | 50 | return false; |
Bobty | 2:6bfef0839102 | 51 | |
Bobty | 2:6bfef0839102 | 52 | // Reset the timer to avoid wrap around problems |
Bobty | 2:6bfef0839102 | 53 | bool firstEdgeDetected = _firstEdgeDetected; |
Bobty | 5:5bccf48799d4 | 54 | _pinTimerMinutes = 0; |
Bobty | 2:6bfef0839102 | 55 | _pinTimer.reset(); |
Bobty | 2:6bfef0839102 | 56 | _firstEdgeDetected = true; |
Bobty | 2:6bfef0839102 | 57 | _lastStableTimeMs = 0; |
Bobty | 2:6bfef0839102 | 58 | |
Bobty | 2:6bfef0839102 | 59 | // Check if this should be returned |
Bobty | 2:6bfef0839102 | 60 | if (!firstEdgeDetected) |
Bobty | 2:6bfef0839102 | 61 | return false; |
Bobty | 2:6bfef0839102 | 62 | _timeBetweenEdgesMs = timeNowMs; |
Bobty | 5:5bccf48799d4 | 63 | _pulseCount++; |
Bobty | 2:6bfef0839102 | 64 | return true; |
Bobty | 2:6bfef0839102 | 65 | } |
Bobty | 2:6bfef0839102 | 66 | |
Bobty | 5:5bccf48799d4 | 67 | int PulsePin::GetPulseRateMs() |
Bobty | 2:6bfef0839102 | 68 | { |
Bobty | 2:6bfef0839102 | 69 | return _timeBetweenEdgesMs; |
Bobty | 2:6bfef0839102 | 70 | } |
Bobty | 5:5bccf48799d4 | 71 | |
Bobty | 5:5bccf48799d4 | 72 | int PulsePin::GetPulseCount() |
Bobty | 5:5bccf48799d4 | 73 | { |
Bobty | 5:5bccf48799d4 | 74 | return _pulseCount; |
Bobty | 5:5bccf48799d4 | 75 | } |
Bobty | 5:5bccf48799d4 | 76 | |
Bobty | 5:5bccf48799d4 | 77 | void PulsePin::SetPulseCount(int pulseCount) |
Bobty | 5:5bccf48799d4 | 78 | { |
Bobty | 5:5bccf48799d4 | 79 | _pulseCount = pulseCount; |
Bobty | 5:5bccf48799d4 | 80 | } |