Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SDFileSystem mbed
Fork of PES4_Programme by
source/sdcard.cpp@65:49eb0934f772, 2018-04-05 (annotated)
- Committer:
- itslinear
- Date:
- Thu Apr 05 17:11:08 2018 +0000
- Revision:
- 65:49eb0934f772
- Parent:
- 64:ca667234c845
- Child:
- 66:0d43bd7ed179
write_medProtocol added
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| cittecla | 51:a98ffbd41e76 | 1 | #include "sdcard.h" | 
| cittecla | 53:1c61cadbcb35 | 2 | #include <stdio.h> | 
| cittecla | 53:1c61cadbcb35 | 3 | #include <string.h> | 
| cittecla | 58:cda5298c9b7f | 4 | #include <math.h> | 
| cittecla | 51:a98ffbd41e76 | 5 | |
| cittecla | 51:a98ffbd41e76 | 6 | Timer timer; | 
| cittecla | 51:a98ffbd41e76 | 7 | |
| cittecla | 51:a98ffbd41e76 | 8 | DigitalIn button(PC_13, PullUp); | 
| cittecla | 51:a98ffbd41e76 | 9 | |
| cittecla | 51:a98ffbd41e76 | 10 | SDFileSystem sd(PA_7, PA_6, PA_5, PB_6, "sd", PA_8, SDFileSystem::SWITCH_NONE, 25000000); | 
| cittecla | 51:a98ffbd41e76 | 11 | char buffer[4096]; | 
| cittecla | 51:a98ffbd41e76 | 12 | |
| cittecla | 51:a98ffbd41e76 | 13 | void writeTest() | 
| cittecla | 51:a98ffbd41e76 | 14 | { | 
| cittecla | 51:a98ffbd41e76 | 15 | //Test write performance by creating a 1MB file | 
| cittecla | 53:1c61cadbcb35 | 16 | printf("Testing %iB write performance...", sizeof(buffer)); | 
| cittecla | 51:a98ffbd41e76 | 17 | FileHandle* file = sd.open("Test File.bin", O_WRONLY | O_CREAT | O_TRUNC); | 
| cittecla | 51:a98ffbd41e76 | 18 | if (file != NULL) { | 
| cittecla | 51:a98ffbd41e76 | 19 | timer.start(); | 
| cittecla | 51:a98ffbd41e76 | 20 | for (int i = 0; i < (1048576 / sizeof(buffer)); i++) { | 
| cittecla | 51:a98ffbd41e76 | 21 | if (file->write(buffer, sizeof(buffer)) != sizeof(buffer)) { | 
| cittecla | 51:a98ffbd41e76 | 22 | timer.stop(); | 
| cittecla | 51:a98ffbd41e76 | 23 | printf("write error!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 24 | timer.reset(); | 
| cittecla | 51:a98ffbd41e76 | 25 | return; | 
| cittecla | 51:a98ffbd41e76 | 26 | } | 
| cittecla | 51:a98ffbd41e76 | 27 | } | 
| cittecla | 51:a98ffbd41e76 | 28 | timer.stop(); | 
| cittecla | 51:a98ffbd41e76 | 29 | if (file->close()) | 
| cittecla | 51:a98ffbd41e76 | 30 | printf("failed to close file!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 31 | else | 
| cittecla | 51:a98ffbd41e76 | 32 | printf("done!\n\r\tResult: %.2fKB/s\n\r", 1024 / (timer.read_us() / 1000000.0)); | 
| cittecla | 51:a98ffbd41e76 | 33 | timer.reset(); | 
| cittecla | 51:a98ffbd41e76 | 34 | } else { | 
| cittecla | 51:a98ffbd41e76 | 35 | printf("failed to create file!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 36 | } | 
| cittecla | 51:a98ffbd41e76 | 37 | } | 
| cittecla | 51:a98ffbd41e76 | 38 | |
| cittecla | 51:a98ffbd41e76 | 39 | void readTest() | 
| cittecla | 51:a98ffbd41e76 | 40 | { | 
| cittecla | 51:a98ffbd41e76 | 41 | //Test read performance by reading the 1MB file created by writeTest() | 
| cittecla | 51:a98ffbd41e76 | 42 | printf("Testing %iB read performance...", sizeof(buffer)); | 
| cittecla | 51:a98ffbd41e76 | 43 | FileHandle* file = sd.open("Test File.bin", O_RDONLY); | 
| cittecla | 51:a98ffbd41e76 | 44 | if (file != NULL) { | 
| cittecla | 51:a98ffbd41e76 | 45 | timer.start(); | 
| cittecla | 51:a98ffbd41e76 | 46 | int iterations = 0; | 
| cittecla | 51:a98ffbd41e76 | 47 | while (file->read(buffer, sizeof(buffer)) == sizeof(buffer)) | 
| cittecla | 51:a98ffbd41e76 | 48 | iterations++; | 
| cittecla | 51:a98ffbd41e76 | 49 | timer.stop(); | 
| cittecla | 51:a98ffbd41e76 | 50 | if (iterations != (1048576 / sizeof(buffer))) | 
| cittecla | 51:a98ffbd41e76 | 51 | printf("read error!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 52 | else if (file->close()) | 
| cittecla | 51:a98ffbd41e76 | 53 | printf("failed to close file!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 54 | else if (sd.remove("Test File.bin")) | 
| cittecla | 51:a98ffbd41e76 | 55 | printf("failed to delete file!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 56 | else | 
| cittecla | 51:a98ffbd41e76 | 57 | printf("done!\n\r\tResult: %.2fKB/s\n\r", 1024 / (timer.read_us() / 1000000.0)); | 
| cittecla | 51:a98ffbd41e76 | 58 | timer.reset(); | 
| cittecla | 51:a98ffbd41e76 | 59 | } else { | 
| cittecla | 51:a98ffbd41e76 | 60 | printf("failed to open file!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 61 | } | 
| cittecla | 51:a98ffbd41e76 | 62 | } | 
| cittecla | 51:a98ffbd41e76 | 63 | |
| cittecla | 51:a98ffbd41e76 | 64 | void testSd() | 
| cittecla | 51:a98ffbd41e76 | 65 | { | 
| cittecla | 51:a98ffbd41e76 | 66 | //Configure CRC, large frames, and write validation | 
| cittecla | 51:a98ffbd41e76 | 67 | sd.crc(true); | 
| cittecla | 51:a98ffbd41e76 | 68 | sd.large_frames(true); | 
| cittecla | 51:a98ffbd41e76 | 69 | sd.write_validation(true); | 
| cittecla | 51:a98ffbd41e76 | 70 | |
| cittecla | 51:a98ffbd41e76 | 71 | //Fill the buffer with random data for the write test | 
| cittecla | 51:a98ffbd41e76 | 72 | srand(time(NULL)); | 
| cittecla | 51:a98ffbd41e76 | 73 | for (int i = 0; i < sizeof(buffer); i++) | 
| cittecla | 51:a98ffbd41e76 | 74 | buffer[i] = rand(); | 
| cittecla | 51:a98ffbd41e76 | 75 | |
| cittecla | 51:a98ffbd41e76 | 76 | |
| cittecla | 53:1c61cadbcb35 | 77 | //Print the start message | 
| cittecla | 53:1c61cadbcb35 | 78 | printf("\n\rStarting SD Card test application:"); | 
| cittecla | 51:a98ffbd41e76 | 79 | |
| cittecla | 51:a98ffbd41e76 | 80 | |
| cittecla | 53:1c61cadbcb35 | 81 | //Make sure a card is present | 
| cittecla | 53:1c61cadbcb35 | 82 | if (!sd.card_present()) { | 
| cittecla | 53:1c61cadbcb35 | 83 | printf("\n\rNo card present!\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 84 | } | 
| cittecla | 51:a98ffbd41e76 | 85 | |
| cittecla | 53:1c61cadbcb35 | 86 | //Try to mount the SD card | 
| cittecla | 53:1c61cadbcb35 | 87 | printf("\n\rMounting SD card..."); | 
| cittecla | 53:1c61cadbcb35 | 88 | if (sd.mount() != 0) { | 
| cittecla | 53:1c61cadbcb35 | 89 | printf("failed!\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 90 | } | 
| cittecla | 53:1c61cadbcb35 | 91 | printf("success!\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 92 | |
| cittecla | 53:1c61cadbcb35 | 93 | //Display the card type | 
| cittecla | 53:1c61cadbcb35 | 94 | printf("\tCard type: "); | 
| cittecla | 53:1c61cadbcb35 | 95 | SDFileSystem::CardType cardType = sd.card_type(); | 
| cittecla | 53:1c61cadbcb35 | 96 | if (cardType == SDFileSystem::CARD_NONE) | 
| cittecla | 53:1c61cadbcb35 | 97 | printf("None\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 98 | else if (cardType == SDFileSystem::CARD_MMC) | 
| cittecla | 53:1c61cadbcb35 | 99 | printf("MMC\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 100 | else if (cardType == SDFileSystem::CARD_SD) | 
| cittecla | 53:1c61cadbcb35 | 101 | printf("SD\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 102 | else if (cardType == SDFileSystem::CARD_SDHC) | 
| cittecla | 53:1c61cadbcb35 | 103 | printf("SDHC\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 104 | else | 
| cittecla | 53:1c61cadbcb35 | 105 | printf("Unknown\n\r"); | 
| cittecla | 51:a98ffbd41e76 | 106 | |
| cittecla | 53:1c61cadbcb35 | 107 | //Display the card capacity | 
| cittecla | 53:1c61cadbcb35 | 108 | printf("\tSectors: %u\n\r", sd.disk_sectors()); | 
| cittecla | 53:1c61cadbcb35 | 109 | printf("\tCapacity: %.1fMB\n\r", sd.disk_sectors() / 2048.0); | 
| cittecla | 51:a98ffbd41e76 | 110 | |
| cittecla | 53:1c61cadbcb35 | 111 | /*//Format the card | 
| cittecla | 53:1c61cadbcb35 | 112 | printf("Formatting SD card..."); | 
| cittecla | 53:1c61cadbcb35 | 113 | if (sd.format() != 0) { | 
| cittecla | 53:1c61cadbcb35 | 114 | printf("failed!\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 115 | continue; | 
| cittecla | 53:1c61cadbcb35 | 116 | } | 
| cittecla | 53:1c61cadbcb35 | 117 | printf("success!\n\r");*/ | 
| cittecla | 51:a98ffbd41e76 | 118 | |
| cittecla | 53:1c61cadbcb35 | 119 | //Perform a read/write test | 
| cittecla | 53:1c61cadbcb35 | 120 | writeTest(); | 
| cittecla | 53:1c61cadbcb35 | 121 | readTest(); | 
| cittecla | 51:a98ffbd41e76 | 122 | |
| cittecla | 51:a98ffbd41e76 | 123 | |
| cittecla | 51:a98ffbd41e76 | 124 | |
| cittecla | 53:1c61cadbcb35 | 125 | printf("write to SD card on a txt file:\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 126 | printf("\tHello World!\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 127 | |
| cittecla | 53:1c61cadbcb35 | 128 | FILE *fp = fopen("/sd/medication/sdtest.txt", "a"); | 
| cittecla | 53:1c61cadbcb35 | 129 | if(fp == NULL) { | 
| cittecla | 53:1c61cadbcb35 | 130 | printf("\tCould not open file for write\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 131 | } else { | 
| cittecla | 53:1c61cadbcb35 | 132 | printf("\tfile opened\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 133 | } | 
| cittecla | 53:1c61cadbcb35 | 134 | |
| cittecla | 53:1c61cadbcb35 | 135 | fprintf(fp, "Hello fun SD Card World!\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 136 | fclose(fp); | 
| cittecla | 53:1c61cadbcb35 | 137 | |
| cittecla | 53:1c61cadbcb35 | 138 | printf("\tText written to SD card\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 139 | printf("\tGoodbye World!\r\n"); | 
| cittecla | 53:1c61cadbcb35 | 140 | |
| cittecla | 53:1c61cadbcb35 | 141 | //Unmount the SD card | 
| cittecla | 53:1c61cadbcb35 | 142 | sd.unmount(); | 
| cittecla | 53:1c61cadbcb35 | 143 | } | 
| cittecla | 53:1c61cadbcb35 | 144 | |
| cittecla | 53:1c61cadbcb35 | 145 | s_user readMedication(int user) | 
| cittecla | 53:1c61cadbcb35 | 146 | { | 
| cittecla | 53:1c61cadbcb35 | 147 | s_user userfile; | 
| cittecla | 53:1c61cadbcb35 | 148 | userfile.valid = false; | 
| cittecla | 53:1c61cadbcb35 | 149 | char filepath[] = "/sd/medication/medicationUser2.txt"; | 
| cittecla | 53:1c61cadbcb35 | 150 | if(user==0) { | 
| cittecla | 53:1c61cadbcb35 | 151 | strcpy(filepath, "/sd/medication/medicationUser1.txt"); | 
| cittecla | 53:1c61cadbcb35 | 152 | } | 
| cittecla | 53:1c61cadbcb35 | 153 | |
| cittecla | 53:1c61cadbcb35 | 154 | //Configure CRC, large frames, and write validation | 
| cittecla | 53:1c61cadbcb35 | 155 | sd.crc(true); | 
| cittecla | 53:1c61cadbcb35 | 156 | sd.large_frames(true); | 
| cittecla | 53:1c61cadbcb35 | 157 | sd.write_validation(true); | 
| cittecla | 51:a98ffbd41e76 | 158 | |
| cittecla | 53:1c61cadbcb35 | 159 | //mount SD card | 
| cittecla | 53:1c61cadbcb35 | 160 | printf("\n\rMounting SD card..."); | 
| cittecla | 53:1c61cadbcb35 | 161 | if (sd.mount() != 0) { | 
| cittecla | 53:1c61cadbcb35 | 162 | printf("failed!\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 163 | return userfile; | 
| cittecla | 53:1c61cadbcb35 | 164 | } | 
| cittecla | 53:1c61cadbcb35 | 165 | printf("success!\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 166 | |
| cittecla | 53:1c61cadbcb35 | 167 | //open file for read | 
| cittecla | 53:1c61cadbcb35 | 168 | printf("open file %s...",filepath); | 
| cittecla | 53:1c61cadbcb35 | 169 | FILE *fp = fopen(filepath, "r"); | 
| cittecla | 53:1c61cadbcb35 | 170 | if(fp == NULL) { | 
| cittecla | 53:1c61cadbcb35 | 171 | printf("failed!\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 172 | return userfile; | 
| cittecla | 53:1c61cadbcb35 | 173 | } | 
| cittecla | 53:1c61cadbcb35 | 174 | printf("success!\n\r"); | 
| cittecla | 53:1c61cadbcb35 | 175 | |
| cittecla | 53:1c61cadbcb35 | 176 | char string[STR_LEN]; | 
| cittecla | 53:1c61cadbcb35 | 177 | int linecounter = 1; | 
| cittecla | 57:79fed71031da | 178 | int checksum = 0; | 
| cittecla | 57:79fed71031da | 179 | int checksum2 = 0; | 
| cittecla | 53:1c61cadbcb35 | 180 | while (fgets(string, STR_LEN, fp) != NULL) { | 
| cittecla | 53:1c61cadbcb35 | 181 | switch (linecounter) { | 
| cittecla | 53:1c61cadbcb35 | 182 | case 1: | 
| cittecla | 53:1c61cadbcb35 | 183 | printf("Reading Line 1..."); | 
| cittecla | 58:cda5298c9b7f | 184 | strcpy(userfile.firstName, strtok(string, TOKEN)); | 
| cittecla | 53:1c61cadbcb35 | 185 | strcpy(userfile.secondName, strtok(NULL, TOKEN)); | 
| cittecla | 53:1c61cadbcb35 | 186 | printf("done!\r\n"); | 
| cittecla | 58:cda5298c9b7f | 187 | printf("\tFirst Name:\t%s \n\r\tLast Name:\t%s \n\r", userfile.firstName,userfile.secondName); | 
| cittecla | 53:1c61cadbcb35 | 188 | break; | 
| cittecla | 57:79fed71031da | 189 | case 30: | 
| cittecla | 57:79fed71031da | 190 | printf("reading checksum..."); | 
| cittecla | 57:79fed71031da | 191 | checksum2 = atoi(strtok(string, TOKEN)); | 
| cittecla | 57:79fed71031da | 192 | printf("done!\r\n"); | 
| cittecla | 57:79fed71031da | 193 | if(checksum == checksum2) userfile.valid = true; | 
| cittecla | 57:79fed71031da | 194 | break; | 
| cittecla | 55:bdab541f434d | 195 | default: | 
| cittecla | 55:bdab541f434d | 196 | printf("reading medication %d...",(linecounter-1)); | 
| cittecla | 56:218601547d13 | 197 | int daycounter = (linecounter-2)/4; | 
| cittecla | 56:218601547d13 | 198 | int momentcounter = (linecounter-2)%4; | 
| cittecla | 57:79fed71031da | 199 | |
| cittecla | 57:79fed71031da | 200 | userfile.medication.day[daycounter].moment[momentcounter].time.hour = atoi(strtok(string, TOKEN)); | 
| cittecla | 57:79fed71031da | 201 | userfile.medication.day[daycounter].moment[momentcounter].time.minute = atoi(strtok(NULL, TOKEN)); | 
| cittecla | 57:79fed71031da | 202 | userfile.medication.day[daycounter].moment[momentcounter].timeOffsetMinus = atoi(strtok(NULL, TOKEN)); | 
| cittecla | 57:79fed71031da | 203 | userfile.medication.day[daycounter].moment[momentcounter].timeOffsetPlus = atoi(strtok(NULL, TOKEN)); | 
| cittecla | 57:79fed71031da | 204 | |
| cittecla | 57:79fed71031da | 205 | checksum += userfile.medication.day[daycounter].moment[momentcounter].time.hour; | 
| cittecla | 57:79fed71031da | 206 | checksum += userfile.medication.day[daycounter].moment[momentcounter].time.minute; | 
| cittecla | 57:79fed71031da | 207 | checksum += userfile.medication.day[daycounter].moment[momentcounter].timeOffsetMinus; | 
| cittecla | 57:79fed71031da | 208 | checksum += userfile.medication.day[daycounter].moment[momentcounter].timeOffsetPlus; | 
| cittecla | 57:79fed71031da | 209 | |
| cittecla | 56:218601547d13 | 210 | int medication = atoi(strtok(NULL, TOKEN)); | 
| cittecla | 55:bdab541f434d | 211 | |
| cittecla | 56:218601547d13 | 212 | for( int ii = 0; ii<6; ii++) { | 
| cittecla | 56:218601547d13 | 213 | userfile.medication.day[daycounter].moment[momentcounter].medContainer.container[5-ii] = medication % 10; | 
| cittecla | 56:218601547d13 | 214 | medication = medication/10; | 
| cittecla | 57:79fed71031da | 215 | checksum += userfile.medication.day[daycounter].moment[momentcounter].medContainer.container[5-ii]; | 
| cittecla | 55:bdab541f434d | 216 | } | 
| cittecla | 53:1c61cadbcb35 | 217 | printf("done!\r\n"); | 
| cittecla | 53:1c61cadbcb35 | 218 | break; | 
| cittecla | 51:a98ffbd41e76 | 219 | } | 
| cittecla | 53:1c61cadbcb35 | 220 | linecounter++; | 
| cittecla | 53:1c61cadbcb35 | 221 | } | 
| cittecla | 58:cda5298c9b7f | 222 | //close file | 
| cittecla | 58:cda5298c9b7f | 223 | fclose(fp); | 
| cittecla | 55:bdab541f434d | 224 | //Unmount the SD card | 
| cittecla | 55:bdab541f434d | 225 | sd.unmount(); | 
| cittecla | 55:bdab541f434d | 226 | |
| cittecla | 53:1c61cadbcb35 | 227 | return userfile; | 
| cittecla | 58:cda5298c9b7f | 228 | } | 
| cittecla | 58:cda5298c9b7f | 229 | |
| cittecla | 58:cda5298c9b7f | 230 | int writeMedication(int user, s_user userfile) | 
| cittecla | 58:cda5298c9b7f | 231 | { | 
| cittecla | 58:cda5298c9b7f | 232 | |
| cittecla | 58:cda5298c9b7f | 233 | char filepath[] = "/sd/medication/medicationUser2.txt"; | 
| cittecla | 58:cda5298c9b7f | 234 | if(user==0) { | 
| cittecla | 58:cda5298c9b7f | 235 | strcpy(filepath, "/sd/medication/medicationUser1.txt"); | 
| cittecla | 58:cda5298c9b7f | 236 | } | 
| cittecla | 58:cda5298c9b7f | 237 | |
| cittecla | 58:cda5298c9b7f | 238 | //Configure CRC, large frames, and write validation | 
| cittecla | 58:cda5298c9b7f | 239 | sd.crc(true); | 
| cittecla | 58:cda5298c9b7f | 240 | sd.large_frames(true); | 
| cittecla | 58:cda5298c9b7f | 241 | sd.write_validation(true); | 
| cittecla | 58:cda5298c9b7f | 242 | |
| cittecla | 58:cda5298c9b7f | 243 | //mount SD card | 
| cittecla | 58:cda5298c9b7f | 244 | printf("\n\rMounting SD card..."); | 
| cittecla | 58:cda5298c9b7f | 245 | if (sd.mount() != 0) { | 
| cittecla | 58:cda5298c9b7f | 246 | printf("failed!\n\r"); | 
| cittecla | 58:cda5298c9b7f | 247 | return EXIT_FAILURE; | 
| cittecla | 58:cda5298c9b7f | 248 | } | 
| cittecla | 58:cda5298c9b7f | 249 | printf("success!\n\r"); | 
| cittecla | 58:cda5298c9b7f | 250 | |
| cittecla | 58:cda5298c9b7f | 251 | //delete file | 
| cittecla | 58:cda5298c9b7f | 252 | //sd.remove(filepath); | 
| cittecla | 58:cda5298c9b7f | 253 | |
| cittecla | 58:cda5298c9b7f | 254 | //open file for write | 
| cittecla | 58:cda5298c9b7f | 255 | printf("open file %s...",filepath); | 
| cittecla | 58:cda5298c9b7f | 256 | FILE *fp = fopen(filepath, "w"); | 
| cittecla | 58:cda5298c9b7f | 257 | if(fp == NULL) { | 
| cittecla | 58:cda5298c9b7f | 258 | printf("failed!\n\r"); | 
| cittecla | 58:cda5298c9b7f | 259 | return EXIT_FAILURE; | 
| cittecla | 58:cda5298c9b7f | 260 | } | 
| cittecla | 58:cda5298c9b7f | 261 | printf("success!\n\r"); | 
| cittecla | 58:cda5298c9b7f | 262 | printf("writing to SD card..."); | 
| cittecla | 58:cda5298c9b7f | 263 | fprintf(fp,"%s;%s",userfile.firstName,userfile.secondName); | 
| cittecla | 58:cda5298c9b7f | 264 | int checksum = 0; | 
| cittecla | 58:cda5298c9b7f | 265 | for(int i = 2; i< 30; i++) { | 
| cittecla | 58:cda5298c9b7f | 266 | int daycounter = (i-2)/4; | 
| cittecla | 58:cda5298c9b7f | 267 | int momentcounter = (i-2)%4; | 
| cittecla | 58:cda5298c9b7f | 268 | int hour = userfile.medication.day[daycounter].moment[momentcounter].time.hour; | 
| cittecla | 58:cda5298c9b7f | 269 | int minute = userfile.medication.day[daycounter].moment[momentcounter].time.minute; | 
| cittecla | 58:cda5298c9b7f | 270 | int offsetMinus = userfile.medication.day[daycounter].moment[momentcounter].timeOffsetMinus; | 
| cittecla | 58:cda5298c9b7f | 271 | int offsetPlus = userfile.medication.day[daycounter].moment[momentcounter].timeOffsetPlus; | 
| cittecla | 58:cda5298c9b7f | 272 | checksum += hour+minute+offsetMinus+offsetPlus; | 
| cittecla | 58:cda5298c9b7f | 273 | int medication = 0; | 
| cittecla | 58:cda5298c9b7f | 274 | for(int ii = 0; ii < 6; ii++) { | 
| cittecla | 58:cda5298c9b7f | 275 | int devider = pow(10.0f,ii); | 
| cittecla | 58:cda5298c9b7f | 276 | medication += 100000/devider * userfile.medication.day[daycounter].moment[momentcounter].medContainer.container[ii]; | 
| cittecla | 58:cda5298c9b7f | 277 | checksum+=userfile.medication.day[daycounter].moment[momentcounter].medContainer.container[ii]; | 
| cittecla | 58:cda5298c9b7f | 278 | } | 
| cittecla | 58:cda5298c9b7f | 279 | fprintf(fp,"%02d;%02d;%02d;%02d;%06d\r\n",hour, minute,offsetMinus, offsetPlus, medication); | 
| cittecla | 58:cda5298c9b7f | 280 | |
| cittecla | 58:cda5298c9b7f | 281 | } | 
| cittecla | 58:cda5298c9b7f | 282 | fprintf(fp,"%d",checksum); | 
| cittecla | 58:cda5298c9b7f | 283 | printf("done!\r\n"); | 
| cittecla | 58:cda5298c9b7f | 284 | |
| cittecla | 58:cda5298c9b7f | 285 | //close file | 
| cittecla | 58:cda5298c9b7f | 286 | printf("closing file %s...",filepath); | 
| cittecla | 58:cda5298c9b7f | 287 | fclose(fp); | 
| cittecla | 58:cda5298c9b7f | 288 | printf("done!\n\r"); | 
| cittecla | 58:cda5298c9b7f | 289 | //Unmount the SD card | 
| cittecla | 58:cda5298c9b7f | 290 | printf("Unmounting SD card..."); | 
| cittecla | 58:cda5298c9b7f | 291 | sd.unmount(); | 
| cittecla | 58:cda5298c9b7f | 292 | printf("done!\n\r"); | 
| cittecla | 58:cda5298c9b7f | 293 | |
| cittecla | 58:cda5298c9b7f | 294 | return EXIT_SUCCESS; | 
| itslinear | 64:ca667234c845 | 295 | } | 
| itslinear | 64:ca667234c845 | 296 | |
| itslinear | 64:ca667234c845 | 297 | |
| itslinear | 64:ca667234c845 | 298 | |
| itslinear | 64:ca667234c845 | 299 | |
| itslinear | 64:ca667234c845 | 300 | char read_medProtocol(char user) | 
| itslinear | 64:ca667234c845 | 301 | { | 
| itslinear | 64:ca667234c845 | 302 | char buffer[200]; | 
| itslinear | 64:ca667234c845 | 303 | char filepath[] = "/sd/protocol/medProtocolUser2.txt"; // Muss noch auf SD Karte erstellt werden!!! | 
| itslinear | 64:ca667234c845 | 304 | if(user==0) { | 
| itslinear | 64:ca667234c845 | 305 | strcpy(filepath, "/sd/protocol/medProtocolUser1.txt"); // Muss noch auf SD Karte erstellt werden!!! | 
| itslinear | 64:ca667234c845 | 306 | } | 
| itslinear | 64:ca667234c845 | 307 | |
| itslinear | 64:ca667234c845 | 308 | //Configure CRC, large frames, and write validation | 
| itslinear | 64:ca667234c845 | 309 | sd.crc(true); | 
| itslinear | 64:ca667234c845 | 310 | sd.large_frames(true); | 
| itslinear | 64:ca667234c845 | 311 | sd.write_validation(true); | 
| itslinear | 64:ca667234c845 | 312 | |
| itslinear | 64:ca667234c845 | 313 | //mount SD card | 
| itslinear | 64:ca667234c845 | 314 | printf("\n\rMounting SD card..."); | 
| itslinear | 64:ca667234c845 | 315 | if (sd.mount() != 0) { | 
| itslinear | 64:ca667234c845 | 316 | printf("failed!\n\r"); | 
| itslinear | 64:ca667234c845 | 317 | return buffer; | 
| itslinear | 64:ca667234c845 | 318 | } | 
| itslinear | 64:ca667234c845 | 319 | printf("success!\n\r"); | 
| itslinear | 64:ca667234c845 | 320 | |
| itslinear | 64:ca667234c845 | 321 | //open file for read | 
| itslinear | 64:ca667234c845 | 322 | printf("open file %s...",filepath); | 
| itslinear | 64:ca667234c845 | 323 | FILE *fp = fopen(filepath, "r"); | 
| itslinear | 64:ca667234c845 | 324 | if(fp == NULL) { | 
| itslinear | 64:ca667234c845 | 325 | printf("failed!\n\r"); | 
| itslinear | 64:ca667234c845 | 326 | return buffer; | 
| itslinear | 64:ca667234c845 | 327 | } | 
| itslinear | 64:ca667234c845 | 328 | printf("success!\n\r"); | 
| itslinear | 64:ca667234c845 | 329 | |
| itslinear | 64:ca667234c845 | 330 | |
| itslinear | 64:ca667234c845 | 331 | while (fgets(buffer,200, fp) != NULL) { // nicht fertig | 
| itslinear | 64:ca667234c845 | 332 | |
| itslinear | 64:ca667234c845 | 333 | |
| itslinear | 64:ca667234c845 | 334 | } | 
| itslinear | 64:ca667234c845 | 335 | //close file | 
| itslinear | 64:ca667234c845 | 336 | fclose(fp); | 
| itslinear | 64:ca667234c845 | 337 | //Unmount the SD card | 
| itslinear | 64:ca667234c845 | 338 | sd.unmount(); | 
| itslinear | 64:ca667234c845 | 339 | |
| itslinear | 64:ca667234c845 | 340 | return buffer; | 
| itslinear | 64:ca667234c845 | 341 | } | 
| itslinear | 64:ca667234c845 | 342 | |
| itslinear | 64:ca667234c845 | 343 | |
| itslinear | 64:ca667234c845 | 344 | |
| itslinear | 65:49eb0934f772 | 345 | int write_medProtocol(s_time medicationTime, s_time outputTime, s_medContainer medication, int success) | 
| itslinear | 64:ca667234c845 | 346 | { | 
| itslinear | 64:ca667234c845 | 347 | char filepath[] = "/sd/protocol/medProtocolUser2.txt"; // Muss noch auf SD Karte erstellt werden!!! | 
| itslinear | 64:ca667234c845 | 348 | if(user==0) { | 
| itslinear | 64:ca667234c845 | 349 | strcpy(filepath, "/sd/protocol/medProtocolUser1.txt"; // Muss noch auf SD Karte erstellt werden!!! | 
| itslinear | 64:ca667234c845 | 350 | } | 
| itslinear | 64:ca667234c845 | 351 | |
| itslinear | 64:ca667234c845 | 352 | //Configure CRC, large frames, and write validation | 
| itslinear | 64:ca667234c845 | 353 | sd.crc(true); | 
| itslinear | 64:ca667234c845 | 354 | sd.large_frames(true); | 
| itslinear | 64:ca667234c845 | 355 | sd.write_validation(true); | 
| itslinear | 64:ca667234c845 | 356 | |
| itslinear | 64:ca667234c845 | 357 | //mount SD card | 
| itslinear | 64:ca667234c845 | 358 | printf("\n\rMounting SD card..."); | 
| itslinear | 64:ca667234c845 | 359 | if (sd.mount() != 0) { | 
| itslinear | 64:ca667234c845 | 360 | printf("failed!\n\r"); | 
| itslinear | 64:ca667234c845 | 361 | return EXIT_FAILURE; | 
| itslinear | 64:ca667234c845 | 362 | } | 
| itslinear | 64:ca667234c845 | 363 | printf("success!\n\r"); | 
| itslinear | 64:ca667234c845 | 364 | |
| itslinear | 64:ca667234c845 | 365 | //delete file | 
| itslinear | 64:ca667234c845 | 366 | //sd.remove(filepath); | 
| itslinear | 64:ca667234c845 | 367 | |
| itslinear | 64:ca667234c845 | 368 | //open file for write | 
| itslinear | 64:ca667234c845 | 369 | printf("open file %s...",filepath); | 
| itslinear | 64:ca667234c845 | 370 | FILE *fp = fopen(filepath, "a"); | 
| itslinear | 64:ca667234c845 | 371 | if(fp == NULL) { | 
| itslinear | 64:ca667234c845 | 372 | printf("failed!\n\r"); | 
| itslinear | 64:ca667234c845 | 373 | return EXIT_FAILURE; | 
| itslinear | 64:ca667234c845 | 374 | } | 
| itslinear | 64:ca667234c845 | 375 | printf("success!\n\r"); | 
| itslinear | 64:ca667234c845 | 376 | printf("writing to SD card..."); | 
| itslinear | 65:49eb0934f772 | 377 | fprintf(fp,"%d;%02d;%02d;%02d;%02d;%02d;%d;%02d;%02d;%s;",outputTime.weekday,outputTime.day,outputTime.month,outputTime.year,medicationTime.hour,medicationTime.minute,success,outputTime.hour,outputTime.minute,medication); | 
| itslinear | 64:ca667234c845 | 378 | |
| itslinear | 64:ca667234c845 | 379 | printf("done!\r\n"); | 
| itslinear | 64:ca667234c845 | 380 | |
| itslinear | 64:ca667234c845 | 381 | //close file | 
| itslinear | 64:ca667234c845 | 382 | printf("closing file %s...",filepath); | 
| itslinear | 64:ca667234c845 | 383 | fclose(fp); | 
| itslinear | 64:ca667234c845 | 384 | printf("done!\n\r"); | 
| itslinear | 64:ca667234c845 | 385 | //Unmount the SD card | 
| itslinear | 64:ca667234c845 | 386 | printf("Unmounting SD card..."); | 
| itslinear | 64:ca667234c845 | 387 | sd.unmount(); | 
| itslinear | 64:ca667234c845 | 388 | printf("done!\n\r"); | 
| itslinear | 64:ca667234c845 | 389 | |
| itslinear | 64:ca667234c845 | 390 | return EXIT_SUCCESS; | 
| itslinear | 64:ca667234c845 | 391 | } | 
| itslinear | 64:ca667234c845 | 392 | 
