annotate projects/basic_button/render.cpp @ 269:ac8eb07afcf5

Oxygen text added to each render.cpp file for the default projects. Text includes project explanation from Wiki, edited in places. Empty project added as a default project. Doxyfile updated. Each of the project locations added to INPUT configuration option. Consider just watching the whole project file so all new projects are automatically pulled through.
author Robert Jack <robert.h.jack@gmail.com>
date Tue, 17 May 2016 15:40:16 +0100
parents da5ce9876fcc
children 5433c83ce04e
rev   line source
robert@269 1 /*
robert@269 2 ____ _____ _ _
robert@269 3 | __ )| ____| | / \
robert@269 4 | _ \| _| | | / _ \
robert@269 5 | |_) | |___| |___ / ___ \
robert@269 6 |____/|_____|_____/_/ \_\.io
robert@269 7
robert@269 8 */
robert@269 9
giuliomoro@246 10 /*
giuliomoro@77 11 *
giuliomoro@77 12 * Andrew McPherson and Victor Zappi
giuliomoro@77 13 * Queen Mary, University of London
giuliomoro@77 14 */
giuliomoro@77 15
robert@269 16 /**
robert@269 17 \example 2_digital_button
robert@269 18
robert@269 19 Switching an LED on and off
robert@269 20 ---------------------------
robert@269 21
robert@269 22 This sketch brings together digital out with digital in. The program will read
robert@269 23 a button and turn the LED on and off according to the state of the button.
robert@269 24
robert@269 25 - connect an LED in series with a 470ohm resistor between P8_07 and ground.
robert@269 26 - connect a 1k resistor to P9_03 (+3.3V),
robert@269 27 - connect the other end of the resistor to both a button and P8_08
robert@269 28 - connect the other end of the button to ground.
robert@269 29
robert@269 30 You will notice that the LED will normally stay on and will turn off as long as
robert@269 31 the button is pressed. This is due to the fact that the LED is set to the same
robert@269 32 value read at input P8_08. When the button is not pressed, P8_08 is `HIGH` and so
robert@269 33 P8_07 is set to `HIGH` as well, so that the LED conducts and emits light. When
robert@269 34 the button is pressed, P8_08 goes `LOW` and P8_07 is set to `LOW`, turning off the LED.
robert@269 35
robert@269 36 As an exercise try and change the code so that the LED only turns on when
robert@269 37 the button is pressed
robert@269 38 */
robert@269 39
robert@269 40
giuliomoro@77 41 #include <BeagleRT.h>
giuliomoro@77 42 #include <Utilities.h>
giuliomoro@77 43 #include <cmath>
giuliomoro@77 44 #include <rtdk.h>
giuliomoro@246 45 #include <stdlib.h>
giuliomoro@77 46
giuliomoro@77 47 // setup() is called once before the audio rendering starts.
giuliomoro@77 48 // Use it to perform any initialisation and allocation which is dependent
giuliomoro@77 49 // on the period size or sample rate.
giuliomoro@77 50 //
giuliomoro@77 51 // userData holds an opaque pointer to a data structure that was passed
giuliomoro@77 52 // in from the call to initAudio().
giuliomoro@77 53 //
giuliomoro@77 54 // Return true on success; returning false halts the program.
giuliomoro@77 55
giuliomoro@77 56 bool setup(BeagleRTContext *context, void *userData)
giuliomoro@77 57 {
giuliomoro@246 58 pinModeFrame(context, 0, P8_08, INPUT);
giuliomoro@246 59 pinModeFrame(context, 0, P8_07, OUTPUT);
giuliomoro@77 60 return true;
giuliomoro@77 61 }
giuliomoro@77 62
giuliomoro@77 63 // render() is called regularly at the highest priority by the audio engine.
giuliomoro@77 64 // Input and output are given from the audio hardware and the other
giuliomoro@77 65 // ADCs and DACs (if available). If only audio is available, numAnalogFrames
giuliomoro@77 66 // will be 0.
giuliomoro@77 67
giuliomoro@77 68 /* basic_button
giuliomoro@77 69 * - connect an LED in series with a 470ohm resistor between P8_07 and ground.
giuliomoro@77 70 * - connect a 1k resistor to P9_03 (+3.3V),
giuliomoro@77 71 * - connect the other end of the resistor to both a button and P8_08
giuliomoro@77 72 * - connect the other end of the button to ground.
giuliomoro@77 73 * The program will read the button and make the LED blink when the button is pressed.
giuliomoro@77 74 */
giuliomoro@77 75
giuliomoro@77 76 void render(BeagleRTContext *context, void *userData)
giuliomoro@77 77 {
giuliomoro@77 78 for(unsigned int n=0; n<context->digitalFrames; n++){
giuliomoro@223 79 int status=digitalReadFrame(context, 0, P8_08); //read the value of the button
giuliomoro@246 80 digitalWriteFrameOnce(context, n, P8_07, status); //write the status to the LED
giuliomoro@246 81 float out = 0.1 * status * rand() / (float)RAND_MAX * 2 - 1; //generate some noise, gated by the button
giuliomoro@246 82 for(unsigned int j = 0; j < context->audioChannels; j++){
giuliomoro@246 83 audioWriteFrame(context, n, j, out); //write the audio output
giuliomoro@246 84 }
giuliomoro@223 85 }
giuliomoro@77 86 }
giuliomoro@77 87
giuliomoro@77 88 // cleanup() is called once at the end, after the audio has stopped.
giuliomoro@77 89 // Release any resources that were allocated in setup().
giuliomoro@77 90
giuliomoro@77 91 void cleanup(BeagleRTContext *context, void *userData)
giuliomoro@77 92 {
giuliomoro@77 93 // Nothing to do here
giuliomoro@77 94 }
giuliomoro@246 95