avec dfu

Fork of Utils by POTLESS

Committer:
POTLESS_2
Date:
Thu Mar 08 15:37:14 2018 +0000
Revision:
13:ff0f36a6d800
Parent:
12:9dd7c462a2c0
Child:
14:fbf4be649b6a
update;

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 9:fa2818db80cf 4 //SDBlockDevice bd(Flash_MOSI, Flash_MISO, Flash_SCK, Flash_CS);
POTLESS_2 9:fa2818db80cf 5 SPIFBlockDevice bd(Flash_MOSI, Flash_MISO, Flash_SCK, Flash_CS);
POTLESS_2 11:a41267cffa61 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 11:a41267cffa61 11 //Init des PIN i2c pour l'eeprom
POTLESS_2 11:a41267cffa61 12 I2C I2C_EEPROM(SDA_PIN, SCL_PIN);
POTLESS_2 11:a41267cffa61 13
POTLESS_2 0:794c15bd30b0 14 void return_error(int ret_val)
POTLESS_2 0:794c15bd30b0 15 {
POTLESS_2 4:872676dd87a4 16 Exist_Val = 0;
POTLESS_2 6:8bae97034114 17 if (ret_val) {
POTLESS_2 12:9dd7c462a2c0 18 DEEP_DEBUG(" Problème Flash = %d\r\n", ret_val);
POTLESS_2 3:01677db45b2a 19 Exist_Val = 0;
POTLESS_2 6:8bae97034114 20 } else {
POTLESS_2 12:9dd7c462a2c0 21 DEEP_DEBUG(" Flash -> OK.\r\n");
POTLESS_2 3:01677db45b2a 22 Exist_Val = 1;
POTLESS_2 6:8bae97034114 23 }
POTLESS_2 6:8bae97034114 24 wait_ms(100);
POTLESS_2 0:794c15bd30b0 25 }
POTLESS_2 0:794c15bd30b0 26
POTLESS_2 0:794c15bd30b0 27 void errno_error(void* ret_val)
POTLESS_2 0:794c15bd30b0 28 {
POTLESS_2 4:872676dd87a4 29 Exist_Val = 0;
POTLESS_2 6:8bae97034114 30 if (ret_val == NULL) {
POTLESS_2 12:9dd7c462a2c0 31 DEEP_DEBUG(" Problème Flash = %d \r\n", errno);
POTLESS_2 3:01677db45b2a 32 Exist_Val = 0;
POTLESS_2 6:8bae97034114 33 } else {
POTLESS_2 12:9dd7c462a2c0 34 DEEP_DEBUG(" Flash -> OK.\r\n");
POTLESS_2 3:01677db45b2a 35 Exist_Val = 1;
POTLESS_2 6:8bae97034114 36 }
POTLESS_2 6:8bae97034114 37 wait_ms(100);
POTLESS_2 0:794c15bd30b0 38 }
POTLESS_2 0:794c15bd30b0 39
POTLESS_2 8:23fd400c4edd 40 void UTILS::Flash_Infos()
POTLESS_2 8:23fd400c4edd 41 {
POTLESS_2 8:23fd400c4edd 42 printf("\r\n");
POTLESS_2 8:23fd400c4edd 43 printf("Flash size : %llu Mb\r\n", bd.size() / 1048576);
POTLESS_2 8:23fd400c4edd 44 printf("Flash read size : %llu\r\n", bd.get_read_size());
POTLESS_2 8:23fd400c4edd 45 printf("Flash program size : %llu\r\n", bd.get_program_size());
POTLESS_2 8:23fd400c4edd 46 printf("Flash erase size : %llu\r\n", bd.get_erase_size());
POTLESS_2 8:23fd400c4edd 47 }
POTLESS_2 8:23fd400c4edd 48
POTLESS_2 11:a41267cffa61 49 void UTILS::Get_File_Size(char* File_Name)
POTLESS_2 11:a41267cffa61 50 {
POTLESS_2 11:a41267cffa61 51
POTLESS_2 8:23fd400c4edd 52 char filename[20];
POTLESS_2 8:23fd400c4edd 53 sprintf(filename, "/Root/%s", File_Name);
POTLESS_2 8:23fd400c4edd 54
POTLESS_2 8:23fd400c4edd 55 FILE* fd = fopen(filename, "r");
POTLESS_2 11:a41267cffa61 56 errno_error(fd);
POTLESS_2 11:a41267cffa61 57
POTLESS_2 8:23fd400c4edd 58 fseek(fd, 0, SEEK_END);
POTLESS_2 8:23fd400c4edd 59 int size = ftell(fd);
POTLESS_2 8:23fd400c4edd 60 fseek(fd, 0, SEEK_SET);
POTLESS_2 11:a41267cffa61 61
POTLESS_2 8:23fd400c4edd 62 fclose(fd);
POTLESS_2 11:a41267cffa61 63
POTLESS_2 8:23fd400c4edd 64 printf("\r\n");
POTLESS_2 8:23fd400c4edd 65 printf(" Taille du fichier %s = %d Kb\r\n", filename, size / 1024);
POTLESS_2 8:23fd400c4edd 66 }
POTLESS_2 8:23fd400c4edd 67
POTLESS_2 0:794c15bd30b0 68 void UTILS::Store_A_Val(float Val_To_Store, char* File_Name)
POTLESS_2 0:794c15bd30b0 69 {
POTLESS_2 0:794c15bd30b0 70
POTLESS_2 0:794c15bd30b0 71 char filename[20];
POTLESS_2 2:5af7a1fb1903 72 sprintf(filename, "/Root/%s.txt", File_Name);
POTLESS_2 0:794c15bd30b0 73
POTLESS_2 0:794c15bd30b0 74 FILE* fd = fopen(filename, "w");
POTLESS_2 0:794c15bd30b0 75 errno_error(fd);
POTLESS_2 0:794c15bd30b0 76 fprintf(fd, "%f\r\n", Val_To_Store);
POTLESS_2 0:794c15bd30b0 77 fclose(fd);
POTLESS_2 0:794c15bd30b0 78
POTLESS_2 12:9dd7c462a2c0 79 DEEP_DEBUG(" \r\n %s sauvegardée = %f\r\n", filename, Val_To_Store);
POTLESS_2 0:794c15bd30b0 80
POTLESS_2 0:794c15bd30b0 81 }
POTLESS_2 0:794c15bd30b0 82
POTLESS_2 0:794c15bd30b0 83 float UTILS::Read_A_Val(char* File_Name)
POTLESS_2 0:794c15bd30b0 84 {
POTLESS_2 0:794c15bd30b0 85 char buffer[10] = {0};
POTLESS_2 0:794c15bd30b0 86 char c = {0};
POTLESS_2 0:794c15bd30b0 87 char *token;
POTLESS_2 0:794c15bd30b0 88 int i = 0;
POTLESS_2 0:794c15bd30b0 89
POTLESS_2 0:794c15bd30b0 90 char filename[20];
POTLESS_2 2:5af7a1fb1903 91 sprintf(filename, "/Root/%s.txt", File_Name);
POTLESS_2 0:794c15bd30b0 92
POTLESS_2 12:9dd7c462a2c0 93 DEEP_DEBUG(" \n Récupération de %s...\r\n", filename);
POTLESS_2 0:794c15bd30b0 94
POTLESS_2 0:794c15bd30b0 95 FILE* fd = fopen(filename, "r");
POTLESS_2 0:794c15bd30b0 96 errno_error(fd);
POTLESS_2 6:8bae97034114 97
POTLESS_2 0:794c15bd30b0 98 while ((c != '\n') && (i < 10)) {
POTLESS_2 0:794c15bd30b0 99 c = fgetc(fd);
POTLESS_2 0:794c15bd30b0 100 buffer[i] = c;
POTLESS_2 0:794c15bd30b0 101 i++;
POTLESS_2 0:794c15bd30b0 102 }
POTLESS_2 0:794c15bd30b0 103
POTLESS_2 0:794c15bd30b0 104 token = strtok(buffer, "\n");
POTLESS_2 0:794c15bd30b0 105
POTLESS_2 0:794c15bd30b0 106 float val = atof(token);
POTLESS_2 0:794c15bd30b0 107
POTLESS_2 12:9dd7c462a2c0 108 DEEP_DEBUG(" Valeur Récupérée = %f\r\n", val);
POTLESS_2 0:794c15bd30b0 109
POTLESS_2 0:794c15bd30b0 110 fclose(fd);
POTLESS_2 0:794c15bd30b0 111
POTLESS_2 0:794c15bd30b0 112 return val;
POTLESS_2 0:794c15bd30b0 113 }
POTLESS_2 0:794c15bd30b0 114
POTLESS_2 0:794c15bd30b0 115
POTLESS_2 6:8bae97034114 116 void UTILS::Write_Flash_File(char* To_Store, char* File_Name)
POTLESS_2 0:794c15bd30b0 117 {
POTLESS_2 2:5af7a1fb1903 118 char filename[50];
POTLESS_2 2:5af7a1fb1903 119 sprintf(filename, "/Root/%s", (string)File_Name);
POTLESS_2 6:8bae97034114 120
POTLESS_2 0:794c15bd30b0 121 FILE* fd = fopen(filename, "a");
POTLESS_2 6:8bae97034114 122
POTLESS_2 0:794c15bd30b0 123 errno_error(fd);
POTLESS_2 0:794c15bd30b0 124 fprintf(fd, "%s\r\n", To_Store);
POTLESS_2 0:794c15bd30b0 125 fclose(fd);
POTLESS_2 0:794c15bd30b0 126
POTLESS_2 12:9dd7c462a2c0 127 DEEP_DEBUG(" Enregistrement de %s OK\r\n\r\n", filename);
POTLESS_2 0:794c15bd30b0 128 }
POTLESS_2 0:794c15bd30b0 129
POTLESS_2 6:8bae97034114 130 /*
POTLESS_2 6:8bae97034114 131 void UTILS::Read_Flash_File(char* File_Name)
POTLESS_2 0:794c15bd30b0 132 {
POTLESS_2 0:794c15bd30b0 133 char filename[20];
POTLESS_2 2:5af7a1fb1903 134 sprintf(filename, "/Root/%s", File_Name);
POTLESS_2 6:8bae97034114 135
POTLESS_2 0:794c15bd30b0 136 FILE* fd = fopen(filename, "r");
POTLESS_2 0:794c15bd30b0 137 errno_error(fd);
POTLESS_2 0:794c15bd30b0 138
POTLESS_2 6:8bae97034114 139 //printf(" Contenu du fichier :\r\n\r\n");
POTLESS_2 6:8bae97034114 140 char buff[100] = {0};
POTLESS_2 11:a41267cffa61 141
POTLESS_2 11:a41267cffa61 142 while (!feof(fd)) {
POTLESS_2 0:794c15bd30b0 143 int size = fread(&buff[0], 1, 15, fd);
POTLESS_2 11:a41267cffa61 144 fwrite(&buff[0], 1, size, stdout);
POTLESS_2 0:794c15bd30b0 145 }
POTLESS_2 6:8bae97034114 146 //printf("\r\n Fin du fichier.\r\n");
POTLESS_2 0:794c15bd30b0 147 fclose(fd);
POTLESS_2 11:a41267cffa61 148
POTLESS_2 6:8bae97034114 149 }
POTLESS_2 6:8bae97034114 150 */
POTLESS_2 6:8bae97034114 151
POTLESS_2 6:8bae97034114 152 void UTILS::Read_Flash_File(char* File_Name)
POTLESS_2 6:8bae97034114 153 {
POTLESS_2 6:8bae97034114 154 char filename[20];
POTLESS_2 6:8bae97034114 155 sprintf(filename, "/Root/%s", File_Name);
POTLESS_2 6:8bae97034114 156
POTLESS_2 6:8bae97034114 157 FILE* fd = fopen(filename, "r");
POTLESS_2 11:a41267cffa61 158 errno_error(fd);
POTLESS_2 6:8bae97034114 159 char s1[100];
POTLESS_2 8:23fd400c4edd 160 printf("\r\n");
POTLESS_2 6:8bae97034114 161 printf(" Contenu du fichier :\r\n\r\n");
POTLESS_2 6:8bae97034114 162 while (!feof(fd)) {
POTLESS_2 6:8bae97034114 163 memset(s1, 0, sizeof(s1));
POTLESS_2 6:8bae97034114 164 fgets(s1, sizeof(s1), fd);
POTLESS_2 6:8bae97034114 165 printf("%s", s1);
POTLESS_2 6:8bae97034114 166 fflush(stdout);
POTLESS_2 6:8bae97034114 167 }
POTLESS_2 11:a41267cffa61 168 fclose(fd);
POTLESS_2 11:a41267cffa61 169 printf("\r\n Fin du fichier.\r\n");
POTLESS_2 0:794c15bd30b0 170 }
POTLESS_2 0:794c15bd30b0 171
POTLESS_2 5:0f3b6e6677ef 172 bool UTILS::File_Exist(string File_Name)
POTLESS_2 3:01677db45b2a 173 {
POTLESS_2 3:01677db45b2a 174 char filename[20];
POTLESS_2 3:01677db45b2a 175 sprintf(filename, "/Root/%s.txt", File_Name);
POTLESS_2 6:8bae97034114 176
POTLESS_2 3:01677db45b2a 177 FILE* fd = fopen(filename, "r");
POTLESS_2 3:01677db45b2a 178 errno_error(fd);
POTLESS_2 6:8bae97034114 179
POTLESS_2 6:8bae97034114 180 if (Exist_Val == 0) {
POTLESS_2 10:21f3ff5d6a3d 181 DEBUG(" Le fichier %s n'existe pas....\r\n", filename);
POTLESS_2 4:872676dd87a4 182 fclose(fd);
POTLESS_2 3:01677db45b2a 183 return false;
POTLESS_2 5:0f3b6e6677ef 184 }
POTLESS_2 6:8bae97034114 185
POTLESS_2 6:8bae97034114 186 if (Exist_Val == 1) {
POTLESS_2 3:01677db45b2a 187 fclose(fd);
POTLESS_2 3:01677db45b2a 188 return true;
POTLESS_2 6:8bae97034114 189 }
POTLESS_2 3:01677db45b2a 190 }
POTLESS_2 3:01677db45b2a 191
POTLESS_2 6:8bae97034114 192 void UTILS::Delete_Flash_File(char* File_Name)
POTLESS_2 0:794c15bd30b0 193 {
POTLESS_2 0:794c15bd30b0 194 char filename[20];
POTLESS_2 6:8bae97034114 195 sprintf(filename, "%s", File_Name);
POTLESS_2 0:794c15bd30b0 196 int error = 0;
POTLESS_2 2:5af7a1fb1903 197 error = Root.remove(filename);
POTLESS_2 0:794c15bd30b0 198 return_error(error);
POTLESS_2 0:794c15bd30b0 199
POTLESS_2 12:9dd7c462a2c0 200 DEBUG(" Fichier %s effacé.\r\n", filename);
POTLESS_2 0:794c15bd30b0 201 }
POTLESS_2 0:794c15bd30b0 202
POTLESS_2 6:8bae97034114 203 void UTILS::Clean_Flash()
POTLESS_2 6:8bae97034114 204 {
POTLESS_2 12:9dd7c462a2c0 205 DEBUG(" Nettoyage de la Flash.\r\n");
POTLESS_2 12:9dd7c462a2c0 206 //float Saved_O2 = UTILS::Read_A_Val("Calibration_O2");
POTLESS_2 6:8bae97034114 207 int Saved_Motor = (int)UTILS::Read_A_Val("Servo_Poumon");
POTLESS_2 11:a41267cffa61 208 int ARNSRS_ID = (int)UTILS::Read_A_Val("ARNSRS_ID");
POTLESS_2 6:8bae97034114 209 UTILS::UnMount_Flash();
POTLESS_2 6:8bae97034114 210 UTILS::Format_Flash();
POTLESS_2 6:8bae97034114 211 UTILS::Mount_Flash();
POTLESS_2 12:9dd7c462a2c0 212 //UTILS::Store_A_Val(Saved_O2, "Calibration_O2");
POTLESS_2 6:8bae97034114 213 UTILS::Store_A_Val(Saved_Motor, "Servo_Poumon");
POTLESS_2 11:a41267cffa61 214 UTILS::Store_A_Val(ARNSRS_ID, "ARNSRS_ID");
POTLESS_2 12:9dd7c462a2c0 215 DEBUG(" Flash nettoyée.\r\n");
POTLESS_2 12:9dd7c462a2c0 216 DEBUG(" Redémmarage code.\r\n");
POTLESS_2 13:ff0f36a6d800 217 UTILS::UnMount_Flash();
POTLESS_2 11:a41267cffa61 218 NVIC_SystemReset();
POTLESS_2 6:8bae97034114 219 }
POTLESS_2 6:8bae97034114 220
POTLESS_2 6:8bae97034114 221 void UTILS::Rename_Flash_File(char* Old_File_Name, char* New_File_Name)
POTLESS_2 0:794c15bd30b0 222 {
POTLESS_2 0:794c15bd30b0 223 char Oldfilename[20];
POTLESS_2 2:5af7a1fb1903 224 sprintf(Oldfilename, "/Root/%s", Old_File_Name);
POTLESS_2 0:794c15bd30b0 225 char Newfilename[20];
POTLESS_2 2:5af7a1fb1903 226 sprintf(Newfilename, "/Root/%s", New_File_Name);
POTLESS_2 0:794c15bd30b0 227
POTLESS_2 0:794c15bd30b0 228 int error = 0;
POTLESS_2 2:5af7a1fb1903 229 error = Root.rename(Oldfilename, Newfilename);
POTLESS_2 0:794c15bd30b0 230 return_error(error);
POTLESS_2 0:794c15bd30b0 231
POTLESS_2 12:9dd7c462a2c0 232 DEBUG(" Fichier %s renommé en %s.\r\n", Oldfilename, Newfilename);
POTLESS_2 0:794c15bd30b0 233 }
POTLESS_2 0:794c15bd30b0 234
POTLESS_2 6:8bae97034114 235 void UTILS::Mount_Flash()
POTLESS_2 0:794c15bd30b0 236 {
POTLESS_2 6:8bae97034114 237 //Montage Flash
POTLESS_2 10:21f3ff5d6a3d 238 DEBUG(" Montage Flash \"/Root\". \r\n\r\n");
POTLESS_2 0:794c15bd30b0 239 int error = 0;
POTLESS_2 2:5af7a1fb1903 240 error = Root.mount(&bd);
POTLESS_2 0:794c15bd30b0 241 return_error(error);
POTLESS_2 1:a937433a95c8 242 if (error > 0) {
POTLESS_2 0:794c15bd30b0 243 //On re format s'il n'y a as de file system...normalement une seul fois...
POTLESS_2 10:21f3ff5d6a3d 244 DEBUG("Pas de File system, on format... ");
POTLESS_2 6:8bae97034114 245 Format_Flash();
POTLESS_2 0:794c15bd30b0 246 }
POTLESS_2 0:794c15bd30b0 247 }
POTLESS_2 0:794c15bd30b0 248
POTLESS_2 6:8bae97034114 249 void UTILS::UnMount_Flash()
POTLESS_2 0:794c15bd30b0 250 {
POTLESS_2 6:8bae97034114 251 //De Montage Flash
POTLESS_2 10:21f3ff5d6a3d 252 DEBUG(" Demontage Flash \"/Root\". \r\n\r\n");
POTLESS_2 0:794c15bd30b0 253 int error = 0;
POTLESS_2 2:5af7a1fb1903 254 error = Root.unmount();
POTLESS_2 0:794c15bd30b0 255 return_error(error);
POTLESS_2 0:794c15bd30b0 256 }
POTLESS_2 0:794c15bd30b0 257
POTLESS_2 6:8bae97034114 258 void UTILS::Format_Flash()
POTLESS_2 0:794c15bd30b0 259 {
POTLESS_2 6:8bae97034114 260 //Formatage Flash
POTLESS_2 10:21f3ff5d6a3d 261 DEBUG(" Formatage Flash\r\n\r\n");
POTLESS_2 0:794c15bd30b0 262 int error = 0;
POTLESS_2 4:872676dd87a4 263 error = FATFileSystem::format(&bd);
POTLESS_2 2:5af7a1fb1903 264 return_error(error);
POTLESS_2 2:5af7a1fb1903 265 }
POTLESS_2 2:5af7a1fb1903 266
POTLESS_2 6:8bae97034114 267 void UTILS::Dir_Flash(char* Dir_Name)
POTLESS_2 2:5af7a1fb1903 268 {
POTLESS_2 2:5af7a1fb1903 269 int error = 0;
POTLESS_2 3:01677db45b2a 270 int nb = 0;
POTLESS_2 12:9dd7c462a2c0 271 DEEP_DEBUG("\r\n Ouverture du répertoire %s\r\n", Dir_Name);
POTLESS_2 2:5af7a1fb1903 272 char Dirname[20];
POTLESS_2 2:5af7a1fb1903 273 sprintf(Dirname, "/Root/%s", Dir_Name);
POTLESS_2 6:8bae97034114 274
POTLESS_2 2:5af7a1fb1903 275 DIR* dir = opendir(Dirname);
POTLESS_2 2:5af7a1fb1903 276
POTLESS_2 2:5af7a1fb1903 277 struct dirent* de;
POTLESS_2 12:9dd7c462a2c0 278 DEBUG(" Fichiers du répertoire :\r\n\r\n");
POTLESS_2 6:8bae97034114 279 while((de = readdir(dir)) != NULL) {
POTLESS_2 6:8bae97034114 280 printf(" %s\r\n", &(de->d_name)[0]);
POTLESS_2 6:8bae97034114 281 fflush(stdout);
POTLESS_2 6:8bae97034114 282 nb++;
POTLESS_2 6:8bae97034114 283 }
POTLESS_2 10:21f3ff5d6a3d 284 DEBUG("\r\n Nombre de fichiers = %d\r\n", nb);
POTLESS_2 12:9dd7c462a2c0 285 DEEP_DEBUG(" Fermeture du répertoire.\r\n");
POTLESS_2 6:8bae97034114 286 error = closedir(dir);
POTLESS_2 0:794c15bd30b0 287 return_error(error);
POTLESS_2 0:794c15bd30b0 288 }
POTLESS_2 0:794c15bd30b0 289
POTLESS_2 3:01677db45b2a 290 int UTILS::File_Index()
POTLESS_2 3:01677db45b2a 291 {
POTLESS_2 3:01677db45b2a 292 int error = 0;
POTLESS_2 3:01677db45b2a 293 int nb = 0;
POTLESS_2 6:8bae97034114 294
POTLESS_2 3:01677db45b2a 295 DIR* dir = opendir("/Root/");
POTLESS_2 3:01677db45b2a 296 struct dirent* de;
POTLESS_2 6:8bae97034114 297 while((de = readdir(dir)) != NULL) {
POTLESS_2 6:8bae97034114 298 nb++;
POTLESS_2 3:01677db45b2a 299 }
POTLESS_2 3:01677db45b2a 300 error = closedir(dir);
POTLESS_2 3:01677db45b2a 301 return_error(error);
POTLESS_2 6:8bae97034114 302
POTLESS_2 3:01677db45b2a 303 return nb - 2;
POTLESS_2 3:01677db45b2a 304 }
POTLESS_2 3:01677db45b2a 305
POTLESS_2 0:794c15bd30b0 306 //Remapping d'une valeur dans une autre échelle
POTLESS_2 0:794c15bd30b0 307 float UTILS::Remap(float x, float in_min, float in_max, float out_min, float out_max)
POTLESS_2 0:794c15bd30b0 308 {
POTLESS_2 0:794c15bd30b0 309 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
POTLESS_2 0:794c15bd30b0 310 }
POTLESS_2 0:794c15bd30b0 311
POTLESS_2 0:794c15bd30b0 312 //Contraint une valeur entre deux limites
POTLESS_2 0:794c15bd30b0 313 float UTILS::constrain(float x, float a, float b)
POTLESS_2 0:794c15bd30b0 314 {
POTLESS_2 0:794c15bd30b0 315 if(x < a) {
POTLESS_2 0:794c15bd30b0 316 return a;
POTLESS_2 0:794c15bd30b0 317 } else if(b < x) {
POTLESS_2 0:794c15bd30b0 318 return b;
POTLESS_2 0:794c15bd30b0 319 } else
POTLESS_2 0:794c15bd30b0 320 return x;
POTLESS_2 6:8bae97034114 321 }
POTLESS_2 6:8bae97034114 322
POTLESS_2 6:8bae97034114 323 void UTILS::Help()
POTLESS_2 6:8bae97034114 324 {
POTLESS_2 6:8bae97034114 325 printf("\r\n");
POTLESS_2 8:23fd400c4edd 326 printf(" COMMANDES GENERALES\r\n\r\n");
POTLESS_2 8:23fd400c4edd 327 printf(" help = liste des commandes.\r\n");
POTLESS_2 8:23fd400c4edd 328 printf(" start = démmarage des sorties moniteur.\r\n");
POTLESS_2 8:23fd400c4edd 329 printf(" stop = arrêt des sorties moniteur.\r\n");
POTLESS_2 6:8bae97034114 330 printf(" time X = met la RTC à l'heure, X en UNIX TIME.\n\r");
POTLESS_2 12:9dd7c462a2c0 331 printf(" ARNSRS_ID X = entrée de l'identifiant de l'appareil X.\r\n");
POTLESS_2 8:23fd400c4edd 332 printf(" sleep = mise en veille.\n\r");
POTLESS_2 8:23fd400c4edd 333 printf(" reset = reset de la carte.\n\r\n\r");
POTLESS_2 8:23fd400c4edd 334 printf(" COMMANDES MEMOIRE\r\n\r\n");
POTLESS_2 8:23fd400c4edd 335 printf(" rec = démarrage / arrêt enregistrement.\r\n");
POTLESS_2 8:23fd400c4edd 336 printf(" flash_i = informations sur la flash.\r\n");
POTLESS_2 13:ff0f36a6d800 337 printf(" flash_u = démonte la flash. A faire avant de débrancher ou recharger un code.\r\n");
POTLESS_2 13:ff0f36a6d800 338 printf(" flash_m = monte la flash.\r\n");
POTLESS_2 13:ff0f36a6d800 339 printf(" flash_c = nettoyage flash, suppression des fichiers LOG.\r\n");
POTLESS_2 8:23fd400c4edd 340 printf(" dir = liste des fichiers de la flash.\r\n");
POTLESS_2 8:23fd400c4edd 341 printf(" del X = effacer le fichier LOG_X.\n\r");
POTLESS_2 8:23fd400c4edd 342 printf(" get X = récupérer le contenu du fichier LOG_X.\n\r");
POTLESS_2 8:23fd400c4edd 343 printf(" file_s X = récupérer la taille du fichier LOG_X.\n\r\n\r");
POTLESS_2 8:23fd400c4edd 344 //printf(" file_i X = récupérer les informations sur le fichier LOG_X.\n\r\n\r");
POTLESS_2 11:a41267cffa61 345 printf(" COMMANDES MOTEURS\r\n\r\n");
POTLESS_2 8:23fd400c4edd 346 printf(" c_pou X = changement consigne volet poumon à X.\n\r");
POTLESS_2 8:23fd400c4edd 347 printf(" c_fui X = changement consigne volet fuite à X.\n\r");
POTLESS_2 8:23fd400c4edd 348 printf(" calib_p = calibration à 0 du volet poumon.\n\r");
POTLESS_2 11:a41267cffa61 349 printf(" calib_f = calibration à 0 du volet fuite.\n\r");
POTLESS_2 12:9dd7c462a2c0 350 printf(" secu = mise des volets en mode sécu.\n\r");
POTLESS_2 12:9dd7c462a2c0 351 printf(" check_F = sortie des valeurs 0 moteurs et ARNSRS_ID enregistrées.\n\r\n\r");
POTLESS_2 11:a41267cffa61 352 printf(" COMMANDES CAPTEURS\r\n\r\n");
POTLESS_2 11:a41267cffa61 353 printf(" Head_ID X = Numérote le numéro X d'identification de la sensor Head dans l'eeprom.\n\r");
POTLESS_2 11:a41267cffa61 354 printf(" O2_1_ID X = Numérote le numéro X d'identification de la cellule O2 1 dans l'eeprom.\n\r");
POTLESS_2 11:a41267cffa61 355 printf(" O2_2_ID X = Numérote le numéro X d'identification de la cellule O2 2 dans l'eeprom.\n\r");
POTLESS_2 11:a41267cffa61 356 printf(" CO2_ID X = Numérote le numéro X d'identification du capteur CO2 dans l'eeprom.\n\r");
POTLESS_2 12:9dd7c462a2c0 357 printf(" check_E = sortie des valeurs de calibration enregistrées dans l'eeprom.\n\r\n\r");
POTLESS_2 12:9dd7c462a2c0 358 printf(" calib_O2 X = calibration des cellules O2 dans l'air avec X itérations.\n\r\n\r");
POTLESS_2 12:9dd7c462a2c0 359
POTLESS_2 8:23fd400c4edd 360 printf(" Les commandes pour le Mini-r sont à entrer conformément à la doc. Les plus utilisée :\n\r");
POTLESS_2 8:23fd400c4edd 361 printf(" G = calibration du capteur CO2 dans l'air.\n\r");
POTLESS_2 11:a41267cffa61 362 printf(" K 0 = arrêt du capteur CO2.\n\r");
POTLESS_2 8:23fd400c4edd 363 printf(" K 1 = capteur CO2 mode streaming.\n\r");
POTLESS_2 8:23fd400c4edd 364 printf(" K 2 = capteur CO2 mode spooling.\n\r");
POTLESS_2 11:a41267cffa61 365 printf(" A X = digi filter X. X égal à 32, 64, 128...\n\r");
POTLESS_2 6:8bae97034114 366 printf("\r\n\r\n");
POTLESS_2 6:8bae97034114 367 fflush(stdout);
POTLESS_2 11:a41267cffa61 368 }
POTLESS_2 11:a41267cffa61 369
POTLESS_2 11:a41267cffa61 370 void UTILS::clean_line_EEPROM(unsigned int eeaddress, int address)
POTLESS_2 11:a41267cffa61 371 {
POTLESS_2 12:9dd7c462a2c0 372 DEEP_DEBUG(" Effacage de la ligne à l'adresse %d de l'eeprom.\r\n", eeaddress);
POTLESS_2 11:a41267cffa61 373
POTLESS_2 11:a41267cffa61 374 int size = 64;
POTLESS_2 11:a41267cffa61 375 char deleteData[] = {" "};
POTLESS_2 11:a41267cffa61 376
POTLESS_2 11:a41267cffa61 377 char i2cBuffer[size + 2];
POTLESS_2 11:a41267cffa61 378 i2cBuffer[0] = (unsigned char)(eeaddress >> 8); // MSB
POTLESS_2 11:a41267cffa61 379 i2cBuffer[1] = (unsigned char)(eeaddress & 0xFF); // LSB
POTLESS_2 11:a41267cffa61 380
POTLESS_2 11:a41267cffa61 381 for (int i = 0; i < size; i++) {
POTLESS_2 11:a41267cffa61 382 i2cBuffer[i + 2] = deleteData[i];
POTLESS_2 11:a41267cffa61 383 }
POTLESS_2 11:a41267cffa61 384
POTLESS_2 11:a41267cffa61 385 int result = I2C_EEPROM.write(address, i2cBuffer, size + 2, false);
POTLESS_2 11:a41267cffa61 386 wait_ms(6);
POTLESS_2 11:a41267cffa61 387
POTLESS_2 12:9dd7c462a2c0 388 DEEP_DEBUG(" Ligne à l'adresse %d de l'eeprom effacée.\r\n", eeaddress);
POTLESS_2 11:a41267cffa61 389 }
POTLESS_2 11:a41267cffa61 390
POTLESS_2 11:a41267cffa61 391 //Max 63 bit en écriture
POTLESS_2 11:a41267cffa61 392 void UTILS::write_EEPROM(char *data, unsigned int eeaddress, int address)
POTLESS_2 11:a41267cffa61 393 {
POTLESS_2 11:a41267cffa61 394 //Nettoyage de la ligne ou on va stocker la nouvelle data
POTLESS_2 11:a41267cffa61 395 clean_line_EEPROM(eeaddress);
POTLESS_2 11:a41267cffa61 396
POTLESS_2 11:a41267cffa61 397 // On calcul de la taille de la chaine a enregistrer
POTLESS_2 11:a41267cffa61 398 char size = 0;
POTLESS_2 11:a41267cffa61 399 do {
POTLESS_2 11:a41267cffa61 400 size++;
POTLESS_2 11:a41267cffa61 401 } while (data[size]);
POTLESS_2 11:a41267cffa61 402
POTLESS_2 11:a41267cffa61 403 char i2cBuffer[size + 2];
POTLESS_2 11:a41267cffa61 404 i2cBuffer[0] = (unsigned char)(eeaddress >> 8); // MSB
POTLESS_2 11:a41267cffa61 405 i2cBuffer[1] = (unsigned char)(eeaddress & 0xFF); // LSB
POTLESS_2 11:a41267cffa61 406
POTLESS_2 11:a41267cffa61 407 for (int i = 0; i < size; i++) {
POTLESS_2 11:a41267cffa61 408 i2cBuffer[i + 2] = data[i];
POTLESS_2 11:a41267cffa61 409 }
POTLESS_2 11:a41267cffa61 410
POTLESS_2 11:a41267cffa61 411 int res = I2C_EEPROM.write(address, i2cBuffer, size + 2, false);
POTLESS_2 11:a41267cffa61 412
POTLESS_2 11:a41267cffa61 413 wait_ms(6);
POTLESS_2 11:a41267cffa61 414
POTLESS_2 12:9dd7c462a2c0 415 DEEP_DEBUG(" Chaine écrite à l'adresse %d de l'eeprom : %s\r\n", eeaddress , data);
POTLESS_2 11:a41267cffa61 416 }
POTLESS_2 11:a41267cffa61 417
POTLESS_2 12:9dd7c462a2c0 418 char* UTILS::read_EEPROM(unsigned int eeaddress , int address)
POTLESS_2 12:9dd7c462a2c0 419 {
POTLESS_2 12:9dd7c462a2c0 420 //On lit toute la ligne
POTLESS_2 12:9dd7c462a2c0 421 int size = 17;
POTLESS_2 12:9dd7c462a2c0 422 static char data[17];
POTLESS_2 12:9dd7c462a2c0 423
POTLESS_2 12:9dd7c462a2c0 424 char i2cBuffer[2];
POTLESS_2 12:9dd7c462a2c0 425 i2cBuffer[0] = (unsigned char)(eeaddress >> 8); // MSB
POTLESS_2 12:9dd7c462a2c0 426 i2cBuffer[1] = (unsigned char)(eeaddress & 0xFF); // LSB
POTLESS_2 12:9dd7c462a2c0 427
POTLESS_2 12:9dd7c462a2c0 428 // Reset eeprom pointer address
POTLESS_2 12:9dd7c462a2c0 429 int result = I2C_EEPROM.write(address, i2cBuffer, 2, false);
POTLESS_2 12:9dd7c462a2c0 430 wait_ms(6);
POTLESS_2 12:9dd7c462a2c0 431
POTLESS_2 12:9dd7c462a2c0 432 // Read eeprom
POTLESS_2 12:9dd7c462a2c0 433 I2C_EEPROM.read(address, data, size);
POTLESS_2 12:9dd7c462a2c0 434 wait_ms(6);
POTLESS_2 12:9dd7c462a2c0 435
POTLESS_2 12:9dd7c462a2c0 436 DEEP_DEBUG(" Char lu à l'adresse %d de l'eeprom : %s\r\n", eeaddress, &data);
POTLESS_2 12:9dd7c462a2c0 437
POTLESS_2 12:9dd7c462a2c0 438 return data;
POTLESS_2 12:9dd7c462a2c0 439 }
POTLESS_2 12:9dd7c462a2c0 440
POTLESS_2 12:9dd7c462a2c0 441 void UTILS::read_C_EEPROM(char *data, unsigned int eeaddress ,int address)
POTLESS_2 12:9dd7c462a2c0 442 {
POTLESS_2 12:9dd7c462a2c0 443 //On lit toute la ligne
POTLESS_2 12:9dd7c462a2c0 444 int size = 64;
POTLESS_2 12:9dd7c462a2c0 445
POTLESS_2 12:9dd7c462a2c0 446 char i2cBuffer[2];
POTLESS_2 12:9dd7c462a2c0 447 i2cBuffer[0] = (unsigned char)(eeaddress >> 8); // MSB
POTLESS_2 12:9dd7c462a2c0 448 i2cBuffer[1] = (unsigned char)(eeaddress & 0xFF); // LSB
POTLESS_2 12:9dd7c462a2c0 449
POTLESS_2 12:9dd7c462a2c0 450 // Reset eeprom pointer address
POTLESS_2 12:9dd7c462a2c0 451 int result = I2C_EEPROM.write(address, i2cBuffer, 2, false);
POTLESS_2 12:9dd7c462a2c0 452 wait_ms(6);
POTLESS_2 12:9dd7c462a2c0 453
POTLESS_2 12:9dd7c462a2c0 454 // Read eeprom
POTLESS_2 12:9dd7c462a2c0 455 I2C_EEPROM.read(address, data, size);
POTLESS_2 12:9dd7c462a2c0 456 wait_ms(6);
POTLESS_2 12:9dd7c462a2c0 457
POTLESS_2 12:9dd7c462a2c0 458 DEEP_DEBUG(" Char lu à l'adresse %d de l'eeprom : %s\r\n", eeaddress, data);
POTLESS_2 12:9dd7c462a2c0 459 }
POTLESS_2 11:a41267cffa61 460
POTLESS_2 11:a41267cffa61 461 float UTILS::read_F_EEPROM(unsigned int eeaddress ,int address)
POTLESS_2 11:a41267cffa61 462 {
POTLESS_2 11:a41267cffa61 463 //On lit toute la ligne
POTLESS_2 11:a41267cffa61 464 int size = 64;
POTLESS_2 11:a41267cffa61 465 char data[64];
POTLESS_2 11:a41267cffa61 466
POTLESS_2 11:a41267cffa61 467 char i2cBuffer[2];
POTLESS_2 11:a41267cffa61 468 i2cBuffer[0] = (unsigned char)(eeaddress >> 8); // MSB
POTLESS_2 11:a41267cffa61 469 i2cBuffer[1] = (unsigned char)(eeaddress & 0xFF); // LSB
POTLESS_2 11:a41267cffa61 470
POTLESS_2 11:a41267cffa61 471 int result = I2C_EEPROM.write(address, i2cBuffer, 2, false);
POTLESS_2 11:a41267cffa61 472 wait_ms(6);
POTLESS_2 11:a41267cffa61 473
POTLESS_2 11:a41267cffa61 474 I2C_EEPROM.read(address, data, size);
POTLESS_2 11:a41267cffa61 475 wait_ms(6);
POTLESS_2 11:a41267cffa61 476
POTLESS_2 11:a41267cffa61 477 float result_F = atof(data);
POTLESS_2 12:9dd7c462a2c0 478 DEEP_DEBUG(" Float lu à l'adresse %d de l'eeprom : %f\r\n", eeaddress, result_F);
POTLESS_2 11:a41267cffa61 479 return result_F;
POTLESS_2 11:a41267cffa61 480 }
POTLESS_2 11:a41267cffa61 481
POTLESS_2 11:a41267cffa61 482
POTLESS_2 11:a41267cffa61 483 int UTILS::read_I_EEPROM(unsigned int eeaddress ,int address)
POTLESS_2 11:a41267cffa61 484 {
POTLESS_2 11:a41267cffa61 485 //On lit toute la ligne
POTLESS_2 11:a41267cffa61 486 int size = 64;
POTLESS_2 11:a41267cffa61 487 char data[64];
POTLESS_2 11:a41267cffa61 488
POTLESS_2 11:a41267cffa61 489 char i2cBuffer[2];
POTLESS_2 11:a41267cffa61 490 i2cBuffer[0] = (unsigned char)(eeaddress >> 8); // MSB
POTLESS_2 11:a41267cffa61 491 i2cBuffer[1] = (unsigned char)(eeaddress & 0xFF); // LSB
POTLESS_2 11:a41267cffa61 492
POTLESS_2 11:a41267cffa61 493 int result = I2C_EEPROM.write(address, i2cBuffer, 2, false);
POTLESS_2 11:a41267cffa61 494 wait_ms(6);
POTLESS_2 11:a41267cffa61 495
POTLESS_2 11:a41267cffa61 496 I2C_EEPROM.read(address, data, size);
POTLESS_2 11:a41267cffa61 497 wait_ms(6);
POTLESS_2 11:a41267cffa61 498
POTLESS_2 11:a41267cffa61 499 int result_I = atoi(data);
POTLESS_2 12:9dd7c462a2c0 500 DEEP_DEBUG(" Int lu à l'adresse %d l'eeprom : %d\r\n", eeaddress, result_I);
POTLESS_2 11:a41267cffa61 501 return result_I;
POTLESS_2 11:a41267cffa61 502 }
POTLESS_2 11:a41267cffa61 503
POTLESS_2 11:a41267cffa61 504 void UTILS::clean_EEPROM(int address)
POTLESS_2 11:a41267cffa61 505 {
POTLESS_2 11:a41267cffa61 506 DEBUG(" Nettoyage total de l'Eeprom (64 colonnes, 4096 lignes).\r\n");
POTLESS_2 11:a41267cffa61 507 int i;
POTLESS_2 11:a41267cffa61 508 int pointer;
POTLESS_2 11:a41267cffa61 509 char deleteData[] = {" "};
POTLESS_2 11:a41267cffa61 510 for (i = 0; i < 4096; i++) {
POTLESS_2 11:a41267cffa61 511 pointer = 64 * i;
POTLESS_2 11:a41267cffa61 512 clean_line_EEPROM(pointer, address);
POTLESS_2 11:a41267cffa61 513 }
POTLESS_2 11:a41267cffa61 514 DEBUG("Eeprom néttoyée.\r\n");
POTLESS_2 11:a41267cffa61 515 }