robert@464: /* robert@464: ____ _____ _ _ robert@464: | __ )| ____| | / \ robert@464: | _ \| _| | | / _ \ robert@464: | |_) | |___| |___ / ___ \ robert@464: |____/|_____|_____/_/ \_\ robert@464: robert@464: The platform for ultra-low latency audio and sensor processing robert@464: robert@464: http://bela.io robert@464: robert@464: A project of the Augmented Instruments Laboratory within the robert@464: Centre for Digital Music at Queen Mary University of London. robert@464: http://www.eecs.qmul.ac.uk/~andrewm robert@464: robert@464: (c) 2016 Augmented Instruments Laboratory: Andrew McPherson, robert@464: Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack, robert@464: Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved. robert@464: robert@464: The Bela software is distributed under the GNU Lesser General Public License robert@464: (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt robert@464: */ robert@464: robert@464: robert@464: #include robert@464: #include robert@464: #include robert@464: robert@464: bool setup(BelaContext *context, void *userData) robert@464: { robert@464: pinMode(context, 0, P8_07, OUTPUT); robert@464: return true; robert@464: } robert@464: robert@464: void render(BelaContext *context, void *userData) robert@464: { robert@464: static int count=0; //counts elapsed samples robert@464: float interval=0.5; //how often to toggle the LED (in seconds) robert@464: static int status=GPIO_LOW; robert@464: for(unsigned int n=0; ndigitalFrames; n++){ robert@464: if(count==context->digitalSampleRate*interval){ //if enough samples have elapsed robert@464: count=0; //reset the counter robert@464: // status=digitalRead(context, 0, P8_07); robert@464: if(status==GPIO_LOW) { //toggle the status robert@464: digitalWrite(context, n, P8_07, status); //write the status to the LED robert@464: status=GPIO_HIGH; robert@464: } robert@464: else { robert@464: digitalWrite(context, n, P8_07, status); //write the status to the LED robert@464: status=GPIO_LOW; robert@464: } robert@464: } robert@464: count++; robert@464: } robert@464: } robert@464: robert@464: void cleanup(BelaContext *context, void *userData) robert@464: { robert@464: // Nothing to do here robert@464: } robert@464: robert@464: robert@464: /** robert@500: \example digital-output/render.cpp robert@464: robert@464: Blinking an LED robert@464: --------------- robert@464: robert@464: This sketch shows the simplest case of digital out. robert@464: robert@464: - Connect an LED in series with a 470ohm resistor between P8_07 and ground. robert@464: robert@464: The led is blinked on and off by setting the digital pin `HIGH` and `LOW` every interval seconds which is set in robert@464: `render()`. robert@464: robert@464: In `setup()` the pin mode must be set to output mode via `pinMode()`. For example: robert@464: `pinMode(context, 0, P8_07, OUTPUT)`. robert@464: In `render()` the output of the digital pins is set by `digitalWrite()`. For example: robert@464: `digitalWrite(context, n, P8_07, status)` where `status` can be equal to robert@464: either `HIGH` or `LOW`. When set `HIGH` the pin will give 3.3V, when set to robert@464: `LOW` 0V. robert@464: chris@543: 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: robert@464: To keep track of elapsed time we have a sample counter count. When the count reaches robert@464: a certain limit it switches state to either `HIGH` or `LOW` depending on its current robert@464: value. In this case the limit is `context->digitalSampleRate*interval` which robert@464: allows us to write the desired interval in seconds, stored in `interval`. robert@464: */ robert@464: