Skip to content

Instantly share code, notes, and snippets.

@giuliomoro
Created January 28, 2021 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giuliomoro/63c6268b4ebd5d67044ed0ec468e58b9 to your computer and use it in GitHub Desktop.
Save giuliomoro/63c6268b4ebd5d67044ed0ec468e58b9 to your computer and use it in GitHub Desktop.
void loop(void*)
{
unsigned int logIdx = 0;
std::vector<float> logs(10000);
unsigned int writeFileSize = logs.size() + 5000;
freq_mod_tone_bela.setBufferSize(writeFileSize);
while(!Bela_stopRequested())
{
// Read locations from Trill sensor
unsigned int q = touchSensor.size() < NUM_TOUCH ? touchSensor.size() : NUM_TOUCH;
for(unsigned int n = 0; n < q; ++n){
// read current sensor & number of active touches.
touchSensor[n]->readI2C();
gNumActiveTouches[n] = touchSensor[n]->getNumTouches();
// loop thru active touches, get size & location.
for(unsigned int i = 0; i < gNumActiveTouches[n]; i++) {
gTouchLocation[n][i] = touchSensor[n]->touchLocation(i);
gTouchSize[n][i] = touchSensor[n]->touchSize(i);
}
// For all inactive touches, set location and size to 0
for(unsigned int i = gNumActiveTouches[n]; i < NUM_TOUCH; i++) {
gTouchLocation[n][i] = 0.0;
gTouchSize[n][i] = 0.0;
}
// if there are active touches.
if(touchSensor[n]->getNumTouches())
{
past_read[n] = read_out[n]; // store past read.
read_out[n] = gTouchLocation[n][0]; // get touch location for first touch (of 5).
active_touch[n]=1.0; // variable to tell audio loop whether there is active touch.
// now convert position to value between 0 & 360.
read_out_degrees[n] = read_out[n] * 360.0;
// now compute sine of that value to get y-coordinate.
// if using bar sensor, don't use y-value.
if(n==1){
y_coordinate_now[n] = sinf(read_out_degrees[n]*M_PI/180.0) * 0.5; //NECESSARY WITH RING.
} else {
y_coordinate_now[n] = cosf(read_out_degrees[n]*M_PI/180.0) * 0.5;// if bar sensor, motion is along x-axis.
}
// determine frequency.
frequency[n] = map(y_coordinate_now[n], -0.5, 0.5, peakFrequency[n]-freqRange_aroundPeak, peakFrequency[n]+freqRange_aroundPeak);
// log sensor data IF new touch location.
if(read_out[n]!=past_read[n]){
float data_out_now[5] = { timestamp_ms, float(n), read_out[n], y_coordinate_now[n], frequency[n] };
//instead of logging every time ...
//freq_mod_tone_bela.log(data_out_now,5);
//we log a larger buffer at once, so the WriteFile thread does not run in parallel to us
for(unsigned int n = 0; n < 5; ++n)
{
logs[logIdx++] = data_out_now[n]; // fill an array in memory
//if((logIdx % 100) == 0)
//printf("logIdx: %u\n", logIdx);
if(logs.size() == logIdx)
{
// when the array is full, dump it to disk
// given how we have setBufferSize() above, WriteFile's buffer
// should have enough space for all of logs
printf("Dumping\n");
freq_mod_tone_bela.log(logs.data(), logIdx);
logIdx = 0;
// now we need to wait until the WriteFile thread has written everything to disk
// unfortunately it will always keep up to 4096 samples inside its buffer
while(freq_mod_tone_bela.getBufferStatus() < (writeFileSize - logs.size()) / writeFileSize)
{
printf("waiting %f\n", freq_mod_tone_bela.getBufferStatus());
usleep(10000);
}
printf("Running\n");
}
}
gMyVar[n] = gTouchLocation[n][0];
}
} else {
active_touch[n]=0.0;
}
}
//usleep(gTaskSleepTime);
}
// send WriteFile any leftover stuff
freq_mod_tone_bela.log(logs.data(), logIdx);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment