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
Diff: source/sdcard.cpp
- Revision:
- 94:b24d2b432b27
- Parent:
- 90:d0805b58b0ae
- Parent:
- 89:f63e4736d875
--- a/source/sdcard.cpp	Tue Apr 10 07:39:48 2018 +0000
+++ b/source/sdcard.cpp	Tue Apr 10 07:47:18 2018 +0000
@@ -418,6 +418,7 @@
     }
     printf("success!\n\r");
     printf("writing to SD card...");
+    fprintf(fp,"%d\r\n", medInventory.userNumber);    
     fprintf(fp,"%s\r\n", medInventory.pill[0]);
     fprintf(fp,"%02d\r\n",medInventory.medContainer.container[0]);
     fprintf(fp,"%s\r\n", medInventory.pill[1]);
@@ -431,7 +432,7 @@
     fprintf(fp,"%s\r\n", medInventory.pill[5]);
     fprintf(fp,"%02d\r\n",medInventory.medContainer.container[5]);
     fprintf(fp,"%02d;%02d;%02d;%02d;%02d;\r\n", medInventory.currentTime.day, medInventory.currentTime.month, medInventory.currentTime.year, medInventory.currentTime.hour, medInventory.currentTime.minute);
-    
+
     printf("done!\r\n");
 
     //close file
@@ -448,7 +449,7 @@
 
 int write_medError(char error[], s_time time)
 {
-    char filepath[] = "/sd/protocol/Error.txt";              // Muss noch auf SD Karte erstellt werden!!!
+    char filepath[] = "/sd/protocol/Error.txt";              
 
     //Configure CRC, large frames, and write validation
     sd.crc(true);
@@ -614,28 +615,31 @@
     char *str;
 
     int linecounter = 1;
-    int line = 1;
+    int line = 3;
     int i = 0;
 
     while (fgets(str, 50, fp) != NULL) {
 
         switch (line) {
             case 1:
-                if(linecounter != 13){
-                strcpy(Inventory.pill[i], strtok(str, "\r"));
-                //printf("%s\r\n",Inventory.pill[i]);
-                line = 2;
+                if(linecounter != 14) {
+                    strcpy(Inventory.pill[i], strtok(str, "\r"));
+                    line = 2;
                 }
-                
+
                 break;
 
             case 2:
-                int temp = atoi(strtok(str, "\r"));
-                //printf("%d\r\n",temp);
-                Inventory.medContainer.container[i] = temp;
+                Inventory.medContainer.container[i] = atoi(strtok(str, "\r"));
                 line =1;
                 i++;
                 break;
+                
+            case 3:
+                Inventory.userNumber = atoi(strtok(str, "\r"));
+                line = 1;
+
+                break;
         }
         linecounter++;
     }
@@ -711,7 +715,6 @@
 
     char filepath[] = "/sd/protocol/medInventory.txt";
 
-
     //Configure CRC, large frames, and write validation
     sd.crc(true);
     sd.large_frames(true);
@@ -761,8 +764,59 @@
 }
 
 
+char *read_Medication(char user)
+{
+    static char    *buff;
+    long    numbytes;
+    
+    char filepath[] = "/sd/medication/medicationUser2.txt";
+    if(user==0) {
+        strcpy(filepath, "/sd/medication/medicationUser1.txt");
+    }
 
+    //Configure CRC, large frames, and write validation
+    sd.crc(true);
+    sd.large_frames(true);
+    sd.write_validation(true);
 
+    //mount SD card
+    printf("\n\rMounting SD card...");
+    if (sd.mount() != 0) {
+        printf("failed!\n\r");
+        return NULL;
+    }
+    printf("success!\n\r");
 
+    //open file for read
+    printf("open file %s...",filepath);
+    FILE *fp = fopen(filepath, "r");
+    if(fp == NULL) {
+        printf("failed!\n\r");
+        return NULL;
+    }
+    printf("success!\n\r");
 
+    /* Get the number of bytes */
+    fseek(fp, 0L, SEEK_END);
+    numbytes = ftell(fp);
 
+    /* reset the file position indicator to
+    the beginning of the file */
+    fseek(fp, 0L, SEEK_SET);
+
+    /* grab sufficient memory for the
+    buffer to hold the text */
+    buff = (char*)calloc(numbytes, sizeof(char));
+
+    /* copy all the text into the buffer */
+    fread(buff, sizeof(char), numbytes-1, fp);
+    fclose(fp);
+
+    /* free the memory we used for the buffer */
+    //free(buff);
+
+    //Unmount the SD card
+    sd.unmount();
+
+    return buff;
+}
    