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

Connecting and Detecting Multiple Trill

This example scans the I2C bus for Trill sensors and will then generate a GUI element for each one.

NOTE: as this example scans several addresses on the i2c bus it could cause non-Trill peripherals connected to it to malfunction.

/*
____ _____ _ _
| __ )| ____| | / \
| _ \| _| | | / _ \
| |_) | |___| |___ / ___ \
|____/|_____|_____/_/ \_\
http://bela.io
*/
#include <Bela.h>
#include <libraries/Trill/Trill.h>
#include <libraries/Gui/Gui.h>
Gui gGui;
std::vector<Trill*> gTouchSensors;
unsigned int gSampleCount = 0;
float gSendInterval = 0.1;
unsigned int gSendIntervalSamples;
bool controlCallback(JSONObject& json, void*)
{
// when any data from the Gui is received (which means it's ready)
// we send back the id and type of each sensor.
JSONObject root;
root[L"event"] = new JSONValue(L"connectedDevices");
JSONArray devices;
for(auto t : gTouchSensors) {
devices.push_back(new JSONValue(JSON::s2ws(Trill::getNameFromDevice(t->deviceType()))));
}
root[L"devices"] = new JSONValue(devices);
JSONValue value(root);
gGui.sendControl(&value);
return true;
}
void readLoop(void*)
{
{
for(unsigned int n = 0; n < gTouchSensors.size(); ++n)
{
Trill* t = gTouchSensors[n];
t->readI2C();
}
usleep(50000);
}
}
bool setup(BelaContext *context, void *userData)
{
unsigned int i2cBus = 1;
for(uint8_t addr = 0x20; addr <= 0x50; ++addr)
{
Trill::Device device = Trill::probe(i2cBus, addr);
if(Trill::NONE != device && Trill::CRAFT != device)
{
gTouchSensors.push_back(new Trill(i2cBus, device, addr));
gTouchSensors.back()->printDetails();
}
}
gGui.setup(context->projectName);
gGui.setControlDataCallback(controlCallback);
gSendIntervalSamples = context->audioSampleRate * gSendInterval;
return true;
}
void render(BelaContext *context, void *userData)
{
for(unsigned int n = 0; n < context->audioFrames; ++n)
{
gSampleCount++;
if(gSampleCount == gSendIntervalSamples)
{
gSampleCount = 0;
float arr[3];
for(unsigned int t = 0; t < gTouchSensors.size(); ++t) {
arr[0] = gTouchSensors[t]->compoundTouchSize();
arr[1] = gTouchSensors[t]->compoundTouchLocation();
arr[2] = gTouchSensors[t]->compoundTouchHorizontalLocation();
gGui.sendBuffer(t, arr);
rt_printf("[%d] %2.3f %.3f %.3f ", t, arr[0], arr[1], arr[2]);
}
rt_printf("\n");
}
}
}
void cleanup(BelaContext *context, void *userData)
{
for(auto t : gTouchSensors)
delete t;
}
Main Bela public API.
Definition Gui.h:15
Definition JSONValue.h:38
A class to use the Trill family of capacitive sensors. http://bela.io/trill.
Definition Trill.h:14
Device
Definition Trill.h:31
@ CRAFT
Trill Craft
Definition Trill.h:36
@ NONE
No device.
Definition Trill.h:32
int readI2C(bool shouldReadStatusByte=false)
Read data from the device.
Definition Trill.cpp:691
static const std::string & getNameFromDevice(Device device)
Definition Trill.cpp:267
static Device probe(unsigned int i2c_bus, uint8_t i2c_address)
Definition Trill.cpp:236
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