Provides a simple way to capture the wave form of a pin. For example it can be used to capture the digital state transitions of a pin connected to an IR receiver. These signals can later be played back using the Signal Generator library to control IR devices. The library supports two means of capturing the signal. The signal can be captured on any digital pin using polling while for higher resolution and more accurate timing the interrupt driven capture feature can be used.
Diff: DigitalPinCapture.cpp
- Revision:
- 0:c6f86d422a7e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DigitalPinCapture.cpp Sat Sep 27 04:04:16 2014 +0000 @@ -0,0 +1,74 @@ +/////////////////////////////////////////////////////////////////////////////// +// Signal Capture Library +// Author: Chris Taylor (taylorza) +#include "mbed.h" +#include "DigitalPinCapture.h" + +DigitalPinCapture::DigitalPinCapture(PinName pin, PinMode mode) : + _signalPin(pin, mode), + _readTimeout(10000000) +{ + +} + +void DigitalPinCapture::setReadTimeout(uint32_t us) +{ + _readTimeout = us; +} + +int DigitalPinCapture::read(bool triggerState, uint32_t *pReadings, int count) +{ + return readInternal(&triggerState, pReadings, count, true); +} + +int DigitalPinCapture::read(bool *pInitialState, uint32_t *pReadings, int count) +{ + return readInternal(pInitialState, pReadings, count, false); +} + +int DigitalPinCapture::readInternal(bool *pPinState, uint32_t *pReadings, int count, bool waitForTrigger) +{ + Timer timer; + + int lastPinState; + int lastTransitionTime; + + timer.start(); + if (waitForTrigger) + { + while (_signalPin == *pPinState) + { + if (timer.read_us() >= _readTimeout) return 0; + } + + while (_signalPin != *pPinState) + { + if (timer.read_us() >= _readTimeout) return 0; + } + lastTransitionTime = timer.read_us(); + } + else + { + *pPinState = _signalPin ? true : false; + lastTransitionTime = timer.read_us(); + } + + lastPinState = *pPinState; + + int bufferIndex = 0; + while (bufferIndex < count && timer.read_us() < _readTimeout) + { + if (_signalPin != lastPinState) + { + uint32_t transitionTime = timer.read_us(); + pReadings[bufferIndex++] = transitionTime - lastTransitionTime; + lastPinState = !lastPinState; + lastTransitionTime = transitionTime; + } + } + + timer.stop(); + + return bufferIndex; +} +