comparison examples/06-Sensors/basic-pulseIn/render.cpp @ 468:85cf9c0da052 prerelease

merge
author Giulio Moro <giuliomoro@yahoo.it>
date Mon, 20 Jun 2016 17:08:02 +0100
parents 8fcfbfb32aa0
children
comparison
equal deleted inserted replaced
467:03a2cd5f151b 468:85cf9c0da052
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 }