From f6ef24e3124e8e4088d18c26c2b72c9d6e9ae8ec Mon Sep 17 00:00:00 2001 From: Geoffrey Frogeye Date: Tue, 1 May 2018 13:34:05 +0200 Subject: [PATCH] Debug filename changed --- chef/Makefile | 2 +- chef/src/debug.c | 54 +++++++++++++++++++++++++++++++---------- chef/src/debug.h | 3 +++ chef/src/ihm.c | 62 ++++++++++++++++++++++++------------------------ 4 files changed, 77 insertions(+), 44 deletions(-) diff --git a/chef/Makefile b/chef/Makefile index 3a7b35b..4c3e038 100644 --- a/chef/Makefile +++ b/chef/Makefile @@ -40,7 +40,7 @@ bin/premier: obj/premier.o $(OBJS_O) bin/test%: obj/test%.o $(OBJS_O) # Programme de test sur PC, n'embarquant pas wiringPi -bin/local: obj/local.o obj/debug.o +bin/local: obj/local.o $(CC) $(CFLAGS) $(CFLAGS_CUSTOM) -lpthread $^ -o $@ # Génération des fichiers objets diff --git a/chef/src/debug.c b/chef/src/debug.c index d30a9a0..aa5e672 100644 --- a/chef/src/debug.c +++ b/chef/src/debug.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -5,6 +6,7 @@ #include #include #include // sleep +#include #include "debug.h" @@ -15,22 +17,51 @@ struct debugArg* listeDebugArgs = NULL; FILE* debugFd; +int nextId() +{ + DIR* d; + struct dirent* dir; + d = opendir("log"); + + int maxId = 0, id, ret; + + if (d) { + while ((dir = readdir(d)) != NULL) { + ret = sscanf(dir->d_name, "%d", &id); + if (ret == 1 && id > maxId) { + maxId = id; + } + } + closedir(d); + } + return maxId + 1; +} + +struct timespec debugStart; +struct timespec debugNow; +struct timespec debugEcoule; + void* TaskDebug(void* pdata) { (void)pdata; - clock_t debugStart; - debugStart = clock(); // TODO struct timespec - - struct timespec tim; // 100 ms - tim.tv_sec = 0; - tim.tv_nsec = 100000000L; + if (DEBUG_INTERVAL <= 0) { + return NULL; + } + clock_gettime(CLOCK_REALTIME, &debugStart); fprintf(debugFd, "\n"); for (;;) { - clock_t t = clock() - debugStart; - fprintf(debugFd, "%ld", t); + clock_gettime(CLOCK_REALTIME, &debugNow); + if ((debugNow.tv_nsec - debugStart.tv_nsec) < 0) { + debugEcoule.tv_sec = debugNow.tv_sec - debugStart.tv_sec - 1; + debugEcoule.tv_nsec = debugNow.tv_nsec - debugStart.tv_nsec + 1000000000UL; + } else { + debugEcoule.tv_sec = debugNow.tv_sec - debugStart.tv_sec; + debugEcoule.tv_nsec = debugNow.tv_nsec - debugStart.tv_nsec; + } + fprintf(debugFd, "%ld.%09ld", debugEcoule.tv_sec, debugEcoule.tv_nsec); struct debugArg* arg = listeDebugArgs; while (arg != NULL) { @@ -58,8 +89,9 @@ void* TaskDebug(void* pdata) arg = arg->next; } fprintf(debugFd, "\n"); + fflush(debugFd); - nanosleep(&tim, NULL); + delay(DEBUG_INTERVAL); } return NULL; @@ -70,9 +102,7 @@ void configureDebug() // Génération du nom de fichier char path[256]; - time_t startTime; - time(&startTime); - sprintf(path, "log/%ld.csv", startTime); + sprintf(path, "log/%d.csv", nextId()); // Open file debugFd = fopen(path, "w"); diff --git a/chef/src/debug.h b/chef/src/debug.h index a133795..12c2e14 100644 --- a/chef/src/debug.h +++ b/chef/src/debug.h @@ -4,6 +4,9 @@ #include #include +// Constantes +#define DEBUG_INTERVAL 100 + // Structures enum debugArgTypes {d, f, ld, lf, s}; diff --git a/chef/src/ihm.c b/chef/src/ihm.c index 96b6fef..0179c1a 100644 --- a/chef/src/ihm.c +++ b/chef/src/ihm.c @@ -25,22 +25,9 @@ void startIHM() pthread_create(&tIHM, NULL, TaskIHM, NULL); } -// t1 - t2 -void diffTimespec(const struct timespec* t1, const struct timespec* t2, struct timespec* td) -{ - if ((t1->tv_nsec - t2->tv_nsec) < 0) { - td->tv_sec = t1->tv_sec - t2->tv_sec - 1; - td->tv_nsec = t1->tv_nsec - t2->tv_nsec + 1000000000UL; - } else { - td->tv_sec = t1->tv_sec - t2->tv_sec; - td->tv_nsec = t1->tv_nsec - t2->tv_nsec; - } -} - bool isDebug = false; bool isOrange = true; bool annuler = false; -clock_t lastCalibrage = 0; pthread_t tParcours; char* orangeStr = "Orange"; @@ -51,6 +38,10 @@ char* getCouleur() return isOrange ? orangeStr : vertStr; } +struct timespec calibrageLast = {0, 0}; +struct timespec calibrageNow; +struct timespec calibrageEcoule; + void* TaskIHM(void* pdata) { (void)pdata; @@ -58,21 +49,21 @@ void* TaskIHM(void* pdata) enum boutons bout; for (;;) { - // Debug - for (;;) { - clearLCD(); - printfToLCD(LCD_LINE_1, "Debug : %s", isDebug ? "On" : "Off"); - if (isDebug) { - printToLCD(LCD_LINE_2, "192.168.0.0 TODO"); - } - bout = pressedButton(BUT_REFRESH_INTERVAL); - - if (bout == rouge) { - isDebug = !isDebug; - } else if (bout == jaune) { - break; - } - } + /* // Debug */ + /* for (;;) { */ + /* clearLCD(); */ + /* printfToLCD(LCD_LINE_1, "Debug : %s", isDebug ? "On" : "Off"); */ + /* if (isDebug) { */ + /* printToLCD(LCD_LINE_2, "192.168.0.0 TODO"); */ + /* } */ + /* bout = pressedButton(BUT_REFRESH_INTERVAL); */ + /* */ + /* if (bout == rouge) { */ + /* isDebug = !isDebug; */ + /* } else if (bout == jaune) { */ + /* break; */ + /* } */ + /* } */ // Couleur for (;;) { @@ -90,9 +81,18 @@ void* TaskIHM(void* pdata) // Calibrage for (;;) { clearLCD(); - if (lastCalibrage != 0) { + printf("84 %ld\n", calibrageLast.tv_sec); + if (calibrageLast.tv_sec > 0) { + clock_gettime(CLOCK_REALTIME, &calibrageNow); + if ((calibrageNow.tv_nsec - calibrageLast.tv_nsec) > 0) { + calibrageEcoule.tv_sec = calibrageNow.tv_sec - calibrageLast.tv_sec - 1; + calibrageEcoule.tv_nsec = calibrageNow.tv_nsec - calibrageLast.tv_nsec + 1000000000UL; + } else { + calibrageEcoule.tv_sec = calibrageNow.tv_sec - calibrageLast.tv_sec; + calibrageEcoule.tv_nsec = calibrageNow.tv_nsec - calibrageLast.tv_nsec; + } printToLCD(LCD_LINE_1, "Calibre il y a"); - printfToLCD(LCD_LINE_2, "%ld secondes", (clock() - lastCalibrage) / CLOCKS_PER_SEC); + printfToLCD(LCD_LINE_2, "%ld secondes", calibrageEcoule.tv_sec); } else { printToLCD(LCD_LINE_1, "Calibrer"); printfToLCD(LCD_LINE_2, "(%s)", getCouleur()); @@ -103,7 +103,7 @@ void* TaskIHM(void* pdata) clearLCD(); printToLCD(LCD_LINE_1, "Calibrage..."); delay(3000); // TODO - lastCalibrage = clock(); // TODO struct timespec + clock_gettime(CLOCK_REALTIME, &calibrageLast); } else if (bout == jaune) { break; }