annotate examples/11-Extras/gpioAnalogLoopbackTest/render.cpp @ 464:8fcfbfb32aa0 prerelease

Examples reorder with subdirectories. Added header to each project. Moved Doxygen to bottom of render.cpp.
author Robert Jack <robert.h.jack@gmail.com>
date Mon, 20 Jun 2016 16:20:38 +0100
parents
children 8f8809c77dda
rev   line source
robert@464 1 #include <Bela.h>
robert@464 2 #include <cmath>
robert@464 3 #include <rtdk.h>
robert@464 4 #include <sys/time.h>
robert@464 5 #include <sys/types.h>
robert@464 6 #include <unistd.h>
robert@464 7 #include <stats.hpp>
robert@464 8 // setup() is called once before the audio rendering starts.
robert@464 9 // Use it to perform any initialisation and allocation which is dependent
robert@464 10 // on the period size or sample rate.
robert@464 11 //
robert@464 12 // userData holds an opaque pointer to a data structure that was passed
robert@464 13 // in from the call to initAudio().
robert@464 14 //
robert@464 15 // Return true on success; returning false halts the program.
robert@464 16
robert@464 17
robert@464 18 // digital inputs can be changed at will (as they are all being processed at the same time)
robert@464 19 // analog channels must be as per below
robert@464 20 int gAnalogOutCh = 1;
robert@464 21 int gDigitalOutCh = 2;
robert@464 22 int gDigitalInACh = 0;
robert@464 23 int gDigitalInDCh = 3;
robert@464 24 int gAnalogOutLoopDelay;
robert@464 25 int gDigitalOutLoopDelay;
robert@464 26 bool setup(BelaContext *context, void *userData)
robert@464 27 {
robert@464 28 rt_printf("For this test you need the following connections:\n"
robert@464 29 "analog%d out->digital%d in, analog%d out->analog%d in, "
robert@464 30 "digital%d out -> digital%d in, digital%d out-> analog%d in\n",
robert@464 31 gAnalogOutCh, gDigitalInACh, gAnalogOutCh, 0, gDigitalOutCh, gDigitalInDCh, gDigitalOutCh, 0);
robert@464 32 rt_printf("Running test with %d analog channels and a buffer size of %d\n",
robert@464 33 context->analogChannels, context->audioFrames);
robert@464 34
robert@464 35 for(unsigned int n = 0; n < context->digitalFrames; n++){
robert@464 36 pinMode(context, n, gDigitalInACh, INPUT);
robert@464 37 pinMode(context, n, gDigitalInDCh, INPUT);
robert@464 38 pinMode(context, n, gDigitalOutCh, OUTPUT);
robert@464 39 }
robert@464 40 switch (context->analogChannels){
robert@464 41 case 2:
robert@464 42 gAnalogOutLoopDelay = context->audioFrames*2 + 3;
robert@464 43 gDigitalOutLoopDelay = context->audioFrames*2 + 2;
robert@464 44 break;
robert@464 45 case 4:
robert@464 46 gAnalogOutLoopDelay = context->audioFrames*2 + 3;
robert@464 47 gDigitalOutLoopDelay = context->audioFrames*2 + 2;
robert@464 48 break;
robert@464 49 case 8:
robert@464 50 gAnalogOutLoopDelay = context->audioFrames + 3;
robert@464 51 gDigitalOutLoopDelay = context->audioFrames + 1;
robert@464 52 break;
robert@464 53 default:
robert@464 54 exit(2);
robert@464 55 }
robert@464 56
robert@464 57 return true;
robert@464 58 }
robert@464 59
robert@464 60 // render() is called regularly at the highest priority by the audio engine.
robert@464 61 // Input and output are given from the audio hardware and the other
robert@464 62 // ADCs and DACs (if available). If only audio is available, numAnalogFrames
robert@464 63 // will be 0.
robert@464 64
robert@464 65 const int patternLength = 31;
robert@464 66 static int anaErrorCount = 0;
robert@464 67 static int digErrorCount = 0;
robert@464 68 void render(BelaContext *context, void *userData)
robert@464 69 {
robert@464 70 static bool writePattern[patternLength] = {
robert@464 71 0,1,0,1,0,0,1,1,
robert@464 72 0,0,0,1,1,1,0,0,
robert@464 73 1,1,1,1,0,0,0,0,
robert@464 74 1,1,1,1,1,0,0};
robert@464 75 // for(int n = 0; n < patternLength; n++){
robert@464 76 // writePattern[n]=1;
robert@464 77 // }
robert@464 78 static int inPointer = 0;
robert@464 79 static int outPointer = 0;
robert@464 80 static int digitalOutPointer = 0;
robert@464 81 static int digitalInPointer = 0;
robert@464 82 static int analogOut = 1;
robert@464 83 /** Checking offset between analog and digital
robert@464 84 * how it should be :
robert@464 85 * The PRU loop does the following (the loop runs at 88.2kHz):
robert@464 86 * - Read/write audio sample (once for the left channel, once for the right channel)
robert@464 87 * - Write DAC 0 or 0/2 or 0/2/4/6
robert@464 88 * - Read ADC 0 or 0/2 or 0/2/4/6, 2 samples (@176.4) older than NOW
robert@464 89 * - /During/ the line above, every two loops we also Read/Write GPIO,
robert@464 90 * therefore reading on ADC 0/2/4/6 a value that is being output from GPIO will lead to undefined results
robert@464 91 * - Write DAC 1 or 1/3 or 1/3/5/7
robert@464 92 * - Read ADC 1 or 1/3 or 1/3/5/7, 2 samples (@176.4) older than NOW
robert@464 93 */
robert@464 94 if(1)
robert@464 95 for(unsigned int n = 0; n < context->audioFrames; n++){
robert@464 96 static bool analog0In = false;
robert@464 97 static bool digitalAIn = false;
robert@464 98 static int count = 0;
robert@464 99 bool doReadWrite = context->analogChannels<=4 ? true : ((context->analogChannels == 8) && (n&1)==0);
robert@464 100 if(doReadWrite){
robert@464 101 digitalAIn = digitalRead(context, n, gDigitalInACh);
robert@464 102 switch(context->analogChannels){
robert@464 103 case 8:
robert@464 104 analog0In = analogRead(context, n/2, 0) > 0.5;
robert@464 105 analogWrite(context, n/2, analogOut, writePattern[outPointer]);
robert@464 106 break;
robert@464 107 case 4:
robert@464 108 analog0In = analogRead(context, n, 0) > 0.5;
robert@464 109 analogWrite(context, n, analogOut, writePattern[outPointer]);
robert@464 110 break;
robert@464 111 case 2:
robert@464 112 analog0In = analogRead(context, n * 2 + 1, 0) > 0.5;
robert@464 113 analogWrite(context, 2 * n, analogOut, writePattern[outPointer]);
robert@464 114 analogWrite(context, 2 * n + 1, analogOut, writePattern[outPointer]);
robert@464 115 break;
robert@464 116 }
robert@464 117 gAnalogOutLoopDelay--;
robert@464 118 outPointer++;
robert@464 119 if(gAnalogOutLoopDelay <= 0){
robert@464 120 if(++inPointer == patternLength){
robert@464 121 inPointer = 0;
robert@464 122 }
robert@464 123 }
robert@464 124 }
robert@464 125 bool expectedIn = writePattern[inPointer];
robert@464 126 if(gAnalogOutLoopDelay <= 0 && doReadWrite == true){
robert@464 127 if(analog0In != expectedIn || digitalAIn != expectedIn){
robert@464 128 rt_printf("expected: %d, received: %d %d, pointer: %d, delay: %d, count: %d\n",
robert@464 129 expectedIn, analog0In, digitalAIn, inPointer, gAnalogOutLoopDelay, count);
robert@464 130 anaErrorCount++;
robert@464 131 }
robert@464 132 }
robert@464 133 count++;
robert@464 134 if(analog0In != digitalAIn){ // at any time the analog and digital in should be the same
robert@464 135 rt_printf("ana %d_%d %d,\n", analog0In, digitalAIn, n);
robert@464 136 }
robert@464 137 if(outPointer == patternLength){
robert@464 138 outPointer = 0;
robert@464 139 }
robert@464 140 }
robert@464 141 if(1)
robert@464 142 for(unsigned int n = 0; n < context->audioFrames; n++){
robert@464 143 static int count = 0;
robert@464 144 static bool analog1In = false;
robert@464 145 static bool digitalDIn = false;
robert@464 146 /* we need to remember the pastAnalog1In because
robert@464 147 * reading GPIO takes place before writing to it, therefore
robert@464 148 * when reading a GPIOout, the GPIOin samples will always be one sample late
robert@464 149 */
robert@464 150 bool doReadWrite = false;
robert@464 151 static bool pastAnalog1In = false;
robert@464 152 digitalWriteOnce(context, n, gDigitalOutCh, writePattern[digitalOutPointer]);
robert@464 153 if(context->analogChannels == 8){
robert@464 154 if((n&1) == 0){ //do it every other sample
robert@464 155 pastAnalog1In = analogRead(context, n/2, 1) > 0.5;
robert@464 156 digitalDIn = digitalRead(context, n, gDigitalInDCh);
robert@464 157 doReadWrite = true;
robert@464 158 }
robert@464 159 }
robert@464 160 if(context->analogChannels == 4){
robert@464 161 pastAnalog1In = analogRead(context, n, 1) > 0.5;
robert@464 162 digitalDIn = digitalRead(context, n, gDigitalInDCh);
robert@464 163 digitalWriteOnce(context, n, gDigitalOutCh, writePattern[digitalOutPointer]);
robert@464 164 doReadWrite = true;
robert@464 165 }
robert@464 166 if(context->analogChannels == 2){
robert@464 167 pastAnalog1In = analogRead(context, n * 2, 1) > 0.5;
robert@464 168 digitalDIn = digitalRead(context, n, gDigitalInDCh);
robert@464 169 digitalWriteOnce(context, n, gDigitalOutCh, writePattern[digitalOutPointer]);
robert@464 170 doReadWrite = true;
robert@464 171 }
robert@464 172 bool expectedDigitalIn = writePattern[digitalInPointer];
robert@464 173 if(doReadWrite == true){
robert@464 174 gDigitalOutLoopDelay--;
robert@464 175 if(gDigitalOutLoopDelay <= 0){
robert@464 176 if(expectedDigitalIn != pastAnalog1In || expectedDigitalIn != digitalDIn){
robert@464 177 rt_printf("D expected: %d, received: %d %d, pointer: %d, delay: %d, count: %d\n",
robert@464 178 expectedDigitalIn, pastAnalog1In, digitalDIn, inPointer, gDigitalOutLoopDelay, count);
robert@464 179 digErrorCount++;
robert@464 180 }
robert@464 181 if(++digitalInPointer == patternLength){
robert@464 182 digitalInPointer = 0;
robert@464 183 }
robert@464 184 }
robert@464 185 pastAnalog1In = analog1In;
robert@464 186 if(++digitalOutPointer == patternLength){
robert@464 187 digitalOutPointer = 0;
robert@464 188 }
robert@464 189 }
robert@464 190 count++;
robert@464 191 }
robert@464 192 if(context->audioFramesElapsed > 30000){
robert@464 193 gShouldStop = true;
robert@464 194 }
robert@464 195 }
robert@464 196
robert@464 197
robert@464 198 void cleanup(BelaContext *context, void *userData)
robert@464 199 {
robert@464 200 if(anaErrorCount == 0 && digErrorCount == 0){
robert@464 201 rt_printf("Test was succesful with %d analog channels and a buffer size of %d\n", context->analogChannels, context->audioFrames);
robert@464 202 } else {
robert@464 203 rt_printf("------------------------\n%danalog %ddigital errors over %dsamples while running test with ",
robert@464 204 anaErrorCount, digErrorCount, context->audioFramesElapsed);
robert@464 205 rt_printf("%d analog channels and a buffer size of %d \n\n\n",
robert@464 206 context->analogChannels, context->audioFrames);
robert@464 207 exit(1);
robert@464 208 }
robert@464 209 }