Traitement-signal-plantes/Code-C/simulateFlux.c

145 lines
3.5 KiB
C
Raw Normal View History

2022-06-08 11:25:19 +02:00
#include "simulateFlux.h"
2022-06-09 17:34:24 +02:00
#include "initialParameters.h"
2022-06-08 11:25:19 +02:00
2022-06-09 17:34:24 +02:00
typedef struct {
char *fileName;
} nameFill;
2022-06-08 11:25:19 +02:00
2022-06-09 17:34:24 +02:00
typedef struct {
nameFill curentFile;
nameFill *nextFile;
}rawDataFile;
nameFill getNameFill(int N){
nameFill file;
file.fileName = (char *) malloc(N*sizeof(char *));
return file;
}
int cptData = 0;
int cptFile = 1;
nameFill FirstFile;
char* convertIntegerToChar(int N)
{
// Count digits in number N
int m = N;
int digit = 0;
while (m) {
digit++;
m /= 10;
}
char* arr;
char arr1[digit];
arr = (char*)malloc(digit);
int index = 0;
while (N) {
arr1[++index] = N % 10 + '0';
N /= 10;
}
int i;
for (i = 0; i < index; i++) {
arr[i] = arr1[index - i];
}
arr[i] = '\0';
return (char*)arr;
}
FILE *createThenOpenNewRawDataFile(){
char *fileName = "RawData";
char *extension = ".csv";
char *fileNumber = convertIntegerToChar(cptFile);
char fileNameNumber[strlen(fileName)+strlen(fileNumber)];
char fullFillName[strlen(fileNameNumber)+strlen(extension)];
strcpy( fileNameNumber, fileName );
strcat( fileNameNumber, fileNumber );
strcpy( fullFillName, fileNameNumber );
strcat( fullFillName, extension );
FILE *file = fopen(fullFillName,"w+");
return file;
}
int64_t millis()
{
struct timespec now;
timespec_get(&now, TIME_UTC);
return ((int64_t) now.tv_sec) * 1000 + ((int64_t) now.tv_nsec) / 1000000;
}
int intInArray(int number , int *array , int N){
for(int i = 0 ; i < N ; i++){
if(array[i] == number) return 0;
}
return -1;
}
int maxInArray(int *array , int N){
int max = 0;
for(int i = 0 ; i < N ; i++){
if(array[i]>max) max = array[i];
}
return max;
}
bool writeOneRawData(FILE *rawDataFile){
2022-06-08 11:25:19 +02:00
char buff[26];
char buff2[18];
int32_t values[8];
uint32_t valbin[8];
quartet value;
2022-06-09 17:34:24 +02:00
if(fread(&buff, 26, 1, stdin)) {
fprintf(rawDataFile , "%d,", millis());
2022-06-08 11:25:19 +02:00
if (strncmp(buff, "#################\n", (size_t)18) == 0) {
if (!(fread(&buff2, 18, 1, stdin))) {
fprintf(stderr, "Erreur lecture après ###...#");
return 2;
} else {
strncpy(buff, &buff[18], 8);
strncpy(&buff[8], buff2, 18);
}
}
2022-06-09 17:34:24 +02:00
int maxCapteurNb = maxInArray(selectionCaptors,sizeSelectionArray);
2022-06-08 11:25:19 +02:00
for (int i = 1; i < 9; i++){
2022-06-09 17:34:24 +02:00
if(intInArray(i,selectionCaptors,sizeSelectionArray)==0){
value.octet1 = buff[3*i+1];
value.octet2 = buff[3*i+2];
value.octet3 = buff[3*i+3];
value.octet4 = 0;
valbin[i] = buff[3*i+1]*256*256*256 + buff[3*i+2]*256*256 + buff[3*i+3]*256;
memcpy(&values[i], &valbin[i], sizeof(uint32_t));
if(i==maxCapteurNb){
fprintf(rawDataFile, "%d\n", values[i]/256);
}
else{
fprintf(rawDataFile, "%d,", values[i]/256);
}
2022-06-08 11:25:19 +02:00
}
}
2022-06-09 17:34:24 +02:00
cptData++;
//sleep(0.004); //simul la freq ech
return true;
2022-06-08 11:25:19 +02:00
}
else {
2022-06-09 17:34:24 +02:00
return false;
2022-06-08 11:25:19 +02:00
}
}
2022-06-09 17:34:24 +02:00
void *simulateFlux(void *vargp){
2022-06-08 11:25:19 +02:00
2022-06-09 17:34:24 +02:00
FILE *f = createThenOpenNewRawDataFile();
while(writeOneRawData(f)){
if(cptData == nRow){
cptData = 0;
cptFile++;
fclose(f);
// initialiser et/ou modifier la structure ici
f = createThenOpenNewRawDataFile();
2022-06-08 12:29:34 +02:00
}
2022-06-08 11:25:19 +02:00
}
}