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

Trill Visualiser

This example will work with all types of Trill sensor and will visualise the raw reading from each capacitive channel.

We are using the Trill library to read from the sensor and the Gui library for the visualisation. The first thing to do is make sure that the correct sensor type is given to touchSensor.setup(). If you have changed the address of the sensor then you will need to add the new address to this function too.

The Trill sensor is scanned on an auxiliary task running parallel to the audio thread and is read in DIFF mode giving the differential reading of each channel on the sensor.

Readings are sent to the integrated p5.js GUI every few milliseconds.

Once you run the project you will be able to visualise the value of each capacitive pad on the sensor by clicking the GUI button in the IDE. Each bar represents a pad on the sensor.

/*
____ _____ _ _
| __ )| ____| | / \
| _ \| _| | | / _ \
| |_) | |___| |___ / ___ \
|____/|_____|_____/_/ \_\
http://bela.io
*/
#include <Bela.h>
#include <libraries/Trill/Trill.h>
#include <libraries/Gui/Gui.h>
// Trill objet declaration
Trill touchSensor;
// Gui object declaration
Gui gui;
// 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;
void loop(void*)
{
while(!Bela_stopRequested()) {
touchSensor.readI2C();
usleep(gTaskSleepTime);
}
}
bool setup(BelaContext *context, void *userData)
{
// Setup a Trill Craft on i2c bus 1, using the default address.
if(touchSensor.setup(1, Trill::CRAFT) != 0) {
fprintf(stderr, "Unable to initialise Trill Craft\n");
return false;
}
// ensure the device is in DIFF mode for printing raw values
touchSensor.setMode(Trill::DIFF);
touchSensor.printDetails();
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 raw data to the GUI after some time has elapsed
if(count >= gTimePeriod*context->audioSampleRate)
{
gui.sendBuffer(0, touchSensor.getNumChannels());
gui.sendBuffer(1, touchSensor.rawData);
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
@ CRAFT
Trill Craft
Definition Trill.h:36
@ DIFF
Definition Trill.h:25
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