giuliomoro@206: /* giuliomoro@206: * render.cpp giuliomoro@206: * giuliomoro@206: * Created on: Oct 24, 2014 giuliomoro@206: * Author: parallels giuliomoro@206: */ giuliomoro@206: giuliomoro@206: #include giuliomoro@206: #include giuliomoro@206: #include giuliomoro@206: #include giuliomoro@206: #include giuliomoro@206: #include giuliomoro@206: giuliomoro@206: // setup() is called once before the audio rendering starts. giuliomoro@206: // Use it to perform any initialisation and allocation which is dependent giuliomoro@206: // on the period size or sample rate. giuliomoro@206: // giuliomoro@206: // userData holds an opaque pointer to a data structure that was passed giuliomoro@206: // in from the call to initAudio(). giuliomoro@206: // giuliomoro@206: // Return true on success; returning false halts the program. giuliomoro@206: PulseIn pulseIn; giuliomoro@206: int gPulseInPin = 0; giuliomoro@206: int gDigitalOutPin = 1; giuliomoro@206: int gPulseLength = 1234; giuliomoro@206: int gSamplesBetweenPulses = 10000; giuliomoro@206: giuliomoro@206: bool setup(BeagleRTContext *context, void *userData) giuliomoro@206: { giuliomoro@206: pinModeFrame(context, 0, gDigitalOutPin, OUTPUT); giuliomoro@206: pulseIn.init(context, gPulseInPin, 1); //third parameter is direction giuliomoro@206: return true; giuliomoro@206: } giuliomoro@206: giuliomoro@206: // render() is called regularly at the highest priority by the audio engine. giuliomoro@206: // Input and output are given from the audio hardware and the other giuliomoro@206: // ADCs and DACs (if available). If only audio is available, numMatrixFrames giuliomoro@206: // will be 0. giuliomoro@206: giuliomoro@206: void render(BeagleRTContext *context, void *userData) giuliomoro@206: { giuliomoro@206: static bool pulseOut = 0; giuliomoro@206: static int count = 0; giuliomoro@206: for(unsigned int n = 0; n < context->digitalFrames; n++){ giuliomoro@206: // detect if a pulse just ended giuliomoro@206: int duration = pulseIn.hasPulsed(context, n); giuliomoro@206: if(duration > 0){ giuliomoro@206: rt_printf("duration = %d\n", duration); giuliomoro@206: } giuliomoro@206: giuliomoro@206: // generate a rectangular waveform as a test signal. giuliomoro@206: // Connect gDigitalOutPin to gPulseInPin giuliomoro@206: // to verify that the detected pulse length is gPulseLength giuliomoro@206: if(count == gPulseLength ){ giuliomoro@206: pulseOut = false; giuliomoro@206: } giuliomoro@206: if(count == (gPulseLength + gSamplesBetweenPulses)){ giuliomoro@206: pulseOut = true; giuliomoro@206: count = 0; giuliomoro@206: } giuliomoro@206: digitalWriteFrame(context, n, gDigitalOutPin, pulseOut); giuliomoro@206: count++; giuliomoro@206: } giuliomoro@206: } giuliomoro@206: giuliomoro@206: // cleanup() is called once at the end, after the audio has stopped. giuliomoro@206: // Release any resources that were allocated in setup(). giuliomoro@206: giuliomoro@206: void cleanup(BeagleRTContext *context, void *userData) giuliomoro@206: { giuliomoro@206: giuliomoro@206: }