annotate examples/02-Digital/digital-input/render.cpp @ 507:1cec96845a23 prerelease

Explanted explantation
author Giulio Moro <giuliomoro@yahoo.it>
date Wed, 22 Jun 2016 01:51:17 +0100
parents b935f890e512
children 8f8809c77dda
rev   line source
robert@464 1 /*
robert@464 2 ____ _____ _ _
robert@464 3 | __ )| ____| | / \
robert@464 4 | _ \| _| | | / _ \
robert@464 5 | |_) | |___| |___ / ___ \
robert@464 6 |____/|_____|_____/_/ \_\
robert@464 7
robert@464 8 The platform for ultra-low latency audio and sensor processing
robert@464 9
robert@464 10 http://bela.io
robert@464 11
robert@464 12 A project of the Augmented Instruments Laboratory within the
robert@464 13 Centre for Digital Music at Queen Mary University of London.
robert@464 14 http://www.eecs.qmul.ac.uk/~andrewm
robert@464 15
robert@464 16 (c) 2016 Augmented Instruments Laboratory: Andrew McPherson,
robert@464 17 Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack,
robert@464 18 Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved.
robert@464 19
robert@464 20 The Bela software is distributed under the GNU Lesser General Public License
robert@464 21 (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt
robert@464 22 */
robert@464 23
robert@464 24
robert@464 25 #include <Bela.h>
robert@464 26 #include <cmath>
robert@464 27 #include <rtdk.h>
robert@464 28 #include <stdlib.h>
robert@464 29
robert@464 30
robert@464 31 bool setup(BelaContext *context, void *userData)
robert@464 32 {
robert@464 33 pinMode(context, 0, P8_08, INPUT);
robert@464 34 pinMode(context, 0, P8_07, OUTPUT);
robert@464 35 return true;
robert@464 36 }
robert@464 37
robert@464 38
robert@464 39 void render(BelaContext *context, void *userData)
robert@464 40 {
robert@464 41 for(unsigned int n=0; n<context->digitalFrames; n++){
robert@464 42 int status=digitalRead(context, 0, P8_08); //read the value of the button
robert@464 43 digitalWriteOnce(context, n, P8_07, status); //write the status to the LED
robert@464 44 float out = 0.1 * status * rand() / (float)RAND_MAX * 2 - 1; //generate some noise, gated by the button
robert@464 45 for(unsigned int j = 0; j < context->audioChannels; j++){
robert@464 46 audioWrite(context, n, j, out); //write the audio output
robert@464 47 }
robert@464 48 }
robert@464 49 }
robert@464 50
robert@464 51
robert@464 52 void cleanup(BelaContext *context, void *userData)
robert@464 53 {
robert@464 54 // Nothing to do here
robert@464 55 }
robert@464 56
robert@464 57
robert@464 58 /**
robert@500 59 \example digital-input/render.cpp
robert@464 60
robert@464 61 Switching an LED on and off
robert@464 62 ---------------------------
robert@464 63
robert@464 64 This example brings together digital input and digital output. The program will read
robert@464 65 a button and turn the LED on and off according to the state of the button.
robert@464 66
robert@464 67 - connect an LED in series with a 470ohm resistor between P8_07 and ground.
robert@464 68 - connect a 1k resistor to P9_03 (+3.3V),
robert@464 69 - connect the other end of the resistor to both a button and P8_08
robert@464 70 - connect the other end of the button to ground.
robert@464 71
robert@464 72 You will notice that the LED will normally stay on and will turn off as long as
robert@464 73 the button is pressed. This is due to the fact that the LED is set to the same
robert@464 74 value read at input P8_08. When the button is not pressed, P8_08 is `HIGH` and so
robert@464 75 P8_07 is set to `HIGH` as well, so that the LED conducts and emits light. When
robert@464 76 the button is pressed, P8_08 goes `LOW` and P8_07 is set to `LOW`, turning off the LED.
robert@464 77
robert@464 78 As an exercise try and change the code so that the LED only turns on when
robert@464 79 the button is pressed.
robert@464 80 */
robert@464 81