PES 4 - Smart Medication Dispenser / PES4_ProgrammeforDesignReview2

Dependencies:   SDFileSystem mbed

Fork of PES4_Programme by PES 4 - Smart Medication Dispenser

Revision:
53:1c61cadbcb35
Parent:
52:701d0c2f47d7
Child:
55:bdab541f434d
diff -r 701d0c2f47d7 -r 1c61cadbcb35 source/sdcard.cpp
--- a/source/sdcard.cpp	Thu Mar 29 18:27:02 2018 +0000
+++ b/source/sdcard.cpp	Fri Mar 30 11:22:20 2018 +0000
@@ -1,5 +1,6 @@
 #include "sdcard.h"
-
+#include <stdio.h>
+#include <string.h>
 
 Timer timer;
 
@@ -11,7 +12,7 @@
 void writeTest()
 {
     //Test write performance by creating a 1MB file
-    printf("Testing %iB write performance...\n\r", sizeof(buffer));
+    printf("Testing %iB write performance...", sizeof(buffer));
     FileHandle* file = sd.open("Test File.bin", O_WRONLY | O_CREAT | O_TRUNC);
     if (file != NULL) {
         timer.start();
@@ -72,70 +73,132 @@
         buffer[i] = rand();
 
 
-        //Print the start message
-        printf("\n\rStarting SD Card test application:");
+    //Print the start message
+    printf("\n\rStarting SD Card test application:");
 
 
-        //Make sure a card is present
-        if (!sd.card_present()) {
-            printf("\n\rNo card present!\n\r");
-        }
+    //Make sure a card is present
+    if (!sd.card_present()) {
+        printf("\n\rNo card present!\n\r");
+    }
 
-        //Try to mount the SD card
-        printf("\n\rMounting SD card...");
-        if (sd.mount() != 0) {
-            printf("failed!\n\r");
-        }
-        printf("success!\n\r");
+    //Try to mount the SD card
+    printf("\n\rMounting SD card...");
+    if (sd.mount() != 0) {
+        printf("failed!\n\r");
+    }
+    printf("success!\n\r");
 
-        //Display the card type
-        printf("\tCard type: ");
-        SDFileSystem::CardType cardType = sd.card_type();
-        if (cardType == SDFileSystem::CARD_NONE)
-            printf("None\n\r");
-        else if (cardType == SDFileSystem::CARD_MMC)
-            printf("MMC\n\r");
-        else if (cardType == SDFileSystem::CARD_SD)
-            printf("SD\n\r");
-        else if (cardType == SDFileSystem::CARD_SDHC)
-            printf("SDHC\n\r");
-        else
-            printf("Unknown\n\r");
+    //Display the card type
+    printf("\tCard type: ");
+    SDFileSystem::CardType cardType = sd.card_type();
+    if (cardType == SDFileSystem::CARD_NONE)
+        printf("None\n\r");
+    else if (cardType == SDFileSystem::CARD_MMC)
+        printf("MMC\n\r");
+    else if (cardType == SDFileSystem::CARD_SD)
+        printf("SD\n\r");
+    else if (cardType == SDFileSystem::CARD_SDHC)
+        printf("SDHC\n\r");
+    else
+        printf("Unknown\n\r");
 
-        //Display the card capacity
-        printf("\tSectors: %u\n\r", sd.disk_sectors());
-        printf("\tCapacity: %.1fMB\n\r", sd.disk_sectors() / 2048.0);
+    //Display the card capacity
+    printf("\tSectors: %u\n\r", sd.disk_sectors());
+    printf("\tCapacity: %.1fMB\n\r", sd.disk_sectors() / 2048.0);
 
-        /*//Format the card
-        printf("Formatting SD card...");
-        if (sd.format() != 0) {
-            printf("failed!\n\r");
-            continue;
-        }
-        printf("success!\n\r");*/
+    /*//Format the card
+    printf("Formatting SD card...");
+    if (sd.format() != 0) {
+        printf("failed!\n\r");
+        continue;
+    }
+    printf("success!\n\r");*/
 
-        //Perform a read/write test
-        writeTest();
-        readTest();
+    //Perform a read/write test
+    writeTest();
+    readTest();
 
 
 
-        printf("write to SD card on a txt file:\n\r");
-        printf("\tHello World!\n\r");
+    printf("write to SD card on a txt file:\n\r");
+    printf("\tHello World!\n\r");
+
+    FILE *fp = fopen("/sd/medication/sdtest.txt", "a");
+    if(fp == NULL) {
+        printf("\tCould not open file for write\n\r");
+    } else {
+        printf("\tfile opened\n\r");
+    }
+
+    fprintf(fp, "Hello fun SD Card World!\n\r");
+    fclose(fp);
+
+    printf("\tText written to SD card\n\r");
+    printf("\tGoodbye World!\r\n");
+
+    //Unmount the SD card
+    sd.unmount();
+}
+
+s_user readMedication(int user)
+{
+    s_user userfile;
+    userfile.valid = false;
+    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);
 
-        FILE *fp = fopen("/sd/medication/sdtest.txt", "a");
-        if(fp == NULL) {
-            printf("\tCould not open file for write\n\r");
-        } else {
-            printf("\tfile opened\n\r");
+    //mount SD card
+    printf("\n\rMounting SD card...");
+    if (sd.mount() != 0) {
+        printf("failed!\n\r");
+        return userfile;
+    }
+    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 userfile;
+    }
+    printf("success!\n\r");
+
+    char string[STR_LEN];
+    int linecounter = 1;
+    while (fgets(string, STR_LEN, fp) != NULL) {
+
+        switch (linecounter) {
+            case 1:
+                printf("Reading Line 1...");
+                userfile.side = atoi(strtok(string, TOKEN));
+                strcpy(userfile.firstName, strtok(NULL, TOKEN));
+                strcpy(userfile.secondName, strtok(NULL, TOKEN));
+                printf("done!\r\n");
+                printf("\tside:\t\t%d \n\r\tFirst Name:\t%s \n\r\tLast Name:\t%s \n\r",userfile.side, userfile.firstName,userfile.secondName);
+                break;
+            case 2:
+                printf("Reading Line 2...");
+                int mo = atoi(strtok(string, TOKEN));
+                int lu = atoi(strtok(NULL, TOKEN));
+                int di = atoi(strtok(NULL, TOKEN));
+                int ev = atoi(strtok(NULL, TOKEN));
+                printf("done!\r\n");
+                printf("\tMedication for Monday:\r\n\tmorning:\t%06d\r\n\tlunch:\t\t%06d\r\n\tdinner:\t\t%06d\r\n\tevening:\t%06d\r\n",mo,lu,di,ev);
+                break;
+            default:
+                break;
         }
-        
-        fprintf(fp, "Hello fun SD Card World!\n\r");
-        fclose(fp);
-        
-        printf("\tText written to SD card\n\r");
-        printf("\tGoodbye World!\r\n");
-        
-        //Unmount the SD card
-        sd.unmount();
-}
+        linecounter++;
+    }
+    userfile.valid = true;
+    return userfile;
+}
\ No newline at end of file