annotate examples/06-Sensors/basic-pulseIn/render.cpp @ 475:faa5f58c71af prerelease

Headers
author Giulio Moro <giuliomoro@yahoo.it>
date Mon, 20 Jun 2016 20:39:00 +0100
parents 8fcfbfb32aa0
children
rev   line source
robert@464 1 /*
robert@464 2 ____ _____ _ _
robert@464 3 | __ )| ____| | / \
robert@464 4 | _ \| _| | | / _ \
robert@464 5 | |_) | |___| |___ / ___ \
robert@464 6 |____/|_____|_____/_/ \_\
robert@464 7
robert@464 8 The platform for ultra-low latency audio and sensor processing
robert@464 9
robert@464 10 http://bela.io
robert@464 11
robert@464 12 A project of the Augmented Instruments Laboratory within the
robert@464 13 Centre for Digital Music at Queen Mary University of London.
robert@464 14 http://www.eecs.qmul.ac.uk/~andrewm
robert@464 15
robert@464 16 (c) 2016 Augmented Instruments Laboratory: Andrew McPherson,
robert@464 17 Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack,
robert@464 18 Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved.
robert@464 19
robert@464 20 The Bela software is distributed under the GNU Lesser General Public License
robert@464 21 (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt
robert@464 22 */
robert@464 23
robert@464 24
robert@464 25 #include <Bela.h>
robert@464 26 #include <PulseIn.h>
robert@464 27 #include <stdlib.h>
robert@464 28 #include <rtdk.h>
robert@464 29 #include <cmath>
robert@464 30
robert@464 31 PulseIn pulseIn;
robert@464 32 int gPulseInPin = 0;
robert@464 33 int gDigitalOutPin = 1;
robert@464 34 int gPulseLength = 1234;
robert@464 35 int gSamplesBetweenPulses = 10000;
robert@464 36
robert@464 37 bool setup(BelaContext *context, void *userData)
robert@464 38 {
robert@464 39 pinMode(context, 0, gDigitalOutPin, OUTPUT);
robert@464 40 pulseIn.init(context, gPulseInPin, 1); //third parameter is direction
robert@464 41 return true;
robert@464 42 }
robert@464 43
robert@464 44 void render(BelaContext *context, void *userData)
robert@464 45 {
robert@464 46 static bool pulseOut = 0;
robert@464 47 static int count = 0;
robert@464 48 for(unsigned int n = 0; n < context->digitalFrames; n++){
robert@464 49 // detect if a pulse just ended
robert@464 50 int duration = pulseIn.hasPulsed(context, n);
robert@464 51 if(duration > 0){
robert@464 52 rt_printf("duration = %d\n", duration);
robert@464 53 }
robert@464 54
robert@464 55 // generate a rectangular waveform as a test signal.
robert@464 56 // Connect gDigitalOutPin to gPulseInPin
robert@464 57 // to verify that the detected pulse length is gPulseLength
robert@464 58 if(count == gPulseLength ){
robert@464 59 pulseOut = false;
robert@464 60 }
robert@464 61 if(count == (gPulseLength + gSamplesBetweenPulses)){
robert@464 62 pulseOut = true;
robert@464 63 count = 0;
robert@464 64 }
robert@464 65 digitalWrite(context, n, gDigitalOutPin, pulseOut);
robert@464 66 count++;
robert@464 67 }
robert@464 68 }
robert@464 69
robert@464 70 void cleanup(BelaContext *context, void *userData)
robert@464 71 {
robert@464 72
robert@464 73 }