Mercurial > hg > beaglert
comparison examples/11-Extras/gpioAnalogLoopbackTest/render.cpp @ 543:8f8809c77dda prerelease
updated basics, digital, instruments, extras examples
author | chnrx <chris.heinrichs@gmail.com> |
---|---|
date | Fri, 24 Jun 2016 13:19:52 +0100 |
parents | 8fcfbfb32aa0 |
children |
comparison
equal
deleted
inserted
replaced
542:3016638b4da2 | 543:8f8809c77dda |
---|---|
23 int gDigitalInDCh = 3; | 23 int gDigitalInDCh = 3; |
24 int gAnalogOutLoopDelay; | 24 int gAnalogOutLoopDelay; |
25 int gDigitalOutLoopDelay; | 25 int gDigitalOutLoopDelay; |
26 bool setup(BelaContext *context, void *userData) | 26 bool setup(BelaContext *context, void *userData) |
27 { | 27 { |
28 // For this test we need the same amount of audio and analog input and output channels | |
29 if(context->audioInChannels != context->audioOutChannels || | |
30 context->analogInChannels != context-> analogOutChannels){ | |
31 printf("Error: for this project, you need the same number of input and output channels.\n"); | |
32 return false; | |
33 } | |
34 | |
28 rt_printf("For this test you need the following connections:\n" | 35 rt_printf("For this test you need the following connections:\n" |
29 "analog%d out->digital%d in, analog%d out->analog%d in, " | 36 "analog%d out->digital%d in, analog%d out->analog%d in, " |
30 "digital%d out -> digital%d in, digital%d out-> analog%d in\n", | 37 "digital%d out -> digital%d in, digital%d out-> analog%d in\n", |
31 gAnalogOutCh, gDigitalInACh, gAnalogOutCh, 0, gDigitalOutCh, gDigitalInDCh, gDigitalOutCh, 0); | 38 gAnalogOutCh, gDigitalInACh, gAnalogOutCh, 0, gDigitalOutCh, gDigitalInDCh, gDigitalOutCh, 0); |
32 rt_printf("Running test with %d analog channels and a buffer size of %d\n", | 39 rt_printf("Running test with %d analog channels and a buffer size of %d\n", |
33 context->analogChannels, context->audioFrames); | 40 context->analogInChannels, context->audioFrames); |
34 | 41 |
35 for(unsigned int n = 0; n < context->digitalFrames; n++){ | 42 for(unsigned int n = 0; n < context->digitalFrames; n++){ |
36 pinMode(context, n, gDigitalInACh, INPUT); | 43 pinMode(context, n, gDigitalInACh, INPUT); |
37 pinMode(context, n, gDigitalInDCh, INPUT); | 44 pinMode(context, n, gDigitalInDCh, INPUT); |
38 pinMode(context, n, gDigitalOutCh, OUTPUT); | 45 pinMode(context, n, gDigitalOutCh, OUTPUT); |
39 } | 46 } |
40 switch (context->analogChannels){ | 47 switch (context->analogOutChannels){ |
41 case 2: | 48 case 2: |
42 gAnalogOutLoopDelay = context->audioFrames*2 + 3; | 49 gAnalogOutLoopDelay = context->audioFrames*2 + 3; |
43 gDigitalOutLoopDelay = context->audioFrames*2 + 2; | 50 gDigitalOutLoopDelay = context->audioFrames*2 + 2; |
44 break; | 51 break; |
45 case 4: | 52 case 4: |
94 if(1) | 101 if(1) |
95 for(unsigned int n = 0; n < context->audioFrames; n++){ | 102 for(unsigned int n = 0; n < context->audioFrames; n++){ |
96 static bool analog0In = false; | 103 static bool analog0In = false; |
97 static bool digitalAIn = false; | 104 static bool digitalAIn = false; |
98 static int count = 0; | 105 static int count = 0; |
99 bool doReadWrite = context->analogChannels<=4 ? true : ((context->analogChannels == 8) && (n&1)==0); | 106 bool doReadWrite = context->analogInChannels<=4 ? true : ((context->analogInChannels == 8) && (n&1)==0); |
100 if(doReadWrite){ | 107 if(doReadWrite){ |
101 digitalAIn = digitalRead(context, n, gDigitalInACh); | 108 digitalAIn = digitalRead(context, n, gDigitalInACh); |
102 switch(context->analogChannels){ | 109 switch(context->analogInChannels){ |
103 case 8: | 110 case 8: |
104 analog0In = analogRead(context, n/2, 0) > 0.5; | 111 analog0In = analogRead(context, n/2, 0) > 0.5; |
105 analogWrite(context, n/2, analogOut, writePattern[outPointer]); | 112 analogWrite(context, n/2, analogOut, writePattern[outPointer]); |
106 break; | 113 break; |
107 case 4: | 114 case 4: |
148 * when reading a GPIOout, the GPIOin samples will always be one sample late | 155 * when reading a GPIOout, the GPIOin samples will always be one sample late |
149 */ | 156 */ |
150 bool doReadWrite = false; | 157 bool doReadWrite = false; |
151 static bool pastAnalog1In = false; | 158 static bool pastAnalog1In = false; |
152 digitalWriteOnce(context, n, gDigitalOutCh, writePattern[digitalOutPointer]); | 159 digitalWriteOnce(context, n, gDigitalOutCh, writePattern[digitalOutPointer]); |
153 if(context->analogChannels == 8){ | 160 if(context->analogInChannels == 8){ |
154 if((n&1) == 0){ //do it every other sample | 161 if((n&1) == 0){ //do it every other sample |
155 pastAnalog1In = analogRead(context, n/2, 1) > 0.5; | 162 pastAnalog1In = analogRead(context, n/2, 1) > 0.5; |
156 digitalDIn = digitalRead(context, n, gDigitalInDCh); | 163 digitalDIn = digitalRead(context, n, gDigitalInDCh); |
157 doReadWrite = true; | 164 doReadWrite = true; |
158 } | 165 } |
159 } | 166 } |
160 if(context->analogChannels == 4){ | 167 if(context->analogInChannels == 4){ |
161 pastAnalog1In = analogRead(context, n, 1) > 0.5; | 168 pastAnalog1In = analogRead(context, n, 1) > 0.5; |
162 digitalDIn = digitalRead(context, n, gDigitalInDCh); | 169 digitalDIn = digitalRead(context, n, gDigitalInDCh); |
163 digitalWriteOnce(context, n, gDigitalOutCh, writePattern[digitalOutPointer]); | 170 digitalWriteOnce(context, n, gDigitalOutCh, writePattern[digitalOutPointer]); |
164 doReadWrite = true; | 171 doReadWrite = true; |
165 } | 172 } |
166 if(context->analogChannels == 2){ | 173 if(context->analogInChannels == 2){ |
167 pastAnalog1In = analogRead(context, n * 2, 1) > 0.5; | 174 pastAnalog1In = analogRead(context, n * 2, 1) > 0.5; |
168 digitalDIn = digitalRead(context, n, gDigitalInDCh); | 175 digitalDIn = digitalRead(context, n, gDigitalInDCh); |
169 digitalWriteOnce(context, n, gDigitalOutCh, writePattern[digitalOutPointer]); | 176 digitalWriteOnce(context, n, gDigitalOutCh, writePattern[digitalOutPointer]); |
170 doReadWrite = true; | 177 doReadWrite = true; |
171 } | 178 } |
196 | 203 |
197 | 204 |
198 void cleanup(BelaContext *context, void *userData) | 205 void cleanup(BelaContext *context, void *userData) |
199 { | 206 { |
200 if(anaErrorCount == 0 && digErrorCount == 0){ | 207 if(anaErrorCount == 0 && digErrorCount == 0){ |
201 rt_printf("Test was succesful with %d analog channels and a buffer size of %d\n", context->analogChannels, context->audioFrames); | 208 rt_printf("Test was succesful with %d analog channels and a buffer size of %d\n", context->analogInChannels, context->audioFrames); |
202 } else { | 209 } else { |
203 rt_printf("------------------------\n%danalog %ddigital errors over %dsamples while running test with ", | 210 rt_printf("------------------------\n%danalog %ddigital errors over %dsamples while running test with ", |
204 anaErrorCount, digErrorCount, context->audioFramesElapsed); | 211 anaErrorCount, digErrorCount, context->audioFramesElapsed); |
205 rt_printf("%d analog channels and a buffer size of %d \n\n\n", | 212 rt_printf("%d analog channels and a buffer size of %d \n\n\n", |
206 context->analogChannels, context->audioFrames); | 213 context->analogInChannels, context->audioFrames); |
207 exit(1); | 214 exit(1); |
208 } | 215 } |
209 } | 216 } |