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