SD card functionality
Dependents: ELEC350_Project2 SDcard
SDCard.cpp
- Committer:
- Swabey89
- Date:
- 2018-12-21
- Revision:
- 11:89960c1f2234
- Parent:
- 10:f2b8e3b587d5
- Child:
- 12:3198f0b7b36f
File content as of revision 11:89960c1f2234:
#include "SDCard.hpp" #include <string> void SDcard(void) { static time_t seconds; //static reqiured? //Initialise the SD card if (sd.init() != 0) { printlock.lock(); pc->printf("WARNING:SD CARD INITIALISATION FAILED\n\r"); printlock.unlock(); sd_init = false; //lcd.cls(); //lcd.printf("CANNOT INIT SD"); //errorCode(FATAL); } else { //Create a filing system for SD Card fs = new FATFileSystem("sd", &sd); printlock.lock(); pc->printf("SD CARD INITIALISED\n\r"); printlock.unlock(); //Open to WRITE char fileDate[30]; timeLock.lock(); seconds = time(NULL); timeData = localtime(&seconds); set_time(mktime(timeData)); strftime(fileDate, 30, "sd/log_%d_%m_%y.csv", timeData); timeLock.unlock(); fp = fopen(fileDate,"a"); if (fp == NULL) { printlock.lock(); pc->printf("WARNING: COULD NOT OPEN FILE FOR WRITE\n\r"); printlock.unlock(); //lcd.cls(); //lcd.printf("CANNOT OPEN FILE\n\n"); //errorCode(FATAL); } else { printlock.lock(); pc->printf("FILE OPEN FOR WRITING\n\n\n\r"); printlock.unlock(); sd_init = true; } fclose(fp); } } void SDread(int n) { //Read n samples from the SD card printlock.lock(); if (n == -1) { unsigned int i=newestIndex; unsigned int j = 0; bufferLock.lock(); while (i != oldestIndex) { i = (i ? i : BUFFERSIZE) - 1; //if i is not 0 subtract 1. if it is 0, set to buffersize - 1 pc->printf("Date/Time: %s\tTemperature: %5.2f\tPressure: %5.2f\tLight: %5.2f\n\r", buffer[i].getTime(), buffer[i].gettemp(), buffer[i].getpress(), buffer[i].getlight()); j++; } bufferLock.unlock(); printf("%d records read\r\n\n\n", j); } else { //printf("Received command to read %d\n\r", n); /* Read from newestIndex back by n, until either newestIndex - n or oldestindex */ unsigned int i; unsigned int j = newestIndex; bufferLock.lock(); for (i = 0; i<n; i++) { if (j == oldestIndex) {break;} j = (j ? j : BUFFERSIZE) - 1; pc->printf("Date/Time: %s\tTemperature: %5.2f\tPressure: %5.2f\tLight: %5.2f\n\r", buffer[j].getTime(), buffer[j].gettemp(), buffer[j].getpress(), buffer[j].getlight()); } bufferLock.unlock(); printf("%d records read\r\n\n\n", i); } printlock.unlock(); } void SDdelete(int n) { //Delete n samples from the SD card printlock.lock(); if (n == -1) { unsigned int i = newestIndex; unsigned int j = 0; while (i != oldestIndex) { i = (i ? i : BUFFERSIZE) - 1; j++; } oldestIndex = 0; newestIndex = 0; pc->printf("Deleted %d records\r\n\n\n", j); } else { //printf("Received command to delete %d\n", n); //move oldestIndex forwards for the given number or until you hit newestIndex unsigned int i; unsigned int j = oldestIndex; bufferLock.lock(); for (i = 0; i<n; i++) { if (j == newestIndex) {break;} j = (j + 1) % BUFFERSIZE; } oldestIndex = j; bufferLock.unlock(); printf("Deleted %d records\r\n\n\n", i); } printlock.unlock(); } //UNUSED void SDaddSample(double temp, double pressure) { //Add the sampled data to the SD card yellowLED = !yellowLED; //debugging fp = fopen("/sd/q.csv","a"); fprintf(fp, "%6.1f,%.2f\n\r", temp, pressure); fclose(fp); } void SDalive(void) { //Signal that the SD thread is still alive //puts("SD THREAD ALIVE\n"); } void SDmount(void) { while(true) { Thread::signal_wait(SIGNAL_SD); if (sd_init) { fclose(fp); sd.deinit(); printlock.lock(); pc->printf("SD CARD UNMOUNTED\n\r"); printlock.unlock(); LCDlock.lock(); lcd.cls(); lcd.printf("Unmounted.."); Thread::wait(5000); LCDlock.unlock(); sd_init = false; } else { SDcard(); LCDlock.lock(); lcd.cls(); if (sd_init) { lcd.printf("SD mounted.."); } else { lcd.printf("SD FAILED.."); } Thread::wait(5000); LCDlock.unlock(); } } }