comparison examples/06-Sensors/basic-pulseIn/render.cpp @ 464:8fcfbfb32aa0 prerelease

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