Mercurial > hg > beaglert
comparison projects/analogDigitalDemo/render.cpp @ 23:182ae9367104 matrix_gpio
- persistency: the last frame of each digital and analogOut buffers is used to initialize all the frames in the next buffer. This means that once a value is set, the pin will hold the value until you change it
- AnalogXyz macros have been renamed to analogXyz
- the short option -P has been removed. The long option --pru-file has to be used instead
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Tue, 05 May 2015 17:28:00 +0100 |
parents | fbfeb5895efd |
children | 83baffda5786 |
comparison
equal
deleted
inserted
replaced
22:fbfeb5895efd | 23:182ae9367104 |
---|---|
1 /* | 1 /* |
2 * | 2 * |
3 * First assignment for ECS732 RTDSP, to implement a 2-way audio crossover | 3 * First assignment for ECS732 RTDSP, to implement a 2-way audio crossover |
4 * using the BeagleBone Black. | 4 * using the BeagleBone Black. |
5 * | 5 * |
6 * Andrew McPherson and Victor Zappi | 6 * Andrew McPherson and Victor Zappi |
27 int numAudioFramesPerPeriod, | 27 int numAudioFramesPerPeriod, |
28 float analogSampleRate, float audioSampleRate, | 28 float analogSampleRate, float audioSampleRate, |
29 void *userData) | 29 void *userData) |
30 { | 30 { |
31 gNumAnalogChannels=numAnalogChannels; | 31 gNumAnalogChannels=numAnalogChannels; |
32 gNumDigitalChannels=numDigitalChannels; | |
32 return true; | 33 return true; |
33 } | 34 } |
34 | 35 |
35 // render() is called regularly at the highest priority by the audio engine. | 36 // render() is called regularly at the highest priority by the audio engine. |
36 // Input and output are given from the audio hardware and the other | 37 // Input and output are given from the audio hardware and the other |
39 | 40 |
40 long int gCountFrames=0; | 41 long int gCountFrames=0; |
41 void render(int numAnalogFrames, int numDigitalFrames, int numAudioFrames, float *audioIn, float *audioOut, | 42 void render(int numAnalogFrames, int numDigitalFrames, int numAudioFrames, float *audioIn, float *audioOut, |
42 float *analogIn, float *analogOut, uint32_t *digital) | 43 float *analogIn, float *analogOut, uint32_t *digital) |
43 /* | 44 /* |
44 * Hey, expect buffer underruns to happen here, as we are doing lots of printfs | 45 we assume that gNumAnalogChannels=8, numAnalogFrames==8 and numDigitalFrames==numAudioFrames |
45 * */ | 46 * */ |
46 { | 47 { |
47 gNumDigitalFrames=numDigitalFrames; | 48 gNumDigitalFrames=numDigitalFrames; |
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 | 49 |
49 // that is, P8_29 will pulse at the beginning of each buffer | 50 if((gCountFrames&31)==0){ //every 32 frames... |
51 //ANALOG channels | |
52 analogWrite(0, 0, analogRead(0,0)); // read the input0 at frame0 and write it to output0 frame0. Using analogWrite will fill the rest of the buffer with the same value | |
53 // The value at the last frame will persist through the successive buffers until is set again. | |
54 // This effectively is a pass-through with downsampling by 32 times | |
55 analogWrite(3, 0, 1.0); // write 1.0 to channel3 from frame0 to the end of the buffer | |
56 analogWrite(3, 4, 0.1); // write 0.1 to channel3 from frame4 to the end of the buffer | |
57 analogWriteFrame(3,6,0.2); //write 0.2 to channel3 only on frame 6 | |
58 //this buffer for channel 3 will look like this: 1 1 1 1 0.1 0.1 0.2 0.1 | |
59 //the next buffers for channel 3 will be filled up with 0.1 .... | |
60 //DIGITAL channels | |
61 digitalWrite(P8_07,0,GPIO_HIGH); //sets all the frames to HIGH for channel 0 | |
62 digitalWriteFrame(P8_07,4,GPIO_LOW); //only frame 4 will be LOW for channel 0 | |
63 // in this buffer the frames of channel 0 will look like this: 1 1 1 1 0 1 1 1 ...... 1 | |
64 // in the next buffer each frame of channel 0 will be initialized to 1 (the last value of this buffer) | |
65 digitalWrite(P8_08,0,GPIO_HIGH); | |
66 digitalWrite(P8_08,2,GPIO_LOW); | |
67 digitalWrite(P8_08,4,GPIO_HIGH); | |
68 digitalWrite(P8_08,5,GPIO_LOW); | |
69 // in this buffer the frames of channel 1 will look like this: 1 1 0 0 1 0 0 0 .... 0 | |
70 // in the next buffer each frame of channel 1 will be initialized to 0 (the last value of this buffer) | |
50 } | 71 } |
51 for(int i=1; i<gNumDigitalFrames; i++) | 72 for(int n=0; n<numAudioFrames; n++){ |
52 digitalWriteAll(i, GPIO_LOW); //write all channels on the given frame. Initialize them to zero | 73 for(int c=0; c<gNumAudioChannels; c++){ |
53 digitalWrite(0, 4, GPIO_HIGH); // set pin 0 HIGH from the current frame to the end of the buffer | 74 audioOut[n*gNumAudioChannels + c]=audioIn[n*gNumAudioChannels + c]; |
54 for(int n=0; n<numAnalogFrames; n++) { | 75 } |
55 for(int c=0; c<gNumAnalogChannels; c++) | 76 //use digital channels 2-5 to create a 4 bit binary counter |
56 AnalogWriteFrame(c,n,0); //set channel c on frame n to 0, equivalent to analogOut[n*numAnalogChannels+c]=0; | 77 digital[n]=digital[n] & (~0b111100); // set to zero (GPIO_OUTPUT) the bits in the lower word |
78 digital[n]=digital[n] & (~0b111100<<16); //initialize to zero the bits in the higher word (output value) | |
79 digital[n]=digital[n] | (n<<(16+2)); // set the bits in the higher word to the desired output value | |
57 } | 80 } |
58 AnalogWrite(0,3,0.2); //set channel 0 to 0.2 from frame 3 onwards ... | 81 |
59 AnalogWrite(1,3,0.7); //set channel 1 to 0.7 from frame 3 onwards ... | 82 for(int n=0; n<numAnalogFrames; n++){ |
60 AnalogWrite(2,6,0.5); //set channel 2 to 0.5 from frame 6 onwards ... | 83 analogWriteFrame(1,n,(gCountFrames&8191)/8192.0); // writes a single frame. channel 1 is a ramp that follows gCountFrames |
61 for(int n=0; n<numAudioFrames; n++){ | 84 analogWriteFrame(2,n,analogRead(2,n)); // writes a single frame. channel2 is just a passthrough |
62 printf("Digital frame %d: 0x%08x;\n",n,digital[n]); | 85 // rt_printf("Analog out frame %d :",n); |
86 // for(int c=0; c<gNumAnalogChannels; c++) | |
87 // rt_printf("%.1f ",analogOut[n*gNumAnalogChannels + c]); | |
88 // rt_printf("\n"); | |
89 gCountFrames++; | |
63 } | 90 } |
64 for(int n=0; n<numAnalogFrames; n++){ | 91 return; |
65 printf("Analog out frame %d :",n); | 92 |
66 for(int c=0; c<gNumAnalogChannels; c++) | |
67 printf("%.1f ",analogOut[n*gNumAnalogChannels + c]); | |
68 printf("\n"); | |
69 } | |
70 } | 93 } |
71 // cleanup_render() is called once at the end, after the audio has stopped. | 94 // cleanup_render() is called once at the end, after the audio has stopped. |
72 // Release any resources that were allocated in initialise_render(). | 95 // Release any resources that were allocated in initialise_render(). |
73 | 96 |
74 void cleanup_render() | 97 void cleanup_render() |