I think this solution is good. The idea is to convert the CSV file in a binary file (a sequence of floats). After the conversion you may supply the binary file to the MCU and use the function getVal() (below indicated) to read the data.
This code converts a file data.csv into a file data.bin:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <malloc.h>
int main(void)
{
FILE * fin=NULL;
FILE * fout=NULL;
char * buff, *tmp;
float val;
int cnt=0;
size_t bsize;
fin=fopen("data.csv","r");
if (fin==NULL) {
perror("1 - Error");
return errno;
}
fseek(fin,0,SEEK_END);
bsize=ftell(fin);
if (!bsize) {
puts("The file doesn't contain data!");
return -1;
}
buff=malloc(bsize+1);
if (buff==NULL) {
perror("2 - Error");
return errno;
}
fout=fopen("data.bin","w");
if (fout==NULL) {
perror("3 - Error");
return errno;
}
fseek(fin,0,SEEK_SET);
fread(buff,1,bsize,fin);
buff[bsize]=0;
fclose(fin);
tmp=buff;
do {
val=strtof(tmp,&tmp);
printf("%12g ",val);
if ((++cnt)%5==0)
puts("");
fwrite(&val,sizeof(val),1,fout);
if (*tmp!=0)
tmp++;
} while(*tmp!=0);
puts("\nEnd of conversion\n");
fclose(fout);
free(buff);
return 0;
}
This function read a field from a binary file that contains float data in binary format:
float getVal(FILE *f, size_t index)
{
float val;
fseek(f,index*sizeof(val),SEEK_SET);
fread(&val,1,sizeof(val),f);
return val;
}
This main uses the function getVal() to take data from the file data.bin:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
float getVal(FILE *f, size_t index);
int main(void)
{
FILE * fin;
size_t index;
fin=fopen("data.bin","r");
if (fin==NULL) {
perror("1 - Error");
return errno;
}
while(1) {
printf("Insert index from 1 to n [Insert 0 to end]: ");
scanf("%lu",&index);
if (index==0)
break;
printf("%g\n",getVal(fin,index-1));
}
fclose(fin);
return 0 ;
}