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