Mercurial > hg > beaglert
view examples/basic_pulseIn/render.cpp @ 301:e4392164b458 prerelease
RENAMED BeagleRT to Bela AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA, scripts probably not working
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Fri, 27 May 2016 14:34:41 +0100 |
parents | dbeed520b014 |
children | 1feb9c23ac57 |
line wrap: on
line source
/* * render.cpp * * Created on: Oct 24, 2014 * Author: parallels */ #include <Bela.h> #include <PulseIn.h> #include <Utilities.h> #include <stdlib.h> #include <rtdk.h> #include <cmath> // setup() is called once before the audio rendering starts. // Use it to perform any initialisation and allocation which is dependent // on the period size or sample rate. // // userData holds an opaque pointer to a data structure that was passed // in from the call to initAudio(). // // Return true on success; returning false halts the program. PulseIn pulseIn; int gPulseInPin = 0; int gDigitalOutPin = 1; int gPulseLength = 1234; int gSamplesBetweenPulses = 10000; bool setup(BelaContext *context, void *userData) { pinModeFrame(context, 0, gDigitalOutPin, OUTPUT); pulseIn.init(context, gPulseInPin, 1); //third parameter is direction return true; } // render() is called regularly at the highest priority by the audio engine. // Input and output are given from the audio hardware and the other // ADCs and DACs (if available). If only audio is available, numMatrixFrames // will be 0. void render(BelaContext *context, void *userData) { static bool pulseOut = 0; static int count = 0; for(unsigned int n = 0; n < context->digitalFrames; n++){ // detect if a pulse just ended int duration = pulseIn.hasPulsed(context, n); if(duration > 0){ rt_printf("duration = %d\n", duration); } // generate a rectangular waveform as a test signal. // Connect gDigitalOutPin to gPulseInPin // to verify that the detected pulse length is gPulseLength if(count == gPulseLength ){ pulseOut = false; } if(count == (gPulseLength + gSamplesBetweenPulses)){ pulseOut = true; count = 0; } digitalWriteFrame(context, n, gDigitalOutPin, pulseOut); count++; } } // cleanup() is called once at the end, after the audio has stopped. // Release any resources that were allocated in setup(). void cleanup(BelaContext *context, void *userData) { }