giuliomoro@193: /* giuliomoro@193: * PulseIn.cpp giuliomoro@193: * giuliomoro@193: * Created on: 4 Feb 2016 giuliomoro@193: * Author: giulio giuliomoro@193: */ giuliomoro@193: giuliomoro@193: #include "../include/PulseIn.h" giuliomoro@193: giuliomoro@301: void PulseIn::init(BelaContext* context, unsigned int digitalInput, int direction){ giuliomoro@193: _digitalInput = digitalInput; giuliomoro@193: _pulseIsOn = false; giuliomoro@193: _pulseOnState = direction == 1 ? 1 : 0; giuliomoro@193: _array.resize(context->digitalFrames); giuliomoro@193: _lastContext = (uint64_t)-1; giuliomoro@193: pinModeFrame(context, 0, digitalInput, INPUT); //context is used to allocate the number of elements in the array giuliomoro@193: } giuliomoro@193: giuliomoro@301: void PulseIn::check(BelaContext* context){ giuliomoro@193: if(_digitalInput == -1){ //must be init'ed before calling check(); giuliomoro@193: throw(1); giuliomoro@193: } giuliomoro@193: for(unsigned int n = 0; n < context->digitalFrames; n++){ giuliomoro@193: _array[n] = 0; //maybe a few of these will be overwritten below giuliomoro@193: } giuliomoro@193: for(unsigned int n = 0; n < context->digitalFrames; n++){ giuliomoro@193: if(_pulseIsOn == false){ // look for start edge andrewm@308: if(digitalRead(context, n, _digitalInput) == _pulseOnState){ giuliomoro@193: _pulseStart = context->audioSampleCount + n; // store location of start edge giuliomoro@193: _pulseIsOn = true; giuliomoro@193: } giuliomoro@193: } else { // _pulseIsOn == true; andrewm@308: if(digitalRead(context, n, _digitalInput) == !_pulseOnState){ // look for stop edge giuliomoro@193: _array[n] = context->audioSampleCount + n - _pulseStart; // compute and store pulse duration giuliomoro@193: _pulseIsOn = false; giuliomoro@193: } giuliomoro@193: } giuliomoro@193: } giuliomoro@193: _lastContext = context->audioSampleCount; giuliomoro@193: }; giuliomoro@193: giuliomoro@193: PulseIn::~PulseIn() { giuliomoro@193: // TODO Auto-generated destructor stub giuliomoro@193: } giuliomoro@193: