Controlling a digital pin synchronously
This sketch shows how to control a digital pin synchronously from your code. This is an alternative approach to using the Bela Digital I/Os, which are sampled at audio rate. With this approach, the sampling time is not periodic.
Examples use cases:
- accurate, minimally invasive measurements of CPU time for specific blocks of code (simply toggle a pin before and after the section you want to measure and monitor it with an external oscilloscope)
- performing non-time-critical digital I/O (e.g.: reading buttons or driving LEDs) when you need more than the 16 I/Os provided by the Bela core.
- manually driving the on-board LEDs for BelaMini (requires running with --disable-led).
See here for more details: https://github.com/BelaPlatform/Bela/wiki/Using-arbitrary-GPIO-pins
In this example we use as an output GPIO pin 89, which on Bela is P8.30 and on BelaMini is P1.04, but is also connected to the on-board red LED. Additionally, we read the button on the Bela cape. DO NOT press the button for more than 2 seconds at a time, unless you disabled it globally on your system, or you will trigger the default action (system shutdown).
Preparation:
On Bela:
- Connect an LED in series with a 470ohm resistor between digital pin P8.30 and ground.
- Make sure you run it with Bela's digitals disabled. This is not normally needed when using the Gpio class, unless - like in this case - one of the pins in use would otherwise be used by the Bela Digital I/Os. On BelaMini:
- use the onboard LED, overriding its default behaviour
- make sure you run with --disable-led
In both cases, you have to disable the default behaviour of the button on the cape by running with --disable-cape-button-monitoring. Again, DO NOT hold-press the button for more than 2 seconds at a time unless it is disabled globally.
The led is blinked on and off by setting the digital pin to 1 and 0 every gInterval seconds, unless the button is pressed, in which case the blinking is temporarily disable.
In setup() the pin is opened and initialized as an output mode via a call to Gpio::open().
In render() the output of the output digital pin is set by Gpio::write(), and the input digital pin is read calling Gpio::read().
To keep track of elapsed time we have a sample counter gCount. When gCount reaches the desired value, it switches the state of the LED pin.
 
#include <Gpio.h>
 
bool gShouldBlink = 1;
int gInputPin = 115; 
int gOutputPin = 89; 
float gInterval = 0.1; 
int gCount = 0; 
bool gStatus = false;
 
{
        gpioIn.
open(gInputPin, Gpio::INPUT); 
        gpioOut.open(gOutputPin, Gpio::OUTPUT); 
        return true;
}
 
{
        if(gpioIn.read()) 
                gShouldBlink = true; 
        else
                gShouldBlink = false; 
        for(
unsigned int n = 0; n < context->
audioFrames; ++n){
 
                        gCount = 0; 
                        
                        if(gStatus == 0)
                                gStatus = 1;
                        else
                                gStatus = 0;
                        if(gShouldBlink)
                                gpioOut.write(gStatus); 
                }
                gCount++;
        }
}
 
{
        
}
int open(unsigned int pin, Direction direction, bool unexport=true)
void render(BelaContext *context, void *userData)
User-defined callback function to process audio and sensor data.
Definition render.cpp:68
bool setup(BelaContext *context, void *userData)
User-defined initialisation function which runs before audio rendering begins.
Definition render.cpp:51
void cleanup(BelaContext *context, void *userData)
User-defined cleanup function which runs when the program finishes.
Definition render.cpp:96
Structure holding audio and sensor settings and pointers to I/O data buffers.
Definition Bela.h:231
const uint32_t audioFrames
The number of audio frames per block.
Definition Bela.h:322
const float audioSampleRate
The audio sample rate in Hz (currently always 44100.0).
Definition Bela.h:328