This sketch allows you to hook up an MPR121 capactive touch sensing device to Bela, for example the SparkFun Capacitive Touch Sensor Breakout - MPR121. The breakout board gives you 12 electrode connections.
To get this working with Bela you need to connect the breakout board to the I2C terminal on the Bela board. See the Pin guide for details of which pin is which.
 
#include <cmath>
#include "I2C_MPR121.h"
 
#define NUM_TOUCH_PINS 12
 
#undef DEBUG_MPR121
 
int readInterval = 50;
 
int threshold = 40;
 
int sensorValue[NUM_TOUCH_PINS];
 
 
float gFrequencies[NUM_TOUCH_PINS] = {261.63, 293.66, 329.63, 349.23, 392.00, 440.00, 493.88, 523.25, 587.33, 659.25, 698.25, 783.99};
 
float gNormFrequencies[NUM_TOUCH_PINS];
float gPhases[NUM_TOUCH_PINS] = {0};
 
 
 
int readCount = 0;                      
int readIntervalSamples = 0; 
 
void readMPR121(void*);
 
{
        if(!mpr121.begin(1, 0x5A)) {
                rt_printf("Error initialising MPR121\n");
                return false;
        }
 
 
        for(int i = 0; i < NUM_TOUCH_PINS; i++) {
                gNormFrequencies[i] = 2.0 * M_PI * gFrequencies[i] / context->
audioSampleRate;
        }
 
        return true;
}
 
{
        for(
unsigned int n = 0; n < context->
audioFrames; n++) {
 
                
                if(++readCount >= readIntervalSamples) {
                        readCount = 0;
                }
 
                float sample = 0.0;
 
                
                for(int i = 0; i < NUM_TOUCH_PINS; i++) {
                        float amplitude = sensorValue[i] / 400.f;
 
                        
                        if(amplitude > 0.5)
                                amplitude = 0.5;
 
                        sample += amplitude * sinf(gPhases[i]);
                        gPhases[i] += gNormFrequencies[i];
                        if(gPhases[i] > M_PI)
                                gPhases[i] -= 2.0f * (float)M_PI;
                }
 
        }
}
 
{ }
 
void readMPR121(void*)
{
        for(int i = 0; i < NUM_TOUCH_PINS; i++) {
                sensorValue[i] = -(mpr121.filteredData(i) - mpr121.baselineData(i));
                sensorValue[i] -= threshold;
                if(sensorValue[i] < 0)
                        sensorValue[i] = 0;
#ifdef DEBUG_MPR121
                rt_printf("%d ", sensorValue[i]);
#endif
        }
#ifdef DEBUG_MPR121
        rt_printf("\n");
#endif
 
        
        
}
Definition I2C_MPR121.h:59
void * AuxiliaryTask
Definition Bela.h:561
int Bela_scheduleAuxiliaryTask(AuxiliaryTask task)
Run an auxiliary task which has previously been created.
AuxiliaryTask Bela_createAuxiliaryTask(void(*callback)(void *), int priority, const char *name, void *arg=NULL)
Create a new auxiliary task.
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 uint32_t audioInChannels
The number of audio input channels.
Definition Bela.h:324
const float audioSampleRate
The audio sample rate in Hz (currently always 44100.0).
Definition Bela.h:328
float *const audioOut
Buffer holding audio output samples.
Definition Bela.h:264