avec dfu
Fork of Utils by
Utils.cpp
- Committer:
- POTLESS_2
- Date:
- 2018-02-13
- Revision:
- 5:0f3b6e6677ef
- Parent:
- 4:872676dd87a4
- Child:
- 6:8bae97034114
File content as of revision 5:0f3b6e6677ef:
#include "mbed.h" #include "Utils.h" SDBlockDevice bd(SD_MOSI, SD_MISO, SD_SCK, SD_CS); FATFileSystem Root("Root"); int Exist_Val = 0; void return_error(int ret_val) { Exist_Val = 0; if (ret_val){ printf(" Problème carte SD = %d\r\n", ret_val); Exist_Val = 0; }else{ printf(" SD -> OK.\r\n"); Exist_Val = 1; } } void errno_error(void* ret_val) { Exist_Val = 0; if (ret_val == NULL){ printf(" Problème carte SD = %d \r\n", errno); Exist_Val = 0; }else{ printf(" SD -> OK.\r\n"); Exist_Val = 1; } } void UTILS::Store_A_Val(float Val_To_Store, char* File_Name) { char filename[20]; sprintf(filename, "/Root/%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, "/Root/%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(char* To_Store, char* File_Name) { char filename[50]; sprintf(filename, "/Root/%s", (string)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(char* File_Name) { char filename[20]; sprintf(filename, "/Root/%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("\r\n Fin du fichier.\r\n"); fclose(fd); } bool UTILS::File_Exist(string File_Name) { char filename[20]; sprintf(filename, "/Root/%s.txt", File_Name); FILE* fd = fopen(filename, "r"); errno_error(fd); if (Exist_Val == 0){ printf(" Le fichier %s n'existe pas....\r\n", filename); fclose(fd); return false; } if (Exist_Val == 1){ fclose(fd); return true; } } void UTILS::Delete_SD_File(char* File_Name) { char filename[20]; sprintf(filename, "/Root/%s", File_Name); int error = 0; error = Root.remove(filename); return_error(error); printf("Fichier effacé.\n"); } void UTILS::Rename_SD_File(char* Old_File_Name, char* New_File_Name) { char Oldfilename[20]; sprintf(Oldfilename, "/Root/%s", Old_File_Name); char Newfilename[20]; sprintf(Newfilename, "/Root/%s", New_File_Name); int error = 0; error = Root.rename(Oldfilename, Newfilename); return_error(error); printf("Fichier renommé.\n"); } void UTILS::Mount_SD() { //Montage carte SD printf(" Montage carte SD \"/Root\". \r\n\r\n"); int error = 0; error = Root.mount(&bd); return_error(error); if (error > 0) { //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 \"/Root\". \r\n\r\n"); int error = 0; error = Root.unmount(); return_error(error); } void UTILS::Format_SD() { //Formatage carte SD printf(" Formatage carte SD\r\n\r\n"); int error = 0; error = FATFileSystem::format(&bd); return_error(error); } void UTILS::Dir_SD(char* Dir_Name) { int error = 0; int nb = 0; printf("\r\n Ouverture du répertoire %s\r\n", Dir_Name); char Dirname[20]; sprintf(Dirname, "/Root/%s", Dir_Name); DIR* dir = opendir(Dirname); //errno_error(fd); struct dirent* de; printf(" Fichier du répertoire :\r\n\r\n"); while((de = readdir(dir)) != NULL) { printf(" %s\r\n", &(de->d_name)[0]); nb++; //wait_ms(100); } printf("\r\n Nombre de fichiers = %d\r\n", nb); printf(" Fermeture du répertoire.\r\n"); //error = closedir(dir); return_error(error); } int UTILS::File_Index() { int error = 0; int nb = 0; DIR* dir = opendir("/Root/"); struct dirent* de; while((de = readdir(dir)) != NULL) { nb++; } error = closedir(dir); return_error(error); return nb - 2; } //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; }