some cleaning
[protos/imu.git] / sd_log.c
index 4659936..97496f4 100644 (file)
--- a/sd_log.c
+++ b/sd_log.c
@@ -12,7 +12,7 @@
 
 static struct fat_file_struct *log_fd = NULL;
 
-uint8_t find_file_in_dir(struct fat_fs_struct* fs,
+static uint8_t find_file_in_dir(struct fat_fs_struct* fs,
        struct fat_dir_struct* dd, const char* name,
        struct fat_dir_entry_struct* dir_entry)
 {
@@ -28,7 +28,7 @@ uint8_t find_file_in_dir(struct fat_fs_struct* fs,
        return 0;
 }
 
-struct fat_file_struct *open_file_in_dir(struct fat_fs_struct* fs,
+static struct fat_file_struct *open_file_in_dir(struct fat_fs_struct* fs,
        struct fat_dir_struct* dd, const char* name)
 {
        struct fat_dir_entry_struct file_entry;
@@ -39,7 +39,8 @@ struct fat_file_struct *open_file_in_dir(struct fat_fs_struct* fs,
        return fat_open_file(fs, &file_entry);
 }
 
-struct fat_file_struct *open_log_file(void)
+/* open the log file on the SD card */
+int8_t sd_log_open(void)
 {
        struct fat_file_struct *fd;
        struct fat_fs_struct *fs;
@@ -55,7 +56,7 @@ struct fat_file_struct *open_log_file(void)
 #if SD_DEBUG
                printf_P(PSTR("MMC/SD initialization failed\n"));
 #endif
-               return NULL;
+               return -1;
        }
 
        /* open first partition */
@@ -86,7 +87,7 @@ struct fat_file_struct *open_log_file(void)
 #if SD_DEBUG
                        printf_P(PSTR("opening partition failed\n"));
 #endif
-                       return NULL;
+                       return -1;
                }
        }
 
@@ -96,7 +97,7 @@ struct fat_file_struct *open_log_file(void)
 #if SD_DEBUG
                printf_P(PSTR("opening filesystem failed\n"));
 #endif
-               return NULL;
+               return -1;
        }
 
        /* open root directory */
@@ -106,12 +107,9 @@ struct fat_file_struct *open_log_file(void)
 #if SD_DEBUG
                printf_P(PSTR("opening root directory failed\n"));
 #endif
-               return NULL;
+               return -1;
        }
 
-       /* print some card information as a boot message */
-       //print_disk_info(fs);
-
        printf("choose log file name\n");
        while (1) {
                snprintf(name, sizeof(name), "log%.4d", i++);
@@ -127,13 +125,22 @@ struct fat_file_struct *open_log_file(void)
        fd = open_file_in_dir(fs, dd, name);
        if (!fd) {
                printf_P(PSTR("error opening "));
-               return NULL;
+               return -1;
        }
 
-       return fd;
+       return 0;
+}
+
+/* log output */
+intptr_t sd_log_write(const void *buffer, uintptr_t buffer_len)
+{
+       if (log_fd == NULL)
+               return -1;
+
+       return fat_write_file(log_fd, buffer, buffer_len);
 }
 
-struct fat_file_struct *get_log_file(void)
+uint8_t sd_log_enabled(void)
 {
-       return log_fd;
+       return log_fd != NULL;
 }