SBD-DL22 / Libraries / TF_handler / TF_handler.cpp
TF_handler.cpp
Raw
// Including necessary libraries for SPI communication, handling TF (TransFlash) cards and Energia functions
#include "SPI.h"
#include "Jaffl.h"
#include "TF_handler.h"
#include "Energia.h"

// Uncomment the following line for debugging mode
//#define DEBUG_MODE

#ifdef DEBUG_MODE
// Macro to print log messages when in DEBUG_MODE
#define LOG(m, f) Serial.print(m); Serial.print(": "); Serial.println(f);delay(10)
#else
// Macro that does nothing when not in DEBUG_MODE
#define LOG(m, f) f
#endif

// List of error codes and their meaning omitted...

// Constructor for TF_handler class, taking a pin number as parameter and setting it as input with pullup
TF_handler::TF_handler(int pin)
{
  pinMode(pin, INPUT_PULLUP);
  _pin = pin; //cs pin
}

// Function to initialize the TF card and open the file "DATA.TXT"
int TF_handler::begin(FIL &fp)
{
  Jaffl.begin(_pin);

  LOG("open_code", Jaffl.open(&fp,"DATA.TXT", FA_WRITE | FA_OPEN_ALWAYS));
  if(f_size(&fp)== 0)
  {
    char temp [121];
    snprintf(temp,120,"STATE\tDURATION(S)\tTIME1(mm/dd/yy)(HH:mm:ss)\tTIME2(mm/dd/yy)(HH:mm:ss)\n");
    Jaffl.printf(&fp,temp);
    Jaffl.sync(&fp);
  }

  // Seek to the end of the file to append data
  Jaffl.lseek(&fp, f_size(&fp));
}

// Function to store data into the file "DATA.TXT"
int TF_handler::store_data(FIL &fp,char* unix_time)
{
  LOG("print code1", Jaffl.printf(&fp,unix_time));

  // Flush the data to the file ensuring it gets saved
  LOG("sync code", Jaffl.sync(&fp));
}