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

Switching an LED on and off

This example brings together digital input and digital output. The program will read a button and turn the LED on and off according to the state of the button.

Before using the digital pins we need to set whether they are input or output. This is done via pinMode(context, 0, gInputPin, INPUT);.

You will notice that the LED will normally stay on and will turn off only when the button is pressed. This is due to the fact that the LED is set to the same value read at gInputPin. When the button is not pressed, gInputPin is HIGH and so gOutputPin is set to HIGH as well, so that the LED conducts and emits light. When the button is pressed, gInputPin goes LOW and gOutputPin is set to LOW, turning off the LED.

As an exercise try and change the code so that the LED only turns on when the button is pressed.

/*
____ _____ _ _
| __ )| ____| | / \
| _ \| _| | | / _ \
| |_) | |___| |___ / ___ \
|____/|_____|_____/_/ \_\
http://bela.io
*/
#include <Bela.h>
#include <stdlib.h>
int gInputPin = 1; // digital pin 1 - check the pin diagram in the IDE
int gOutputPin = 0; // digital pin 0 - check the pin diagram in the IDE
bool setup(BelaContext *context, void *userData)
{
// Set the mode of digital pins
pinMode(context, 0, gInputPin, INPUT); //set input
pinMode(context, 0, gOutputPin, OUTPUT); // setoutput
return true;
}
void render(BelaContext *context, void *userData)
{
for(unsigned int n=0; n<context->digitalFrames; n++){
int status=digitalRead(context, 0, gInputPin); //read the value of the button
digitalWriteOnce(context, n, gOutputPin, status); //write the status to the LED
float out = 0.1f * status * rand() / (float)RAND_MAX * 2.f - 1.f; //generate some noise, gated by the button
for(unsigned int j = 0; j < context->audioOutChannels; j++){
audioWrite(context, n, j, out); //write the audio output
}
}
}
void cleanup(BelaContext *context, void *userData)
{
// Nothing to do here
}
Main Bela public API.
static int digitalRead(BelaContext *context, int frame, int channel)
Read a digital input, specifying the frame number (when to read) and the pin.
Definition Bela.h:1518
static void audioWrite(BelaContext *context, int frame, int channel, float value)
Write an audio output, specifying the frame number (when to write) and the channel.
Definition Bela.h:1469
static void digitalWriteOnce(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:1538
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 uint32_t audioOutChannels
The number of audio output channels.
Definition Bela.h:326