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

Trill Bar GUI

This is an example of how to communicate with the Trill Bar sensor using the Trill library. We also visualise position of each touch in real time via the integrated Bela p5.js GUI.

The Trill sensor is scanned in an AuxiliaryTask running in parallel with the audio thread and the number of active touches, their position and size are stored in global variables.

Click the GUI button to see a visualisation of the sensor. Up to 5 touches position with touch size are displayed in the sketch.

/*
____ _____ _ _
| __ )| ____| | / \
| _ \| _| | | / _ \
| |_) | |___| |___ / ___ \
|____/|_____|_____/_/ \_\
http://bela.io
*/
#include <Bela.h>
#include <cmath>
#include <libraries/Trill/Trill.h>
#include <libraries/Gui/Gui.h>
#define NUM_TOUCH 5 // Number of touches on Trill sensor
// Gui object declaration
Gui gui;
// 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 (in seconds) after which data will be sent to the GUI
float gTimePeriod = 0.015;
/*
* 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();
// noise may cause some spurious touches with very small size to be
// detected. Silence them here
touchSensor.setMinimumTouchSize(0.1);
usleep(10000);
// Set and schedule auxiliary task for reading sensor data from the I2C bus
// Setup GUI
gui.setup(context->projectName);
return true;
}
void render(BelaContext *context, void *userData)
{
static unsigned int count = 0;
for(unsigned int n = 0; n < context->audioFrames; n++) {
// Send number of touches, touch location and size to the GUI
// after some time has elapsed.
if(count >= gTimePeriod*context->audioSampleRate)
{
gui.sendBuffer(0, gNumActiveTouches);
gui.sendBuffer(1, gTouchLocation);
gui.sendBuffer(2, gTouchSize);
count = 0;
}
count++;
}
}
void cleanup(BelaContext *context, void *userData)
{}
Main Bela public API.
Definition Gui.h:15
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.
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
char projectName[MAX_PROJECTNAME_LENGTH]
Name of running project.
Definition Bela.h:417
const float audioSampleRate
The audio sample rate in Hz (currently always 44100.0).
Definition Bela.h:328