avec dfu

Fork of Utils by POTLESS

Committer:
POTLESS_2
Date:
Tue Feb 13 08:32:46 2018 +0000
Revision:
3:01677db45b2a
Parent:
2:5af7a1fb1903
Child:
4:872676dd87a4
d'autres fonctions sd

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 0:794c15bd30b0 4 SDBlockDevice bd(SD_MOSI, SD_MISO, SD_SCK, SD_CS);
POTLESS_2 2:5af7a1fb1903 5 FATFileSystem Root("Root");
POTLESS_2 0:794c15bd30b0 6
POTLESS_2 3:01677db45b2a 7 int Exist_Val = 0;
POTLESS_2 3:01677db45b2a 8
POTLESS_2 0:794c15bd30b0 9 void return_error(int ret_val)
POTLESS_2 0:794c15bd30b0 10 {
POTLESS_2 3:01677db45b2a 11
POTLESS_2 3:01677db45b2a 12 if (ret_val){
POTLESS_2 0:794c15bd30b0 13 printf(" Problème carte SD = %d\r\n", ret_val);
POTLESS_2 3:01677db45b2a 14 Exist_Val = 0;
POTLESS_2 3:01677db45b2a 15 }else{
POTLESS_2 0:794c15bd30b0 16 printf(" SD -> OK.\r\n");
POTLESS_2 3:01677db45b2a 17 Exist_Val = 1;
POTLESS_2 3:01677db45b2a 18 }
POTLESS_2 0:794c15bd30b0 19 }
POTLESS_2 0:794c15bd30b0 20
POTLESS_2 0:794c15bd30b0 21 void errno_error(void* ret_val)
POTLESS_2 0:794c15bd30b0 22 {
POTLESS_2 3:01677db45b2a 23 if (ret_val == NULL){
POTLESS_2 0:794c15bd30b0 24 printf(" Problème carte SD = %d \r\n", errno);
POTLESS_2 3:01677db45b2a 25 Exist_Val = 0;
POTLESS_2 3:01677db45b2a 26 }else{
POTLESS_2 0:794c15bd30b0 27 printf(" SD -> OK.\r\n");
POTLESS_2 3:01677db45b2a 28 Exist_Val = 1;
POTLESS_2 3:01677db45b2a 29 }
POTLESS_2 0:794c15bd30b0 30 }
POTLESS_2 0:794c15bd30b0 31
POTLESS_2 0:794c15bd30b0 32 void UTILS::Store_A_Val(float Val_To_Store, char* File_Name)
POTLESS_2 0:794c15bd30b0 33 {
POTLESS_2 0:794c15bd30b0 34
POTLESS_2 0:794c15bd30b0 35 char filename[20];
POTLESS_2 2:5af7a1fb1903 36 sprintf(filename, "/Root/%s.txt", File_Name);
POTLESS_2 0:794c15bd30b0 37
POTLESS_2 0:794c15bd30b0 38 FILE* fd = fopen(filename, "w");
POTLESS_2 0:794c15bd30b0 39 errno_error(fd);
POTLESS_2 0:794c15bd30b0 40 fprintf(fd, "%f\r\n", Val_To_Store);
POTLESS_2 0:794c15bd30b0 41 fclose(fd);
POTLESS_2 0:794c15bd30b0 42
POTLESS_2 0:794c15bd30b0 43 printf(" \r\n %s sauvegardée = %f\r\n", filename, Val_To_Store);
POTLESS_2 0:794c15bd30b0 44
POTLESS_2 0:794c15bd30b0 45 }
POTLESS_2 0:794c15bd30b0 46
POTLESS_2 0:794c15bd30b0 47 float UTILS::Read_A_Val(char* File_Name)
POTLESS_2 0:794c15bd30b0 48 {
POTLESS_2 0:794c15bd30b0 49 char buffer[10] = {0};
POTLESS_2 0:794c15bd30b0 50 char c = {0};
POTLESS_2 0:794c15bd30b0 51 char *token;
POTLESS_2 0:794c15bd30b0 52 int i = 0;
POTLESS_2 0:794c15bd30b0 53
POTLESS_2 0:794c15bd30b0 54 char filename[20];
POTLESS_2 2:5af7a1fb1903 55 sprintf(filename, "/Root/%s.txt", File_Name);
POTLESS_2 0:794c15bd30b0 56
POTLESS_2 0:794c15bd30b0 57 printf(" \n Récupération de %s...\r\n", filename);
POTLESS_2 0:794c15bd30b0 58
POTLESS_2 0:794c15bd30b0 59 FILE* fd = fopen(filename, "r");
POTLESS_2 0:794c15bd30b0 60 errno_error(fd);
POTLESS_2 1:a937433a95c8 61
POTLESS_2 0:794c15bd30b0 62 while ((c != '\n') && (i < 10)) {
POTLESS_2 0:794c15bd30b0 63 c = fgetc(fd);
POTLESS_2 0:794c15bd30b0 64 buffer[i] = c;
POTLESS_2 0:794c15bd30b0 65 i++;
POTLESS_2 0:794c15bd30b0 66 }
POTLESS_2 0:794c15bd30b0 67
POTLESS_2 0:794c15bd30b0 68 token = strtok(buffer, "\n");
POTLESS_2 0:794c15bd30b0 69
POTLESS_2 0:794c15bd30b0 70 float val = atof(token);
POTLESS_2 0:794c15bd30b0 71
POTLESS_2 0:794c15bd30b0 72 printf(" Valeur Récupérée = %f\r\n", val);
POTLESS_2 0:794c15bd30b0 73
POTLESS_2 0:794c15bd30b0 74 fclose(fd);
POTLESS_2 0:794c15bd30b0 75
POTLESS_2 0:794c15bd30b0 76 return val;
POTLESS_2 0:794c15bd30b0 77 }
POTLESS_2 0:794c15bd30b0 78
POTLESS_2 0:794c15bd30b0 79
POTLESS_2 2:5af7a1fb1903 80 void UTILS::Write_SD_File(char* To_Store, char* File_Name)
POTLESS_2 0:794c15bd30b0 81 {
POTLESS_2 2:5af7a1fb1903 82 char filename[50];
POTLESS_2 2:5af7a1fb1903 83 sprintf(filename, "/Root/%s", (string)File_Name);
POTLESS_2 2:5af7a1fb1903 84
POTLESS_2 0:794c15bd30b0 85 FILE* fd = fopen(filename, "a");
POTLESS_2 2:5af7a1fb1903 86
POTLESS_2 0:794c15bd30b0 87 errno_error(fd);
POTLESS_2 0:794c15bd30b0 88 fprintf(fd, "%s\r\n", To_Store);
POTLESS_2 0:794c15bd30b0 89 fclose(fd);
POTLESS_2 0:794c15bd30b0 90
POTLESS_2 0:794c15bd30b0 91 //printf(" Sauvegarde OK\r\n\r\n");
POTLESS_2 0:794c15bd30b0 92 }
POTLESS_2 0:794c15bd30b0 93
POTLESS_2 2:5af7a1fb1903 94 void UTILS::Read_SD_File(char* File_Name)
POTLESS_2 0:794c15bd30b0 95 {
POTLESS_2 0:794c15bd30b0 96 char filename[20];
POTLESS_2 2:5af7a1fb1903 97 sprintf(filename, "/Root/%s", File_Name);
POTLESS_2 2:5af7a1fb1903 98
POTLESS_2 0:794c15bd30b0 99 FILE* fd = fopen(filename, "r");
POTLESS_2 0:794c15bd30b0 100 errno_error(fd);
POTLESS_2 0:794c15bd30b0 101
POTLESS_2 3:01677db45b2a 102 printf(" Contenu du fichier :\r\n\r\n");
POTLESS_2 0:794c15bd30b0 103 char buff[16] = {0};
POTLESS_2 0:794c15bd30b0 104 while (!feof(fd)) {
POTLESS_2 0:794c15bd30b0 105 int size = fread(&buff[0], 1, 15, fd);
POTLESS_2 0:794c15bd30b0 106 fwrite(&buff[0], 1, size, stdout);
POTLESS_2 0:794c15bd30b0 107 }
POTLESS_2 3:01677db45b2a 108 printf("\r\n Fin du fichier.\r\n");
POTLESS_2 0:794c15bd30b0 109 fclose(fd);
POTLESS_2 0:794c15bd30b0 110 }
POTLESS_2 0:794c15bd30b0 111
POTLESS_2 3:01677db45b2a 112 bool UTILS::File_Exist(char* File_Name)
POTLESS_2 3:01677db45b2a 113 {
POTLESS_2 3:01677db45b2a 114 char filename[20];
POTLESS_2 3:01677db45b2a 115 sprintf(filename, "/Root/%s.txt", File_Name);
POTLESS_2 3:01677db45b2a 116
POTLESS_2 3:01677db45b2a 117 FILE* fd = fopen(filename, "r");
POTLESS_2 3:01677db45b2a 118 errno_error(fd);
POTLESS_2 3:01677db45b2a 119
POTLESS_2 3:01677db45b2a 120 if (Exist_Val == 0){
POTLESS_2 3:01677db45b2a 121 printf(" Le fichier recherché n'existe pas....\r\n");
POTLESS_2 3:01677db45b2a 122 return false;
POTLESS_2 3:01677db45b2a 123 }else{
POTLESS_2 3:01677db45b2a 124 fclose(fd);
POTLESS_2 3:01677db45b2a 125 return true;
POTLESS_2 3:01677db45b2a 126 }
POTLESS_2 3:01677db45b2a 127 }
POTLESS_2 3:01677db45b2a 128
POTLESS_2 2:5af7a1fb1903 129 void UTILS::Delete_SD_File(char* File_Name)
POTLESS_2 0:794c15bd30b0 130 {
POTLESS_2 0:794c15bd30b0 131 char filename[20];
POTLESS_2 2:5af7a1fb1903 132 sprintf(filename, "/Root/%s", File_Name);
POTLESS_2 0:794c15bd30b0 133 int error = 0;
POTLESS_2 2:5af7a1fb1903 134 error = Root.remove(filename);
POTLESS_2 0:794c15bd30b0 135 return_error(error);
POTLESS_2 0:794c15bd30b0 136
POTLESS_2 0:794c15bd30b0 137 printf("Fichier effacé.\n");
POTLESS_2 0:794c15bd30b0 138 }
POTLESS_2 0:794c15bd30b0 139
POTLESS_2 0:794c15bd30b0 140 void UTILS::Rename_SD_File(string Old_File_Name, string New_File_Name)
POTLESS_2 0:794c15bd30b0 141 {
POTLESS_2 0:794c15bd30b0 142 char Oldfilename[20];
POTLESS_2 2:5af7a1fb1903 143 sprintf(Oldfilename, "/Root/%s", Old_File_Name);
POTLESS_2 0:794c15bd30b0 144 char Newfilename[20];
POTLESS_2 2:5af7a1fb1903 145 sprintf(Newfilename, "/Root/%s", New_File_Name);
POTLESS_2 0:794c15bd30b0 146
POTLESS_2 0:794c15bd30b0 147 int error = 0;
POTLESS_2 2:5af7a1fb1903 148 error = Root.rename(Oldfilename, Newfilename);
POTLESS_2 0:794c15bd30b0 149 return_error(error);
POTLESS_2 0:794c15bd30b0 150
POTLESS_2 0:794c15bd30b0 151 printf("Fichier renommé.\n");
POTLESS_2 0:794c15bd30b0 152 }
POTLESS_2 0:794c15bd30b0 153
POTLESS_2 0:794c15bd30b0 154 void UTILS::Mount_SD()
POTLESS_2 0:794c15bd30b0 155 {
POTLESS_2 0:794c15bd30b0 156 //Montage carte SD
POTLESS_2 2:5af7a1fb1903 157 printf(" Montage carte SD \"/Root\". \r\n\r\n");
POTLESS_2 0:794c15bd30b0 158 int error = 0;
POTLESS_2 2:5af7a1fb1903 159 error = Root.mount(&bd);
POTLESS_2 0:794c15bd30b0 160 return_error(error);
POTLESS_2 1:a937433a95c8 161 if (error > 0) {
POTLESS_2 0:794c15bd30b0 162 //On re format s'il n'y a as de file system...normalement une seul fois...
POTLESS_2 0:794c15bd30b0 163 printf("Pas de File system, on format... ");
POTLESS_2 0:794c15bd30b0 164 Format_SD();
POTLESS_2 0:794c15bd30b0 165 }
POTLESS_2 0:794c15bd30b0 166 }
POTLESS_2 0:794c15bd30b0 167
POTLESS_2 0:794c15bd30b0 168 void UTILS::UnMount_SD()
POTLESS_2 0:794c15bd30b0 169 {
POTLESS_2 0:794c15bd30b0 170 //Montage carte SD
POTLESS_2 2:5af7a1fb1903 171 printf(" Demontage carte SD \"/Root\". \r\n\r\n");
POTLESS_2 0:794c15bd30b0 172 int error = 0;
POTLESS_2 2:5af7a1fb1903 173 error = Root.unmount();
POTLESS_2 0:794c15bd30b0 174 return_error(error);
POTLESS_2 0:794c15bd30b0 175 }
POTLESS_2 0:794c15bd30b0 176
POTLESS_2 0:794c15bd30b0 177 void UTILS::Format_SD()
POTLESS_2 0:794c15bd30b0 178 {
POTLESS_2 0:794c15bd30b0 179 //Formatage carte SD
POTLESS_2 0:794c15bd30b0 180 printf(" Formatage carte SD\r\n\r\n");
POTLESS_2 0:794c15bd30b0 181 int error = 0;
POTLESS_2 2:5af7a1fb1903 182 error = Root.format(&bd);
POTLESS_2 2:5af7a1fb1903 183 return_error(error);
POTLESS_2 2:5af7a1fb1903 184 }
POTLESS_2 2:5af7a1fb1903 185
POTLESS_2 3:01677db45b2a 186 void UTILS::Dir_SD(char* Dir_Name)
POTLESS_2 2:5af7a1fb1903 187 {
POTLESS_2 2:5af7a1fb1903 188 int error = 0;
POTLESS_2 3:01677db45b2a 189 int nb = 0;
POTLESS_2 3:01677db45b2a 190 printf("\r\n Ouverture du répertoire %s\r\n", Dir_Name);
POTLESS_2 2:5af7a1fb1903 191 char Dirname[20];
POTLESS_2 2:5af7a1fb1903 192 sprintf(Dirname, "/Root/%s", Dir_Name);
POTLESS_2 2:5af7a1fb1903 193
POTLESS_2 2:5af7a1fb1903 194 DIR* dir = opendir(Dirname);
POTLESS_2 2:5af7a1fb1903 195 //errno_error(fd);
POTLESS_2 2:5af7a1fb1903 196
POTLESS_2 2:5af7a1fb1903 197 struct dirent* de;
POTLESS_2 3:01677db45b2a 198 printf(" Fichier du répertoire :\r\n\r\n");
POTLESS_2 2:5af7a1fb1903 199 while((de = readdir(dir)) != NULL)
POTLESS_2 2:5af7a1fb1903 200 {
POTLESS_2 2:5af7a1fb1903 201 printf(" %s\r\n", &(de->d_name)[0]);
POTLESS_2 3:01677db45b2a 202 nb++;
POTLESS_2 2:5af7a1fb1903 203 //wait_ms(100);
POTLESS_2 2:5af7a1fb1903 204 }
POTLESS_2 3:01677db45b2a 205 printf("\r\n Nombre de fichiers = %d\r\n", nb);
POTLESS_2 3:01677db45b2a 206 printf(" Fermeture du répertoire.\r\n");
POTLESS_2 2:5af7a1fb1903 207 error = closedir(dir);
POTLESS_2 0:794c15bd30b0 208 return_error(error);
POTLESS_2 0:794c15bd30b0 209 }
POTLESS_2 0:794c15bd30b0 210
POTLESS_2 3:01677db45b2a 211 int UTILS::File_Index()
POTLESS_2 3:01677db45b2a 212 {
POTLESS_2 3:01677db45b2a 213 int error = 0;
POTLESS_2 3:01677db45b2a 214 int nb = 0;
POTLESS_2 3:01677db45b2a 215
POTLESS_2 3:01677db45b2a 216 DIR* dir = opendir("/Root/");
POTLESS_2 3:01677db45b2a 217 struct dirent* de;
POTLESS_2 3:01677db45b2a 218 while((de = readdir(dir)) != NULL)
POTLESS_2 3:01677db45b2a 219 {
POTLESS_2 3:01677db45b2a 220 nb++;
POTLESS_2 3:01677db45b2a 221 }
POTLESS_2 3:01677db45b2a 222 error = closedir(dir);
POTLESS_2 3:01677db45b2a 223 return_error(error);
POTLESS_2 3:01677db45b2a 224
POTLESS_2 3:01677db45b2a 225 return nb - 2;
POTLESS_2 3:01677db45b2a 226 }
POTLESS_2 3:01677db45b2a 227
POTLESS_2 0:794c15bd30b0 228 //Remapping d'une valeur dans une autre échelle
POTLESS_2 0:794c15bd30b0 229 float UTILS::Remap(float x, float in_min, float in_max, float out_min, float out_max)
POTLESS_2 0:794c15bd30b0 230 {
POTLESS_2 0:794c15bd30b0 231 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
POTLESS_2 0:794c15bd30b0 232 }
POTLESS_2 0:794c15bd30b0 233
POTLESS_2 0:794c15bd30b0 234 //Contraint une valeur entre deux limites
POTLESS_2 0:794c15bd30b0 235 float UTILS::constrain(float x, float a, float b)
POTLESS_2 0:794c15bd30b0 236 {
POTLESS_2 0:794c15bd30b0 237 if(x < a) {
POTLESS_2 0:794c15bd30b0 238 return a;
POTLESS_2 0:794c15bd30b0 239 } else if(b < x) {
POTLESS_2 0:794c15bd30b0 240 return b;
POTLESS_2 0:794c15bd30b0 241 } else
POTLESS_2 0:794c15bd30b0 242 return x;
POTLESS_2 0:794c15bd30b0 243 }