PES 4 - Smart Medication Dispenser / PES4_ProgrammeforDesignReview2

Dependencies:   SDFileSystem mbed

Fork of PES4_Programme by PES 4 - Smart Medication Dispenser

Committer:
itslinear
Date:
Mon Apr 09 15:43:45 2018 +0000
Revision:
90:d0805b58b0ae
Parent:
81:a869abf56e85
Child:
94:b24d2b432b27
added stuff;

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 81:a869abf56e85 421 fprintf(fp,"%s\r\n", medInventory.pill[0]);
itslinear 80:6e3eb8246ced 422 fprintf(fp,"%02d\r\n",medInventory.medContainer.container[0]);
itslinear 81:a869abf56e85 423 fprintf(fp,"%s\r\n", medInventory.pill[1]);
itslinear 80:6e3eb8246ced 424 fprintf(fp,"%02d\r\n",medInventory.medContainer.container[1]);
itslinear 81:a869abf56e85 425 fprintf(fp,"%s\r\n", medInventory.pill[2]);
itslinear 80:6e3eb8246ced 426 fprintf(fp,"%02d\r\n",medInventory.medContainer.container[2]);
itslinear 81:a869abf56e85 427 fprintf(fp,"%s\r\n", medInventory.pill[3]);
itslinear 80:6e3eb8246ced 428 fprintf(fp,"%02d\r\n",medInventory.medContainer.container[3]);
itslinear 81:a869abf56e85 429 fprintf(fp,"%s\r\n", medInventory.pill[4]);
itslinear 80:6e3eb8246ced 430 fprintf(fp,"%02d\r\n",medInventory.medContainer.container[4]);
itslinear 81:a869abf56e85 431 fprintf(fp,"%s\r\n", medInventory.pill[5]);
itslinear 80:6e3eb8246ced 432 fprintf(fp,"%02d\r\n",medInventory.medContainer.container[5]);
itslinear 80:6e3eb8246ced 433 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 81:a869abf56e85 434
itslinear 66:0d43bd7ed179 435 printf("done!\r\n");
itslinear 80:6e3eb8246ced 436
itslinear 66:0d43bd7ed179 437 //close file
itslinear 66:0d43bd7ed179 438 printf("closing file %s...",filepath);
itslinear 66:0d43bd7ed179 439 fclose(fp);
itslinear 66:0d43bd7ed179 440 printf("done!\n\r");
itslinear 66:0d43bd7ed179 441 //Unmount the SD card
itslinear 66:0d43bd7ed179 442 printf("Unmounting SD card...");
itslinear 66:0d43bd7ed179 443 sd.unmount();
itslinear 66:0d43bd7ed179 444 printf("done!\n\r");
itslinear 80:6e3eb8246ced 445
itslinear 66:0d43bd7ed179 446 return EXIT_SUCCESS;
itslinear 66:0d43bd7ed179 447 }
itslinear 66:0d43bd7ed179 448
itslinear 66:0d43bd7ed179 449 int write_medError(char error[], s_time time)
itslinear 66:0d43bd7ed179 450 {
itslinear 66:0d43bd7ed179 451 char filepath[] = "/sd/protocol/Error.txt"; // Muss noch auf SD Karte erstellt werden!!!
itslinear 66:0d43bd7ed179 452
itslinear 66:0d43bd7ed179 453 //Configure CRC, large frames, and write validation
itslinear 66:0d43bd7ed179 454 sd.crc(true);
itslinear 66:0d43bd7ed179 455 sd.large_frames(true);
itslinear 66:0d43bd7ed179 456 sd.write_validation(true);
itslinear 66:0d43bd7ed179 457
itslinear 66:0d43bd7ed179 458 //mount SD card
itslinear 66:0d43bd7ed179 459 printf("\n\rMounting SD card...");
itslinear 66:0d43bd7ed179 460 if (sd.mount() != 0) {
itslinear 66:0d43bd7ed179 461 printf("failed!\n\r");
itslinear 66:0d43bd7ed179 462 return EXIT_FAILURE;
itslinear 66:0d43bd7ed179 463 }
itslinear 66:0d43bd7ed179 464 printf("success!\n\r");
itslinear 66:0d43bd7ed179 465
itslinear 77:c2e22d1e5d44 466 //open file for append
itslinear 66:0d43bd7ed179 467 printf("open file %s...",filepath);
itslinear 66:0d43bd7ed179 468 FILE *fp = fopen(filepath, "a");
itslinear 66:0d43bd7ed179 469 if(fp == NULL) {
itslinear 66:0d43bd7ed179 470 printf("failed!\n\r");
itslinear 66:0d43bd7ed179 471 return EXIT_FAILURE;
itslinear 66:0d43bd7ed179 472 }
itslinear 66:0d43bd7ed179 473 printf("success!\n\r");
itslinear 66:0d43bd7ed179 474 printf("writing to SD card...");
itslinear 77:c2e22d1e5d44 475 fprintf(fp,"%02d;%02d;%02d;%02d;%02d;%s\r\n", time.day, time.month, time.year, time.hour, time.minute, error);
itslinear 80:6e3eb8246ced 476
itslinear 66:0d43bd7ed179 477 printf("done!\r\n");
itslinear 80:6e3eb8246ced 478
itslinear 66:0d43bd7ed179 479 //close file
itslinear 66:0d43bd7ed179 480 printf("closing file %s...",filepath);
itslinear 66:0d43bd7ed179 481 fclose(fp);
itslinear 66:0d43bd7ed179 482 printf("done!\n\r");
itslinear 66:0d43bd7ed179 483 //Unmount the SD card
itslinear 66:0d43bd7ed179 484 printf("Unmounting SD card...");
itslinear 66:0d43bd7ed179 485 sd.unmount();
itslinear 66:0d43bd7ed179 486 printf("done!\n\r");
itslinear 80:6e3eb8246ced 487
itslinear 66:0d43bd7ed179 488 return EXIT_SUCCESS;
itslinear 66:0d43bd7ed179 489 }
itslinear 66:0d43bd7ed179 490
itslinear 77:c2e22d1e5d44 491 int erase_medError()
itslinear 66:0d43bd7ed179 492 {
itslinear 80:6e3eb8246ced 493 char filepath[] = "/sd/protocol/Error.txt";
itslinear 80:6e3eb8246ced 494
itslinear 66:0d43bd7ed179 495 //Configure CRC, large frames, and write validation
itslinear 66:0d43bd7ed179 496 sd.crc(true);
itslinear 66:0d43bd7ed179 497 sd.large_frames(true);
itslinear 66:0d43bd7ed179 498 sd.write_validation(true);
itslinear 66:0d43bd7ed179 499
itslinear 66:0d43bd7ed179 500 //mount SD card
itslinear 66:0d43bd7ed179 501 printf("\n\rMounting SD card...");
itslinear 66:0d43bd7ed179 502 if (sd.mount() != 0) {
itslinear 66:0d43bd7ed179 503 printf("failed!\n\r");
itslinear 66:0d43bd7ed179 504 return EXIT_FAILURE;
itslinear 66:0d43bd7ed179 505 }
itslinear 66:0d43bd7ed179 506
itslinear 77:c2e22d1e5d44 507 //open file for write
itslinear 77:c2e22d1e5d44 508 printf("open file %s...",filepath);
itslinear 77:c2e22d1e5d44 509 FILE *fp = fopen(filepath, "w");
itslinear 77:c2e22d1e5d44 510 if(fp == NULL) {
itslinear 77:c2e22d1e5d44 511 printf("failed!\n\r");
itslinear 77:c2e22d1e5d44 512 return EXIT_FAILURE;
itslinear 77:c2e22d1e5d44 513 }
itslinear 80:6e3eb8246ced 514
itslinear 77:c2e22d1e5d44 515 //close file
itslinear 77:c2e22d1e5d44 516 printf("closing file %s...",filepath);
itslinear 77:c2e22d1e5d44 517 fclose(fp);
itslinear 80:6e3eb8246ced 518
itslinear 66:0d43bd7ed179 519 sd.unmount();
itslinear 80:6e3eb8246ced 520
itslinear 77:c2e22d1e5d44 521 printf("done");
itslinear 80:6e3eb8246ced 522
itslinear 66:0d43bd7ed179 523 return EXIT_SUCCESS;
itslinear 80:6e3eb8246ced 524 }
itslinear 80:6e3eb8246ced 525
itslinear 80:6e3eb8246ced 526 char *read_medProtocol(char user)
itslinear 80:6e3eb8246ced 527 {
itslinear 80:6e3eb8246ced 528
itslinear 80:6e3eb8246ced 529 static char *buff;
itslinear 80:6e3eb8246ced 530 long numbytes;
itslinear 80:6e3eb8246ced 531
itslinear 81:a869abf56e85 532 char filepath[] = "/sd/protocol/medProtocolUser2.txt";
itslinear 80:6e3eb8246ced 533 if(user==0) {
itslinear 81:a869abf56e85 534 strcpy(filepath, "/sd/protocol/medProtocolUser1.txt");
itslinear 80:6e3eb8246ced 535 }
itslinear 80:6e3eb8246ced 536
itslinear 80:6e3eb8246ced 537 //Configure CRC, large frames, and write validation
itslinear 80:6e3eb8246ced 538 sd.crc(true);
itslinear 80:6e3eb8246ced 539 sd.large_frames(true);
itslinear 80:6e3eb8246ced 540 sd.write_validation(true);
itslinear 80:6e3eb8246ced 541
itslinear 80:6e3eb8246ced 542 //mount SD card
itslinear 80:6e3eb8246ced 543 printf("\n\rMounting SD card...");
itslinear 80:6e3eb8246ced 544 if (sd.mount() != 0) {
itslinear 80:6e3eb8246ced 545 printf("failed!\n\r");
itslinear 80:6e3eb8246ced 546 return NULL;
itslinear 80:6e3eb8246ced 547 }
itslinear 80:6e3eb8246ced 548 printf("success!\n\r");
itslinear 80:6e3eb8246ced 549
itslinear 80:6e3eb8246ced 550 //open file for read
itslinear 80:6e3eb8246ced 551 printf("open file %s...",filepath);
itslinear 80:6e3eb8246ced 552 FILE *fp = fopen(filepath, "r");
itslinear 80:6e3eb8246ced 553 if(fp == NULL) {
itslinear 80:6e3eb8246ced 554 printf("failed!\n\r");
itslinear 80:6e3eb8246ced 555 return NULL;
itslinear 80:6e3eb8246ced 556 }
itslinear 80:6e3eb8246ced 557 printf("success!\n\r");
itslinear 80:6e3eb8246ced 558
itslinear 80:6e3eb8246ced 559 /* Get the number of bytes */
itslinear 80:6e3eb8246ced 560 fseek(fp, 0L, SEEK_END);
itslinear 80:6e3eb8246ced 561 numbytes = ftell(fp);
itslinear 80:6e3eb8246ced 562
itslinear 80:6e3eb8246ced 563 /* reset the file position indicator to
itslinear 80:6e3eb8246ced 564 the beginning of the file */
itslinear 80:6e3eb8246ced 565 fseek(fp, 0L, SEEK_SET);
itslinear 80:6e3eb8246ced 566
itslinear 80:6e3eb8246ced 567 /* grab sufficient memory for the
itslinear 80:6e3eb8246ced 568 buffer to hold the text */
itslinear 80:6e3eb8246ced 569 buff = (char*)calloc(numbytes, sizeof(char));
itslinear 80:6e3eb8246ced 570
itslinear 80:6e3eb8246ced 571
itslinear 80:6e3eb8246ced 572 /* copy all the text into the buffer */
itslinear 80:6e3eb8246ced 573 fread(buff, sizeof(char), numbytes-1, fp);
itslinear 80:6e3eb8246ced 574 fclose(fp);
itslinear 80:6e3eb8246ced 575
itslinear 80:6e3eb8246ced 576 /* free the memory we used for the buffer */
itslinear 80:6e3eb8246ced 577 //free(buff);
itslinear 80:6e3eb8246ced 578
itslinear 80:6e3eb8246ced 579 //Unmount the SD card
itslinear 80:6e3eb8246ced 580 sd.unmount();
itslinear 80:6e3eb8246ced 581
itslinear 80:6e3eb8246ced 582 return buff;
itslinear 80:6e3eb8246ced 583 }
itslinear 80:6e3eb8246ced 584
itslinear 80:6e3eb8246ced 585
itslinear 80:6e3eb8246ced 586 s_medInventory read_medInventory()
itslinear 80:6e3eb8246ced 587 {
itslinear 81:a869abf56e85 588 s_medInventory Inventory;
itslinear 80:6e3eb8246ced 589
itslinear 80:6e3eb8246ced 590 char filepath[] = "/sd/protocol/medInventory.txt";
itslinear 80:6e3eb8246ced 591
itslinear 80:6e3eb8246ced 592 //Configure CRC, large frames, and write validation
itslinear 80:6e3eb8246ced 593 sd.crc(true);
itslinear 80:6e3eb8246ced 594 sd.large_frames(true);
itslinear 80:6e3eb8246ced 595 sd.write_validation(true);
itslinear 80:6e3eb8246ced 596
itslinear 80:6e3eb8246ced 597 //mount SD card
itslinear 80:6e3eb8246ced 598 printf("\n\rMounting SD card...");
itslinear 80:6e3eb8246ced 599 if (sd.mount() != 0) {
itslinear 80:6e3eb8246ced 600 printf("failed!\n\r");
itslinear 81:a869abf56e85 601 return Inventory;
itslinear 80:6e3eb8246ced 602 }
itslinear 80:6e3eb8246ced 603 printf("success!\n\r");
itslinear 80:6e3eb8246ced 604
itslinear 80:6e3eb8246ced 605 //open file for read
itslinear 80:6e3eb8246ced 606 printf("open file %s...",filepath);
itslinear 80:6e3eb8246ced 607 FILE *fp = fopen(filepath, "r");
itslinear 80:6e3eb8246ced 608 if(fp == NULL) {
itslinear 80:6e3eb8246ced 609 printf("failed!\n\r");
itslinear 81:a869abf56e85 610 return Inventory;
itslinear 80:6e3eb8246ced 611 }
itslinear 80:6e3eb8246ced 612 printf("success!\n\r");
itslinear 80:6e3eb8246ced 613
itslinear 81:a869abf56e85 614 char *str;
itslinear 80:6e3eb8246ced 615
itslinear 81:a869abf56e85 616 int linecounter = 1;
itslinear 81:a869abf56e85 617 int line = 1;
itslinear 81:a869abf56e85 618 int i = 0;
itslinear 81:a869abf56e85 619
itslinear 81:a869abf56e85 620 while (fgets(str, 50, fp) != NULL) {
itslinear 81:a869abf56e85 621
itslinear 80:6e3eb8246ced 622 switch (line) {
itslinear 80:6e3eb8246ced 623 case 1:
itslinear 81:a869abf56e85 624 if(linecounter != 13){
itslinear 81:a869abf56e85 625 strcpy(Inventory.pill[i], strtok(str, "\r"));
itslinear 81:a869abf56e85 626 //printf("%s\r\n",Inventory.pill[i]);
itslinear 81:a869abf56e85 627 line = 2;
itslinear 81:a869abf56e85 628 }
itslinear 81:a869abf56e85 629
itslinear 80:6e3eb8246ced 630 break;
itslinear 80:6e3eb8246ced 631
itslinear 80:6e3eb8246ced 632 case 2:
itslinear 81:a869abf56e85 633 int temp = atoi(strtok(str, "\r"));
itslinear 81:a869abf56e85 634 //printf("%d\r\n",temp);
itslinear 81:a869abf56e85 635 Inventory.medContainer.container[i] = temp;
itslinear 81:a869abf56e85 636 line =1;
itslinear 81:a869abf56e85 637 i++;
itslinear 80:6e3eb8246ced 638 break;
itslinear 80:6e3eb8246ced 639 }
itslinear 80:6e3eb8246ced 640 linecounter++;
itslinear 80:6e3eb8246ced 641 }
itslinear 80:6e3eb8246ced 642 //close file
itslinear 80:6e3eb8246ced 643 fclose(fp);
itslinear 80:6e3eb8246ced 644 //Unmount the SD card
itslinear 80:6e3eb8246ced 645 sd.unmount();
itslinear 80:6e3eb8246ced 646
itslinear 81:a869abf56e85 647 return Inventory;
itslinear 81:a869abf56e85 648 }
itslinear 81:a869abf56e85 649
itslinear 81:a869abf56e85 650
itslinear 81:a869abf56e85 651 char *read_medError()
itslinear 81:a869abf56e85 652 {
itslinear 81:a869abf56e85 653 static char *buff;
itslinear 81:a869abf56e85 654 long numbytes;
itslinear 81:a869abf56e85 655
itslinear 81:a869abf56e85 656 char filepath[] = "/sd/protocol/Error.txt";
itslinear 81:a869abf56e85 657
itslinear 81:a869abf56e85 658
itslinear 81:a869abf56e85 659 //Configure CRC, large frames, and write validation
itslinear 81:a869abf56e85 660 sd.crc(true);
itslinear 81:a869abf56e85 661 sd.large_frames(true);
itslinear 81:a869abf56e85 662 sd.write_validation(true);
itslinear 81:a869abf56e85 663
itslinear 81:a869abf56e85 664 //mount SD card
itslinear 81:a869abf56e85 665 printf("\n\rMounting SD card...");
itslinear 81:a869abf56e85 666 if (sd.mount() != 0) {
itslinear 81:a869abf56e85 667 printf("failed!\n\r");
itslinear 81:a869abf56e85 668 return NULL;
itslinear 81:a869abf56e85 669 }
itslinear 81:a869abf56e85 670 printf("success!\n\r");
itslinear 81:a869abf56e85 671
itslinear 81:a869abf56e85 672 //open file for read
itslinear 81:a869abf56e85 673 printf("open file %s...",filepath);
itslinear 81:a869abf56e85 674 FILE *fp = fopen(filepath, "r");
itslinear 81:a869abf56e85 675 if(fp == NULL) {
itslinear 81:a869abf56e85 676 printf("failed!\n\r");
itslinear 81:a869abf56e85 677 return NULL;
itslinear 81:a869abf56e85 678 }
itslinear 81:a869abf56e85 679 printf("success!\n\r");
itslinear 81:a869abf56e85 680
itslinear 81:a869abf56e85 681 /* Get the number of bytes */
itslinear 81:a869abf56e85 682 fseek(fp, 0L, SEEK_END);
itslinear 81:a869abf56e85 683 numbytes = ftell(fp);
itslinear 81:a869abf56e85 684
itslinear 81:a869abf56e85 685 /* reset the file position indicator to
itslinear 81:a869abf56e85 686 the beginning of the file */
itslinear 81:a869abf56e85 687 fseek(fp, 0L, SEEK_SET);
itslinear 81:a869abf56e85 688
itslinear 81:a869abf56e85 689 /* grab sufficient memory for the
itslinear 81:a869abf56e85 690 buffer to hold the text */
itslinear 81:a869abf56e85 691 buff = (char*)calloc(numbytes, sizeof(char));
itslinear 81:a869abf56e85 692
itslinear 81:a869abf56e85 693
itslinear 81:a869abf56e85 694 /* copy all the text into the buffer */
itslinear 81:a869abf56e85 695 fread(buff, sizeof(char), numbytes-1, fp);
itslinear 81:a869abf56e85 696 fclose(fp);
itslinear 81:a869abf56e85 697
itslinear 81:a869abf56e85 698 /* free the memory we used for the buffer */
itslinear 81:a869abf56e85 699 //free(buff);
itslinear 81:a869abf56e85 700
itslinear 81:a869abf56e85 701 //Unmount the SD card
itslinear 81:a869abf56e85 702 sd.unmount();
itslinear 81:a869abf56e85 703
itslinear 81:a869abf56e85 704 return buff;
itslinear 80:6e3eb8246ced 705 }
itslinear 80:6e3eb8246ced 706
itslinear 90:d0805b58b0ae 707 char *read_Inventory()
itslinear 90:d0805b58b0ae 708 {
itslinear 90:d0805b58b0ae 709 static char *buff;
itslinear 90:d0805b58b0ae 710 long numbytes;
itslinear 90:d0805b58b0ae 711
itslinear 90:d0805b58b0ae 712 char filepath[] = "/sd/protocol/medInventory.txt";
itslinear 90:d0805b58b0ae 713
itslinear 90:d0805b58b0ae 714
itslinear 90:d0805b58b0ae 715 //Configure CRC, large frames, and write validation
itslinear 90:d0805b58b0ae 716 sd.crc(true);
itslinear 90:d0805b58b0ae 717 sd.large_frames(true);
itslinear 90:d0805b58b0ae 718 sd.write_validation(true);
itslinear 90:d0805b58b0ae 719
itslinear 90:d0805b58b0ae 720 //mount SD card
itslinear 90:d0805b58b0ae 721 printf("\n\rMounting SD card...");
itslinear 90:d0805b58b0ae 722 if (sd.mount() != 0) {
itslinear 90:d0805b58b0ae 723 printf("failed!\n\r");
itslinear 90:d0805b58b0ae 724 return NULL;
itslinear 90:d0805b58b0ae 725 }
itslinear 90:d0805b58b0ae 726 printf("success!\n\r");
itslinear 90:d0805b58b0ae 727
itslinear 90:d0805b58b0ae 728 //open file for read
itslinear 90:d0805b58b0ae 729 printf("open file %s...",filepath);
itslinear 90:d0805b58b0ae 730 FILE *fp = fopen(filepath, "r");
itslinear 90:d0805b58b0ae 731 if(fp == NULL) {
itslinear 90:d0805b58b0ae 732 printf("failed!\n\r");
itslinear 90:d0805b58b0ae 733 return NULL;
itslinear 90:d0805b58b0ae 734 }
itslinear 90:d0805b58b0ae 735 printf("success!\n\r");
itslinear 90:d0805b58b0ae 736
itslinear 90:d0805b58b0ae 737 /* Get the number of bytes */
itslinear 90:d0805b58b0ae 738 fseek(fp, 0L, SEEK_END);
itslinear 90:d0805b58b0ae 739 numbytes = ftell(fp);
itslinear 90:d0805b58b0ae 740
itslinear 90:d0805b58b0ae 741 /* reset the file position indicator to
itslinear 90:d0805b58b0ae 742 the beginning of the file */
itslinear 90:d0805b58b0ae 743 fseek(fp, 0L, SEEK_SET);
itslinear 90:d0805b58b0ae 744
itslinear 90:d0805b58b0ae 745 /* grab sufficient memory for the
itslinear 90:d0805b58b0ae 746 buffer to hold the text */
itslinear 90:d0805b58b0ae 747 buff = (char*)calloc(numbytes, sizeof(char));
itslinear 90:d0805b58b0ae 748
itslinear 90:d0805b58b0ae 749
itslinear 90:d0805b58b0ae 750 /* copy all the text into the buffer */
itslinear 90:d0805b58b0ae 751 fread(buff, sizeof(char), numbytes-1, fp);
itslinear 90:d0805b58b0ae 752 fclose(fp);
itslinear 90:d0805b58b0ae 753
itslinear 90:d0805b58b0ae 754 /* free the memory we used for the buffer */
itslinear 90:d0805b58b0ae 755 //free(buff);
itslinear 90:d0805b58b0ae 756
itslinear 90:d0805b58b0ae 757 //Unmount the SD card
itslinear 90:d0805b58b0ae 758 sd.unmount();
itslinear 90:d0805b58b0ae 759
itslinear 90:d0805b58b0ae 760 return buff;
itslinear 90:d0805b58b0ae 761 }
itslinear 80:6e3eb8246ced 762
itslinear 80:6e3eb8246ced 763
itslinear 80:6e3eb8246ced 764
itslinear 80:6e3eb8246ced 765
itslinear 80:6e3eb8246ced 766
itslinear 80:6e3eb8246ced 767
itslinear 80:6e3eb8246ced 768