avec dfu

Fork of Utils by POTLESS

Committer:
POTLESS_2
Date:
Mon Feb 12 07:22:14 2018 +0000
Revision:
2:5af7a1fb1903
Parent:
1:a937433a95c8
Child:
3:01677db45b2a
Fonction Dir

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