SD card functionality
Dependents: ELEC350_Project2 SDcard
SDCard.cpp@8:ee8f65745141, 2018-12-19 (annotated)
- Committer:
- Swabey89
- Date:
- Wed Dec 19 12:02:14 2018 +0000
- Revision:
- 8:ee8f65745141
- Parent:
- 7:393fa8184388
- Child:
- 9:113f4934b907
on start, attempt to initialize and set initialized state to false is unable to.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Swabey89 | 0:0fc2cf27ff9e | 1 | #include "SDCard.hpp" |
Swabey89 | 7:393fa8184388 | 2 | #include <string> |
Swabey89 | 3:c6e5dd2faa22 | 3 | |
Swabey89 | 1:c3af0c26ded2 | 4 | void SDcard(void) |
Swabey89 | 0:0fc2cf27ff9e | 5 | { |
Swabey89 | 7:393fa8184388 | 6 | static time_t seconds; |
Swabey89 | 2:9a5eea2adbf8 | 7 | |
Swabey89 | 2:9a5eea2adbf8 | 8 | //REQUIRES IMPROVEMENTS, SEE ONENOTE |
Swabey89 | 0:0fc2cf27ff9e | 9 | |
Swabey89 | 2:9a5eea2adbf8 | 10 | //Initialise the SD card |
Swabey89 | 8:ee8f65745141 | 11 | if (sd.init() != 0) { |
Swabey89 | 8:ee8f65745141 | 12 | pc->printf("WARNING:SD CARD INITIALISATION FAILED\n\r"); |
Swabey89 | 8:ee8f65745141 | 13 | sd_init = false; |
Swabey89 | 8:ee8f65745141 | 14 | //lcd.cls(); |
Swabey89 | 8:ee8f65745141 | 15 | //lcd.printf("CANNOT INIT SD"); |
Swabey89 | 8:ee8f65745141 | 16 | //errorCode(FATAL); |
Swabey89 | 2:9a5eea2adbf8 | 17 | } |
Swabey89 | 8:ee8f65745141 | 18 | else |
Swabey89 | 8:ee8f65745141 | 19 | { |
Swabey89 | 8:ee8f65745141 | 20 | //Create a filing system for SD Card |
Swabey89 | 8:ee8f65745141 | 21 | fs = new FATFileSystem("sd", &sd); |
Swabey89 | 8:ee8f65745141 | 22 | pc->printf("SD CARD INITIALISED\n\r"); |
Swabey89 | 7:393fa8184388 | 23 | |
Swabey89 | 8:ee8f65745141 | 24 | //Open to WRITE |
Swabey89 | 8:ee8f65745141 | 25 | char fileDate[30]; |
Swabey89 | 8:ee8f65745141 | 26 | seconds = time(NULL); |
Swabey89 | 8:ee8f65745141 | 27 | timeData = localtime(&seconds); |
Swabey89 | 8:ee8f65745141 | 28 | set_time(mktime(timeData)); |
Swabey89 | 8:ee8f65745141 | 29 | strftime(fileDate, 30, "sd/log_%d_%m_%y.csv", timeData); |
Swabey89 | 8:ee8f65745141 | 30 | |
Swabey89 | 8:ee8f65745141 | 31 | fp = fopen(fileDate,"a"); |
Swabey89 | 8:ee8f65745141 | 32 | |
Swabey89 | 8:ee8f65745141 | 33 | if (fp == NULL) |
Swabey89 | 8:ee8f65745141 | 34 | { |
Swabey89 | 8:ee8f65745141 | 35 | pc->printf("WARNING: COULD NOT OPEN FILE FOR WRITE\n\r"); |
Swabey89 | 8:ee8f65745141 | 36 | //lcd.cls(); |
Swabey89 | 8:ee8f65745141 | 37 | //lcd.printf("CANNOT OPEN FILE\n\n"); |
Swabey89 | 8:ee8f65745141 | 38 | //errorCode(FATAL); |
Swabey89 | 8:ee8f65745141 | 39 | } |
Swabey89 | 8:ee8f65745141 | 40 | else |
Swabey89 | 8:ee8f65745141 | 41 | { |
Swabey89 | 8:ee8f65745141 | 42 | pc->printf("FILE OPEN FOR WRITING\n\r"); |
Swabey89 | 8:ee8f65745141 | 43 | sd_init = true; |
Swabey89 | 8:ee8f65745141 | 44 | } |
Swabey89 | 8:ee8f65745141 | 45 | fclose(fp); |
Swabey89 | 8:ee8f65745141 | 46 | } |
Swabey89 | 7:393fa8184388 | 47 | |
Swabey89 | 8:ee8f65745141 | 48 | //Last message before sampling begins - probably remove |
Swabey89 | 2:9a5eea2adbf8 | 49 | lcd.cls(); |
Swabey89 | 7:393fa8184388 | 50 | lcd.printf("SYSTEM READY\n\n"); |
Swabey89 | 1:c3af0c26ded2 | 51 | } |
Swabey89 | 1:c3af0c26ded2 | 52 | |
Swabey89 | 1:c3af0c26ded2 | 53 | void SDread(int n) |
Swabey89 | 1:c3af0c26ded2 | 54 | { |
Swabey89 | 3:c6e5dd2faa22 | 55 | //Read n samples from the SD card |
Swabey89 | 2:9a5eea2adbf8 | 56 | if (n == -1) puts("Received command to read all"); |
Swabey89 | 2:9a5eea2adbf8 | 57 | else printf("Received command to read %d\n", n); |
Swabey89 | 2:9a5eea2adbf8 | 58 | } |
Swabey89 | 2:9a5eea2adbf8 | 59 | |
Swabey89 | 2:9a5eea2adbf8 | 60 | void SDdelete(int n) |
Swabey89 | 2:9a5eea2adbf8 | 61 | { |
Swabey89 | 3:c6e5dd2faa22 | 62 | //Delete n samples from the SD card |
Swabey89 | 2:9a5eea2adbf8 | 63 | if (n == -1) puts("Received command to delete all"); |
Swabey89 | 2:9a5eea2adbf8 | 64 | else printf("Received command to delete %d\n", n); |
Swabey89 | 1:c3af0c26ded2 | 65 | } |
Swabey89 | 1:c3af0c26ded2 | 66 | |
Swabey89 | 3:c6e5dd2faa22 | 67 | void SDaddSample(double temp, double pressure) |
Swabey89 | 3:c6e5dd2faa22 | 68 | { |
Swabey89 | 4:dc767b5f917b | 69 | //Add the sampled data to the SD card |
Swabey89 | 6:5646450f583b | 70 | yellowLED = !yellowLED; //debugging |
Swabey89 | 4:dc767b5f917b | 71 | fp = fopen("/sd/q.csv","a"); |
Swabey89 | 3:c6e5dd2faa22 | 72 | fprintf(fp, "%6.1f,%.2f\n\r", temp, pressure); |
Swabey89 | 3:c6e5dd2faa22 | 73 | fclose(fp); |
Swabey89 | 3:c6e5dd2faa22 | 74 | } |
Swabey89 | 3:c6e5dd2faa22 | 75 | |
Swabey89 | 1:c3af0c26ded2 | 76 | void SDalive(void) |
Swabey89 | 1:c3af0c26ded2 | 77 | { |
Swabey89 | 3:c6e5dd2faa22 | 78 | //Signal that the SD thread is still alive |
Swabey89 | 2:9a5eea2adbf8 | 79 | //puts("SD THREAD ALIVE\n"); |
Swabey89 | 7:393fa8184388 | 80 | } |
Swabey89 | 7:393fa8184388 | 81 |