Daniel Levine / CircularBufferSDCardLib

Dependencies:   sd-driver_compatible_with_MAX32630FTHR

Fork of CircularBufferSDCardLib by Daniel Levine

Files at this revision

API Documentation at this revision

Comitter:
DVLevine
Date:
Thu Apr 05 01:22:57 2018 -0400
Parent:
3:df8fb1b5d868
Child:
5:0a4ff027086c
Commit message:
Finished fixing bug pieces but no print statement.....

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
sdCardReader.cpp Show annotated file Show diff for this revision Revisions of this file
sdCardReader.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Apr 04 17:15:36 2018 -0400
+++ b/main.cpp	Thu Apr 05 01:22:57 2018 -0400
@@ -11,7 +11,9 @@
 // main() runs in its own thread in the OS
 int main() {
     //create value (16 bit like prox reading)
-     
+  
+  printf("We are in! \n");
+  
      uint16_t a = 123;
      uint16_t b = 2034;
      uint16_t c = 1040;
@@ -36,11 +38,14 @@
     boba->logTimeStamp(d);
     boba->logTimeStamp(e);
     
-    
+
+    printf("about to save\n");
     //printf("ERE IS %i \n",ere);
 
-    boba->saveLoggedDataAndClearBuffer("SUPERDATAYO.txt"); 
-     
+    boba->saveLoggedDataAndClearBuffer("/fs/SUPERDATAYO.txt"); 
+
+    printf("saved\n");
+    
     led1 = !led1;
      
 }
--- a/sdCardReader.cpp	Wed Apr 04 17:15:36 2018 -0400
+++ b/sdCardReader.cpp	Thu Apr 05 01:22:57 2018 -0400
@@ -1,19 +1,28 @@
 #include "sdCardReader.h"
 
 
+//helpers
+int getSize(int* p){
+  return (sizeof(p)/sizeof(*p));
+}
+
+int getSize(uint16_t* p){
+  return (sizeof(p)/sizeof(*p));
+}
+
 //private essential methods
 // Try to mount the filesystem
 void SDCardReader::mountFileSystem(){
     printf("Mounting the filesystem... ");
     fflush(stdout);
-    int err = m_fs->mount(&this);
+    int err = m_fs->mount(this);
     printf("%s\n", (err ? "Fail :(" : "OK"));
     if (err) {
         // Reformat if we can't mount the filesystem
         // this should only happen on the first boot
         printf("No filesystem found, formatting... ");
         fflush(stdout);
-        err = m_fs->reformat(&this);
+        err = m_fs->reformat(this);
         printf("%s\n", (err ? "Fail :(" : "OK"));
         if (err) {
             error("error: %s (%d)\n", strerror(-err), err);
@@ -24,7 +33,7 @@
 void SDCardReader::unmountFileSystem(){
   printf("Unmounting... ");
   fflush(stdout);
-  err = m_fs->unmount();
+  int err = m_fs->unmount();
   printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
   if (err < 0) {
     error("error: %s (%d)\n", strerror(-err), err);
@@ -33,15 +42,15 @@
 
 
 FILE* SDCardReader::openFile(string filename){
-  printf("Opening \"/fs/%s\"... ",filename);
+  printf("Opening \"/fs/%s\"... ",filename.c_str());
   fflush(stdout);
-  FILE *f = fopen("/fs/"+filename, "r+");
+  FILE *f = fopen(filename.c_str(), "r+");
   printf("%s\n", (!f ? "Fail :(" : "OK"));
   if (!f) {
     // Create the file if it doesn't exist
     printf("No file found, creating a new file... ");
     fflush(stdout);
-    f = fopen("/fs/"+filename, "w+");
+    f = fopen(filename.c_str(), "w+");
     printf("%s\n", (!f ? "Fail :(" : "OK"));
     if (!f) {
       error("error: %s (%d)\n", strerror(errno), -errno);
@@ -54,14 +63,15 @@
   FILE *f = fileToUse;
   printf("%s\n", (!f ? "Fail :(" : "OK"));
   
-  printf("\rWriting datapoint %d", data);
+  printf("\rWriting 16 bit value: %" PRIu16, data);
   fflush(stdout);
 
+  int err = 0;
   //write and check for error
   if (endline){
-    err = fprintf(f, "%d\n",);
+    err = fprintf(f,"%" PRIu16 "\n",data);
   }else{
-    err = fprintf(f, "%d",);
+    err = fprintf(f,"%" PRIu16,data);
   }
 
   if (err < 0) {
@@ -74,15 +84,17 @@
 void SDCardReader::write_uint32_t(uint32_t data, bool endline, FILE* fileToUse){
   FILE *f = fileToUse;
   printf("%s\n", (!f ? "Fail :(" : "OK"));
-  
-  printf("\rWriting datapoint %d", data);
+
+  printf("\rWriting 32 bit value:  %" PRIu32, data);
+  //printf("\rWriting datapoint %d", data);
   fflush(stdout);
 
+  int err = 0;
   //write and check for error
   if (endline){
-    err = fprintf(f, "%d\n",);
+    err = fprintf(f,"%" PRIu32 "\n",data);
   }else{
-    err = fprintf(f, "%d ",);
+    err = fprintf(f,"%" PRIu32,data);
   }
   
   if (err < 0) {
@@ -96,6 +108,8 @@
   // Close the file which also flushes any cached writes
   printf("Closing file...");
   fflush(stdout);
+
+  int err = 0;
   err = fclose(fileToClose);
   printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
   if (err < 0) {
@@ -103,12 +117,11 @@
   } 
 }
 
-
 int SDCardReader::fullWriteProcedure(string filename,int* indexArr, uint32_t* timeArr, uint16_t** allData){
   mountFileSystem();
   FILE* theFile = openFile(filename);
 
-  for (int i = 0; i < indexArr.size; i++){
+  for (int i = 0; i<getSize(indexArr); i++){
     writeDataPoint(theFile,indexArr[i],timeArr[i], allData[i]);
   }
   
@@ -130,10 +143,10 @@
   write_uint32_t(timestamp, false, theFile);
 
   //write data
-  for (int i = 0; i < data.size-1; i++) {
+  for (int i = 0; i < getSize(data)-1; i++) {
     write_uint16_t(data[i], false, theFile);
   }
-  write_uint16_t(data[data.size], false, theFile);
+  write_uint16_t(data[getSize(data)], false, theFile);
   
   closeFile(theFile);
   
@@ -146,7 +159,7 @@
 int SDCardReader::eraseData(){
     printf("Initializing the block device... ");
     fflush(stdout);
-    int err = this.init();
+    int err = this->init();
     printf("%s\n", (err ? "Fail :(" : "OK"));
     if (err) {
         error("error: %s (%d)\n", strerror(-err), err);
@@ -154,7 +167,7 @@
 
     printf("Erasing the block device... ");
     fflush(stdout);
-    err = this.erase(0, this.size());
+    err = this->erase(0, this->size());
     printf("%s\n", (err ? "Fail :(" : "OK"));
     if (err) {
         error("error: %s (%d)\n", strerror(-err), err);
@@ -162,42 +175,12 @@
 
     printf("Deinitializing the block device... ");
     fflush(stdout);
-    err = this.deinit();
+    err = this->deinit();
     printf("%s\n", (err ? "Fail :(" : "OK"));
     if (err) {
         error("error: %s (%d)\n", strerror(-err), err);
     }
+
+    return err;
 } 
-    
-
-/** Extras */
-/*int SDCardReader::writeBlock(uint8_t* blockToWrite, int dataLength){
-
-    if ( 0 != this->init()) {
-       printf("Init failed \n");
-       return -1;
-    }
-    
-     // set the frequency
-    if ( 0 != this->frequency(5000000)) {
-        printf("Error setting frequency \n");
-    }
-    
-    dataLength = 4096;
-    
-    printf("%i\n",this->program(blockToWrite, 0, dataLength));
-    if (0 == this->program(blockToWrite, 0, dataLength)){
-        printf("Block Written!");    
-        //read back contents of block
-        if ( 0 == this->read(blockToWrite, 0, dataLength)) {
-            // print the contents of the block
-            printf("%s", blockToWrite);
-        }
-    }
-     // call the SDBlockDevice instance de-initialisation method.
-    this->deinit();
-     
-     return 0;
-}
-
-
+   
--- a/sdCardReader.h	Wed Apr 04 17:15:36 2018 -0400
+++ b/sdCardReader.h	Thu Apr 05 01:22:57 2018 -0400
@@ -8,6 +8,9 @@
 #include "HeapBlockDevice.h"
 #include "platform/CircularBuffer.h"
 
+//For saving funny int formats
+#include <inttypes.h>
+
 // File systems
 //#include "LittleFileSystem.h"
 #include "FATFileSystem.h"
@@ -21,7 +24,7 @@
   	FILE* openFile(string filename);
 	void closeFile(FILE* fileToClose);
 
-	int writeDataPoint(int index, uint32_t timestamp, uint16_t* data);
+	int writeDataPoint(FILE* theFile, int index, uint32_t timestamp, uint16_t* data);
 	int eraseData();
 
 	int fullWriteProcedure(string filename,int* indexArr, uint32_t* timeArr, uint16_t** allData);