comparison examples/02-Digital/digital-output/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
29 bool setup(BelaContext *context, void *userData)
30 {
31 pinMode(context, 0, P8_07, OUTPUT);
32 return true;
33 }
34
35 void render(BelaContext *context, void *userData)
36 {
37 static int count=0; //counts elapsed samples
38 float interval=0.5; //how often to toggle the LED (in seconds)
39 static int status=GPIO_LOW;
40 for(unsigned int n=0; n<context->digitalFrames; n++){
41 if(count==context->digitalSampleRate*interval){ //if enough samples have elapsed
42 count=0; //reset the counter
43 // status=digitalRead(context, 0, P8_07);
44 if(status==GPIO_LOW) { //toggle the status
45 digitalWrite(context, n, P8_07, status); //write the status to the LED
46 status=GPIO_HIGH;
47 }
48 else {
49 digitalWrite(context, n, P8_07, status); //write the status to the LED
50 status=GPIO_LOW;
51 }
52 }
53 count++;
54 }
55 }
56
57 void cleanup(BelaContext *context, void *userData)
58 {
59 // Nothing to do here
60 }
61
62 /* ------------ Project Explantation ------------ */
63
64 /**
65 \example 02-digital-output
66
67 Blinking an LED
68 ---------------
69
70 This sketch shows the simplest case of digital out.
71
72 - Connect an LED in series with a 470ohm resistor between P8_07 and ground.
73
74 The led is blinked on and off by setting the digital pin `HIGH` and `LOW` every interval seconds which is set in
75 `render()`.
76
77 In `setup()` the pin mode must be set to output mode via `pinMode()`. For example:
78 `pinMode(context, 0, P8_07, OUTPUT)`.
79 In `render()` the output of the digital pins is set by `digitalWrite()`. For example:
80 `digitalWrite(context, n, P8_07, status)` where `status` can be equal to
81 either `HIGH` or `LOW`. When set `HIGH` the pin will give 3.3V, when set to
82 `LOW` 0V.
83
84 To keep track of elapsed time we have a sample counter count. When the count reaches
85 a certain limit it switches state to either `HIGH` or `LOW` depending on its current
86 value. In this case the limit is `context->digitalSampleRate*interval` which
87 allows us to write the desired interval in seconds, stored in `interval`.
88 */
89