SD card functionality
Dependents: ELEC350_Project2 SDcard
SDCard.cpp@12:3198f0b7b36f, 2018-12-22 (annotated)
- Committer:
- Swabey89
- Date:
- Sat Dec 22 21:14:56 2018 +0000
- Revision:
- 12:3198f0b7b36f
- Parent:
- 11:89960c1f2234
- Child:
- 13:5f786448e883
Changes to the way data is saved
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 | 9:113f4934b907 | 6 | static time_t seconds; //static reqiured? |
Swabey89 | 0:0fc2cf27ff9e | 7 | |
Swabey89 | 2:9a5eea2adbf8 | 8 | //Initialise the SD card |
Swabey89 | 8:ee8f65745141 | 9 | if (sd.init() != 0) { |
Swabey89 | 10:f2b8e3b587d5 | 10 | printlock.lock(); |
Swabey89 | 8:ee8f65745141 | 11 | pc->printf("WARNING:SD CARD INITIALISATION FAILED\n\r"); |
Swabey89 | 10:f2b8e3b587d5 | 12 | printlock.unlock(); |
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 | 10:f2b8e3b587d5 | 22 | printlock.lock(); |
Swabey89 | 8:ee8f65745141 | 23 | pc->printf("SD CARD INITIALISED\n\r"); |
Swabey89 | 10:f2b8e3b587d5 | 24 | printlock.unlock(); |
Swabey89 | 7:393fa8184388 | 25 | |
Swabey89 | 8:ee8f65745141 | 26 | //Open to WRITE |
Swabey89 | 8:ee8f65745141 | 27 | char fileDate[30]; |
Swabey89 | 10:f2b8e3b587d5 | 28 | timeLock.lock(); |
Swabey89 | 8:ee8f65745141 | 29 | seconds = time(NULL); |
Swabey89 | 8:ee8f65745141 | 30 | timeData = localtime(&seconds); |
Swabey89 | 8:ee8f65745141 | 31 | set_time(mktime(timeData)); |
Swabey89 | 8:ee8f65745141 | 32 | strftime(fileDate, 30, "sd/log_%d_%m_%y.csv", timeData); |
Swabey89 | 10:f2b8e3b587d5 | 33 | timeLock.unlock(); |
Swabey89 | 8:ee8f65745141 | 34 | fp = fopen(fileDate,"a"); |
Swabey89 | 8:ee8f65745141 | 35 | |
Swabey89 | 8:ee8f65745141 | 36 | if (fp == NULL) |
Swabey89 | 8:ee8f65745141 | 37 | { |
Swabey89 | 10:f2b8e3b587d5 | 38 | printlock.lock(); |
Swabey89 | 8:ee8f65745141 | 39 | pc->printf("WARNING: COULD NOT OPEN FILE FOR WRITE\n\r"); |
Swabey89 | 10:f2b8e3b587d5 | 40 | printlock.unlock(); |
Swabey89 | 8:ee8f65745141 | 41 | //lcd.cls(); |
Swabey89 | 8:ee8f65745141 | 42 | //lcd.printf("CANNOT OPEN FILE\n\n"); |
Swabey89 | 8:ee8f65745141 | 43 | //errorCode(FATAL); |
Swabey89 | 8:ee8f65745141 | 44 | } |
Swabey89 | 8:ee8f65745141 | 45 | else |
Swabey89 | 8:ee8f65745141 | 46 | { |
Swabey89 | 10:f2b8e3b587d5 | 47 | printlock.lock(); |
Swabey89 | 10:f2b8e3b587d5 | 48 | pc->printf("FILE OPEN FOR WRITING\n\n\n\r"); |
Swabey89 | 10:f2b8e3b587d5 | 49 | printlock.unlock(); |
Swabey89 | 8:ee8f65745141 | 50 | sd_init = true; |
Swabey89 | 8:ee8f65745141 | 51 | } |
Swabey89 | 8:ee8f65745141 | 52 | fclose(fp); |
Swabey89 | 8:ee8f65745141 | 53 | } |
Swabey89 | 1:c3af0c26ded2 | 54 | } |
Swabey89 | 1:c3af0c26ded2 | 55 | |
Swabey89 | 1:c3af0c26ded2 | 56 | void SDread(int n) |
Swabey89 | 1:c3af0c26ded2 | 57 | { |
Swabey89 | 12:3198f0b7b36f | 58 | //Read n samples from the SD card |
Swabey89 | 12:3198f0b7b36f | 59 | unsigned int i=0; |
Swabey89 | 12:3198f0b7b36f | 60 | unsigned int j = newestIndex; |
Swabey89 | 12:3198f0b7b36f | 61 | if (n==-1) {n = (BUFFERSIZE-Nspaces);} |
Swabey89 | 12:3198f0b7b36f | 62 | |
Swabey89 | 10:f2b8e3b587d5 | 63 | printlock.lock(); |
Swabey89 | 12:3198f0b7b36f | 64 | bufferLock.lock(); |
Swabey89 | 12:3198f0b7b36f | 65 | while (i < n) //ONLY USE SPACE AVAILABLE - KEEP IN MIND LAST READ WAS BEFORE IT WAS DECREMENTED |
Swabey89 | 12:3198f0b7b36f | 66 | { |
Swabey89 | 12:3198f0b7b36f | 67 | 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()); |
Swabey89 | 12:3198f0b7b36f | 68 | j = (j?j:BUFFERSIZE)-1; |
Swabey89 | 12:3198f0b7b36f | 69 | i++; |
Swabey89 | 12:3198f0b7b36f | 70 | } |
Swabey89 | 12:3198f0b7b36f | 71 | bufferLock.unlock(); |
Swabey89 | 12:3198f0b7b36f | 72 | printf("%d records read\r\n\n", i); |
Swabey89 | 12:3198f0b7b36f | 73 | printlock.unlock(); |
Swabey89 | 2:9a5eea2adbf8 | 74 | } |
Swabey89 | 2:9a5eea2adbf8 | 75 | |
Swabey89 | 12:3198f0b7b36f | 76 | void SDdelete(int n) //MUST RELEASE SPACE AVAILABLE |
Swabey89 | 2:9a5eea2adbf8 | 77 | { |
Swabey89 | 3:c6e5dd2faa22 | 78 | //Delete n samples from the SD card |
Swabey89 | 12:3198f0b7b36f | 79 | unsigned int i = 0; |
Swabey89 | 12:3198f0b7b36f | 80 | if (n==-1) {n = (BUFFERSIZE-Nspaces);} |
Swabey89 | 12:3198f0b7b36f | 81 | bufferLock.lock(); |
Swabey89 | 12:3198f0b7b36f | 82 | while (i < n) |
Swabey89 | 11:89960c1f2234 | 83 | { |
Swabey89 | 12:3198f0b7b36f | 84 | spaceAvailable.release(); |
Swabey89 | 12:3198f0b7b36f | 85 | i++; |
Swabey89 | 11:89960c1f2234 | 86 | } |
Swabey89 | 12:3198f0b7b36f | 87 | bufferLock.unlock(); |
Swabey89 | 12:3198f0b7b36f | 88 | Nspaces += i; |
Swabey89 | 12:3198f0b7b36f | 89 | printlock.lock(); |
Swabey89 | 12:3198f0b7b36f | 90 | pc->printf("Deleted %d records\r\n\n\n", i); |
Swabey89 | 12:3198f0b7b36f | 91 | printlock.unlock(); |
Swabey89 | 1:c3af0c26ded2 | 92 | } |
Swabey89 | 1:c3af0c26ded2 | 93 | |
Swabey89 | 10:f2b8e3b587d5 | 94 | |
Swabey89 | 10:f2b8e3b587d5 | 95 | |
Swabey89 | 10:f2b8e3b587d5 | 96 | //UNUSED |
Swabey89 | 3:c6e5dd2faa22 | 97 | void SDaddSample(double temp, double pressure) |
Swabey89 | 3:c6e5dd2faa22 | 98 | { |
Swabey89 | 4:dc767b5f917b | 99 | //Add the sampled data to the SD card |
Swabey89 | 6:5646450f583b | 100 | yellowLED = !yellowLED; //debugging |
Swabey89 | 4:dc767b5f917b | 101 | fp = fopen("/sd/q.csv","a"); |
Swabey89 | 3:c6e5dd2faa22 | 102 | fprintf(fp, "%6.1f,%.2f\n\r", temp, pressure); |
Swabey89 | 3:c6e5dd2faa22 | 103 | fclose(fp); |
Swabey89 | 3:c6e5dd2faa22 | 104 | } |
Swabey89 | 3:c6e5dd2faa22 | 105 | |
Swabey89 | 1:c3af0c26ded2 | 106 | void SDalive(void) |
Swabey89 | 1:c3af0c26ded2 | 107 | { |
Swabey89 | 3:c6e5dd2faa22 | 108 | //Signal that the SD thread is still alive |
Swabey89 | 2:9a5eea2adbf8 | 109 | //puts("SD THREAD ALIVE\n"); |
Swabey89 | 7:393fa8184388 | 110 | } |
Swabey89 | 7:393fa8184388 | 111 | |
Swabey89 | 9:113f4934b907 | 112 | void SDmount(void) |
Swabey89 | 9:113f4934b907 | 113 | { |
Swabey89 | 9:113f4934b907 | 114 | while(true) |
Swabey89 | 9:113f4934b907 | 115 | { |
Swabey89 | 9:113f4934b907 | 116 | Thread::signal_wait(SIGNAL_SD); |
Swabey89 | 10:f2b8e3b587d5 | 117 | |
Swabey89 | 9:113f4934b907 | 118 | if (sd_init) |
Swabey89 | 9:113f4934b907 | 119 | { |
Swabey89 | 9:113f4934b907 | 120 | fclose(fp); |
Swabey89 | 9:113f4934b907 | 121 | sd.deinit(); |
Swabey89 | 10:f2b8e3b587d5 | 122 | printlock.lock(); |
Swabey89 | 9:113f4934b907 | 123 | pc->printf("SD CARD UNMOUNTED\n\r"); |
Swabey89 | 10:f2b8e3b587d5 | 124 | printlock.unlock(); |
Swabey89 | 10:f2b8e3b587d5 | 125 | |
Swabey89 | 10:f2b8e3b587d5 | 126 | LCDlock.lock(); |
Swabey89 | 9:113f4934b907 | 127 | lcd.cls(); |
Swabey89 | 10:f2b8e3b587d5 | 128 | lcd.printf("Unmounted.."); |
Swabey89 | 10:f2b8e3b587d5 | 129 | Thread::wait(5000); |
Swabey89 | 10:f2b8e3b587d5 | 130 | LCDlock.unlock(); |
Swabey89 | 10:f2b8e3b587d5 | 131 | |
Swabey89 | 9:113f4934b907 | 132 | sd_init = false; |
Swabey89 | 9:113f4934b907 | 133 | |
Swabey89 | 9:113f4934b907 | 134 | } |
Swabey89 | 9:113f4934b907 | 135 | else |
Swabey89 | 9:113f4934b907 | 136 | { |
Swabey89 | 9:113f4934b907 | 137 | SDcard(); |
Swabey89 | 10:f2b8e3b587d5 | 138 | LCDlock.lock(); |
Swabey89 | 10:f2b8e3b587d5 | 139 | lcd.cls(); |
Swabey89 | 10:f2b8e3b587d5 | 140 | if (sd_init) |
Swabey89 | 10:f2b8e3b587d5 | 141 | { |
Swabey89 | 10:f2b8e3b587d5 | 142 | lcd.printf("SD mounted.."); |
Swabey89 | 10:f2b8e3b587d5 | 143 | } |
Swabey89 | 10:f2b8e3b587d5 | 144 | else |
Swabey89 | 10:f2b8e3b587d5 | 145 | { |
Swabey89 | 10:f2b8e3b587d5 | 146 | lcd.printf("SD FAILED.."); |
Swabey89 | 10:f2b8e3b587d5 | 147 | } |
Swabey89 | 10:f2b8e3b587d5 | 148 | Thread::wait(5000); |
Swabey89 | 10:f2b8e3b587d5 | 149 | LCDlock.unlock(); |
Swabey89 | 9:113f4934b907 | 150 | } |
Swabey89 | 9:113f4934b907 | 151 | |
Swabey89 | 9:113f4934b907 | 152 | } |
Swabey89 | 9:113f4934b907 | 153 | } |
Swabey89 | 9:113f4934b907 | 154 |