Hey guys,
Right now I have a program that reads a CSV one line at a time, parses the data, and then outputs the data to a multichannel DAC. Its working but its not great. I would like to speed it up. I have heard of something called a ring buffer? Could someone tell me how to implement that? Will using a buffer cause the progam to stall while it awaits to fill the buffer once it has emptied? This is undesirable in my program, I need to have a steady output with no blips or stalls. Any advice would be excellent!
Also if anyone thinks the code below is really bad please let me know how to improve it! I took most of it from one of the sdfat library examples, but I would bet the code that I have added is certainly not optimised! Thanks!
void playBack() {
char line[18];//buffer to store line grabbed from sd card
//data will look like this "FFFF,FFBB,CC2E"
char line1[5];//buffers to store the parsed data
char line2[5];
char line3[5];
int one=0;
int two=0;
int three=0;
uint16_t red=0;
uint16_t yellow=0;
uint16_t blue=0;
int n;
// open test file
SdFile rdfile("HEX.csv", O_READ);
// check for open error
if (!rdfile.isOpen()) {
Serial.println("error opening file");
}
// read lines from the file
while ((n = rdfile.fgets(line, sizeof(line))) > 0) {
if (line[n - 1] == '\n') {
strcpy(line1, strtok(line , ", "));//parse the line buffer using the delimiters
strcpy(line2, strtok(NULL, ", "));
strcpy(line3 , strtok(NULL, ", "));
one=strtol(line1, NULL, 16);//converts the ascii represented HEX values into long int.
two=strtol(line2, NULL, 16);
three=strtol(line3, NULL, 16);
lineOne=(uint16_t)one;//converts long into uint16 so it can go straight to dac write function
lineTwo=(uint16_t)one;
lineThree=(uint16_t)one;
dataWrite(writeToInputRegisterN, channel1, lineOne, suffix);//outputs data to DAC
dataWrite(writeToInputRegisterN, channel2, lineTwo, suffix);
dataWrite(writeToInputRegisterN, channel3, lineThree, suffix);
digitalWriteDirect(ldacPin,0);
digitalWriteDirect(ldacPin,1);
//Serial.print(lineOne);Serial.print(" "); Serial.print(lineTwo);Serial.print(" "); Serial.print(lineThree);Serial.println(" ");
//delay(500);
}
}
}