PES 4 - Smart Medication Dispenser / PES4_ProgrammeforDesignReview2

Dependencies:   SDFileSystem mbed

Fork of PES4_Programme by PES 4 - Smart Medication Dispenser

Committer:
cittecla
Date:
Tue Apr 10 07:47:18 2018 +0000
Revision:
94:b24d2b432b27
Parent:
90:d0805b58b0ae
Parent:
89:f63e4736d875
merge

Who changed what in which revision?

UserRevisionLine numberNew 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>
itslinear 80:6e3eb8246ced 5 #include "main.h"
cittecla 51:a98ffbd41e76 6
cittecla 51:a98ffbd41e76 7 Timer timer;
cittecla 51:a98ffbd41e76 8
cittecla 51:a98ffbd41e76 9 DigitalIn button(PC_13, PullUp);
cittecla 51:a98ffbd41e76 10
cittecla 51:a98ffbd41e76 11 SDFileSystem sd(PA_7, PA_6, PA_5, PB_6, "sd", PA_8, SDFileSystem::SWITCH_NONE, 25000000);
cittecla 51:a98ffbd41e76 12 char buffer[4096];
cittecla 51:a98ffbd41e76 13
cittecla 51:a98ffbd41e76 14 void writeTest()
cittecla 51:a98ffbd41e76 15 {
cittecla 51:a98ffbd41e76 16 //Test write performance by creating a 1MB file
cittecla 53:1c61cadbcb35 17 printf("Testing %iB write performance...", sizeof(buffer));
cittecla 51:a98ffbd41e76 18 FileHandle* file = sd.open("Test File.bin", O_WRONLY | O_CREAT | O_TRUNC);
cittecla 51:a98ffbd41e76 19 if (file != NULL) {
cittecla 51:a98ffbd41e76 20 timer.start();
cittecla 51:a98ffbd41e76 21 for (int i = 0; i < (1048576 / sizeof(buffer)); i++) {
cittecla 51:a98ffbd41e76 22 if (file->write(buffer, sizeof(buffer)) != sizeof(buffer)) {
cittecla 51:a98ffbd41e76 23 timer.stop();
cittecla 51:a98ffbd41e76 24 printf("write error!\n\r");
cittecla 51:a98ffbd41e76 25 timer.reset();
cittecla 51:a98ffbd41e76 26 return;
cittecla 51:a98ffbd41e76 27 }
cittecla 51:a98ffbd41e76 28 }
cittecla 51:a98ffbd41e76 29 timer.stop();
cittecla 51:a98ffbd41e76 30 if (file->close())
cittecla 51:a98ffbd41e76 31 printf("failed to close file!\n\r");
cittecla 51:a98ffbd41e76 32 else
cittecla 51:a98ffbd41e76 33 printf("done!\n\r\tResult: %.2fKB/s\n\r", 1024 / (timer.read_us() / 1000000.0));
cittecla 51:a98ffbd41e76 34 timer.reset();
cittecla 51:a98ffbd41e76 35 } else {
cittecla 51:a98ffbd41e76 36 printf("failed to create file!\n\r");
cittecla 51:a98ffbd41e76 37 }
cittecla 51:a98ffbd41e76 38 }
cittecla 51:a98ffbd41e76 39
cittecla 51:a98ffbd41e76 40 void readTest()
cittecla 51:a98ffbd41e76 41 {
cittecla 51:a98ffbd41e76 42 //Test read performance by reading the 1MB file created by writeTest()
cittecla 51:a98ffbd41e76 43 printf("Testing %iB read performance...", sizeof(buffer));
cittecla 51:a98ffbd41e76 44 FileHandle* file = sd.open("Test File.bin", O_RDONLY);
cittecla 51:a98ffbd41e76 45 if (file != NULL) {
cittecla 51:a98ffbd41e76 46 timer.start();
cittecla 51:a98ffbd41e76 47 int iterations = 0;
cittecla 51:a98ffbd41e76 48 while (file->read(buffer, sizeof(buffer)) == sizeof(buffer))
cittecla 51:a98ffbd41e76 49 iterations++;
cittecla 51:a98ffbd41e76 50 timer.stop();
cittecla 51:a98ffbd41e76 51 if (iterations != (1048576 / sizeof(buffer)))
cittecla 51:a98ffbd41e76 52 printf("read error!\n\r");
cittecla 51:a98ffbd41e76 53 else if (file->close())
cittecla 51:a98ffbd41e76 54 printf("failed to close file!\n\r");
cittecla 51:a98ffbd41e76 55 else if (sd.remove("Test File.bin"))
cittecla 51:a98ffbd41e76 56 printf("failed to delete file!\n\r");
cittecla 51:a98ffbd41e76 57 else
cittecla 51:a98ffbd41e76 58 printf("done!\n\r\tResult: %.2fKB/s\n\r", 1024 / (timer.read_us() / 1000000.0));
cittecla 51:a98ffbd41e76 59 timer.reset();
cittecla 51:a98ffbd41e76 60 } else {
cittecla 51:a98ffbd41e76 61 printf("failed to open file!\n\r");
cittecla 51:a98ffbd41e76 62 }
cittecla 51:a98ffbd41e76 63 }
cittecla 51:a98ffbd41e76 64
cittecla 51:a98ffbd41e76 65 void testSd()
cittecla 51:a98ffbd41e76 66 {
cittecla 51:a98ffbd41e76 67 //Configure CRC, large frames, and write validation
cittecla 51:a98ffbd41e76 68 sd.crc(true);
cittecla 51:a98ffbd41e76 69 sd.large_frames(true);
cittecla 51:a98ffbd41e76 70 sd.write_validation(true);
cittecla 51:a98ffbd41e76 71
cittecla 51:a98ffbd41e76 72 //Fill the buffer with random data for the write test
cittecla 51:a98ffbd41e76 73 srand(time(NULL));
cittecla 51:a98ffbd41e76 74 for (int i = 0; i < sizeof(buffer); i++)
cittecla 51:a98ffbd41e76 75 buffer[i] = rand();
cittecla 51:a98ffbd41e76 76
cittecla 51:a98ffbd41e76 77
cittecla 53:1c61cadbcb35 78 //Print the start message
cittecla 53:1c61cadbcb35 79 printf("\n\rStarting SD Card test application:");
cittecla 51:a98ffbd41e76 80
cittecla 51:a98ffbd41e76 81
cittecla 53:1c61cadbcb35 82 //Make sure a card is present
cittecla 53:1c61cadbcb35 83 if (!sd.card_present()) {
cittecla 53:1c61cadbcb35 84 printf("\n\rNo card present!\n\r");
cittecla 53:1c61cadbcb35 85 }
cittecla 51:a98ffbd41e76 86
cittecla 53:1c61cadbcb35 87 //Try to mount the SD card
cittecla 53:1c61cadbcb35 88 printf("\n\rMounting SD card...");
cittecla 53:1c61cadbcb35 89 if (sd.mount() != 0) {
cittecla 53:1c61cadbcb35 90 printf("failed!\n\r");
cittecla 53:1c61cadbcb35 91 }
cittecla 53:1c61cadbcb35 92 printf("success!\n\r");
cittecla 51:a98ffbd41e76 93
cittecla 53:1c61cadbcb35 94 //Display the card type
cittecla 53:1c61cadbcb35 95 printf("\tCard type: ");
cittecla 53:1c61cadbcb35 96 SDFileSystem::CardType cardType = sd.card_type();
cittecla 53:1c61cadbcb35 97 if (cardType == SDFileSystem::CARD_NONE)
cittecla 53:1c61cadbcb35 98 printf("None\n\r");
cittecla 53:1c61cadbcb35 99 else if (cardType == SDFileSystem::CARD_MMC)
cittecla 53:1c61cadbcb35 100 printf("MMC\n\r");
cittecla 53:1c61cadbcb35 101 else if (cardType == SDFileSystem::CARD_SD)
cittecla 53:1c61cadbcb35 102 printf("SD\n\r");
cittecla 53:1c61cadbcb35 103 else if (cardType == SDFileSystem::CARD_SDHC)
cittecla 53:1c61cadbcb35 104 printf("SDHC\n\r");
cittecla 53:1c61cadbcb35 105 else
cittecla 53:1c61cadbcb35 106 printf("Unknown\n\r");
cittecla 51:a98ffbd41e76 107
cittecla 53:1c61cadbcb35 108 //Display the card capacity
cittecla 53:1c61cadbcb35 109 printf("\tSectors: %u\n\r", sd.disk_sectors());
cittecla 53:1c61cadbcb35 110 printf("\tCapacity: %.1fMB\n\r", sd.disk_sectors() / 2048.0);
cittecla 51:a98ffbd41e76 111
cittecla 53:1c61cadbcb35 112 /*//Format the card
cittecla 53:1c61cadbcb35 113 printf("Formatting SD card...");
cittecla 53:1c61cadbcb35 114 if (sd.format() != 0) {
cittecla 53:1c61cadbcb35 115 printf("failed!\n\r");
cittecla 53:1c61cadbcb35 116 continue;
cittecla 53:1c61cadbcb35 117 }
cittecla 53:1c61cadbcb35 118 printf("success!\n\r");*/
cittecla 51:a98ffbd41e76 119
cittecla 53:1c61cadbcb35 120 //Perform a read/write test
cittecla 53:1c61cadbcb35 121 writeTest();
cittecla 53:1c61cadbcb35 122 readTest();
cittecla 51:a98ffbd41e76 123
cittecla 51:a98ffbd41e76 124
cittecla 51:a98ffbd41e76 125
cittecla 53:1c61cadbcb35 126 printf("write to SD card on a txt file:\n\r");
cittecla 53:1c61cadbcb35 127 printf("\tHello World!\n\r");
cittecla 53:1c61cadbcb35 128
cittecla 53:1c61cadbcb35 129 FILE *fp = fopen("/sd/medication/sdtest.txt", "a");
cittecla 53:1c61cadbcb35 130 if(fp == NULL) {
cittecla 53:1c61cadbcb35 131 printf("\tCould not open file for write\n\r");
cittecla 53:1c61cadbcb35 132 } else {
cittecla 53:1c61cadbcb35 133 printf("\tfile opened\n\r");
cittecla 53:1c61cadbcb35 134 }
cittecla 53:1c61cadbcb35 135
cittecla 53:1c61cadbcb35 136 fprintf(fp, "Hello fun SD Card World!\n\r");
cittecla 53:1c61cadbcb35 137 fclose(fp);
cittecla 53:1c61cadbcb35 138
cittecla 53:1c61cadbcb35 139 printf("\tText written to SD card\n\r");
cittecla 53:1c61cadbcb35 140 printf("\tGoodbye World!\r\n");
cittecla 53:1c61cadbcb35 141
cittecla 53:1c61cadbcb35 142 //Unmount the SD card
cittecla 53:1c61cadbcb35 143 sd.unmount();
cittecla 53:1c61cadbcb35 144 }
cittecla 53:1c61cadbcb35 145
cittecla 53:1c61cadbcb35 146 s_user readMedication(int user)
cittecla 53:1c61cadbcb35 147 {
cittecla 53:1c61cadbcb35 148 s_user userfile;
cittecla 53:1c61cadbcb35 149 userfile.valid = false;
cittecla 53:1c61cadbcb35 150 char filepath[] = "/sd/medication/medicationUser2.txt";
cittecla 53:1c61cadbcb35 151 if(user==0) {
cittecla 53:1c61cadbcb35 152 strcpy(filepath, "/sd/medication/medicationUser1.txt");
cittecla 53:1c61cadbcb35 153 }
cittecla 53:1c61cadbcb35 154
cittecla 53:1c61cadbcb35 155 //Configure CRC, large frames, and write validation
cittecla 53:1c61cadbcb35 156 sd.crc(true);
cittecla 53:1c61cadbcb35 157 sd.large_frames(true);
cittecla 53:1c61cadbcb35 158 sd.write_validation(true);
cittecla 51:a98ffbd41e76 159
cittecla 53:1c61cadbcb35 160 //mount SD card
cittecla 53:1c61cadbcb35 161 printf("\n\rMounting SD card...");
cittecla 53:1c61cadbcb35 162 if (sd.mount() != 0) {
cittecla 53:1c61cadbcb35 163 printf("failed!\n\r");
cittecla 53:1c61cadbcb35 164 return userfile;
cittecla 53:1c61cadbcb35 165 }
cittecla 53:1c61cadbcb35 166 printf("success!\n\r");
cittecla 53:1c61cadbcb35 167
cittecla 53:1c61cadbcb35 168 //open file for read
cittecla 53:1c61cadbcb35 169 printf("open file %s...",filepath);
cittecla 53:1c61cadbcb35 170 FILE *fp = fopen(filepath, "r");
cittecla 53:1c61cadbcb35 171 if(fp == NULL) {
cittecla 53:1c61cadbcb35 172 printf("failed!\n\r");
cittecla 53:1c61cadbcb35 173 return userfile;
cittecla 53:1c61cadbcb35 174 }
cittecla 53:1c61cadbcb35 175 printf("success!\n\r");
cittecla 53:1c61cadbcb35 176
cittecla 53:1c61cadbcb35 177 char string[STR_LEN];
cittecla 53:1c61cadbcb35 178 int linecounter = 1;
cittecla 57:79fed71031da 179 int checksum = 0;
cittecla 57:79fed71031da 180 int checksum2 = 0;
cittecla 53:1c61cadbcb35 181 while (fgets(string, STR_LEN, fp) != NULL) {
cittecla 53:1c61cadbcb35 182 switch (linecounter) {
cittecla 53:1c61cadbcb35 183 case 1:
cittecla 53:1c61cadbcb35 184 printf("Reading Line 1...");
cittecla 58:cda5298c9b7f 185 strcpy(userfile.firstName, strtok(string, TOKEN));
cittecla 53:1c61cadbcb35 186 strcpy(userfile.secondName, strtok(NULL, TOKEN));
cittecla 53:1c61cadbcb35 187 printf("done!\r\n");
cittecla 58:cda5298c9b7f 188 printf("\tFirst Name:\t%s \n\r\tLast Name:\t%s \n\r", userfile.firstName,userfile.secondName);
cittecla 53:1c61cadbcb35 189 break;
cittecla 57:79fed71031da 190 case 30:
cittecla 57:79fed71031da 191 printf("reading checksum...");
cittecla 57:79fed71031da 192 checksum2 = atoi(strtok(string, TOKEN));
cittecla 57:79fed71031da 193 printf("done!\r\n");
cittecla 57:79fed71031da 194 if(checksum == checksum2) userfile.valid = true;
cittecla 57:79fed71031da 195 break;
cittecla 55:bdab541f434d 196 default:
cittecla 55:bdab541f434d 197 printf("reading medication %d...",(linecounter-1));
cittecla 56:218601547d13 198 int daycounter = (linecounter-2)/4;
cittecla 56:218601547d13 199 int momentcounter = (linecounter-2)%4;
cittecla 57:79fed71031da 200
cittecla 57:79fed71031da 201 userfile.medication.day[daycounter].moment[momentcounter].time.hour = atoi(strtok(string, TOKEN));
cittecla 57:79fed71031da 202 userfile.medication.day[daycounter].moment[momentcounter].time.minute = atoi(strtok(NULL, TOKEN));
cittecla 57:79fed71031da 203 userfile.medication.day[daycounter].moment[momentcounter].timeOffsetMinus = atoi(strtok(NULL, TOKEN));
cittecla 57:79fed71031da 204 userfile.medication.day[daycounter].moment[momentcounter].timeOffsetPlus = atoi(strtok(NULL, TOKEN));
cittecla 57:79fed71031da 205
cittecla 57:79fed71031da 206 checksum += userfile.medication.day[daycounter].moment[momentcounter].time.hour;
cittecla 57:79fed71031da 207 checksum += userfile.medication.day[daycounter].moment[momentcounter].time.minute;
cittecla 57:79fed71031da 208 checksum += userfile.medication.day[daycounter].moment[momentcounter].timeOffsetMinus;
cittecla 57:79fed71031da 209 checksum += userfile.medication.day[daycounter].moment[momentcounter].timeOffsetPlus;
cittecla 57:79fed71031da 210
cittecla 56:218601547d13 211 int medication = atoi(strtok(NULL, TOKEN));
cittecla 55:bdab541f434d 212
cittecla 56:218601547d13 213 for( int ii = 0; ii<6; ii++) {
cittecla 56:218601547d13 214 userfile.medication.day[daycounter].moment[momentcounter].medContainer.container[5-ii] = medication % 10;
cittecla 56:218601547d13 215 medication = medication/10;
cittecla 57:79fed71031da 216 checksum += userfile.medication.day[daycounter].moment[momentcounter].medContainer.container[5-ii];
cittecla 55:bdab541f434d 217 }
cittecla 53:1c61cadbcb35 218 printf("done!\r\n");
cittecla 53:1c61cadbcb35 219 break;
cittecla 51:a98ffbd41e76 220 }
cittecla 53:1c61cadbcb35 221 linecounter++;
cittecla 53:1c61cadbcb35 222 }
cittecla 58:cda5298c9b7f 223 //close file
cittecla 58:cda5298c9b7f 224 fclose(fp);
cittecla 55:bdab541f434d 225 //Unmount the SD card
cittecla 55:bdab541f434d 226 sd.unmount();
cittecla 55:bdab541f434d 227
cittecla 53:1c61cadbcb35 228 return userfile;
cittecla 58:cda5298c9b7f 229 }
cittecla 58:cda5298c9b7f 230
cittecla 58:cda5298c9b7f 231 int writeMedication(int user, s_user userfile)
cittecla 58:cda5298c9b7f 232 {
cittecla 58:cda5298c9b7f 233
cittecla 58:cda5298c9b7f 234 char filepath[] = "/sd/medication/medicationUser2.txt";
cittecla 58:cda5298c9b7f 235 if(user==0) {
cittecla 58:cda5298c9b7f 236 strcpy(filepath, "/sd/medication/medicationUser1.txt");
cittecla 58:cda5298c9b7f 237 }
cittecla 58:cda5298c9b7f 238
cittecla 58:cda5298c9b7f 239 //Configure CRC, large frames, and write validation
cittecla 58:cda5298c9b7f 240 sd.crc(true);
cittecla 58:cda5298c9b7f 241 sd.large_frames(true);
cittecla 58:cda5298c9b7f 242 sd.write_validation(true);
cittecla 58:cda5298c9b7f 243
cittecla 58:cda5298c9b7f 244 //mount SD card
cittecla 58:cda5298c9b7f 245 printf("\n\rMounting SD card...");
cittecla 58:cda5298c9b7f 246 if (sd.mount() != 0) {
cittecla 58:cda5298c9b7f 247 printf("failed!\n\r");
cittecla 58:cda5298c9b7f 248 return EXIT_FAILURE;
cittecla 58:cda5298c9b7f 249 }
cittecla 58:cda5298c9b7f 250 printf("success!\n\r");
itslinear 80:6e3eb8246ced 251
cittecla 58:cda5298c9b7f 252 //delete file
cittecla 58:cda5298c9b7f 253 //sd.remove(filepath);
cittecla 58:cda5298c9b7f 254
cittecla 58:cda5298c9b7f 255 //open file for write
cittecla 58:cda5298c9b7f 256 printf("open file %s...",filepath);
cittecla 58:cda5298c9b7f 257 FILE *fp = fopen(filepath, "w");
cittecla 58:cda5298c9b7f 258 if(fp == NULL) {
cittecla 58:cda5298c9b7f 259 printf("failed!\n\r");
cittecla 58:cda5298c9b7f 260 return EXIT_FAILURE;
cittecla 58:cda5298c9b7f 261 }
cittecla 58:cda5298c9b7f 262 printf("success!\n\r");
cittecla 58:cda5298c9b7f 263 printf("writing to SD card...");
cittecla 58:cda5298c9b7f 264 fprintf(fp,"%s;%s",userfile.firstName,userfile.secondName);
cittecla 58:cda5298c9b7f 265 int checksum = 0;
cittecla 58:cda5298c9b7f 266 for(int i = 2; i< 30; i++) {
cittecla 58:cda5298c9b7f 267 int daycounter = (i-2)/4;
cittecla 58:cda5298c9b7f 268 int momentcounter = (i-2)%4;
cittecla 58:cda5298c9b7f 269 int hour = userfile.medication.day[daycounter].moment[momentcounter].time.hour;
cittecla 58:cda5298c9b7f 270 int minute = userfile.medication.day[daycounter].moment[momentcounter].time.minute;
cittecla 58:cda5298c9b7f 271 int offsetMinus = userfile.medication.day[daycounter].moment[momentcounter].timeOffsetMinus;
cittecla 58:cda5298c9b7f 272 int offsetPlus = userfile.medication.day[daycounter].moment[momentcounter].timeOffsetPlus;
cittecla 58:cda5298c9b7f 273 checksum += hour+minute+offsetMinus+offsetPlus;
cittecla 58:cda5298c9b7f 274 int medication = 0;
cittecla 58:cda5298c9b7f 275 for(int ii = 0; ii < 6; ii++) {
cittecla 58:cda5298c9b7f 276 int devider = pow(10.0f,ii);
cittecla 58:cda5298c9b7f 277 medication += 100000/devider * userfile.medication.day[daycounter].moment[momentcounter].medContainer.container[ii];
cittecla 58:cda5298c9b7f 278 checksum+=userfile.medication.day[daycounter].moment[momentcounter].medContainer.container[ii];
cittecla 58:cda5298c9b7f 279 }
cittecla 58:cda5298c9b7f 280 fprintf(fp,"%02d;%02d;%02d;%02d;%06d\r\n",hour, minute,offsetMinus, offsetPlus, medication);
cittecla 58:cda5298c9b7f 281
cittecla 58:cda5298c9b7f 282 }
cittecla 58:cda5298c9b7f 283 fprintf(fp,"%d",checksum);
cittecla 58:cda5298c9b7f 284 printf("done!\r\n");
itslinear 80:6e3eb8246ced 285
cittecla 58:cda5298c9b7f 286 //close file
cittecla 58:cda5298c9b7f 287 printf("closing file %s...",filepath);
cittecla 58:cda5298c9b7f 288 fclose(fp);
cittecla 58:cda5298c9b7f 289 printf("done!\n\r");
cittecla 58:cda5298c9b7f 290 //Unmount the SD card
cittecla 58:cda5298c9b7f 291 printf("Unmounting SD card...");
cittecla 58:cda5298c9b7f 292 sd.unmount();
cittecla 58:cda5298c9b7f 293 printf("done!\n\r");
itslinear 80:6e3eb8246ced 294
cittecla 58:cda5298c9b7f 295 return EXIT_SUCCESS;
itslinear 64:ca667234c845 296 }
itslinear 64:ca667234c845 297
itslinear 64:ca667234c845 298
itslinear 66:0d43bd7ed179 299 int write_medProtocol(s_time medicationTime, s_time outputTime, s_medContainer medication, int success, char user)
itslinear 64:ca667234c845 300 {
itslinear 64:ca667234c845 301 char filepath[] = "/sd/protocol/medProtocolUser2.txt"; // Muss noch auf SD Karte erstellt werden!!!
itslinear 64:ca667234c845 302 if(user==0) {
itslinear 66:0d43bd7ed179 303 strcpy(filepath, "/sd/protocol/medProtocolUser1.txt"); // Muss noch auf SD Karte erstellt werden!!!
itslinear 64:ca667234c845 304 }
itslinear 64:ca667234c845 305
itslinear 64:ca667234c845 306 //Configure CRC, large frames, and write validation
itslinear 64:ca667234c845 307 sd.crc(true);
itslinear 64:ca667234c845 308 sd.large_frames(true);
itslinear 64:ca667234c845 309 sd.write_validation(true);
itslinear 64:ca667234c845 310
itslinear 64:ca667234c845 311 //mount SD card
itslinear 64:ca667234c845 312 printf("\n\rMounting SD card...");
itslinear 64:ca667234c845 313 if (sd.mount() != 0) {
itslinear 64:ca667234c845 314 printf("failed!\n\r");
itslinear 64:ca667234c845 315 return EXIT_FAILURE;
itslinear 64:ca667234c845 316 }
itslinear 64:ca667234c845 317 printf("success!\n\r");
itslinear 64:ca667234c845 318
itslinear 77:c2e22d1e5d44 319 //open file for append
itslinear 64:ca667234c845 320 printf("open file %s...",filepath);
itslinear 64:ca667234c845 321 FILE *fp = fopen(filepath, "a");
itslinear 64:ca667234c845 322 if(fp == NULL) {
itslinear 64:ca667234c845 323 printf("failed!\n\r");
itslinear 64:ca667234c845 324 return EXIT_FAILURE;
itslinear 64:ca667234c845 325 }
itslinear 64:ca667234c845 326 printf("success!\n\r");
itslinear 64:ca667234c845 327 printf("writing to SD card...");
itslinear 77:c2e22d1e5d44 328 fprintf(fp,"%d;%02d;%02d;%02d;%02d;%02d;%d;%02d;%02d;",outputTime.weekday,outputTime.day,outputTime.month,outputTime.year,medicationTime.hour,medicationTime.minute,success,outputTime.hour,outputTime.minute);
itslinear 77:c2e22d1e5d44 329 fprintf(fp,"%d%d%d%d%d%d\r\n", medication.container[0], medication.container[1], medication.container[2], medication.container[3], medication.container[4], medication.container[5]);
itslinear 80:6e3eb8246ced 330
itslinear 64:ca667234c845 331 printf("done!\r\n");
itslinear 80:6e3eb8246ced 332
itslinear 64:ca667234c845 333 //close file
itslinear 64:ca667234c845 334 printf("closing file %s...",filepath);
itslinear 64:ca667234c845 335 fclose(fp);
itslinear 64:ca667234c845 336 printf("done!\n\r");
itslinear 64:ca667234c845 337 //Unmount the SD card
itslinear 64:ca667234c845 338 printf("Unmounting SD card...");
itslinear 64:ca667234c845 339 sd.unmount();
itslinear 64:ca667234c845 340 printf("done!\n\r");
itslinear 80:6e3eb8246ced 341
itslinear 64:ca667234c845 342 return EXIT_SUCCESS;
itslinear 64:ca667234c845 343 }
itslinear 64:ca667234c845 344
itslinear 77:c2e22d1e5d44 345 int erase_medProtocol()
itslinear 66:0d43bd7ed179 346 {
itslinear 80:6e3eb8246ced 347 char filepath[] = "/sd/protocol/medProtocolUser1.txt";
itslinear 80:6e3eb8246ced 348
itslinear 66:0d43bd7ed179 349 //Configure CRC, large frames, and write validation
itslinear 66:0d43bd7ed179 350 sd.crc(true);
itslinear 66:0d43bd7ed179 351 sd.large_frames(true);
itslinear 66:0d43bd7ed179 352 sd.write_validation(true);
itslinear 66:0d43bd7ed179 353
itslinear 66:0d43bd7ed179 354 //mount SD card
itslinear 66:0d43bd7ed179 355 printf("\n\rMounting SD card...");
itslinear 66:0d43bd7ed179 356 if (sd.mount() != 0) {
itslinear 66:0d43bd7ed179 357 printf("failed!\n\r");
itslinear 66:0d43bd7ed179 358 return EXIT_FAILURE;
itslinear 66:0d43bd7ed179 359 }
itslinear 66:0d43bd7ed179 360
itslinear 77:c2e22d1e5d44 361 //open file for write
itslinear 77:c2e22d1e5d44 362 printf("\n\ropen file %s...",filepath);
itslinear 77:c2e22d1e5d44 363 FILE *fp = fopen(filepath, "w");
itslinear 77:c2e22d1e5d44 364 if(fp == NULL) {
itslinear 77:c2e22d1e5d44 365 printf("failed!\n\r");
itslinear 77:c2e22d1e5d44 366 return EXIT_FAILURE;
itslinear 77:c2e22d1e5d44 367 }
itslinear 80:6e3eb8246ced 368
itslinear 77:c2e22d1e5d44 369 //close file
itslinear 77:c2e22d1e5d44 370 printf("\n\rclosing file %s...",filepath);
itslinear 77:c2e22d1e5d44 371 fclose(fp);
itslinear 77:c2e22d1e5d44 372
itslinear 80:6e3eb8246ced 373 strcpy(filepath, "/sd/protocol/medProtocolUser2.txt");
itslinear 80:6e3eb8246ced 374
itslinear 77:c2e22d1e5d44 375 //open file for write
itslinear 77:c2e22d1e5d44 376 printf("\n\ropen file %s...",filepath);
itslinear 77:c2e22d1e5d44 377 fp = fopen(filepath, "w");
itslinear 77:c2e22d1e5d44 378 if(fp == NULL) {
itslinear 77:c2e22d1e5d44 379 printf("failed!\n\r");
itslinear 77:c2e22d1e5d44 380 return EXIT_FAILURE;
itslinear 77:c2e22d1e5d44 381 }
itslinear 80:6e3eb8246ced 382
itslinear 77:c2e22d1e5d44 383 //close file
itslinear 77:c2e22d1e5d44 384 printf("closing file %s...",filepath);
itslinear 77:c2e22d1e5d44 385 fclose(fp);
itslinear 80:6e3eb8246ced 386
itslinear 66:0d43bd7ed179 387 sd.unmount();
itslinear 80:6e3eb8246ced 388
itslinear 77:c2e22d1e5d44 389 printf("done");
itslinear 80:6e3eb8246ced 390
itslinear 66:0d43bd7ed179 391 return EXIT_SUCCESS;
itslinear 66:0d43bd7ed179 392 }
itslinear 66:0d43bd7ed179 393
itslinear 80:6e3eb8246ced 394 //int write_medInventory(s_medContainer medInventory, s_time currentTime, char med1[], char med2[], char med3[], char med4[], char med5[], char med6[])
itslinear 80:6e3eb8246ced 395 int write_medInventory(s_medInventory medInventory)
itslinear 66:0d43bd7ed179 396 {
itslinear 66:0d43bd7ed179 397 char filepath[] = "/sd/protocol/medInventory.txt"; // Muss noch auf SD Karte erstellt werden!!!
itslinear 66:0d43bd7ed179 398
itslinear 66:0d43bd7ed179 399 //Configure CRC, large frames, and write validation
itslinear 66:0d43bd7ed179 400 sd.crc(true);
itslinear 66:0d43bd7ed179 401 sd.large_frames(true);
itslinear 66:0d43bd7ed179 402 sd.write_validation(true);
itslinear 66:0d43bd7ed179 403
itslinear 66:0d43bd7ed179 404 //mount SD card
itslinear 66:0d43bd7ed179 405 printf("\n\rMounting SD card...");
itslinear 66:0d43bd7ed179 406 if (sd.mount() != 0) {
itslinear 66:0d43bd7ed179 407 printf("failed!\n\r");
itslinear 66:0d43bd7ed179 408 return EXIT_FAILURE;
itslinear 66:0d43bd7ed179 409 }
itslinear 66:0d43bd7ed179 410 printf("success!\n\r");
itslinear 66:0d43bd7ed179 411
itslinear 66:0d43bd7ed179 412 //open file for write
itslinear 66:0d43bd7ed179 413 printf("open file %s...",filepath);
itslinear 66:0d43bd7ed179 414 FILE *fp = fopen(filepath, "w");
itslinear 66:0d43bd7ed179 415 if(fp == NULL) {
itslinear 66:0d43bd7ed179 416 printf("failed!\n\r");
itslinear 66:0d43bd7ed179 417 return EXIT_FAILURE;
itslinear 66:0d43bd7ed179 418 }
itslinear 66:0d43bd7ed179 419 printf("success!\n\r");
itslinear 66:0d43bd7ed179 420 printf("writing to SD card...");
itslinear 89:f63e4736d875 421 fprintf(fp,"%d\r\n", medInventory.userNumber);
itslinear 81:a869abf56e85 422 fprintf(fp,"%s\r\n", medInventory.pill[0]);
itslinear 80:6e3eb8246ced 423 fprintf(fp,"%02d\r\n",medInventory.medContainer.container[0]);
itslinear 81:a869abf56e85 424 fprintf(fp,"%s\r\n", medInventory.pill[1]);
itslinear 80:6e3eb8246ced 425 fprintf(fp,"%02d\r\n",medInventory.medContainer.container[1]);
itslinear 81:a869abf56e85 426 fprintf(fp,"%s\r\n", medInventory.pill[2]);
itslinear 80:6e3eb8246ced 427 fprintf(fp,"%02d\r\n",medInventory.medContainer.container[2]);
itslinear 81:a869abf56e85 428 fprintf(fp,"%s\r\n", medInventory.pill[3]);
itslinear 80:6e3eb8246ced 429 fprintf(fp,"%02d\r\n",medInventory.medContainer.container[3]);
itslinear 81:a869abf56e85 430 fprintf(fp,"%s\r\n", medInventory.pill[4]);
itslinear 80:6e3eb8246ced 431 fprintf(fp,"%02d\r\n",medInventory.medContainer.container[4]);
itslinear 81:a869abf56e85 432 fprintf(fp,"%s\r\n", medInventory.pill[5]);
itslinear 80:6e3eb8246ced 433 fprintf(fp,"%02d\r\n",medInventory.medContainer.container[5]);
itslinear 80:6e3eb8246ced 434 fprintf(fp,"%02d;%02d;%02d;%02d;%02d;\r\n", medInventory.currentTime.day, medInventory.currentTime.month, medInventory.currentTime.year, medInventory.currentTime.hour, medInventory.currentTime.minute);
itslinear 89:f63e4736d875 435
itslinear 66:0d43bd7ed179 436 printf("done!\r\n");
itslinear 80:6e3eb8246ced 437
itslinear 66:0d43bd7ed179 438 //close file
itslinear 66:0d43bd7ed179 439 printf("closing file %s...",filepath);
itslinear 66:0d43bd7ed179 440 fclose(fp);
itslinear 66:0d43bd7ed179 441 printf("done!\n\r");
itslinear 66:0d43bd7ed179 442 //Unmount the SD card
itslinear 66:0d43bd7ed179 443 printf("Unmounting SD card...");
itslinear 66:0d43bd7ed179 444 sd.unmount();
itslinear 66:0d43bd7ed179 445 printf("done!\n\r");
itslinear 80:6e3eb8246ced 446
itslinear 66:0d43bd7ed179 447 return EXIT_SUCCESS;
itslinear 66:0d43bd7ed179 448 }
itslinear 66:0d43bd7ed179 449
itslinear 66:0d43bd7ed179 450 int write_medError(char error[], s_time time)
itslinear 66:0d43bd7ed179 451 {
itslinear 89:f63e4736d875 452 char filepath[] = "/sd/protocol/Error.txt";
itslinear 66:0d43bd7ed179 453
itslinear 66:0d43bd7ed179 454 //Configure CRC, large frames, and write validation
itslinear 66:0d43bd7ed179 455 sd.crc(true);
itslinear 66:0d43bd7ed179 456 sd.large_frames(true);
itslinear 66:0d43bd7ed179 457 sd.write_validation(true);
itslinear 66:0d43bd7ed179 458
itslinear 66:0d43bd7ed179 459 //mount SD card
itslinear 66:0d43bd7ed179 460 printf("\n\rMounting SD card...");
itslinear 66:0d43bd7ed179 461 if (sd.mount() != 0) {
itslinear 66:0d43bd7ed179 462 printf("failed!\n\r");
itslinear 66:0d43bd7ed179 463 return EXIT_FAILURE;
itslinear 66:0d43bd7ed179 464 }
itslinear 66:0d43bd7ed179 465 printf("success!\n\r");
itslinear 66:0d43bd7ed179 466
itslinear 77:c2e22d1e5d44 467 //open file for append
itslinear 66:0d43bd7ed179 468 printf("open file %s...",filepath);
itslinear 66:0d43bd7ed179 469 FILE *fp = fopen(filepath, "a");
itslinear 66:0d43bd7ed179 470 if(fp == NULL) {
itslinear 66:0d43bd7ed179 471 printf("failed!\n\r");
itslinear 66:0d43bd7ed179 472 return EXIT_FAILURE;
itslinear 66:0d43bd7ed179 473 }
itslinear 66:0d43bd7ed179 474 printf("success!\n\r");
itslinear 66:0d43bd7ed179 475 printf("writing to SD card...");
itslinear 77:c2e22d1e5d44 476 fprintf(fp,"%02d;%02d;%02d;%02d;%02d;%s\r\n", time.day, time.month, time.year, time.hour, time.minute, error);
itslinear 80:6e3eb8246ced 477
itslinear 66:0d43bd7ed179 478 printf("done!\r\n");
itslinear 80:6e3eb8246ced 479
itslinear 66:0d43bd7ed179 480 //close file
itslinear 66:0d43bd7ed179 481 printf("closing file %s...",filepath);
itslinear 66:0d43bd7ed179 482 fclose(fp);
itslinear 66:0d43bd7ed179 483 printf("done!\n\r");
itslinear 66:0d43bd7ed179 484 //Unmount the SD card
itslinear 66:0d43bd7ed179 485 printf("Unmounting SD card...");
itslinear 66:0d43bd7ed179 486 sd.unmount();
itslinear 66:0d43bd7ed179 487 printf("done!\n\r");
itslinear 80:6e3eb8246ced 488
itslinear 66:0d43bd7ed179 489 return EXIT_SUCCESS;
itslinear 66:0d43bd7ed179 490 }
itslinear 66:0d43bd7ed179 491
itslinear 77:c2e22d1e5d44 492 int erase_medError()
itslinear 66:0d43bd7ed179 493 {
itslinear 80:6e3eb8246ced 494 char filepath[] = "/sd/protocol/Error.txt";
itslinear 80:6e3eb8246ced 495
itslinear 66:0d43bd7ed179 496 //Configure CRC, large frames, and write validation
itslinear 66:0d43bd7ed179 497 sd.crc(true);
itslinear 66:0d43bd7ed179 498 sd.large_frames(true);
itslinear 66:0d43bd7ed179 499 sd.write_validation(true);
itslinear 66:0d43bd7ed179 500
itslinear 66:0d43bd7ed179 501 //mount SD card
itslinear 66:0d43bd7ed179 502 printf("\n\rMounting SD card...");
itslinear 66:0d43bd7ed179 503 if (sd.mount() != 0) {
itslinear 66:0d43bd7ed179 504 printf("failed!\n\r");
itslinear 66:0d43bd7ed179 505 return EXIT_FAILURE;
itslinear 66:0d43bd7ed179 506 }
itslinear 66:0d43bd7ed179 507
itslinear 77:c2e22d1e5d44 508 //open file for write
itslinear 77:c2e22d1e5d44 509 printf("open file %s...",filepath);
itslinear 77:c2e22d1e5d44 510 FILE *fp = fopen(filepath, "w");
itslinear 77:c2e22d1e5d44 511 if(fp == NULL) {
itslinear 77:c2e22d1e5d44 512 printf("failed!\n\r");
itslinear 77:c2e22d1e5d44 513 return EXIT_FAILURE;
itslinear 77:c2e22d1e5d44 514 }
itslinear 80:6e3eb8246ced 515
itslinear 77:c2e22d1e5d44 516 //close file
itslinear 77:c2e22d1e5d44 517 printf("closing file %s...",filepath);
itslinear 77:c2e22d1e5d44 518 fclose(fp);
itslinear 80:6e3eb8246ced 519
itslinear 66:0d43bd7ed179 520 sd.unmount();
itslinear 80:6e3eb8246ced 521
itslinear 77:c2e22d1e5d44 522 printf("done");
itslinear 80:6e3eb8246ced 523
itslinear 66:0d43bd7ed179 524 return EXIT_SUCCESS;
itslinear 80:6e3eb8246ced 525 }
itslinear 80:6e3eb8246ced 526
itslinear 80:6e3eb8246ced 527 char *read_medProtocol(char user)
itslinear 80:6e3eb8246ced 528 {
itslinear 80:6e3eb8246ced 529
itslinear 80:6e3eb8246ced 530 static char *buff;
itslinear 80:6e3eb8246ced 531 long numbytes;
itslinear 80:6e3eb8246ced 532
itslinear 81:a869abf56e85 533 char filepath[] = "/sd/protocol/medProtocolUser2.txt";
itslinear 80:6e3eb8246ced 534 if(user==0) {
itslinear 81:a869abf56e85 535 strcpy(filepath, "/sd/protocol/medProtocolUser1.txt");
itslinear 80:6e3eb8246ced 536 }
itslinear 80:6e3eb8246ced 537
itslinear 80:6e3eb8246ced 538 //Configure CRC, large frames, and write validation
itslinear 80:6e3eb8246ced 539 sd.crc(true);
itslinear 80:6e3eb8246ced 540 sd.large_frames(true);
itslinear 80:6e3eb8246ced 541 sd.write_validation(true);
itslinear 80:6e3eb8246ced 542
itslinear 80:6e3eb8246ced 543 //mount SD card
itslinear 80:6e3eb8246ced 544 printf("\n\rMounting SD card...");
itslinear 80:6e3eb8246ced 545 if (sd.mount() != 0) {
itslinear 80:6e3eb8246ced 546 printf("failed!\n\r");
itslinear 80:6e3eb8246ced 547 return NULL;
itslinear 80:6e3eb8246ced 548 }
itslinear 80:6e3eb8246ced 549 printf("success!\n\r");
itslinear 80:6e3eb8246ced 550
itslinear 80:6e3eb8246ced 551 //open file for read
itslinear 80:6e3eb8246ced 552 printf("open file %s...",filepath);
itslinear 80:6e3eb8246ced 553 FILE *fp = fopen(filepath, "r");
itslinear 80:6e3eb8246ced 554 if(fp == NULL) {
itslinear 80:6e3eb8246ced 555 printf("failed!\n\r");
itslinear 80:6e3eb8246ced 556 return NULL;
itslinear 80:6e3eb8246ced 557 }
itslinear 80:6e3eb8246ced 558 printf("success!\n\r");
itslinear 80:6e3eb8246ced 559
itslinear 80:6e3eb8246ced 560 /* Get the number of bytes */
itslinear 80:6e3eb8246ced 561 fseek(fp, 0L, SEEK_END);
itslinear 80:6e3eb8246ced 562 numbytes = ftell(fp);
itslinear 80:6e3eb8246ced 563
itslinear 80:6e3eb8246ced 564 /* reset the file position indicator to
itslinear 80:6e3eb8246ced 565 the beginning of the file */
itslinear 80:6e3eb8246ced 566 fseek(fp, 0L, SEEK_SET);
itslinear 80:6e3eb8246ced 567
itslinear 80:6e3eb8246ced 568 /* grab sufficient memory for the
itslinear 80:6e3eb8246ced 569 buffer to hold the text */
itslinear 80:6e3eb8246ced 570 buff = (char*)calloc(numbytes, sizeof(char));
itslinear 80:6e3eb8246ced 571
itslinear 80:6e3eb8246ced 572
itslinear 80:6e3eb8246ced 573 /* copy all the text into the buffer */
itslinear 80:6e3eb8246ced 574 fread(buff, sizeof(char), numbytes-1, fp);
itslinear 80:6e3eb8246ced 575 fclose(fp);
itslinear 80:6e3eb8246ced 576
itslinear 80:6e3eb8246ced 577 /* free the memory we used for the buffer */
itslinear 80:6e3eb8246ced 578 //free(buff);
itslinear 80:6e3eb8246ced 579
itslinear 80:6e3eb8246ced 580 //Unmount the SD card
itslinear 80:6e3eb8246ced 581 sd.unmount();
itslinear 80:6e3eb8246ced 582
itslinear 80:6e3eb8246ced 583 return buff;
itslinear 80:6e3eb8246ced 584 }
itslinear 80:6e3eb8246ced 585
itslinear 80:6e3eb8246ced 586
itslinear 80:6e3eb8246ced 587 s_medInventory read_medInventory()
itslinear 80:6e3eb8246ced 588 {
itslinear 81:a869abf56e85 589 s_medInventory Inventory;
itslinear 80:6e3eb8246ced 590
itslinear 80:6e3eb8246ced 591 char filepath[] = "/sd/protocol/medInventory.txt";
itslinear 80:6e3eb8246ced 592
itslinear 80:6e3eb8246ced 593 //Configure CRC, large frames, and write validation
itslinear 80:6e3eb8246ced 594 sd.crc(true);
itslinear 80:6e3eb8246ced 595 sd.large_frames(true);
itslinear 80:6e3eb8246ced 596 sd.write_validation(true);
itslinear 80:6e3eb8246ced 597
itslinear 80:6e3eb8246ced 598 //mount SD card
itslinear 80:6e3eb8246ced 599 printf("\n\rMounting SD card...");
itslinear 80:6e3eb8246ced 600 if (sd.mount() != 0) {
itslinear 80:6e3eb8246ced 601 printf("failed!\n\r");
itslinear 81:a869abf56e85 602 return Inventory;
itslinear 80:6e3eb8246ced 603 }
itslinear 80:6e3eb8246ced 604 printf("success!\n\r");
itslinear 80:6e3eb8246ced 605
itslinear 80:6e3eb8246ced 606 //open file for read
itslinear 80:6e3eb8246ced 607 printf("open file %s...",filepath);
itslinear 80:6e3eb8246ced 608 FILE *fp = fopen(filepath, "r");
itslinear 80:6e3eb8246ced 609 if(fp == NULL) {
itslinear 80:6e3eb8246ced 610 printf("failed!\n\r");
itslinear 81:a869abf56e85 611 return Inventory;
itslinear 80:6e3eb8246ced 612 }
itslinear 80:6e3eb8246ced 613 printf("success!\n\r");
itslinear 80:6e3eb8246ced 614
itslinear 81:a869abf56e85 615 char *str;
itslinear 80:6e3eb8246ced 616
itslinear 81:a869abf56e85 617 int linecounter = 1;
itslinear 89:f63e4736d875 618 int line = 3;
itslinear 81:a869abf56e85 619 int i = 0;
itslinear 81:a869abf56e85 620
itslinear 81:a869abf56e85 621 while (fgets(str, 50, fp) != NULL) {
itslinear 81:a869abf56e85 622
itslinear 80:6e3eb8246ced 623 switch (line) {
itslinear 80:6e3eb8246ced 624 case 1:
itslinear 89:f63e4736d875 625 if(linecounter != 14) {
itslinear 89:f63e4736d875 626 strcpy(Inventory.pill[i], strtok(str, "\r"));
itslinear 89:f63e4736d875 627 line = 2;
itslinear 81:a869abf56e85 628 }
itslinear 89:f63e4736d875 629
itslinear 80:6e3eb8246ced 630 break;
itslinear 80:6e3eb8246ced 631
itslinear 80:6e3eb8246ced 632 case 2:
itslinear 89:f63e4736d875 633 Inventory.medContainer.container[i] = atoi(strtok(str, "\r"));
itslinear 81:a869abf56e85 634 line =1;
itslinear 81:a869abf56e85 635 i++;
itslinear 80:6e3eb8246ced 636 break;
itslinear 89:f63e4736d875 637
itslinear 89:f63e4736d875 638 case 3:
itslinear 89:f63e4736d875 639 Inventory.userNumber = atoi(strtok(str, "\r"));
itslinear 89:f63e4736d875 640 line = 1;
itslinear 89:f63e4736d875 641
itslinear 89:f63e4736d875 642 break;
itslinear 80:6e3eb8246ced 643 }
itslinear 80:6e3eb8246ced 644 linecounter++;
itslinear 80:6e3eb8246ced 645 }
itslinear 80:6e3eb8246ced 646 //close file
itslinear 80:6e3eb8246ced 647 fclose(fp);
itslinear 80:6e3eb8246ced 648 //Unmount the SD card
itslinear 80:6e3eb8246ced 649 sd.unmount();
itslinear 80:6e3eb8246ced 650
itslinear 81:a869abf56e85 651 return Inventory;
itslinear 81:a869abf56e85 652 }
itslinear 81:a869abf56e85 653
itslinear 81:a869abf56e85 654
itslinear 81:a869abf56e85 655 char *read_medError()
itslinear 81:a869abf56e85 656 {
itslinear 81:a869abf56e85 657 static char *buff;
itslinear 81:a869abf56e85 658 long numbytes;
itslinear 81:a869abf56e85 659
itslinear 81:a869abf56e85 660 char filepath[] = "/sd/protocol/Error.txt";
itslinear 81:a869abf56e85 661
itslinear 81:a869abf56e85 662
itslinear 81:a869abf56e85 663 //Configure CRC, large frames, and write validation
itslinear 81:a869abf56e85 664 sd.crc(true);
itslinear 81:a869abf56e85 665 sd.large_frames(true);
itslinear 81:a869abf56e85 666 sd.write_validation(true);
itslinear 81:a869abf56e85 667
itslinear 81:a869abf56e85 668 //mount SD card
itslinear 81:a869abf56e85 669 printf("\n\rMounting SD card...");
itslinear 81:a869abf56e85 670 if (sd.mount() != 0) {
itslinear 81:a869abf56e85 671 printf("failed!\n\r");
itslinear 81:a869abf56e85 672 return NULL;
itslinear 81:a869abf56e85 673 }
itslinear 81:a869abf56e85 674 printf("success!\n\r");
itslinear 81:a869abf56e85 675
itslinear 81:a869abf56e85 676 //open file for read
itslinear 81:a869abf56e85 677 printf("open file %s...",filepath);
itslinear 81:a869abf56e85 678 FILE *fp = fopen(filepath, "r");
itslinear 81:a869abf56e85 679 if(fp == NULL) {
itslinear 81:a869abf56e85 680 printf("failed!\n\r");
itslinear 81:a869abf56e85 681 return NULL;
itslinear 81:a869abf56e85 682 }
itslinear 81:a869abf56e85 683 printf("success!\n\r");
itslinear 81:a869abf56e85 684
itslinear 81:a869abf56e85 685 /* Get the number of bytes */
itslinear 81:a869abf56e85 686 fseek(fp, 0L, SEEK_END);
itslinear 81:a869abf56e85 687 numbytes = ftell(fp);
itslinear 81:a869abf56e85 688
itslinear 81:a869abf56e85 689 /* reset the file position indicator to
itslinear 81:a869abf56e85 690 the beginning of the file */
itslinear 81:a869abf56e85 691 fseek(fp, 0L, SEEK_SET);
itslinear 81:a869abf56e85 692
itslinear 81:a869abf56e85 693 /* grab sufficient memory for the
itslinear 81:a869abf56e85 694 buffer to hold the text */
itslinear 81:a869abf56e85 695 buff = (char*)calloc(numbytes, sizeof(char));
itslinear 81:a869abf56e85 696
itslinear 81:a869abf56e85 697
itslinear 81:a869abf56e85 698 /* copy all the text into the buffer */
itslinear 81:a869abf56e85 699 fread(buff, sizeof(char), numbytes-1, fp);
itslinear 81:a869abf56e85 700 fclose(fp);
itslinear 81:a869abf56e85 701
itslinear 81:a869abf56e85 702 /* free the memory we used for the buffer */
itslinear 81:a869abf56e85 703 //free(buff);
itslinear 81:a869abf56e85 704
itslinear 81:a869abf56e85 705 //Unmount the SD card
itslinear 81:a869abf56e85 706 sd.unmount();
itslinear 81:a869abf56e85 707
itslinear 81:a869abf56e85 708 return buff;
itslinear 80:6e3eb8246ced 709 }
itslinear 80:6e3eb8246ced 710
itslinear 89:f63e4736d875 711 char *read_Inventory()
itslinear 89:f63e4736d875 712 {
itslinear 89:f63e4736d875 713 static char *buff;
itslinear 89:f63e4736d875 714 long numbytes;
itslinear 89:f63e4736d875 715
itslinear 89:f63e4736d875 716 char filepath[] = "/sd/protocol/medInventory.txt";
itslinear 89:f63e4736d875 717
itslinear 89:f63e4736d875 718 //Configure CRC, large frames, and write validation
itslinear 89:f63e4736d875 719 sd.crc(true);
itslinear 89:f63e4736d875 720 sd.large_frames(true);
itslinear 89:f63e4736d875 721 sd.write_validation(true);
itslinear 89:f63e4736d875 722
itslinear 89:f63e4736d875 723 //mount SD card
itslinear 89:f63e4736d875 724 printf("\n\rMounting SD card...");
itslinear 89:f63e4736d875 725 if (sd.mount() != 0) {
itslinear 89:f63e4736d875 726 printf("failed!\n\r");
itslinear 89:f63e4736d875 727 return NULL;
itslinear 89:f63e4736d875 728 }
itslinear 89:f63e4736d875 729 printf("success!\n\r");
itslinear 89:f63e4736d875 730
itslinear 89:f63e4736d875 731 //open file for read
itslinear 89:f63e4736d875 732 printf("open file %s...",filepath);
itslinear 89:f63e4736d875 733 FILE *fp = fopen(filepath, "r");
itslinear 89:f63e4736d875 734 if(fp == NULL) {
itslinear 89:f63e4736d875 735 printf("failed!\n\r");
itslinear 89:f63e4736d875 736 return NULL;
itslinear 89:f63e4736d875 737 }
itslinear 89:f63e4736d875 738 printf("success!\n\r");
itslinear 89:f63e4736d875 739
itslinear 89:f63e4736d875 740 /* Get the number of bytes */
itslinear 89:f63e4736d875 741 fseek(fp, 0L, SEEK_END);
itslinear 89:f63e4736d875 742 numbytes = ftell(fp);
itslinear 89:f63e4736d875 743
itslinear 89:f63e4736d875 744 /* reset the file position indicator to
itslinear 89:f63e4736d875 745 the beginning of the file */
itslinear 89:f63e4736d875 746 fseek(fp, 0L, SEEK_SET);
itslinear 89:f63e4736d875 747
itslinear 89:f63e4736d875 748 /* grab sufficient memory for the
itslinear 89:f63e4736d875 749 buffer to hold the text */
itslinear 89:f63e4736d875 750 buff = (char*)calloc(numbytes, sizeof(char));
itslinear 89:f63e4736d875 751
itslinear 89:f63e4736d875 752
itslinear 89:f63e4736d875 753 /* copy all the text into the buffer */
itslinear 89:f63e4736d875 754 fread(buff, sizeof(char), numbytes-1, fp);
itslinear 89:f63e4736d875 755 fclose(fp);
itslinear 89:f63e4736d875 756
itslinear 89:f63e4736d875 757 /* free the memory we used for the buffer */
itslinear 89:f63e4736d875 758 //free(buff);
itslinear 89:f63e4736d875 759
itslinear 89:f63e4736d875 760 //Unmount the SD card
itslinear 89:f63e4736d875 761 sd.unmount();
itslinear 89:f63e4736d875 762
itslinear 89:f63e4736d875 763 return buff;
itslinear 89:f63e4736d875 764 }
itslinear 89:f63e4736d875 765
itslinear 80:6e3eb8246ced 766
itslinear 89:f63e4736d875 767 char *read_Medication(char user)
itslinear 89:f63e4736d875 768 {
itslinear 89:f63e4736d875 769 static char *buff;
itslinear 89:f63e4736d875 770 long numbytes;
itslinear 89:f63e4736d875 771
itslinear 89:f63e4736d875 772 char filepath[] = "/sd/medication/medicationUser2.txt";
itslinear 89:f63e4736d875 773 if(user==0) {
itslinear 89:f63e4736d875 774 strcpy(filepath, "/sd/medication/medicationUser1.txt");
itslinear 89:f63e4736d875 775 }
itslinear 89:f63e4736d875 776
itslinear 89:f63e4736d875 777 //Configure CRC, large frames, and write validation
itslinear 89:f63e4736d875 778 sd.crc(true);
itslinear 89:f63e4736d875 779 sd.large_frames(true);
itslinear 89:f63e4736d875 780 sd.write_validation(true);
itslinear 89:f63e4736d875 781
itslinear 89:f63e4736d875 782 //mount SD card
itslinear 89:f63e4736d875 783 printf("\n\rMounting SD card...");
itslinear 89:f63e4736d875 784 if (sd.mount() != 0) {
itslinear 89:f63e4736d875 785 printf("failed!\n\r");
itslinear 89:f63e4736d875 786 return NULL;
itslinear 89:f63e4736d875 787 }
itslinear 89:f63e4736d875 788 printf("success!\n\r");
itslinear 89:f63e4736d875 789
itslinear 89:f63e4736d875 790 //open file for read
itslinear 89:f63e4736d875 791 printf("open file %s...",filepath);
itslinear 89:f63e4736d875 792 FILE *fp = fopen(filepath, "r");
itslinear 89:f63e4736d875 793 if(fp == NULL) {
itslinear 89:f63e4736d875 794 printf("failed!\n\r");
itslinear 89:f63e4736d875 795 return NULL;
itslinear 89:f63e4736d875 796 }
itslinear 89:f63e4736d875 797 printf("success!\n\r");
itslinear 89:f63e4736d875 798
itslinear 89:f63e4736d875 799 /* Get the number of bytes */
itslinear 89:f63e4736d875 800 fseek(fp, 0L, SEEK_END);
itslinear 89:f63e4736d875 801 numbytes = ftell(fp);
itslinear 89:f63e4736d875 802
itslinear 89:f63e4736d875 803 /* reset the file position indicator to
itslinear 89:f63e4736d875 804 the beginning of the file */
itslinear 89:f63e4736d875 805 fseek(fp, 0L, SEEK_SET);
itslinear 89:f63e4736d875 806
itslinear 89:f63e4736d875 807 /* grab sufficient memory for the
itslinear 89:f63e4736d875 808 buffer to hold the text */
itslinear 89:f63e4736d875 809 buff = (char*)calloc(numbytes, sizeof(char));
itslinear 89:f63e4736d875 810
itslinear 89:f63e4736d875 811 /* copy all the text into the buffer */
itslinear 89:f63e4736d875 812 fread(buff, sizeof(char), numbytes-1, fp);
itslinear 89:f63e4736d875 813 fclose(fp);
itslinear 89:f63e4736d875 814
itslinear 89:f63e4736d875 815 /* free the memory we used for the buffer */
itslinear 89:f63e4736d875 816 //free(buff);
itslinear 89:f63e4736d875 817
itslinear 89:f63e4736d875 818 //Unmount the SD card
itslinear 89:f63e4736d875 819 sd.unmount();
itslinear 89:f63e4736d875 820
itslinear 89:f63e4736d875 821 return buff;
itslinear 89:f63e4736d875 822 }