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

Committer:
Bobty
Date:
2015-02-02
Revision:
4:0d3a207680b0
Parent:
2:6bfef0839102
Child:
5:5bccf48799d4

File content as of revision 4:0d3a207680b0:

// Handles a pin that has a slow pulse applied
// Written for a gas meter monitor

#include "PulsePin.h"

PulsePin::PulsePin(DigitalIn& pin, bool detectRisingEdge, int waitForPinStabilisationMs) :
    _pin(pin)
{
    _detectRisingEdge = detectRisingEdge;
    _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 != _detectRisingEdge)
        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;
}