avec dfu

Fork of Utils by POTLESS

Revision:
0:794c15bd30b0
Child:
1:a937433a95c8
diff -r 000000000000 -r 794c15bd30b0 Utils.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Utils.cpp	Tue Feb 06 13:15:53 2018 +0000
@@ -0,0 +1,173 @@
+#include "mbed.h"
+#include "Utils.h"
+
+SDBlockDevice bd(SD_MOSI, SD_MISO, SD_SCK, SD_CS);
+FATFileSystem fs("fs");
+
+void return_error(int ret_val)
+{
+    if (ret_val)
+        printf("  Problème carte SD = %d\r\n", ret_val);
+    else
+        printf("  SD -> OK.\r\n");
+}
+
+void errno_error(void* ret_val)
+{
+    if (ret_val == NULL)
+        printf("  Problème carte SD = %d \r\n", errno);
+    else
+        printf("  SD -> OK.\r\n");
+}
+
+void UTILS::Store_A_Val(float Val_To_Store, char* File_Name)
+{
+
+    char filename[20];
+    sprintf(filename, "/fs/%s.txt", File_Name);
+
+    FILE* fd = fopen(filename, "w");
+    errno_error(fd);
+    fprintf(fd, "%f\r\n", Val_To_Store);
+    fclose(fd);
+
+    printf("  \r\n  %s sauvegardée = %f\r\n", filename, Val_To_Store);
+
+}
+
+float UTILS::Read_A_Val(char* File_Name)
+{
+    char buffer[10] = {0};
+    char c = {0};
+    char *token;
+    int i = 0;
+
+    char filename[20];
+    sprintf(filename, "/fs/%s.txt", File_Name);
+
+    printf("  \n  Récupération de %s...\r\n", filename);
+
+    FILE* fd = fopen(filename, "r");
+    errno_error(fd);
+
+    while ((c != '\n') && (i < 10)) {
+        c = fgetc(fd);
+        buffer[i] = c;
+        i++;
+    }
+
+    token = strtok(buffer, "\n");
+
+    float val = atof(token);
+
+    printf("  Valeur Récupérée = %f\r\n", val);
+
+    fclose(fd);
+
+    return val;
+}
+
+
+void UTILS::Write_SD_File(string To_Store, char* File_Name)
+{
+    char filename[20];
+    sprintf(filename, "/fs/%s", File_Name);
+
+    FILE* fd = fopen(filename, "a");
+    errno_error(fd);
+    fprintf(fd, "%s\r\n", To_Store);
+    fclose(fd);
+
+    //printf("  Sauvegarde OK\r\n\r\n");
+}
+
+void UTILS::Read_SD_File(string File_Name)
+{
+    char filename[20];
+    sprintf(filename, "/fs/%s", File_Name);
+    FILE* fd = fopen(filename, "r");
+    errno_error(fd);
+
+    printf("Contenu du fichier :\r\n\r\n");
+    char buff[16] = {0};
+    while (!feof(fd)) {
+        int size = fread(&buff[0], 1, 15, fd);
+        fwrite(&buff[0], 1, size, stdout);
+    }
+    printf("Fin du fichier.\n");
+    fclose(fd);
+}
+
+void UTILS::Delete_SD_File(string File_Name)
+{
+    char filename[20];
+    sprintf(filename, "/fs/%s", File_Name);
+    int error = 0;
+    error = fs.remove(filename);
+    return_error(error);
+
+    printf("Fichier effacé.\n");
+}
+
+void UTILS::Rename_SD_File(string Old_File_Name, string New_File_Name)
+{
+    char Oldfilename[20];
+    sprintf(Oldfilename, "/fs/%s", Old_File_Name);
+    char Newfilename[20];
+    sprintf(Newfilename, "/fs/%s", New_File_Name);
+
+    int error = 0;
+    error = fs.rename(Oldfilename, Newfilename);
+    return_error(error);
+
+    printf("Fichier renommé.\n");
+}
+
+void UTILS::Mount_SD()
+{
+    //Montage carte SD
+    printf("  Montage carte SD \"/fs\". \r\n\r\n");
+    int error = 0;
+    error = fs.mount(&bd);
+    return_error(error);
+    if (error) {
+        //On re format s'il n'y a as de file system...normalement une seul fois...
+        printf("Pas de File system, on format... ");
+        Format_SD();
+    }
+}
+
+void UTILS::UnMount_SD()
+{
+    //Montage carte SD
+    printf("  Demontage carte SD \"/fs\". \r\n\r\n");
+    int error = 0;
+    error = fs.unmount();
+    return_error(error);
+}
+
+void UTILS::Format_SD()
+{
+    //Formatage carte SD
+    printf("  Formatage carte SD\r\n\r\n");
+    int error = 0;
+    error = fs.format(&bd);
+    return_error(error);
+}
+
+//Remapping d'une valeur dans une autre échelle
+float UTILS::Remap(float x, float in_min, float in_max, float out_min, float out_max)
+{
+    return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
+}
+
+//Contraint une valeur entre deux limites
+float UTILS::constrain(float x, float a, float b)
+{
+    if(x < a) {
+        return a;
+    } else if(b < x) {
+        return b;
+    } else
+        return x;
+}
\ No newline at end of file