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
Diff: PulsePin.cpp
- Revision:
- 2:6bfef0839102
- Child:
- 4:0d3a207680b0
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PulsePin.cpp Fri Nov 07 16:09:15 2014 +0000 @@ -0,0 +1,53 @@ +// Handles a pin that has a slow pulse applied +// Written for a gas meter monitor + +#include "PulsePin.h" + +PulsePin::PulsePin(DigitalIn& pin, bool risingEdge, int waitForPinStabilisationMs) : + _pin(pin) +{ + _risingEdge = risingEdge; + _waitForPinStabilisationMs = waitForPinStabilisationMs; + _pinTimer.start(); + _curPinState = _pin; + _lastStableTimeMs = _pinTimer.read_ms(); + _firstEdgeDetected = false; + _timeBetweenEdgesMs = 0; +} + +bool PulsePin::Service() +{ + // Check time since last edge - looking for stability + int timeNowMs = _pinTimer.read_ms(); + if (timeNowMs < _lastStableTimeMs + _waitForPinStabilisationMs) + return false; + + // Check for a change of state + bool pinState = _pin; + if (pinState == _curPinState) + return false; + + _curPinState = pinState; + _lastStableTimeMs = timeNowMs; + + // Check if this is the direction of edge we're looking for + if (pinState != _risingEdge) + return false; + + // Reset the timer to avoid wrap around problems + bool firstEdgeDetected = _firstEdgeDetected; + _pinTimer.reset(); + _firstEdgeDetected = true; + _lastStableTimeMs = 0; + + // Check if this should be returned + if (!firstEdgeDetected) + return false; + _timeBetweenEdgesMs = timeNowMs; + return true; +} + +int PulsePin::GetLastCycleTimeMs() +{ + return _timeBetweenEdgesMs; +}