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.
DigitalPinCapture.cpp
00001 /////////////////////////////////////////////////////////////////////////////// 00002 // Signal Capture Library 00003 // Author: Chris Taylor (taylorza) 00004 #include "mbed.h" 00005 #include "DigitalPinCapture.h" 00006 00007 DigitalPinCapture::DigitalPinCapture(PinName pin, PinMode mode) : 00008 _signalPin(pin, mode), 00009 _readTimeout(10000000) 00010 { 00011 00012 } 00013 00014 void DigitalPinCapture::setReadTimeout(uint32_t us) 00015 { 00016 _readTimeout = us; 00017 } 00018 00019 int DigitalPinCapture::read(bool triggerState, uint32_t *pReadings, int count) 00020 { 00021 return readInternal(&triggerState, pReadings, count, true); 00022 } 00023 00024 int DigitalPinCapture::read(bool *pInitialState, uint32_t *pReadings, int count) 00025 { 00026 return readInternal(pInitialState, pReadings, count, false); 00027 } 00028 00029 int DigitalPinCapture::readInternal(bool *pPinState, uint32_t *pReadings, int count, bool waitForTrigger) 00030 { 00031 Timer timer; 00032 00033 int lastPinState; 00034 int lastTransitionTime; 00035 00036 timer.start(); 00037 if (waitForTrigger) 00038 { 00039 while (_signalPin == *pPinState) 00040 { 00041 if (timer.read_us() >= _readTimeout) return 0; 00042 } 00043 00044 while (_signalPin != *pPinState) 00045 { 00046 if (timer.read_us() >= _readTimeout) return 0; 00047 } 00048 lastTransitionTime = timer.read_us(); 00049 } 00050 else 00051 { 00052 *pPinState = _signalPin ? true : false; 00053 lastTransitionTime = timer.read_us(); 00054 } 00055 00056 lastPinState = *pPinState; 00057 00058 int bufferIndex = 0; 00059 while (bufferIndex < count && timer.read_us() < _readTimeout) 00060 { 00061 if (_signalPin != lastPinState) 00062 { 00063 uint32_t transitionTime = timer.read_us(); 00064 pReadings[bufferIndex++] = transitionTime - lastTransitionTime; 00065 lastPinState = !lastPinState; 00066 lastTransitionTime = transitionTime; 00067 } 00068 } 00069 00070 timer.stop(); 00071 00072 return bufferIndex; 00073 } 00074
Generated on Fri Jul 15 2022 02:11:20 by
1.7.2