avec dfu

Fork of Utils by POTLESS

Committer:
POTLESS_2
Date:
Wed Feb 21 06:57:58 2018 +0000
Revision:
7:1bd8b236feb1
Parent:
6:8bae97034114
Child:
8:23fd400c4edd
quelques changements

Who changed what in which revision?

UserRevisionLine numberNew contents of line
POTLESS_2 0:794c15bd30b0 1 #include "mbed.h"
POTLESS_2 0:794c15bd30b0 2 #include "Utils.h"
POTLESS_2 0:794c15bd30b0 3
POTLESS_2 6:8bae97034114 4 SDBlockDevice bd(Flash_MOSI, Flash_MISO, Flash_SCK, Flash_CS);
POTLESS_2 6:8bae97034114 5 //SPIFBlockDevice bd(Flash_MOSI, Flash_MISO, Flash_SCK, Flash_CS);
POTLESS_2 6:8bae97034114 6
POTLESS_2 2:5af7a1fb1903 7 FATFileSystem Root("Root");
POTLESS_2 0:794c15bd30b0 8
POTLESS_2 3:01677db45b2a 9 int Exist_Val = 0;
POTLESS_2 3:01677db45b2a 10
POTLESS_2 0:794c15bd30b0 11 void return_error(int ret_val)
POTLESS_2 0:794c15bd30b0 12 {
POTLESS_2 4:872676dd87a4 13 Exist_Val = 0;
POTLESS_2 6:8bae97034114 14 if (ret_val) {
POTLESS_2 6:8bae97034114 15 DEBUG_U(" Problème Flash = %d\r\n", ret_val);
POTLESS_2 3:01677db45b2a 16 Exist_Val = 0;
POTLESS_2 6:8bae97034114 17 } else {
POTLESS_2 6:8bae97034114 18 DEBUG_U(" Flash -> OK.\r\n");
POTLESS_2 3:01677db45b2a 19 Exist_Val = 1;
POTLESS_2 6:8bae97034114 20 }
POTLESS_2 6:8bae97034114 21 wait_ms(100);
POTLESS_2 0:794c15bd30b0 22 }
POTLESS_2 0:794c15bd30b0 23
POTLESS_2 0:794c15bd30b0 24 void errno_error(void* ret_val)
POTLESS_2 0:794c15bd30b0 25 {
POTLESS_2 4:872676dd87a4 26 Exist_Val = 0;
POTLESS_2 6:8bae97034114 27 if (ret_val == NULL) {
POTLESS_2 6:8bae97034114 28 DEBUG_U(" Problème Flash = %d \r\n", errno);
POTLESS_2 3:01677db45b2a 29 Exist_Val = 0;
POTLESS_2 6:8bae97034114 30 } else {
POTLESS_2 6:8bae97034114 31 DEBUG_U(" Flash -> OK.\r\n");
POTLESS_2 3:01677db45b2a 32 Exist_Val = 1;
POTLESS_2 6:8bae97034114 33 }
POTLESS_2 6:8bae97034114 34 wait_ms(100);
POTLESS_2 0:794c15bd30b0 35 }
POTLESS_2 0:794c15bd30b0 36
POTLESS_2 0:794c15bd30b0 37 void UTILS::Store_A_Val(float Val_To_Store, char* File_Name)
POTLESS_2 0:794c15bd30b0 38 {
POTLESS_2 0:794c15bd30b0 39
POTLESS_2 0:794c15bd30b0 40 char filename[20];
POTLESS_2 2:5af7a1fb1903 41 sprintf(filename, "/Root/%s.txt", File_Name);
POTLESS_2 0:794c15bd30b0 42
POTLESS_2 0:794c15bd30b0 43 FILE* fd = fopen(filename, "w");
POTLESS_2 0:794c15bd30b0 44 errno_error(fd);
POTLESS_2 0:794c15bd30b0 45 fprintf(fd, "%f\r\n", Val_To_Store);
POTLESS_2 0:794c15bd30b0 46 fclose(fd);
POTLESS_2 0:794c15bd30b0 47
POTLESS_2 6:8bae97034114 48 DEBUG_U(" \r\n %s sauvegardée = %f\r\n", filename, Val_To_Store);
POTLESS_2 0:794c15bd30b0 49
POTLESS_2 0:794c15bd30b0 50 }
POTLESS_2 0:794c15bd30b0 51
POTLESS_2 0:794c15bd30b0 52 float UTILS::Read_A_Val(char* File_Name)
POTLESS_2 0:794c15bd30b0 53 {
POTLESS_2 0:794c15bd30b0 54 char buffer[10] = {0};
POTLESS_2 0:794c15bd30b0 55 char c = {0};
POTLESS_2 0:794c15bd30b0 56 char *token;
POTLESS_2 0:794c15bd30b0 57 int i = 0;
POTLESS_2 0:794c15bd30b0 58
POTLESS_2 0:794c15bd30b0 59 char filename[20];
POTLESS_2 2:5af7a1fb1903 60 sprintf(filename, "/Root/%s.txt", File_Name);
POTLESS_2 0:794c15bd30b0 61
POTLESS_2 6:8bae97034114 62 DEBUG_U(" \n Récupération de %s...\r\n", filename);
POTLESS_2 0:794c15bd30b0 63
POTLESS_2 0:794c15bd30b0 64 FILE* fd = fopen(filename, "r");
POTLESS_2 0:794c15bd30b0 65 errno_error(fd);
POTLESS_2 6:8bae97034114 66
POTLESS_2 0:794c15bd30b0 67 while ((c != '\n') && (i < 10)) {
POTLESS_2 0:794c15bd30b0 68 c = fgetc(fd);
POTLESS_2 0:794c15bd30b0 69 buffer[i] = c;
POTLESS_2 0:794c15bd30b0 70 i++;
POTLESS_2 0:794c15bd30b0 71 }
POTLESS_2 0:794c15bd30b0 72
POTLESS_2 0:794c15bd30b0 73 token = strtok(buffer, "\n");
POTLESS_2 0:794c15bd30b0 74
POTLESS_2 0:794c15bd30b0 75 float val = atof(token);
POTLESS_2 0:794c15bd30b0 76
POTLESS_2 6:8bae97034114 77 DEBUG_U(" Valeur Récupérée = %f\r\n", val);
POTLESS_2 0:794c15bd30b0 78
POTLESS_2 0:794c15bd30b0 79 fclose(fd);
POTLESS_2 0:794c15bd30b0 80
POTLESS_2 0:794c15bd30b0 81 return val;
POTLESS_2 0:794c15bd30b0 82 }
POTLESS_2 0:794c15bd30b0 83
POTLESS_2 0:794c15bd30b0 84
POTLESS_2 6:8bae97034114 85 void UTILS::Write_Flash_File(char* To_Store, char* File_Name)
POTLESS_2 0:794c15bd30b0 86 {
POTLESS_2 2:5af7a1fb1903 87 char filename[50];
POTLESS_2 2:5af7a1fb1903 88 sprintf(filename, "/Root/%s", (string)File_Name);
POTLESS_2 6:8bae97034114 89
POTLESS_2 0:794c15bd30b0 90 FILE* fd = fopen(filename, "a");
POTLESS_2 6:8bae97034114 91
POTLESS_2 0:794c15bd30b0 92 errno_error(fd);
POTLESS_2 0:794c15bd30b0 93 fprintf(fd, "%s\r\n", To_Store);
POTLESS_2 0:794c15bd30b0 94 fclose(fd);
POTLESS_2 0:794c15bd30b0 95
POTLESS_2 6:8bae97034114 96 DEBUG_U(" Enregistrement de %s OK\r\n\r\n", filename);
POTLESS_2 0:794c15bd30b0 97 }
POTLESS_2 0:794c15bd30b0 98
POTLESS_2 6:8bae97034114 99 /*
POTLESS_2 6:8bae97034114 100 void UTILS::Read_Flash_File(char* File_Name)
POTLESS_2 0:794c15bd30b0 101 {
POTLESS_2 0:794c15bd30b0 102 char filename[20];
POTLESS_2 2:5af7a1fb1903 103 sprintf(filename, "/Root/%s", File_Name);
POTLESS_2 6:8bae97034114 104
POTLESS_2 0:794c15bd30b0 105 FILE* fd = fopen(filename, "r");
POTLESS_2 0:794c15bd30b0 106 errno_error(fd);
POTLESS_2 0:794c15bd30b0 107
POTLESS_2 6:8bae97034114 108 //printf(" Contenu du fichier :\r\n\r\n");
POTLESS_2 6:8bae97034114 109 char buff[100] = {0};
POTLESS_2 6:8bae97034114 110
POTLESS_2 6:8bae97034114 111 while (!feof(fd)) {
POTLESS_2 0:794c15bd30b0 112 int size = fread(&buff[0], 1, 15, fd);
POTLESS_2 6:8bae97034114 113 fwrite(&buff[0], 1, size, stdout);
POTLESS_2 0:794c15bd30b0 114 }
POTLESS_2 6:8bae97034114 115 //printf("\r\n Fin du fichier.\r\n");
POTLESS_2 0:794c15bd30b0 116 fclose(fd);
POTLESS_2 6:8bae97034114 117
POTLESS_2 6:8bae97034114 118 }
POTLESS_2 6:8bae97034114 119 */
POTLESS_2 6:8bae97034114 120
POTLESS_2 6:8bae97034114 121 void UTILS::Read_Flash_File(char* File_Name)
POTLESS_2 6:8bae97034114 122 {
POTLESS_2 6:8bae97034114 123 char filename[20];
POTLESS_2 6:8bae97034114 124 sprintf(filename, "/Root/%s", File_Name);
POTLESS_2 6:8bae97034114 125
POTLESS_2 6:8bae97034114 126 FILE* fd = fopen(filename, "r");
POTLESS_2 6:8bae97034114 127 errno_error(fd);
POTLESS_2 6:8bae97034114 128 char s1[100];
POTLESS_2 6:8bae97034114 129 printf(" Contenu du fichier :\r\n\r\n");
POTLESS_2 6:8bae97034114 130 while (!feof(fd)) {
POTLESS_2 6:8bae97034114 131 memset(s1, 0, sizeof(s1));
POTLESS_2 6:8bae97034114 132 fgets(s1, sizeof(s1), fd);
POTLESS_2 6:8bae97034114 133 printf("%s", s1);
POTLESS_2 6:8bae97034114 134 fflush(stdout);
POTLESS_2 6:8bae97034114 135 }
POTLESS_2 6:8bae97034114 136 fclose(fd);
POTLESS_2 6:8bae97034114 137 printf("\r\n Fin du fichier.\r\n");
POTLESS_2 0:794c15bd30b0 138 }
POTLESS_2 0:794c15bd30b0 139
POTLESS_2 5:0f3b6e6677ef 140 bool UTILS::File_Exist(string File_Name)
POTLESS_2 3:01677db45b2a 141 {
POTLESS_2 3:01677db45b2a 142 char filename[20];
POTLESS_2 3:01677db45b2a 143 sprintf(filename, "/Root/%s.txt", File_Name);
POTLESS_2 6:8bae97034114 144
POTLESS_2 3:01677db45b2a 145 FILE* fd = fopen(filename, "r");
POTLESS_2 3:01677db45b2a 146 errno_error(fd);
POTLESS_2 6:8bae97034114 147
POTLESS_2 6:8bae97034114 148 if (Exist_Val == 0) {
POTLESS_2 6:8bae97034114 149 DEBUG_U(" Le fichier %s n'existe pas....\r\n", filename);
POTLESS_2 4:872676dd87a4 150 fclose(fd);
POTLESS_2 3:01677db45b2a 151 return false;
POTLESS_2 5:0f3b6e6677ef 152 }
POTLESS_2 6:8bae97034114 153
POTLESS_2 6:8bae97034114 154 if (Exist_Val == 1) {
POTLESS_2 3:01677db45b2a 155 fclose(fd);
POTLESS_2 3:01677db45b2a 156 return true;
POTLESS_2 6:8bae97034114 157 }
POTLESS_2 3:01677db45b2a 158 }
POTLESS_2 3:01677db45b2a 159
POTLESS_2 6:8bae97034114 160 void UTILS::Delete_Flash_File(char* File_Name)
POTLESS_2 0:794c15bd30b0 161 {
POTLESS_2 0:794c15bd30b0 162 char filename[20];
POTLESS_2 6:8bae97034114 163 sprintf(filename, "%s", File_Name);
POTLESS_2 0:794c15bd30b0 164 int error = 0;
POTLESS_2 2:5af7a1fb1903 165 error = Root.remove(filename);
POTLESS_2 0:794c15bd30b0 166 return_error(error);
POTLESS_2 0:794c15bd30b0 167
POTLESS_2 6:8bae97034114 168 DEBUG_U("Fichier %s effacé.\r\n", filename);
POTLESS_2 0:794c15bd30b0 169 }
POTLESS_2 0:794c15bd30b0 170
POTLESS_2 6:8bae97034114 171 void UTILS::Clean_Flash()
POTLESS_2 6:8bae97034114 172 {
POTLESS_2 6:8bae97034114 173 DEBUG_U("Nettoyage de la Flash.\r\n");
POTLESS_2 6:8bae97034114 174 float Saved_O2 = UTILS::Read_A_Val("Calibration_O2");
POTLESS_2 6:8bae97034114 175 int Saved_Motor = (int)UTILS::Read_A_Val("Servo_Poumon");
POTLESS_2 6:8bae97034114 176 UTILS::UnMount_Flash();
POTLESS_2 6:8bae97034114 177 UTILS::Format_Flash();
POTLESS_2 6:8bae97034114 178 UTILS::Mount_Flash();
POTLESS_2 6:8bae97034114 179 UTILS::Store_A_Val(Saved_O2, "Calibration_O2");
POTLESS_2 6:8bae97034114 180 UTILS::Store_A_Val(Saved_Motor, "Servo_Poumon");
POTLESS_2 6:8bae97034114 181 DEBUG_U("Flash nettoyée.\r\n");
POTLESS_2 6:8bae97034114 182 DEBUG_U("Redémmarage code.\r\n");
POTLESS_2 6:8bae97034114 183 NVIC_SystemReset();
POTLESS_2 6:8bae97034114 184 }
POTLESS_2 6:8bae97034114 185
POTLESS_2 6:8bae97034114 186 void UTILS::Rename_Flash_File(char* Old_File_Name, char* New_File_Name)
POTLESS_2 0:794c15bd30b0 187 {
POTLESS_2 0:794c15bd30b0 188 char Oldfilename[20];
POTLESS_2 2:5af7a1fb1903 189 sprintf(Oldfilename, "/Root/%s", Old_File_Name);
POTLESS_2 0:794c15bd30b0 190 char Newfilename[20];
POTLESS_2 2:5af7a1fb1903 191 sprintf(Newfilename, "/Root/%s", New_File_Name);
POTLESS_2 0:794c15bd30b0 192
POTLESS_2 0:794c15bd30b0 193 int error = 0;
POTLESS_2 2:5af7a1fb1903 194 error = Root.rename(Oldfilename, Newfilename);
POTLESS_2 0:794c15bd30b0 195 return_error(error);
POTLESS_2 0:794c15bd30b0 196
POTLESS_2 6:8bae97034114 197 DEBUG_U("Fichier %s renommé en %s.\r\n", Oldfilename, Newfilename);
POTLESS_2 0:794c15bd30b0 198 }
POTLESS_2 0:794c15bd30b0 199
POTLESS_2 6:8bae97034114 200 void UTILS::Mount_Flash()
POTLESS_2 0:794c15bd30b0 201 {
POTLESS_2 6:8bae97034114 202 //Montage Flash
POTLESS_2 6:8bae97034114 203 DEBUG_U(" Montage Flash \"/Root\". \r\n\r\n");
POTLESS_2 0:794c15bd30b0 204 int error = 0;
POTLESS_2 2:5af7a1fb1903 205 error = Root.mount(&bd);
POTLESS_2 0:794c15bd30b0 206 return_error(error);
POTLESS_2 1:a937433a95c8 207 if (error > 0) {
POTLESS_2 0:794c15bd30b0 208 //On re format s'il n'y a as de file system...normalement une seul fois...
POTLESS_2 6:8bae97034114 209 DEBUG_U("Pas de File system, on format... ");
POTLESS_2 6:8bae97034114 210 Format_Flash();
POTLESS_2 0:794c15bd30b0 211 }
POTLESS_2 0:794c15bd30b0 212 }
POTLESS_2 0:794c15bd30b0 213
POTLESS_2 6:8bae97034114 214 void UTILS::UnMount_Flash()
POTLESS_2 0:794c15bd30b0 215 {
POTLESS_2 6:8bae97034114 216 //De Montage Flash
POTLESS_2 6:8bae97034114 217 DEBUG_U(" Demontage Flash \"/Root\". \r\n\r\n");
POTLESS_2 0:794c15bd30b0 218 int error = 0;
POTLESS_2 2:5af7a1fb1903 219 error = Root.unmount();
POTLESS_2 0:794c15bd30b0 220 return_error(error);
POTLESS_2 0:794c15bd30b0 221 }
POTLESS_2 0:794c15bd30b0 222
POTLESS_2 6:8bae97034114 223 void UTILS::Format_Flash()
POTLESS_2 0:794c15bd30b0 224 {
POTLESS_2 6:8bae97034114 225 //Formatage Flash
POTLESS_2 6:8bae97034114 226 DEBUG_U(" Formatage Flash\r\n\r\n");
POTLESS_2 0:794c15bd30b0 227 int error = 0;
POTLESS_2 4:872676dd87a4 228 error = FATFileSystem::format(&bd);
POTLESS_2 2:5af7a1fb1903 229 return_error(error);
POTLESS_2 2:5af7a1fb1903 230 }
POTLESS_2 2:5af7a1fb1903 231
POTLESS_2 6:8bae97034114 232 void UTILS::Dir_Flash(char* Dir_Name)
POTLESS_2 2:5af7a1fb1903 233 {
POTLESS_2 2:5af7a1fb1903 234 int error = 0;
POTLESS_2 3:01677db45b2a 235 int nb = 0;
POTLESS_2 6:8bae97034114 236 DEBUG_U("\r\n Ouverture du répertoire %s\r\n", Dir_Name);
POTLESS_2 2:5af7a1fb1903 237 char Dirname[20];
POTLESS_2 2:5af7a1fb1903 238 sprintf(Dirname, "/Root/%s", Dir_Name);
POTLESS_2 6:8bae97034114 239
POTLESS_2 2:5af7a1fb1903 240 DIR* dir = opendir(Dirname);
POTLESS_2 2:5af7a1fb1903 241
POTLESS_2 2:5af7a1fb1903 242 struct dirent* de;
POTLESS_2 6:8bae97034114 243 DEBUG_U(" Fichier du répertoire :\r\n\r\n");
POTLESS_2 6:8bae97034114 244 while((de = readdir(dir)) != NULL) {
POTLESS_2 6:8bae97034114 245 printf(" %s\r\n", &(de->d_name)[0]);
POTLESS_2 6:8bae97034114 246 fflush(stdout);
POTLESS_2 6:8bae97034114 247 nb++;
POTLESS_2 6:8bae97034114 248 }
POTLESS_2 6:8bae97034114 249 DEBUG_U("\r\n Nombre de fichiers = %d\r\n", nb);
POTLESS_2 6:8bae97034114 250 DEBUG_U(" Fermeture du répertoire.\r\n");
POTLESS_2 6:8bae97034114 251 error = closedir(dir);
POTLESS_2 0:794c15bd30b0 252 return_error(error);
POTLESS_2 0:794c15bd30b0 253 }
POTLESS_2 0:794c15bd30b0 254
POTLESS_2 3:01677db45b2a 255 int UTILS::File_Index()
POTLESS_2 3:01677db45b2a 256 {
POTLESS_2 3:01677db45b2a 257 int error = 0;
POTLESS_2 3:01677db45b2a 258 int nb = 0;
POTLESS_2 6:8bae97034114 259
POTLESS_2 3:01677db45b2a 260 DIR* dir = opendir("/Root/");
POTLESS_2 3:01677db45b2a 261 struct dirent* de;
POTLESS_2 6:8bae97034114 262 while((de = readdir(dir)) != NULL) {
POTLESS_2 6:8bae97034114 263 nb++;
POTLESS_2 3:01677db45b2a 264 }
POTLESS_2 3:01677db45b2a 265 error = closedir(dir);
POTLESS_2 3:01677db45b2a 266 return_error(error);
POTLESS_2 6:8bae97034114 267
POTLESS_2 3:01677db45b2a 268 return nb - 2;
POTLESS_2 3:01677db45b2a 269 }
POTLESS_2 3:01677db45b2a 270
POTLESS_2 0:794c15bd30b0 271 //Remapping d'une valeur dans une autre échelle
POTLESS_2 0:794c15bd30b0 272 float UTILS::Remap(float x, float in_min, float in_max, float out_min, float out_max)
POTLESS_2 0:794c15bd30b0 273 {
POTLESS_2 0:794c15bd30b0 274 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
POTLESS_2 0:794c15bd30b0 275 }
POTLESS_2 0:794c15bd30b0 276
POTLESS_2 0:794c15bd30b0 277 //Contraint une valeur entre deux limites
POTLESS_2 0:794c15bd30b0 278 float UTILS::constrain(float x, float a, float b)
POTLESS_2 0:794c15bd30b0 279 {
POTLESS_2 0:794c15bd30b0 280 if(x < a) {
POTLESS_2 0:794c15bd30b0 281 return a;
POTLESS_2 0:794c15bd30b0 282 } else if(b < x) {
POTLESS_2 0:794c15bd30b0 283 return b;
POTLESS_2 0:794c15bd30b0 284 } else
POTLESS_2 0:794c15bd30b0 285 return x;
POTLESS_2 6:8bae97034114 286 }
POTLESS_2 6:8bae97034114 287
POTLESS_2 6:8bae97034114 288 void UTILS::Help()
POTLESS_2 6:8bae97034114 289 {
POTLESS_2 6:8bae97034114 290 printf("\r\n");
POTLESS_2 6:8bae97034114 291 printf(" Commandes à entrer dans le moniteur :\r\n\r\n");
POTLESS_2 6:8bae97034114 292 printf(" help = liste des commandes\r\n");
POTLESS_2 6:8bae97034114 293 printf(" start = démmarage des sorties moniteur\r\n");
POTLESS_2 6:8bae97034114 294 printf(" stop = arrêt des sorties moniteur\r\n");
POTLESS_2 7:1bd8b236feb1 295 printf(" rec = démarrage / arrêt enregistrement\r\n");
POTLESS_2 6:8bae97034114 296 printf(" clean = nettoyage flash, suppression des fichiers LOG\r\n");
POTLESS_2 6:8bae97034114 297 printf(" dir = liste des fichiers de la flash\r\n");
POTLESS_2 6:8bae97034114 298 printf(" del X = effacer le fichier LOG_X\n\r");
POTLESS_2 6:8bae97034114 299 printf(" get X = récupérer le contenu du fichier LOG_X\n\r");
POTLESS_2 6:8bae97034114 300 printf(" time X = met la RTC à l'heure, X en UNIX TIME.\n\r");
POTLESS_2 6:8bae97034114 301 printf(" c_pou X = changement consigne volet poumon à X\n\r");
POTLESS_2 6:8bae97034114 302 printf(" c_fui X = changement consigne volet fuite à X\n\r");
POTLESS_2 6:8bae97034114 303 printf(" calib_p = calibration à 0 du volet poumon\n\r");
POTLESS_2 6:8bae97034114 304 printf(" calib_f = calibration à 0 du volet fuite\n\r");
POTLESS_2 7:1bd8b236feb1 305 printf(" calib_O2 X = calibration des cellules O2 dans l'air avec X itérations\n\r");
POTLESS_2 7:1bd8b236feb1 306 printf(" check = sortie des valeurs de calibrations enregistrées\n\r");
POTLESS_2 6:8bae97034114 307 printf(" sleep = mise en veille\n\r");
POTLESS_2 7:1bd8b236feb1 308 printf(" secu = mise des volets en mode sécu\n\r");
POTLESS_2 6:8bae97034114 309 printf(" reset = reset de la carte\n\r\n\r");
POTLESS_2 6:8bae97034114 310 printf(" Les commandes pour le Mini-r sont à entrer conformément à la doc.\n\r");
POTLESS_2 6:8bae97034114 311 printf("\r\n\r\n");
POTLESS_2 6:8bae97034114 312 fflush(stdout);
POTLESS_2 0:794c15bd30b0 313 }