avec dfu

Fork of Utils by POTLESS

Committer:
POTLESS_2
Date:
Thu Feb 08 12:14:52 2018 +0000
Revision:
1:a937433a95c8
Parent:
0:794c15bd30b0
Child:
2:5af7a1fb1903
mod;

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 0:794c15bd30b0 5 FATFileSystem fs("fs");
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 0:794c15bd30b0 27 sprintf(filename, "/fs/%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 0:794c15bd30b0 46 sprintf(filename, "/fs/%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 0:794c15bd30b0 71 void UTILS::Write_SD_File(string To_Store, char* File_Name)
POTLESS_2 0:794c15bd30b0 72 {
POTLESS_2 0:794c15bd30b0 73 char filename[20];
POTLESS_2 0:794c15bd30b0 74 sprintf(filename, "/fs/%s", File_Name);
POTLESS_2 0:794c15bd30b0 75
POTLESS_2 0:794c15bd30b0 76 FILE* fd = fopen(filename, "a");
POTLESS_2 0:794c15bd30b0 77 errno_error(fd);
POTLESS_2 0:794c15bd30b0 78 fprintf(fd, "%s\r\n", To_Store);
POTLESS_2 0:794c15bd30b0 79 fclose(fd);
POTLESS_2 0:794c15bd30b0 80
POTLESS_2 0:794c15bd30b0 81 //printf(" Sauvegarde OK\r\n\r\n");
POTLESS_2 0:794c15bd30b0 82 }
POTLESS_2 0:794c15bd30b0 83
POTLESS_2 0:794c15bd30b0 84 void UTILS::Read_SD_File(string File_Name)
POTLESS_2 0:794c15bd30b0 85 {
POTLESS_2 0:794c15bd30b0 86 char filename[20];
POTLESS_2 0:794c15bd30b0 87 sprintf(filename, "/fs/%s", File_Name);
POTLESS_2 0:794c15bd30b0 88 FILE* fd = fopen(filename, "r");
POTLESS_2 0:794c15bd30b0 89 errno_error(fd);
POTLESS_2 0:794c15bd30b0 90
POTLESS_2 0:794c15bd30b0 91 printf("Contenu du fichier :\r\n\r\n");
POTLESS_2 0:794c15bd30b0 92 char buff[16] = {0};
POTLESS_2 0:794c15bd30b0 93 while (!feof(fd)) {
POTLESS_2 0:794c15bd30b0 94 int size = fread(&buff[0], 1, 15, fd);
POTLESS_2 0:794c15bd30b0 95 fwrite(&buff[0], 1, size, stdout);
POTLESS_2 0:794c15bd30b0 96 }
POTLESS_2 0:794c15bd30b0 97 printf("Fin du fichier.\n");
POTLESS_2 0:794c15bd30b0 98 fclose(fd);
POTLESS_2 0:794c15bd30b0 99 }
POTLESS_2 0:794c15bd30b0 100
POTLESS_2 0:794c15bd30b0 101 void UTILS::Delete_SD_File(string File_Name)
POTLESS_2 0:794c15bd30b0 102 {
POTLESS_2 0:794c15bd30b0 103 char filename[20];
POTLESS_2 0:794c15bd30b0 104 sprintf(filename, "/fs/%s", File_Name);
POTLESS_2 0:794c15bd30b0 105 int error = 0;
POTLESS_2 0:794c15bd30b0 106 error = fs.remove(filename);
POTLESS_2 0:794c15bd30b0 107 return_error(error);
POTLESS_2 0:794c15bd30b0 108
POTLESS_2 0:794c15bd30b0 109 printf("Fichier effacé.\n");
POTLESS_2 0:794c15bd30b0 110 }
POTLESS_2 0:794c15bd30b0 111
POTLESS_2 0:794c15bd30b0 112 void UTILS::Rename_SD_File(string Old_File_Name, string New_File_Name)
POTLESS_2 0:794c15bd30b0 113 {
POTLESS_2 0:794c15bd30b0 114 char Oldfilename[20];
POTLESS_2 0:794c15bd30b0 115 sprintf(Oldfilename, "/fs/%s", Old_File_Name);
POTLESS_2 0:794c15bd30b0 116 char Newfilename[20];
POTLESS_2 0:794c15bd30b0 117 sprintf(Newfilename, "/fs/%s", New_File_Name);
POTLESS_2 0:794c15bd30b0 118
POTLESS_2 0:794c15bd30b0 119 int error = 0;
POTLESS_2 0:794c15bd30b0 120 error = fs.rename(Oldfilename, Newfilename);
POTLESS_2 0:794c15bd30b0 121 return_error(error);
POTLESS_2 0:794c15bd30b0 122
POTLESS_2 0:794c15bd30b0 123 printf("Fichier renommé.\n");
POTLESS_2 0:794c15bd30b0 124 }
POTLESS_2 0:794c15bd30b0 125
POTLESS_2 0:794c15bd30b0 126 void UTILS::Mount_SD()
POTLESS_2 0:794c15bd30b0 127 {
POTLESS_2 0:794c15bd30b0 128 //Montage carte SD
POTLESS_2 0:794c15bd30b0 129 printf(" Montage carte SD \"/fs\". \r\n\r\n");
POTLESS_2 0:794c15bd30b0 130 int error = 0;
POTLESS_2 0:794c15bd30b0 131 error = fs.mount(&bd);
POTLESS_2 0:794c15bd30b0 132 return_error(error);
POTLESS_2 1:a937433a95c8 133 if (error > 0) {
POTLESS_2 0:794c15bd30b0 134 //On re format s'il n'y a as de file system...normalement une seul fois...
POTLESS_2 0:794c15bd30b0 135 printf("Pas de File system, on format... ");
POTLESS_2 0:794c15bd30b0 136 Format_SD();
POTLESS_2 0:794c15bd30b0 137 }
POTLESS_2 0:794c15bd30b0 138 }
POTLESS_2 0:794c15bd30b0 139
POTLESS_2 0:794c15bd30b0 140 void UTILS::UnMount_SD()
POTLESS_2 0:794c15bd30b0 141 {
POTLESS_2 0:794c15bd30b0 142 //Montage carte SD
POTLESS_2 0:794c15bd30b0 143 printf(" Demontage carte SD \"/fs\". \r\n\r\n");
POTLESS_2 0:794c15bd30b0 144 int error = 0;
POTLESS_2 0:794c15bd30b0 145 error = fs.unmount();
POTLESS_2 0:794c15bd30b0 146 return_error(error);
POTLESS_2 0:794c15bd30b0 147 }
POTLESS_2 0:794c15bd30b0 148
POTLESS_2 0:794c15bd30b0 149 void UTILS::Format_SD()
POTLESS_2 0:794c15bd30b0 150 {
POTLESS_2 0:794c15bd30b0 151 //Formatage carte SD
POTLESS_2 0:794c15bd30b0 152 printf(" Formatage carte SD\r\n\r\n");
POTLESS_2 0:794c15bd30b0 153 int error = 0;
POTLESS_2 0:794c15bd30b0 154 error = fs.format(&bd);
POTLESS_2 0:794c15bd30b0 155 return_error(error);
POTLESS_2 0:794c15bd30b0 156 }
POTLESS_2 0:794c15bd30b0 157
POTLESS_2 0:794c15bd30b0 158 //Remapping d'une valeur dans une autre échelle
POTLESS_2 0:794c15bd30b0 159 float UTILS::Remap(float x, float in_min, float in_max, float out_min, float out_max)
POTLESS_2 0:794c15bd30b0 160 {
POTLESS_2 0:794c15bd30b0 161 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
POTLESS_2 0:794c15bd30b0 162 }
POTLESS_2 0:794c15bd30b0 163
POTLESS_2 0:794c15bd30b0 164 //Contraint une valeur entre deux limites
POTLESS_2 0:794c15bd30b0 165 float UTILS::constrain(float x, float a, float b)
POTLESS_2 0:794c15bd30b0 166 {
POTLESS_2 0:794c15bd30b0 167 if(x < a) {
POTLESS_2 0:794c15bd30b0 168 return a;
POTLESS_2 0:794c15bd30b0 169 } else if(b < x) {
POTLESS_2 0:794c15bd30b0 170 return b;
POTLESS_2 0:794c15bd30b0 171 } else
POTLESS_2 0:794c15bd30b0 172 return x;
POTLESS_2 0:794c15bd30b0 173 }