annotate examples/analogDigitalDemo/render.cpp @ 375:768acdeea362 prerelease

Merge
author Giulio Moro <giuliomoro@yahoo.it>
date Fri, 10 Jun 2016 00:35:18 +0100
parents db2fe4e1b88e
children 9dc5a0ccad25
rev   line source
robert@372 1 /*
robert@372 2 ____ _____ _ _
robert@372 3 | __ )| ____| | / \
robert@372 4 | _ \| _| | | / _ \
robert@372 5 | |_) | |___| |___ / ___ \
robert@372 6 |____/|_____|_____/_/ \_\.io
robert@372 7
robert@372 8 */
robert@372 9
robert@372 10 /*
giuliomoro@19 11 *
giuliomoro@19 12 * Andrew McPherson and Victor Zappi
giuliomoro@19 13 * Queen Mary, University of London
giuliomoro@19 14 */
giuliomoro@19 15
robert@372 16 /**
robert@372 17 \example 3_analogDigitalDemo
robert@372 18
robert@372 19 Analog digital workout
robert@372 20 ----------------------
robert@372 21
robert@372 22 This sketch showcases many different ways to write and read digital pins,
robert@372 23 including generating clocks and creating binary counters.
robert@372 24
robert@372 25 The code as it is will not work properly, as the direction of the pins is not
robert@372 26 set. As an exercise, you will need to set the pin mode before writing or reading
robert@372 27 the digital pins.
robert@372 28
robert@372 29 This is for advanced users only.
robert@372 30 */
robert@372 31
giuliomoro@301 32 #include <Bela.h>
andrewm@56 33 #include <Utilities.h>
giuliomoro@19 34 #include <cmath>
giuliomoro@19 35 #include <rtdk.h>
giuliomoro@19 36
andrewm@56 37 // setup() is called once before the audio rendering starts.
giuliomoro@19 38 // Use it to perform any initialisation and allocation which is dependent
giuliomoro@19 39 // on the period size or sample rate.
giuliomoro@19 40 //
giuliomoro@19 41 // userData holds an opaque pointer to a data structure that was passed
giuliomoro@19 42 // in from the call to initAudio().
giuliomoro@19 43 //
giuliomoro@19 44 // Return true on success; returning false halts the program.
andrewm@52 45
giuliomoro@301 46 bool setup(BelaContext *context, void *userData)
giuliomoro@19 47 {
giuliomoro@19 48 return true;
giuliomoro@19 49 }
giuliomoro@19 50
giuliomoro@19 51 // render() is called regularly at the highest priority by the audio engine.
giuliomoro@19 52 // Input and output are given from the audio hardware and the other
giuliomoro@20 53 // ADCs and DACs (if available). If only audio is available, numAnalogFrames
giuliomoro@19 54 // will be 0.
giuliomoro@19 55
giuliomoro@301 56 void render(BelaContext *context, void *userData)
giuliomoro@19 57 /*
giuliomoro@23 58 we assume that gNumAnalogChannels=8, numAnalogFrames==8 and numDigitalFrames==numAudioFrames
giuliomoro@19 59 * */
giuliomoro@19 60 {
giuliomoro@87 61 /*
giuliomoro@87 62 * TODO: as an exercise, you will need to set the pin mode before writing or reading the digital pins.
giuliomoro@87 63 */
andrewm@311 64 if((context->audioFramesElapsed&31)==0){ //every 32 frames...
giuliomoro@23 65 //ANALOG channels
andrewm@308 66 analogWrite(context, 0, 0, analogRead(context, 0,0));
andrewm@52 67 // read the input0 at frame0 and write it to output0 frame0. Using analogWrite will fill the rest of the buffer with the same value
giuliomoro@23 68 // The value at the last frame will persist through the successive buffers until is set again.
giuliomoro@23 69 // This effectively is a pass-through with downsampling by 32 times
andrewm@308 70 analogWrite(context, 0, 3, 1.0); // write 1.0 to channel3 from frame0 to the end of the buffer
andrewm@308 71 analogWrite(context, 4, 3, 0.1); // write 0.1 to channel3 from frame4 to the end of the buffer
andrewm@308 72 analogWriteOnce(context, 6, 3, 0.2); //write 0.2 to channel3 only on frame 6
giuliomoro@23 73 //this buffer for channel 3 will look like this: 1 1 1 1 0.1 0.1 0.2 0.1
giuliomoro@23 74 //the next buffers for channel 3 will be filled up with 0.1 ....
giuliomoro@23 75 //DIGITAL channels
andrewm@308 76 digitalWrite(context, 0, P8_07, GPIO_HIGH); //sets all the frames to HIGH for channel 0
andrewm@308 77 digitalWriteOnce(context, 4, P8_07, GPIO_LOW); //only frame 4 will be LOW for channel 0
giuliomoro@23 78 // in this buffer the frames of channel 0 will look like this: 1 1 1 1 0 1 1 1 ...... 1
giuliomoro@23 79 // in the next buffer each frame of channel 0 will be initialized to 1 (the last value of this buffer)
andrewm@308 80 digitalWrite(context, 0, P8_08, GPIO_HIGH);
andrewm@308 81 digitalWrite(context, 2, P8_08, GPIO_LOW);
andrewm@308 82 digitalWrite(context, 4, P8_08, GPIO_HIGH);
andrewm@308 83 digitalWrite(context, 5, P8_08, GPIO_LOW);
andrewm@310 84 pinMode(context, 0, P9_16, GPIO_INPUT); // set channel 10 to input
giuliomoro@23 85 // in this buffer the frames of channel 1 will look like this: 1 1 0 0 1 0 0 0 .... 0
giuliomoro@23 86 // in the next buffer each frame of channel 1 will be initialized to 0 (the last value of this buffer)
giuliomoro@19 87 }
andrewm@52 88 for(unsigned int n=0; n<context->audioFrames; n++){
andrewm@52 89 for(unsigned int c=0; c<context->audioChannels; c++){
andrewm@52 90 context->audioOut[n*context->audioChannels + c]=context->audioIn[n*context->audioChannels + c];
giuliomoro@23 91 }
giuliomoro@33 92 //use digital channels 2-8 to create a 7 bit binary counter
andrewm@52 93 context->digital[n]=context->digital[n] & (~0b111111100); // set to zero (GPIO_OUTPUT) the bits in the lower word
andrewm@52 94 context->digital[n]=context->digital[n] & ((~0b111111100<<16) | 0xffff ); //initialize to zero the bits in the higher word (output value)
andrewm@311 95 context->digital[n]=context->digital[n] | ( ((context->audioFramesElapsed&0b1111111)<<(16+2)) ) ; // set the bits in the higher word to the desired output value, keeping the lower word unchanged
andrewm@308 96 digitalWrite(context, n, P8_29, digitalRead(context, n, P8_30)); // echo the input from from channel 15 to channel 14
andrewm@308 97 digitalWrite(context, n, P8_28, digitalRead(context, n, P9_16)); // echo the input from from channel 10 to channel 13
andrewm@310 98 pinMode(context, 0, P8_30, 0); //set channel 15 to input
giuliomoro@19 99 }
giuliomoro@23 100
andrewm@52 101 for(unsigned int n=0; n<context->analogFrames; n++){
andrewm@311 102 analogWrite(context, n, 1, (context->audioFramesElapsed&8191)/8192.0); // writes a single frame. channel 1 is a ramp that follows gCountFrames
andrewm@308 103 analogWrite(context, n, 2, analogRead(context, n, 2)); // writes a single frame. channel2 is just a passthrough
giuliomoro@23 104 // rt_printf("Analog out frame %d :",n);
giuliomoro@23 105 // for(int c=0; c<gNumAnalogChannels; c++)
giuliomoro@23 106 // rt_printf("%.1f ",analogOut[n*gNumAnalogChannels + c]);
giuliomoro@23 107 // rt_printf("\n");
giuliomoro@19 108 }
giuliomoro@23 109 return;
giuliomoro@23 110
giuliomoro@19 111 }
giuliomoro@19 112
andrewm@56 113 // cleanup() is called once at the end, after the audio has stopped.
andrewm@56 114 // Release any resources that were allocated in setup().
andrewm@56 115
giuliomoro@301 116 void cleanup(BelaContext *context, void *userData)
giuliomoro@19 117 {
andrewm@56 118 // Nothing to do here
giuliomoro@19 119 }