comparison projects/basic_pulseIn/render.cpp @ 206:0ec86af13cd1

Added PulseIn example
author Giulio Moro <giuliomoro@yahoo.it>
date Sat, 06 Feb 2016 12:26:59 +0000
parents
children
comparison
equal deleted inserted replaced
205:e90df8c5669f 206:0ec86af13cd1
1 /*
2 * render.cpp
3 *
4 * Created on: Oct 24, 2014
5 * Author: parallels
6 */
7
8 #include <BeagleRT.h>
9 #include <PulseIn.h>
10 #include <Utilities.h>
11 #include <stdlib.h>
12 #include <rtdk.h>
13 #include <cmath>
14
15 // setup() is called once before the audio rendering starts.
16 // Use it to perform any initialisation and allocation which is dependent
17 // on the period size or sample rate.
18 //
19 // userData holds an opaque pointer to a data structure that was passed
20 // in from the call to initAudio().
21 //
22 // Return true on success; returning false halts the program.
23 PulseIn pulseIn;
24 int gPulseInPin = 0;
25 int gDigitalOutPin = 1;
26 int gPulseLength = 1234;
27 int gSamplesBetweenPulses = 10000;
28
29 bool setup(BeagleRTContext *context, void *userData)
30 {
31 pinModeFrame(context, 0, gDigitalOutPin, OUTPUT);
32 pulseIn.init(context, gPulseInPin, 1); //third parameter is direction
33 return true;
34 }
35
36 // render() is called regularly at the highest priority by the audio engine.
37 // Input and output are given from the audio hardware and the other
38 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
39 // will be 0.
40
41 void render(BeagleRTContext *context, void *userData)
42 {
43 static bool pulseOut = 0;
44 static int count = 0;
45 for(unsigned int n = 0; n < context->digitalFrames; n++){
46 // detect if a pulse just ended
47 int duration = pulseIn.hasPulsed(context, n);
48 if(duration > 0){
49 rt_printf("duration = %d\n", duration);
50 }
51
52 // generate a rectangular waveform as a test signal.
53 // Connect gDigitalOutPin to gPulseInPin
54 // to verify that the detected pulse length is gPulseLength
55 if(count == gPulseLength ){
56 pulseOut = false;
57 }
58 if(count == (gPulseLength + gSamplesBetweenPulses)){
59 pulseOut = true;
60 count = 0;
61 }
62 digitalWriteFrame(context, n, gDigitalOutPin, pulseOut);
63 count++;
64 }
65 }
66
67 // cleanup() is called once at the end, after the audio has stopped.
68 // Release any resources that were allocated in setup().
69
70 void cleanup(BeagleRTContext *context, void *userData)
71 {
72
73 }