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