Mercurial > hg > beaglert
comparison core/PulseIn.cpp @ 193:3c3d14654b7f
Added PulseIn.
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Thu, 04 Feb 2016 18:02:52 +0000 |
parents | |
children | e4392164b458 |
comparison
equal
deleted
inserted
replaced
192:1402f22fc99a | 193:3c3d14654b7f |
---|---|
1 /* | |
2 * PulseIn.cpp | |
3 * | |
4 * Created on: 4 Feb 2016 | |
5 * Author: giulio | |
6 */ | |
7 | |
8 #include "../include/PulseIn.h" | |
9 | |
10 void PulseIn::init(BeagleRTContext* context, unsigned int digitalInput, int direction){ | |
11 _digitalInput = digitalInput; | |
12 _pulseIsOn = false; | |
13 _pulseOnState = direction == 1 ? 1 : 0; | |
14 _array.resize(context->digitalFrames); | |
15 _lastContext = (uint64_t)-1; | |
16 pinModeFrame(context, 0, digitalInput, INPUT); //context is used to allocate the number of elements in the array | |
17 } | |
18 | |
19 void PulseIn::check(BeagleRTContext* context){ | |
20 if(_digitalInput == -1){ //must be init'ed before calling check(); | |
21 throw(1); | |
22 } | |
23 for(unsigned int n = 0; n < context->digitalFrames; n++){ | |
24 _array[n] = 0; //maybe a few of these will be overwritten below | |
25 } | |
26 for(unsigned int n = 0; n < context->digitalFrames; n++){ | |
27 if(_pulseIsOn == false){ // look for start edge | |
28 if(digitalReadFrame(context, n, _digitalInput) == _pulseOnState){ | |
29 _pulseStart = context->audioSampleCount + n; // store location of start edge | |
30 _pulseIsOn = true; | |
31 } | |
32 } else { // _pulseIsOn == true; | |
33 if(digitalReadFrame(context, n, _digitalInput) == !_pulseOnState){ // look for stop edge | |
34 _array[n] = context->audioSampleCount + n - _pulseStart; // compute and store pulse duration | |
35 _pulseIsOn = false; | |
36 } | |
37 } | |
38 } | |
39 _lastContext = context->audioSampleCount; | |
40 }; | |
41 | |
42 PulseIn::~PulseIn() { | |
43 // TODO Auto-generated destructor stub | |
44 } | |
45 |