annotate examples/basic_button/render.cpp @ 372:db2fe4e1b88e prerelease

Doxygen content added to each example render.cpp. References to AnalogReadFrame etc. removed from doxygen content.
author Robert Jack <robert.h.jack@gmail.com>
date Thu, 09 Jun 2016 18:16:05 +0100
parents 02c4ca0e3718
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
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@372 16 /**
robert@372 17 \example 2_digital_button
robert@372 18
robert@372 19 Switching an LED on and off
robert@372 20 ---------------------------
robert@372 21
robert@372 22 This example brings together digital input and digital output. The program will read
robert@372 23 a button and turn the LED on and off according to the state of the button.
robert@372 24
robert@372 25 - connect an LED in series with a 470ohm resistor between P8_07 and ground.
robert@372 26 - connect a 1k resistor to P9_03 (+3.3V),
robert@372 27 - connect the other end of the resistor to both a button and P8_08
robert@372 28 - connect the other end of the button to ground.
robert@372 29
robert@372 30 You will notice that the LED will normally stay on and will turn off as long as
robert@372 31 the button is pressed. This is due to the fact that the LED is set to the same
robert@372 32 value read at input P8_08. When the button is not pressed, P8_08 is `HIGH` and so
robert@372 33 P8_07 is set to `HIGH` as well, so that the LED conducts and emits light. When
robert@372 34 the button is pressed, P8_08 goes `LOW` and P8_07 is set to `LOW`, turning off the LED.
robert@372 35
robert@372 36 As an exercise try and change the code so that the LED only turns on when
robert@372 37 the button is pressed.
robert@372 38 */
robert@372 39
giuliomoro@301 40 #include <Bela.h>
giuliomoro@77 41 #include <Utilities.h>
giuliomoro@77 42 #include <cmath>
giuliomoro@77 43 #include <rtdk.h>
giuliomoro@246 44 #include <stdlib.h>
giuliomoro@77 45
giuliomoro@77 46 // setup() is called once before the audio rendering starts.
giuliomoro@77 47 // Use it to perform any initialisation and allocation which is dependent
giuliomoro@77 48 // on the period size or sample rate.
giuliomoro@77 49 //
giuliomoro@77 50 // userData holds an opaque pointer to a data structure that was passed
giuliomoro@77 51 // in from the call to initAudio().
giuliomoro@77 52 //
giuliomoro@77 53 // Return true on success; returning false halts the program.
giuliomoro@77 54
giuliomoro@301 55 bool setup(BelaContext *context, void *userData)
giuliomoro@77 56 {
andrewm@310 57 pinMode(context, 0, P8_08, INPUT);
andrewm@310 58 pinMode(context, 0, P8_07, OUTPUT);
giuliomoro@77 59 return true;
giuliomoro@77 60 }
giuliomoro@77 61
giuliomoro@77 62 // render() is called regularly at the highest priority by the audio engine.
giuliomoro@77 63 // Input and output are given from the audio hardware and the other
giuliomoro@77 64 // ADCs and DACs (if available). If only audio is available, numAnalogFrames
giuliomoro@77 65 // will be 0.
giuliomoro@77 66
giuliomoro@77 67 /* basic_button
giuliomoro@77 68 * - connect an LED in series with a 470ohm resistor between P8_07 and ground.
giuliomoro@77 69 * - connect a 1k resistor to P9_03 (+3.3V),
giuliomoro@77 70 * - connect the other end of the resistor to both a button and P8_08
giuliomoro@77 71 * - connect the other end of the button to ground.
giuliomoro@77 72 * The program will read the button and make the LED blink when the button is pressed.
giuliomoro@77 73 */
giuliomoro@77 74
giuliomoro@301 75 void render(BelaContext *context, void *userData)
giuliomoro@77 76 {
giuliomoro@77 77 for(unsigned int n=0; n<context->digitalFrames; n++){
andrewm@308 78 int status=digitalRead(context, 0, P8_08); //read the value of the button
andrewm@308 79 digitalWriteOnce(context, n, P8_07, status); //write the status to the LED
giuliomoro@246 80 float out = 0.1 * status * rand() / (float)RAND_MAX * 2 - 1; //generate some noise, gated by the button
giuliomoro@246 81 for(unsigned int j = 0; j < context->audioChannels; j++){
andrewm@308 82 audioWrite(context, n, j, out); //write the audio output
giuliomoro@246 83 }
giuliomoro@223 84 }
giuliomoro@77 85 }
giuliomoro@77 86
giuliomoro@77 87 // cleanup() is called once at the end, after the audio has stopped.
giuliomoro@77 88 // Release any resources that were allocated in setup().
giuliomoro@77 89
giuliomoro@301 90 void cleanup(BelaContext *context, void *userData)
giuliomoro@77 91 {
giuliomoro@77 92 // Nothing to do here
giuliomoro@77 93 }
giuliomoro@246 94