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