Bela
Real-time, ultra-low-latency audio and sensor processing system for BeagleBone Black
Loading...
Searching...
No Matches
Digital/digital-output/render.cpp

Blinking an LED

This sketch shows the simplest case of digital out.

The led is blinked on and off by setting the digital pin 1 and 0 every `gInterval seconds.

In setup() the pin mode must be set to output mode via pinMode(). For example: pinMode(context, 0, gOutputPin, OUTPUT). In render() the output of the digital pins is set by digitalWrite(). For example: digitalWrite(context, n, gOutputPin, status) where status can be equal to either 1 or 0. When set 1 the pin will give 3.3V, when set to 0 0V.

To keep track of elapsed time we have a sample counter count. When the count reaches a certain limit it switches state to either 1 or 0 depending on its current value. In this case the limit is context->digitalSampleRate * gInterval which is the desired interval expressed in samples.

/*
____ _____ _ _
| __ )| ____| | / \
| _ \| _| | | / _ \
| |_) | |___| |___ / ___ \
|____/|_____|_____/_/ \_\
http://bela.io
*/
#include <Bela.h>
int gOutputPin = 0; // digital pin 0 - check the pin diagram in the IDE
float gInterval = 0.5; // how often to toggle the LED (in seconds)
int gCount = 0; //counts elapsed samples
bool gStatus = false;
bool setup(BelaContext *context, void *userData)
{
pinMode(context, 0, gOutputPin, OUTPUT); // Set gOutputPin as output
return true;
}
void render(BelaContext *context, void *userData)
{
for(unsigned int n = 0; n < context->digitalFrames; ++n){
if(gCount == (int)(context->digitalSampleRate * gInterval)){ //if enough samples have elapsed
gCount = 0; //reset the counter
//toggle the status
if(gStatus == 0)
gStatus = 1;
else
gStatus = 0;
digitalWrite(context, n, gOutputPin, gStatus); //write the status to the LED (gOutputPin)
}
gCount++;
}
}
void cleanup(BelaContext *context, void *userData)
{
// Nothing to do here
}
Main Bela public API.
static void digitalWrite(BelaContext *context, int frame, int channel, int value)
Write a digital output, specifying the frame number (when to write) and the pin.
Definition Bela.h:1525
static void pinMode(BelaContext *context, int frame, int channel, int mode)
Set the direction of a digital pin to input or output.
Definition Bela.h:1548
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 digitalFrames
Number of digital frames per period.
Definition Bela.h:365
const float digitalSampleRate
Digital sample rate in Hz (currently always 44100.0)
Definition Bela.h:371