Xavier Gouesnard / Mbed 2 deprecated Assignment_2_XG

Dependencies:   FatFileSystem MCP23017 WattBob_TextLCD mbed

Fork of Assignment_2_herpe by Xavier Herpe

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sdcard.cpp Source File

sdcard.cpp

00001 /**
00002  * Task that logs the state to a file on the SD card in csv format
00003  *
00004  * Author: Jacob Baungard Hansen
00005  */
00006 
00007 #include "sdcard_task.h"
00008 
00009 /**
00010  *
00011  * @param starting_offset       when the task should but run first
00012  * @param frequency_ms              the frequency in ms of how often the task should run
00013 * @param state                          pointer to the state object to print
00014  */
00015 SDCardTask::SDCardTask(int starting_offset, int frequency_ms,
00016                                                                      State * state)
00017                                     : Task(starting_offset, frequency_ms) {
00018                                         
00019     this->state = state;
00020     // mount the file system
00021     this->sd = new SDFileSystem(p5, p6, p7, p8, "sd");
00022     
00023     // set the folder
00024     this->folder = "/sd/embedded";
00025     
00026     // ensure folder exists
00027     struct stat s = {0};
00028 
00029     if (!stat(folder.c_str(), &s)) { }
00030     else {
00031       mkdir(folder.c_str(), 0777); 
00032     }
00033     
00034     // set file to log to
00035     this->file.append(folder);
00036     file.append("/data.csv");
00037     
00038     // clear previous data and set headers
00039     FILE *fp = fopen(file.c_str(), "w");
00040     if(fp == NULL) {
00041       error("Could not open file for write\n");
00042     }
00043     fprintf(fp, "frequency,digital1,digital2,analog1,analog2\n");
00044     fclose(fp);
00045 
00046 }
00047 
00048 SDCardTask::~SDCardTask() {
00049     delete this->sd;
00050 }
00051 
00052 void SDCardTask::action() {
00053     
00054     // ensure file can be opened
00055     FILE *fp = fopen(file.c_str(), "a");
00056     if(fp == NULL) {
00057       error("Could not open file for write\n");
00058     }
00059     // print current state
00060     fprintf(fp, "%d,%d,%d,%f,%f\n", this->state->get_freq(), this->state->get_digital_1(), 
00061                                                                 this->state->get_digital_2(), this->state->get_avg_analog_1(),
00062                                                                 this->state->get_avg_analog_2());
00063     fclose(fp); 
00064     
00065     
00066 }