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