robert@464: /* robert@464: ____ _____ _ _ robert@464: | __ )| ____| | / \ robert@464: | _ \| _| | | / _ \ robert@464: | |_) | |___| |___ / ___ \ robert@464: |____/|_____|_____/_/ \_\ robert@464: robert@464: The platform for ultra-low latency audio and sensor processing robert@464: robert@464: http://bela.io robert@464: robert@464: A project of the Augmented Instruments Laboratory within the robert@464: Centre for Digital Music at Queen Mary University of London. robert@464: http://www.eecs.qmul.ac.uk/~andrewm robert@464: robert@464: (c) 2016 Augmented Instruments Laboratory: Andrew McPherson, robert@464: Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack, robert@464: Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved. robert@464: robert@464: The Bela software is distributed under the GNU Lesser General Public License robert@464: (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt robert@464: */ robert@464: robert@464: robert@464: #include robert@464: #include robert@464: #include robert@464: #include robert@464: #include robert@464: robert@464: PulseIn pulseIn; robert@464: int gPulseInPin = 0; robert@464: int gDigitalOutPin = 1; robert@464: int gPulseLength = 1234; robert@464: int gSamplesBetweenPulses = 10000; robert@464: robert@464: bool setup(BelaContext *context, void *userData) robert@464: { robert@464: pinMode(context, 0, gDigitalOutPin, OUTPUT); robert@464: pulseIn.init(context, gPulseInPin, 1); //third parameter is direction robert@464: return true; robert@464: } robert@464: robert@464: void render(BelaContext *context, void *userData) robert@464: { robert@464: static bool pulseOut = 0; robert@464: static int count = 0; robert@464: for(unsigned int n = 0; n < context->digitalFrames; n++){ robert@464: // detect if a pulse just ended robert@464: int duration = pulseIn.hasPulsed(context, n); robert@464: if(duration > 0){ robert@464: rt_printf("duration = %d\n", duration); robert@464: } robert@464: robert@464: // generate a rectangular waveform as a test signal. robert@464: // Connect gDigitalOutPin to gPulseInPin robert@464: // to verify that the detected pulse length is gPulseLength robert@464: if(count == gPulseLength ){ robert@464: pulseOut = false; robert@464: } robert@464: if(count == (gPulseLength + gSamplesBetweenPulses)){ robert@464: pulseOut = true; robert@464: count = 0; robert@464: } robert@464: digitalWrite(context, n, gDigitalOutPin, pulseOut); robert@464: count++; robert@464: } robert@464: } robert@464: robert@464: void cleanup(BelaContext *context, void *userData) robert@464: { robert@464: robert@464: }