- Edited
hi - I'm running some C++ code I wrote for the Bela to send 8 of its ADC input readings over serial/usb to my Mac (Pure Data). I don't need super high sample rate so the code averages each of the 16 values (per buffer) and then transmits that value. The numbers come into Pd as ASCII numbers so I use ',' and '#' to separate voltage values from the ADC# that they're coming from. The values are coming into PD fine but I wanted to ask about the Bela CPU usage. The IDE reports that while running this task the Bela is at 80%-85% CPU usage. This seems high for a simple process like this? Because my C++ coding ability is very low, I'm wondering if I've somehow written this code poorly for the Bela to function optimally? Thanks in advance
#include <Bela.h>
#include <libraries/Pipe/Pipe.h>
#include <libraries/Serial/Serial.h>
#include <cmath>
#include <iostream>
#include <string>
Serial gSerial;
Pipe gPipe;
float input;
float array[8];
int writevalue = 0;
float analogsum = 0;
float analogavg = 0;
AuxiliaryTask serialCommsTask;
void serialIo(void* arg) {
while(!Bela_stopRequested())
{
for(unsigned int ars = 0; ars < 8; ars++) {
gSerial.write((std::to_string(array[ars]).c_str())); //send the cv
gSerial.write(","); //44
gSerial.write(std::to_string(ars).c_str()); //send the adc #
gSerial.write("*"); //42
}
}
}
bool setup(BelaContext *context, void *userData) {
gSerial.setup ("/dev/ttyGS0", 115200);
AuxiliaryTask serialCommsTask = Bela_createAuxiliaryTask(serialIo, 0, "serial-thread", NULL);
Bela_scheduleAuxiliaryTask(serialCommsTask);
gPipe.setup("serialpipe", 1024);
return true;
}
void render(BelaContext *context, void *userData)
{
//READ FRAME BY FRAME OF THE BUFFER
for(unsigned int ar = 0; ar < 8; ar++) {
analogsum = 0;
for(unsigned int n = 0; n < context->audioFrames; n++) {
input = (analogRead(context, n, ar));
analogsum = input + analogsum;
}
analogavg = analogsum/16;
array[ar] = analogavg;
}
//Bela_scheduleAuxiliaryTask(serialCommsTask);
}
void cleanup(BelaContext *context, void *userData) {}