Leon Wehmeier / Mbed OS fiasco_max32630

Dependencies:   SoftSerial MAX14690 Buffer

Fork of rtos_threading_with_callback by mbed_example

sdLogger/sdLogger.cpp

Committer:
lwehmeier
Date:
2018-03-30
Revision:
3:d7ec6dc025b0
Parent:
2:bf699e054b34

File content as of revision 3:d7ec6dc025b0:

#include "mbed.h"
#include "rtos.h"
#include "global.h"
#include <stdio.h>
#include <errno.h>
#include "SDBlockDevice.h"
#include "LittleFileSystem.h"
#include "FATFileSystem.h"
#include "logger.h"

static void return_error(int ret_val){
  if (ret_val)
    printf("SD: Failure. %d\r\n", ret_val);
  //else
    //printf("SD: done.\r\n");
}

static void errno_error(void* ret_val){
  if (ret_val == NULL)
    printf("SD:  Failure. %d \r\n", errno);
  //else
    //printf("SD:  done.\r\n");
}
class SDLogger
{
public:
    static void run()
    {
        for (;;) {
            //char buffer[200];
            //sprintf(buffer, "Pressure: %4.1f \t Temperature: %3.2f\t Altitude: %4.1f\r\nACC1:%2.1f;%2.1f;%2.1f\r\nACC2:%2.1f;%2.1f;%2.1f\r\n", pressure, temperature, altitude, (acc1[0]), (acc1[1]), (acc1[2]),(acc2[0]), (acc2[1]), (acc2[2]));
            char* msg = (char*) Logger::q.get(osWaitForever).value.p;
            SD_appendData(msg);
            delete[] msg;
            rtos::Thread::wait(6000);//10 datasets per minute. TODO: log current system time
        }
    }
    SDLogger()
    {
        testSD();
        registerThread(SDLogger::run);
    }
    static void testSD()
{
    printf("SD: Mounting the filesystem on \"/fs\". ");
  int error = fs.mount(&bd);
  return_error(error);

  printf("SD: Opening a new file, numbers.txt.");
  FILE* fd = fopen("/fs/numbers.txt", "w");
  errno_error(fd);

  for (int i = 0; i < 20; i++){
    printf("SD: Writing decimal numbers to a file (%d/20)\r", i);
    fprintf(fd, "%d\r\n", i);
  }
  printf("SD: Writing decimal numbers to a file (20/20) done.\r\n");

  printf("SD: Closing file.\r\n");
  fclose(fd);
  printf("SD:  done.\r\n");
}
static void SD_appendData(const char* data)
{

  //printf("SD: Opening file data.txt.\r\n");
  FILE* fd = fopen("/fs/data.txt", "w+");
  errno_error(fd);

  fprintf(fd, "%s", data);
  //printf("SD: Writing done.\r\n");

  //printf("SD: Closing file.\r\n");
  fclose(fd);
  //printf("SD: done.\r\n");
}
    static SDBlockDevice bd;//PinName mosi, PinName miso, PinName sclk, PinName cs
    static FATFileSystem fs;
};
SDBlockDevice SDLogger::bd(P0_5, P0_6, P0_4, P0_7);
FATFileSystem SDLogger::fs("fs");

// some witchcraft to register run function without touching anything outside our library
static SDLogger _dummy;