Mercurial > hg > beaglert
comparison projects/basic_button/render.cpp @ 246:da5ce9876fcc
Improved basic_button project
| author | Giulio Moro <giuliomoro@yahoo.it> | 
|---|---|
| date | Tue, 19 Apr 2016 22:22:26 +0100 | 
| parents | db76215feb69 | 
| children | ac8eb07afcf5 | 
   comparison
  equal
  deleted
  inserted
  replaced
| 245:ba0b63625146 | 246:da5ce9876fcc | 
|---|---|
| 1 /* | 1 /* | 
| 2 * | 2 * | 
| 3 * Andrew McPherson and Victor Zappi | 3 * Andrew McPherson and Victor Zappi | 
| 4 * Queen Mary, University of London | 4 * Queen Mary, University of London | 
| 5 */ | 5 */ | 
| 6 | 6 | 
| 7 #include <BeagleRT.h> | 7 #include <BeagleRT.h> | 
| 8 #include <Utilities.h> | 8 #include <Utilities.h> | 
| 9 #include <cmath> | 9 #include <cmath> | 
| 10 #include <rtdk.h> | 10 #include <rtdk.h> | 
| 11 #include <stdlib.h> | |
| 11 | 12 | 
| 12 // setup() is called once before the audio rendering starts. | 13 // setup() is called once before the audio rendering starts. | 
| 13 // Use it to perform any initialisation and allocation which is dependent | 14 // Use it to perform any initialisation and allocation which is dependent | 
| 14 // on the period size or sample rate. | 15 // on the period size or sample rate. | 
| 15 // | 16 // | 
| 18 // | 19 // | 
| 19 // Return true on success; returning false halts the program. | 20 // Return true on success; returning false halts the program. | 
| 20 | 21 | 
| 21 bool setup(BeagleRTContext *context, void *userData) | 22 bool setup(BeagleRTContext *context, void *userData) | 
| 22 { | 23 { | 
| 23 for(unsigned int n=0; n<context->digitalFrames; n++){ | 24 pinModeFrame(context, 0, P8_08, INPUT); | 
| 24 pinModeFrame(context, 0, P8_08, INPUT); | 25 pinModeFrame(context, 0, P8_07, OUTPUT); | 
| 25 pinModeFrame(context, 0, P8_07, OUTPUT); | |
| 26 } | |
| 27 return true; | 26 return true; | 
| 28 } | 27 } | 
| 29 | 28 | 
| 30 // render() is called regularly at the highest priority by the audio engine. | 29 // render() is called regularly at the highest priority by the audio engine. | 
| 31 // Input and output are given from the audio hardware and the other | 30 // Input and output are given from the audio hardware and the other | 
| 40 * The program will read the button and make the LED blink when the button is pressed. | 39 * The program will read the button and make the LED blink when the button is pressed. | 
| 41 */ | 40 */ | 
| 42 | 41 | 
| 43 void render(BeagleRTContext *context, void *userData) | 42 void render(BeagleRTContext *context, void *userData) | 
| 44 { | 43 { | 
| 45 float sum=0; | |
| 46 for(unsigned int n=0; n<context->digitalFrames; n++){ | 44 for(unsigned int n=0; n<context->digitalFrames; n++){ | 
| 47 int status=digitalReadFrame(context, 0, P8_08); //read the value of the button | 45 int status=digitalReadFrame(context, 0, P8_08); //read the value of the button | 
| 48 sum += status; | 46 digitalWriteFrameOnce(context, n, P8_07, status); //write the status to the LED | 
| 49 digitalWriteFrame(context, n, P8_07, status); //write the status to the LED | 47 float out = 0.1 * status * rand() / (float)RAND_MAX * 2 - 1; //generate some noise, gated by the button | 
| 48 for(unsigned int j = 0; j < context->audioChannels; j++){ | |
| 49 audioWriteFrame(context, n, j, out); //write the audio output | |
| 50 } | |
| 50 } | 51 } | 
| 51 rt_printf("status: %f\n", sum); | |
| 52 } | 52 } | 
| 53 | 53 | 
| 54 // cleanup() is called once at the end, after the audio has stopped. | 54 // cleanup() is called once at the end, after the audio has stopped. | 
| 55 // Release any resources that were allocated in setup(). | 55 // Release any resources that were allocated in setup(). | 
| 56 | 56 | 
| 57 void cleanup(BeagleRTContext *context, void *userData) | 57 void cleanup(BeagleRTContext *context, void *userData) | 
| 58 { | 58 { | 
| 59 // Nothing to do here | 59 // Nothing to do here | 
| 60 } | 60 } | 
| 61 | 
