Hi all, I have a question about using Bela to control parameters in TouchDesigner, which I hope to use Bela to interact with my visual work. Basically I'm planning on sending out the parameters of Trill or sensors using i2c through OSC, and the parameters could then received by TouchDesigner. Just asking is it doable?
Bela with Touchdesinger
sure, in fact you can run this example https://github.com/BelaPlatform/Trill-Linux/tree/master/examples/trill-osc it will send to the specified host and port the touch readings of all detected devices in this format:
1D devices in centroid mode:
/trill/readings/<id>/touches <num-touches> <loc0> <pos0> <loc1> <pos1> ...
2D devices in centroid mode (compoundTouch):
/trill/readings/<id>/touchXY <num-touch> <loc0> <pos0>
Devices in raw/baseline/diff mode:
/trill/readings/<id>/raw <numChannels> <c0> <c1> ...
/trill/readings/<id>/baseline <numChannels> <c0> <c1> ...
/trill/readings/<id>/diff <numChannels> <c0> <c1> ...
To specify the I2C bus and remote host/port, start it with command line options --auto BUS HOST:PORT
, where BUS should be 1 if you connect Trill to the I2C-1 pins (those highlighted on the pin diagram for your board) and HOST
should be the IP of the device you want to send to. For instance, to send to the host computer that is connected to Bela via USB, use 192.168.7.1 (or 192.168.6.1 if on Windows) and a valid port number, e.g. : 5678, which should be the one used by touch designer (note that this is a UDP port). So something like this: --auto 1 192.168.7.2:5678
.
- Edited
giuliomoro Hi giulio, thank you very much for the quick reply. I ran the example you provided, basically the trill-osc.ccp, oscpkt.hh, and the udp.hh file, but it's giving me an error. Should I include them in my project? Thank s.
ok I fixed that in the source. Get the updated version of the file from the repo, then you should be able to apply the same change you already did of replacing Trill.h
with libraries/Trill/Trill.h
and it should work. You can set the command line options here:
giuliomoro Hi giulio, thank you for the update on the source. I don't have my Bela on my hand right now but can't wait to try it out later today!
giuliomoro Hi giulio, I tested the updated github example and it seems work fine. Does the --auto 1 192.168.7.2:5678 command mean that it is sending message to port 5678? Also, a silly question: how should I include the three files in my project to send the osc message of each trill? Thank you!
Keyi Does the --auto 1 192.168.7.2:5678 command mean that it is sending message to port 5678?
it means it is sending to port 5678 of host 192.168.7.2 . As you were asking how to send this to TouchDesigner, that would run on the host computer which is on 192.168.7.1 .
Keyi how should I include the three files in my project to send the osc message of each trill? Thank you!
what project of yours ? All you said was about sending data from Trill via OSC to a host computer.
giuliomoro Hi giulio, sorry that I didn't state it clear. I mean how should I include the function in the render function(or set up function)?
- Edited
just get the relevant code from the osc-test.cpp
file and use it in your project, you'll need to include the .hh files as well. It's mostly about calling createAllDevicesOnBus()
( or manually populating gTouchSensors
). Then put the for loop that is currently inside while(!shouldStop)
into a while(!Bela_stopRequested())
loop inside an AuxiliaryTask.
However, that code may be too complicated for that, as it was designed to be generic. You may be better off with something simpler like this:
#include <Bela.h>
#include <cmath>
#include <libraries/Trill/Trill.h>
#include <libraries/OscSender/OscSender.h>
#include <oscpkt.hh>
#define NUM_TOUCH 5 // Number of touches on Trill sensor
OscSender oscSender;
Trill touchSensor;
float gTouchLocation[NUM_TOUCH] = { 0.0, 0.0, 0.0, 0.0, 0.0 };
float gTouchSize[NUM_TOUCH] = { 0.0, 0.0, 0.0, 0.0, 0.0 };
int gNumActiveTouches = 0;
unsigned int gTaskSleepTime = 7500; // microseconds
void loop(void*)
{
std::string addr = "/trill/readings/0/touches";
while(!Bela_stopRequested())
{
// Read locations from Trill sensor
touchSensor.readI2C();
oscpkt::Message msg(addr);
gNumActiveTouches = touchSensor.getNumTouches();
msg.pushFloat(gNumActiveTouches);
for(unsigned int i = 0; i < gNumActiveTouches; i++) {
gTouchLocation[i] = touchSensor.touchLocation(i);
gTouchSize[i] = touchSensor.touchSize(i);
msg.pushFloat(gTouchLocation[i]);
msg.pushFloat(gTouchSize[i]);
}
oscSender.sendNonRt(msg);
// 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)
{
if(touchSensor.setup(1, Trill::HEX, 0x40) != 0) {
fprintf(stderr, "Unable to initialise Trill Bar\n");
return false;
}
touchSensor.printDetails();
oscSender.setup(5678, "192.168.7.1");
// Set and schedule auxiliary task for reading sensor data from the I2C bus
Bela_runAuxiliaryTask(loop);
return true;
}
void render(BelaContext *context, void *userData)
{
// your stuff
}
void cleanup(BelaContext *context, void *userData)
{}
giuliomoro Hi giulio, thank you so much for the reply and the patient with me. I tried to use the code you provided, but I don't know where to put msg.pushFloat in my code since I am using multiply trills. When I use oscSender.sendNonRt(msg), the console says: no member named 'sendNonRt' in 'OscSender' column: 13, line: 122.
Keyi o member named 'sendNonRt' in 'OscSender' column: 13, line: 122.
this is because you need to update your Bela core code to the latest dev
branch. This is done downloading this archive https://github.com/BelaPlatform/Bela/archive/refs/heads/dev.zip and uploading it on the IDE via the "Update Bela" button in the settings tab as per this image:
Keyi o member named 'sendNonRt' in 'OscSender' column: 13, line: 122.
this is because you need to update your Bela core code to the latest dev
branch. This is done downloading this archive https://github.com/BelaPlatform/Bela/archive/refs/heads/dev.zip and uploading it on the IDE via the "Update Bela" button in the settings tab as per this image:
as you have multiple sensors, you can use something like this (i.e.: changing the address for each sensor, creating and sending the message from inside the for gTouchSensors loop):
void readLoop(void*)
{
while(!Bela_stopRequested())
{
for(unsigned int n = 0; n < gTouchSensors.size(); ++n)
{
Trill* t = gTouchSensors[n];
t->readI2C();
std::string addr = "/trill/readings/" + std::to_string(n) + "/touches";
// Read locations from Trill sensor
oscpkt::Message msg(addr);
msg.pushFloat(t->getNumTouches());
for(unsigned int i = 0; i < t->getNumTouches(); i++) {
msg.pushFloat(t->touchLocation(i));
msg.pushFloat(t->touchSize(i));
}
oscSender.sendNonRt(msg);
}
usleep(gTaskSleepTime);
}
}
- Edited
giuliomoro Hi giulio,
no, there's an earlier error that was printed. Can you scroll back up a bit and show what happened there?
giuliomoro Hi giulio, I tried the second time and it breaks the connection after updated, so I couldn't scroll up to find the earlier error. I connected it once again and it is saying this
Does it build the program that was using sendNonRt()? If you scroll further down on the right hand side tab, where the "update" button was, there is also a "Version details" button, what does it say?
giuliomoro After loosing teh connection, I lost the bela.io page. I refresh it and it came back to the program that was using sendNonRt(). This is the version detail says:
- Edited
Maybe everything went well (seems unlikely?)? Try powering off the board and try build the project with sendNonRt() again.
giuliomoro It's pretty weird that my other programs runs fine, but this one does not generates sound even with the osc part commented out. The cpu is only around 1 and the terminal is saying: