Mercurial > hg > beaglert
annotate core/PulseIn.cpp @ 549:ff0e9e827dcd prerelease
Updated uploader.py and build_pd_heavy for a more graceful failure
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Fri, 24 Jun 2016 12:42:48 +0100 |
parents | 493a07f6ec09 |
children |
rev | line source |
---|---|
giuliomoro@193 | 1 /* |
giuliomoro@193 | 2 * PulseIn.cpp |
giuliomoro@193 | 3 * |
giuliomoro@193 | 4 * Created on: 4 Feb 2016 |
giuliomoro@193 | 5 * Author: giulio |
giuliomoro@193 | 6 */ |
giuliomoro@193 | 7 |
giuliomoro@193 | 8 #include "../include/PulseIn.h" |
giuliomoro@193 | 9 |
giuliomoro@301 | 10 void PulseIn::init(BelaContext* context, unsigned int digitalInput, int direction){ |
giuliomoro@193 | 11 _digitalInput = digitalInput; |
giuliomoro@193 | 12 _pulseIsOn = false; |
giuliomoro@193 | 13 _pulseOnState = direction == 1 ? 1 : 0; |
giuliomoro@193 | 14 _array.resize(context->digitalFrames); |
giuliomoro@193 | 15 _lastContext = (uint64_t)-1; |
andrewm@310 | 16 pinMode(context, 0, digitalInput, INPUT); //context is used to allocate the number of elements in the array |
giuliomoro@193 | 17 } |
giuliomoro@193 | 18 |
giuliomoro@301 | 19 void PulseIn::check(BelaContext* context){ |
giuliomoro@193 | 20 if(_digitalInput == -1){ //must be init'ed before calling check(); |
giuliomoro@193 | 21 throw(1); |
giuliomoro@193 | 22 } |
giuliomoro@193 | 23 for(unsigned int n = 0; n < context->digitalFrames; n++){ |
giuliomoro@193 | 24 _array[n] = 0; //maybe a few of these will be overwritten below |
giuliomoro@193 | 25 } |
giuliomoro@193 | 26 for(unsigned int n = 0; n < context->digitalFrames; n++){ |
giuliomoro@193 | 27 if(_pulseIsOn == false){ // look for start edge |
andrewm@308 | 28 if(digitalRead(context, n, _digitalInput) == _pulseOnState){ |
andrewm@311 | 29 _pulseStart = context->audioFramesElapsed + n; // store location of start edge |
giuliomoro@193 | 30 _pulseIsOn = true; |
giuliomoro@193 | 31 } |
giuliomoro@193 | 32 } else { // _pulseIsOn == true; |
andrewm@308 | 33 if(digitalRead(context, n, _digitalInput) == !_pulseOnState){ // look for stop edge |
andrewm@311 | 34 _array[n] = context->audioFramesElapsed + n - _pulseStart; // compute and store pulse duration |
giuliomoro@193 | 35 _pulseIsOn = false; |
giuliomoro@193 | 36 } |
giuliomoro@193 | 37 } |
giuliomoro@193 | 38 } |
andrewm@311 | 39 _lastContext = context->audioFramesElapsed; |
giuliomoro@193 | 40 }; |
giuliomoro@193 | 41 |
giuliomoro@193 | 42 PulseIn::~PulseIn() { |
giuliomoro@193 | 43 // TODO Auto-generated destructor stub |
giuliomoro@193 | 44 } |
giuliomoro@193 | 45 |