![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Test session
Dependencies: FatFileSystem MCP23017 WattBob_TextLCD mbed
Fork of Assignment_2_herpe by
sdcard.cpp@4:48761259552a, 2017-03-14 (annotated)
- Committer:
- xouf2114
- Date:
- Tue Mar 14 14:46:43 2017 +0000
- Revision:
- 4:48761259552a
Test of Assignment 2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
xouf2114 | 4:48761259552a | 1 | /** |
xouf2114 | 4:48761259552a | 2 | * Task that logs the state to a file on the SD card in csv format |
xouf2114 | 4:48761259552a | 3 | * |
xouf2114 | 4:48761259552a | 4 | * Author: Jacob Baungard Hansen |
xouf2114 | 4:48761259552a | 5 | */ |
xouf2114 | 4:48761259552a | 6 | |
xouf2114 | 4:48761259552a | 7 | #include "sdcard_task.h" |
xouf2114 | 4:48761259552a | 8 | |
xouf2114 | 4:48761259552a | 9 | /** |
xouf2114 | 4:48761259552a | 10 | * |
xouf2114 | 4:48761259552a | 11 | * @param starting_offset when the task should but run first |
xouf2114 | 4:48761259552a | 12 | * @param frequency_ms the frequency in ms of how often the task should run |
xouf2114 | 4:48761259552a | 13 | * @param state pointer to the state object to print |
xouf2114 | 4:48761259552a | 14 | */ |
xouf2114 | 4:48761259552a | 15 | SDCardTask::SDCardTask(int starting_offset, int frequency_ms, |
xouf2114 | 4:48761259552a | 16 | State * state) |
xouf2114 | 4:48761259552a | 17 | : Task(starting_offset, frequency_ms) { |
xouf2114 | 4:48761259552a | 18 | |
xouf2114 | 4:48761259552a | 19 | this->state = state; |
xouf2114 | 4:48761259552a | 20 | // mount the file system |
xouf2114 | 4:48761259552a | 21 | this->sd = new SDFileSystem(p5, p6, p7, p8, "sd"); |
xouf2114 | 4:48761259552a | 22 | |
xouf2114 | 4:48761259552a | 23 | // set the folder |
xouf2114 | 4:48761259552a | 24 | this->folder = "/sd/embedded"; |
xouf2114 | 4:48761259552a | 25 | |
xouf2114 | 4:48761259552a | 26 | // ensure folder exists |
xouf2114 | 4:48761259552a | 27 | struct stat s = {0}; |
xouf2114 | 4:48761259552a | 28 | |
xouf2114 | 4:48761259552a | 29 | if (!stat(folder.c_str(), &s)) { } |
xouf2114 | 4:48761259552a | 30 | else { |
xouf2114 | 4:48761259552a | 31 | mkdir(folder.c_str(), 0777); |
xouf2114 | 4:48761259552a | 32 | } |
xouf2114 | 4:48761259552a | 33 | |
xouf2114 | 4:48761259552a | 34 | // set file to log to |
xouf2114 | 4:48761259552a | 35 | this->file.append(folder); |
xouf2114 | 4:48761259552a | 36 | file.append("/data.csv"); |
xouf2114 | 4:48761259552a | 37 | |
xouf2114 | 4:48761259552a | 38 | // clear previous data and set headers |
xouf2114 | 4:48761259552a | 39 | FILE *fp = fopen(file.c_str(), "w"); |
xouf2114 | 4:48761259552a | 40 | if(fp == NULL) { |
xouf2114 | 4:48761259552a | 41 | error("Could not open file for write\n"); |
xouf2114 | 4:48761259552a | 42 | } |
xouf2114 | 4:48761259552a | 43 | fprintf(fp, "frequency,digital1,digital2,analog1,analog2\n"); |
xouf2114 | 4:48761259552a | 44 | fclose(fp); |
xouf2114 | 4:48761259552a | 45 | |
xouf2114 | 4:48761259552a | 46 | } |
xouf2114 | 4:48761259552a | 47 | |
xouf2114 | 4:48761259552a | 48 | SDCardTask::~SDCardTask() { |
xouf2114 | 4:48761259552a | 49 | delete this->sd; |
xouf2114 | 4:48761259552a | 50 | } |
xouf2114 | 4:48761259552a | 51 | |
xouf2114 | 4:48761259552a | 52 | void SDCardTask::action() { |
xouf2114 | 4:48761259552a | 53 | |
xouf2114 | 4:48761259552a | 54 | // ensure file can be opened |
xouf2114 | 4:48761259552a | 55 | FILE *fp = fopen(file.c_str(), "a"); |
xouf2114 | 4:48761259552a | 56 | if(fp == NULL) { |
xouf2114 | 4:48761259552a | 57 | error("Could not open file for write\n"); |
xouf2114 | 4:48761259552a | 58 | } |
xouf2114 | 4:48761259552a | 59 | // print current state |
xouf2114 | 4:48761259552a | 60 | fprintf(fp, "%d,%d,%d,%f,%f\n", this->state->get_freq(), this->state->get_digital_1(), |
xouf2114 | 4:48761259552a | 61 | this->state->get_digital_2(), this->state->get_avg_analog_1(), |
xouf2114 | 4:48761259552a | 62 | this->state->get_avg_analog_2()); |
xouf2114 | 4:48761259552a | 63 | fclose(fp); |
xouf2114 | 4:48761259552a | 64 | |
xouf2114 | 4:48761259552a | 65 | |
xouf2114 | 4:48761259552a | 66 | } |