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

Trill Bar LED

This is example of how to communicate with the Trill Bar sensor using the Trill library. It also visualises position of different touches in real time via a series of LEDs connected to the digital outputs.

The Trill sensor is scanned on an auxiliary task running parallel to the audio thread and the number of active touches, their position and size stored on global variables.

Twelve LEDs are used to represent positions on the Trill sensor. The length of the Trill Bar sensor is divided into 12 different sections. When a touch occurs on one of these sections, the corresponding LED turns on.

/*
____ _____ _ _
| __ )| ____| | / \
| _ \| _| | | / _ \
| |_) | |___| |___ / ___ \
|____/|_____|_____/_/ \_\
http://bela.io
*/
#include <Bela.h>
#include <libraries/Trill/Trill.h>
#include <cmath>
#define NUM_TOUCH 5 // Number of touches on Trill sensor
#define NUM_LED 12 // Number of LEDs used for visualisation
// Trill object declaration
Trill touchSensor;
// Location of touches on Trill Bar
float gTouchLocation[NUM_TOUCH] = { 0.0, 0.0, 0.0, 0.0, 0.0 };
// Size of touches on Trill bar
float gTouchSize[NUM_TOUCH] = { 0.0, 0.0, 0.0, 0.0, 0.0 };
// Number of active touches
int gNumActiveTouches = 0;
// Sleep time for auxiliary task
unsigned int gTaskSleepTime = 12000; // microseconds
// Time period for printing
float gTimePeriod = 0.01; // seconds
// Digital pins assigned to LEDs used for visualisation
unsigned int gLedPins[NUM_LED] = { 0, 1, 2, 3, 4, 5, 8, 9, 11, 12, 13, 14 };
// Status of LEDs (1: ON, 0: OFF)
bool gLedStatus[NUM_LED] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
/*
* Function to be run on an auxiliary task that reads data from the Trill sensor.
* Here, a loop is defined so that the task runs recurrently for as long as the
* audio thread is running.
*/
void loop(void*)
{
{
// Read locations from Trill sensor
touchSensor.readI2C();
gNumActiveTouches = touchSensor.getNumTouches();
for(unsigned int i = 0; i < gNumActiveTouches; i++)
{
gTouchLocation[i] = touchSensor.touchLocation(i);
gTouchSize[i] = touchSensor.touchSize(i);
}
// For all inactive touches, set location and size to 0
for(unsigned int i = gNumActiveTouches; i < NUM_TOUCH; i++)
{
gTouchLocation[i] = 0.0;
gTouchSize[i] = 0.0;
}
usleep(gTaskSleepTime);
}
}
bool setup(BelaContext *context, void *userData)
{
// Setup a Trill Bar sensor on i2c bus 1, using the default mode and address
if(touchSensor.setup(1, Trill::BAR) != 0) {
fprintf(stderr, "Unable to initialise Trill Bar\n");
return false;
}
touchSensor.printDetails();
// Set and schedule auxiliary task for reading sensor data from the I2C bus
// Set all digital pins corresponding to LEDs as outputs
for(unsigned int l = 0; l < NUM_LED; l++)
pinMode(context, 0, gLedPins[l], OUTPUT);
return true;
}
void render(BelaContext *context, void *userData)
{
// Active sections of the Trill BAR
bool activeSections[NUM_LED] = { false };
// Printing counter
static unsigned int count = 0;
// Each audio frame, check location of active touches and round to the the number
// of sections on which the Trill bar has been devided.
// Set LED status based on activations of corresponding sections.
// Print LED status.
for(unsigned int n = 0; n < context->audioFrames; n++) {
for(unsigned int t = 0; t < gNumActiveTouches; t++) {
int section = floor( NUM_LED * gTouchLocation[t] );
activeSections[section] = activeSections[section] || 1;
}
for(unsigned int l = 0; l < NUM_LED; l++) {
gLedStatus[l] = activeSections[l];
digitalWrite(context, n, gLedPins[l], gLedStatus[l]);
}
if(count >= gTimePeriod*context->audioSampleRate)
{
for(unsigned int l = 0; l < NUM_LED; l++)
rt_printf("%d ",gLedStatus[l]);
rt_printf("\n");
count = 0;
}
count++;
}
}
void cleanup(BelaContext *context, void *userData)
{}
Main Bela public API.
A class to use the Trill family of capacitive sensors. http://bela.io/trill.
Definition Trill.h:14
void printDetails()
Definition Trill.cpp:492
@ BAR
Trill Bar
Definition Trill.h:34
AuxiliaryTask Bela_runAuxiliaryTask(void(*callback)(void *), int priority=0, void *arg=nullptr)
Create and start an AuxiliaryTask.
int Bela_stopRequested()
Check whether the program should stop.
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 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