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

179 lines
4.8 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-10 15:05:04 +02:00
#include "queue.h"
2022-06-08 11:25:19 +02:00
2022-06-14 10:46:12 +02:00
/**
* @brief convert an interger N into a char*
*
* @param N
* @return char*
*/
2022-06-09 17:34:24 +02:00
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];
2022-06-14 18:01:52 +02:00
arr = (char*)malloc(digit*sizeof(char));
2022-06-09 17:34:24 +02:00
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;
}
2022-06-14 10:46:12 +02:00
/**
* @brief Create a New Raw Data File Name
*
* @return char*
*/
2022-06-10 10:47:17 +02:00
char *createNewRawDataFileName(){
2022-06-10 15:05:04 +02:00
char *fileName = "../RawDataFiles/RawData";
2022-06-13 15:03:51 +02:00
char *extension = ".csv\0";
2022-06-09 17:34:24 +02:00
char *fileNumber = convertIntegerToChar(cptFile);
2022-06-14 18:01:52 +02:00
//char *fileNumber;
//sprintf(fileNumber, "%d", cptFile);
//printf("%s\n" , fileNumber);
2022-06-09 17:34:24 +02:00
char fileNameNumber[strlen(fileName)+strlen(fileNumber)];
2022-06-13 15:03:51 +02:00
char *fullFileName = malloc((strlen(fileNameNumber)+strlen(extension)) * sizeof(char*));
2022-06-09 17:34:24 +02:00
strcpy( fileNameNumber, fileName );
strcat( fileNameNumber, fileNumber );
2022-06-10 11:13:03 +02:00
strcpy( fullFileName, fileNameNumber );
strcat( fullFileName, extension );
2022-06-13 15:03:51 +02:00
2022-06-10 11:13:03 +02:00
return fullFileName;
2022-06-09 17:34:24 +02:00
}
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;
}
2022-06-14 10:46:12 +02:00
/**
* @brief return the unix time in millisecond
*
* @return int64_t
*/
2022-06-10 10:47:17 +02:00
int64_t millis()
{
struct timespec now;
timespec_get(&now, TIME_UTC);
return ((int64_t) now.tv_sec) * 1000 + ((int64_t) now.tv_nsec) / 1000000;
}
2022-06-14 10:46:12 +02:00
2022-06-15 10:49:21 +02:00
int lastIndexCaptor(){
int lastIndex = 0;
for(int i = 1 ; i < 8 ; i++){
if(selectionCaptors[i]){
lastIndex = i;
}
}
return lastIndex;
}
2022-06-17 17:38:53 +02:00
2022-06-14 10:46:12 +02:00
/**
* @brief write one lign of rawData in the file @param rawDataFile (simulate of freq of the Vegetal Signals Captor)
*
* @param rawDataFile
* @return true if the lign is correctly write , else :
* @return false
*/
2022-06-10 11:13:03 +02:00
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-10 10:47:17 +02:00
if(fread(&buff, 26, 1, stdin)) {
2022-06-17 17:38:53 +02:00
//printf("%d\n",cptValue);
if(cptValue < nbRowBinFile - nbRowIgnore){
FILE *timeFile = fopen("timeFile.csv" , "a+");
fprintf(timeFile , "%ld\n", millis());
fclose(timeFile);
fprintf(rawDataFile , "%ld,", millis());
if (strncmp(buff, "#################\n", (size_t)18) == 0) {
if (!(fread(&buff2, 18, 1, stdin))) {
fprintf(stderr, "Erreur lecture après ###...#");
} else {
strncpy(buff, &buff[18], 8);
strncpy(&buff[8], buff2, 18);
2022-06-09 17:34:24 +02:00
}
2022-06-17 17:38:53 +02:00
}
int lastIndex = lastIndexCaptor();
for (int i = 1; i < 9; i++){
if(selectionCaptors[i-1]){
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));
FILE* allRawDataFile = fopen("AllRawData.csv" , "a+");
if(i-1==lastIndex){
fprintf(rawDataFile, "%d\n", values[i]/256);
fprintf(allRawDataFile, "%d\n", values[i]/256);
}
else{
fprintf(rawDataFile, "%d,", values[i]/256);
fprintf(allRawDataFile, "%d,", values[i]/256);
}
fclose(allRawDataFile);
2022-06-09 17:34:24 +02:00
}
2022-06-08 11:25:19 +02:00
}
2022-06-17 17:38:53 +02:00
cptData++;
//sleep(0.004); //simul freq here
cptValue++;
return true;
}
else {
return false;
2022-06-08 11:25:19 +02:00
}
}
}
2022-06-10 10:47:17 +02:00
2022-06-14 10:46:12 +02:00
void *threadSimulateFlux(void *vargp){
2022-06-13 15:03:51 +02:00
2022-06-10 11:13:03 +02:00
char *fileName = createNewRawDataFileName();
FILE *rawDataFile = fopen(fileName,"w+");
2022-06-10 15:05:04 +02:00
2022-06-10 11:13:03 +02:00
while(writeOneRawData(rawDataFile)){
2022-06-16 17:05:12 +02:00
if(cptData == nRowRawData){
2022-06-13 15:19:05 +02:00
fclose(rawDataFile);
2022-06-09 17:34:24 +02:00
cptData = 0;
cptFile++;
2022-06-10 11:13:03 +02:00
//create struct here
2022-06-13 15:03:51 +02:00
queueAddLastQ(firstRawDataQueue , fileName , strlen(fileName));
//prepare next file now
fileName = createNewRawDataFileName();
2022-06-10 11:13:03 +02:00
FILE *rawDataFile = fopen(fileName,"w+");
2022-06-08 12:29:34 +02:00
}
2022-06-08 11:25:19 +02:00
}
2022-06-13 17:11:16 +02:00
rawDataWriteFlag = false;
2022-06-10 11:13:03 +02:00
}