annotate projects/matrix_gpio_demo/render.cpp @ 18:31503d9de101 matrix_gpio

- digitalWrite and analogWrite macros are now persistent: they write a value on the given channel from the current frame to the end of the buffer. When this is not needed you can use digitalWriteFrame and analogWriteFrame instead. - included the matrix_gpio_demo code - the Eclipe project is somehow broken
author Giulio Moro <giuliomoro@yahoo.it>
date Thu, 30 Apr 2015 16:02:47 +0100
parents
children
rev   line source
giuliomoro@18 1 /*
giuliomoro@18 2 *
giuliomoro@18 3 * First assignment for ECS732 RTDSP, to implement a 2-way audio crossover
giuliomoro@18 4 * using the BeagleBone Black.
giuliomoro@18 5 *
giuliomoro@18 6 * Andrew McPherson and Victor Zappi
giuliomoro@18 7 * Queen Mary, University of London
giuliomoro@18 8 */
giuliomoro@18 9
giuliomoro@18 10 #include "../include/render.h"
giuliomoro@18 11 #include <cmath>
giuliomoro@18 12 #include <rtdk.h>
giuliomoro@18 13
giuliomoro@18 14 /* TASK: declare any global variables you need here */
giuliomoro@18 15
giuliomoro@18 16 // initialise_render() is called once before the audio rendering starts.
giuliomoro@18 17 // Use it to perform any initialisation and allocation which is dependent
giuliomoro@18 18 // on the period size or sample rate.
giuliomoro@18 19 //
giuliomoro@18 20 // userData holds an opaque pointer to a data structure that was passed
giuliomoro@18 21 // in from the call to initAudio().
giuliomoro@18 22 //
giuliomoro@18 23 // Return true on success; returning false halts the program.
giuliomoro@18 24 int gNumMatrixGpioFrames=0;
giuliomoro@18 25 bool initialise_render(int numMatrixChannels, int numMatrixGpioChannels, int numAudioChannels,
giuliomoro@18 26 int numMatrixFramesPerPeriod,
giuliomoro@18 27 int numAudioFramesPerPeriod,
giuliomoro@18 28 float matrixSampleRate, float audioSampleRate,
giuliomoro@18 29 void *userData)
giuliomoro@18 30 {
giuliomoro@18 31 gNumMatrixChannels=numMatrixChannels;
giuliomoro@18 32 return true;
giuliomoro@18 33 }
giuliomoro@18 34
giuliomoro@18 35 // render() is called regularly at the highest priority by the audio engine.
giuliomoro@18 36 // Input and output are given from the audio hardware and the other
giuliomoro@18 37 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
giuliomoro@18 38 // will be 0.
giuliomoro@18 39
giuliomoro@18 40 long int gCountFrames=0;
giuliomoro@18 41 void render(int numMatrixFrames, int numMatrixGpioFrames, int numAudioFrames, float *audioIn, float *audioOut,
giuliomoro@18 42 float *matrixIn, float *matrixOut, uint32_t *matrixGpio)
giuliomoro@18 43 /*
giuliomoro@18 44 * Hey, expect buffer underruns to happen here, as we are doing lots of printfs
giuliomoro@18 45 * */
giuliomoro@18 46 {
giuliomoro@18 47 gNumMatrixGpioFrames=numMatrixGpioFrames;
giuliomoro@18 48 if(gCountFrames==0){ //this will be executed only on the first call to render(), but the bits will go through this cycle for every subsequent buffer
giuliomoro@18 49 // that is, P8_29 will pulse at the beginning of each buffer
giuliomoro@18 50 }
giuliomoro@18 51 for(int i=1; i<gNumMatrixGpioFrames; i++)
giuliomoro@18 52 digitalWriteAll(i, GPIO_LOW); //write all channels on the given frame. Initialize them to zero
giuliomoro@18 53 digitalWrite(0, 4, GPIO_HIGH); // set pin 0 HIGH from the current frame to the end of the buffer
giuliomoro@18 54 for(int n=0; n<numMatrixFrames; n++) {
giuliomoro@18 55 for(int c=0; c<gNumMatrixChannels; c++)
giuliomoro@18 56 analogWriteFrame(c,n,0); //set channel c on frame n to 0, equivalent to matrixOut[n*numMatrixChannels+c]=0;
giuliomoro@18 57 }
giuliomoro@18 58 analogWrite(0,3,0.2); //set channel 0 to 0.2 from frame 3 onwards ...
giuliomoro@18 59 analogWrite(1,3,0.7); //set channel 1 to 0.7 from frame 3 onwards ...
giuliomoro@18 60 analogWrite(2,6,0.5); //set channel 2 to 0.5 from frame 6 onwards ...
giuliomoro@18 61 for(int n=0; n<numAudioFrames; n++){
giuliomoro@18 62 printf("Digital frame %d: 0x%08x;\n",n,matrixGpio[n]);
giuliomoro@18 63 }
giuliomoro@18 64 for(int n=0; n<numMatrixFrames; n++){
giuliomoro@18 65 printf("Analog out frame %d :",n);
giuliomoro@18 66 for(int c=0; c<gNumMatrixChannels; c++)
giuliomoro@18 67 printf("%.1f ",matrixOut[n*gNumMatrixChannels + c]);
giuliomoro@18 68 printf("\n");
giuliomoro@18 69 }
giuliomoro@18 70 }
giuliomoro@18 71 // cleanup_render() is called once at the end, after the audio has stopped.
giuliomoro@18 72 // Release any resources that were allocated in initialise_render().
giuliomoro@18 73
giuliomoro@18 74 void cleanup_render()
giuliomoro@18 75 {
giuliomoro@18 76 /* TASK:
giuliomoro@18 77 * If you allocate any memory, be sure to release it here.
giuliomoro@18 78 * You may or may not need anything in this function, depending
giuliomoro@18 79 * on your implementation.
giuliomoro@18 80 */
giuliomoro@18 81 }