PES 4 - Smart Medication Dispenser / PES4_ProgrammeforDesignReview2

Dependencies:   SDFileSystem mbed

Fork of PES4_Programme by PES 4 - Smart Medication Dispenser

Committer:
cittecla
Date:
Fri Mar 30 11:22:20 2018 +0000
Revision:
53:1c61cadbcb35
Parent:
52:701d0c2f47d7
Child:
55:bdab541f434d
added readMedication() to sdcard.cpp;

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 51:a98ffbd41e76 4
cittecla 51:a98ffbd41e76 5 Timer timer;
cittecla 51:a98ffbd41e76 6
cittecla 51:a98ffbd41e76 7 DigitalIn button(PC_13, PullUp);
cittecla 51:a98ffbd41e76 8
cittecla 51:a98ffbd41e76 9 SDFileSystem sd(PA_7, PA_6, PA_5, PB_6, "sd", PA_8, SDFileSystem::SWITCH_NONE, 25000000);
cittecla 51:a98ffbd41e76 10 char buffer[4096];
cittecla 51:a98ffbd41e76 11
cittecla 51:a98ffbd41e76 12 void writeTest()
cittecla 51:a98ffbd41e76 13 {
cittecla 51:a98ffbd41e76 14 //Test write performance by creating a 1MB file
cittecla 53:1c61cadbcb35 15 printf("Testing %iB write performance...", sizeof(buffer));
cittecla 51:a98ffbd41e76 16 FileHandle* file = sd.open("Test File.bin", O_WRONLY | O_CREAT | O_TRUNC);
cittecla 51:a98ffbd41e76 17 if (file != NULL) {
cittecla 51:a98ffbd41e76 18 timer.start();
cittecla 51:a98ffbd41e76 19 for (int i = 0; i < (1048576 / sizeof(buffer)); i++) {
cittecla 51:a98ffbd41e76 20 if (file->write(buffer, sizeof(buffer)) != sizeof(buffer)) {
cittecla 51:a98ffbd41e76 21 timer.stop();
cittecla 51:a98ffbd41e76 22 printf("write error!\n\r");
cittecla 51:a98ffbd41e76 23 timer.reset();
cittecla 51:a98ffbd41e76 24 return;
cittecla 51:a98ffbd41e76 25 }
cittecla 51:a98ffbd41e76 26 }
cittecla 51:a98ffbd41e76 27 timer.stop();
cittecla 51:a98ffbd41e76 28 if (file->close())
cittecla 51:a98ffbd41e76 29 printf("failed to close file!\n\r");
cittecla 51:a98ffbd41e76 30 else
cittecla 51:a98ffbd41e76 31 printf("done!\n\r\tResult: %.2fKB/s\n\r", 1024 / (timer.read_us() / 1000000.0));
cittecla 51:a98ffbd41e76 32 timer.reset();
cittecla 51:a98ffbd41e76 33 } else {
cittecla 51:a98ffbd41e76 34 printf("failed to create file!\n\r");
cittecla 51:a98ffbd41e76 35 }
cittecla 51:a98ffbd41e76 36 }
cittecla 51:a98ffbd41e76 37
cittecla 51:a98ffbd41e76 38 void readTest()
cittecla 51:a98ffbd41e76 39 {
cittecla 51:a98ffbd41e76 40 //Test read performance by reading the 1MB file created by writeTest()
cittecla 51:a98ffbd41e76 41 printf("Testing %iB read performance...", sizeof(buffer));
cittecla 51:a98ffbd41e76 42 FileHandle* file = sd.open("Test File.bin", O_RDONLY);
cittecla 51:a98ffbd41e76 43 if (file != NULL) {
cittecla 51:a98ffbd41e76 44 timer.start();
cittecla 51:a98ffbd41e76 45 int iterations = 0;
cittecla 51:a98ffbd41e76 46 while (file->read(buffer, sizeof(buffer)) == sizeof(buffer))
cittecla 51:a98ffbd41e76 47 iterations++;
cittecla 51:a98ffbd41e76 48 timer.stop();
cittecla 51:a98ffbd41e76 49 if (iterations != (1048576 / sizeof(buffer)))
cittecla 51:a98ffbd41e76 50 printf("read error!\n\r");
cittecla 51:a98ffbd41e76 51 else if (file->close())
cittecla 51:a98ffbd41e76 52 printf("failed to close file!\n\r");
cittecla 51:a98ffbd41e76 53 else if (sd.remove("Test File.bin"))
cittecla 51:a98ffbd41e76 54 printf("failed to delete file!\n\r");
cittecla 51:a98ffbd41e76 55 else
cittecla 51:a98ffbd41e76 56 printf("done!\n\r\tResult: %.2fKB/s\n\r", 1024 / (timer.read_us() / 1000000.0));
cittecla 51:a98ffbd41e76 57 timer.reset();
cittecla 51:a98ffbd41e76 58 } else {
cittecla 51:a98ffbd41e76 59 printf("failed to open file!\n\r");
cittecla 51:a98ffbd41e76 60 }
cittecla 51:a98ffbd41e76 61 }
cittecla 51:a98ffbd41e76 62
cittecla 51:a98ffbd41e76 63 void testSd()
cittecla 51:a98ffbd41e76 64 {
cittecla 51:a98ffbd41e76 65 //Configure CRC, large frames, and write validation
cittecla 51:a98ffbd41e76 66 sd.crc(true);
cittecla 51:a98ffbd41e76 67 sd.large_frames(true);
cittecla 51:a98ffbd41e76 68 sd.write_validation(true);
cittecla 51:a98ffbd41e76 69
cittecla 51:a98ffbd41e76 70 //Fill the buffer with random data for the write test
cittecla 51:a98ffbd41e76 71 srand(time(NULL));
cittecla 51:a98ffbd41e76 72 for (int i = 0; i < sizeof(buffer); i++)
cittecla 51:a98ffbd41e76 73 buffer[i] = rand();
cittecla 51:a98ffbd41e76 74
cittecla 51:a98ffbd41e76 75
cittecla 53:1c61cadbcb35 76 //Print the start message
cittecla 53:1c61cadbcb35 77 printf("\n\rStarting SD Card test application:");
cittecla 51:a98ffbd41e76 78
cittecla 51:a98ffbd41e76 79
cittecla 53:1c61cadbcb35 80 //Make sure a card is present
cittecla 53:1c61cadbcb35 81 if (!sd.card_present()) {
cittecla 53:1c61cadbcb35 82 printf("\n\rNo card present!\n\r");
cittecla 53:1c61cadbcb35 83 }
cittecla 51:a98ffbd41e76 84
cittecla 53:1c61cadbcb35 85 //Try to mount the SD card
cittecla 53:1c61cadbcb35 86 printf("\n\rMounting SD card...");
cittecla 53:1c61cadbcb35 87 if (sd.mount() != 0) {
cittecla 53:1c61cadbcb35 88 printf("failed!\n\r");
cittecla 53:1c61cadbcb35 89 }
cittecla 53:1c61cadbcb35 90 printf("success!\n\r");
cittecla 51:a98ffbd41e76 91
cittecla 53:1c61cadbcb35 92 //Display the card type
cittecla 53:1c61cadbcb35 93 printf("\tCard type: ");
cittecla 53:1c61cadbcb35 94 SDFileSystem::CardType cardType = sd.card_type();
cittecla 53:1c61cadbcb35 95 if (cardType == SDFileSystem::CARD_NONE)
cittecla 53:1c61cadbcb35 96 printf("None\n\r");
cittecla 53:1c61cadbcb35 97 else if (cardType == SDFileSystem::CARD_MMC)
cittecla 53:1c61cadbcb35 98 printf("MMC\n\r");
cittecla 53:1c61cadbcb35 99 else if (cardType == SDFileSystem::CARD_SD)
cittecla 53:1c61cadbcb35 100 printf("SD\n\r");
cittecla 53:1c61cadbcb35 101 else if (cardType == SDFileSystem::CARD_SDHC)
cittecla 53:1c61cadbcb35 102 printf("SDHC\n\r");
cittecla 53:1c61cadbcb35 103 else
cittecla 53:1c61cadbcb35 104 printf("Unknown\n\r");
cittecla 51:a98ffbd41e76 105
cittecla 53:1c61cadbcb35 106 //Display the card capacity
cittecla 53:1c61cadbcb35 107 printf("\tSectors: %u\n\r", sd.disk_sectors());
cittecla 53:1c61cadbcb35 108 printf("\tCapacity: %.1fMB\n\r", sd.disk_sectors() / 2048.0);
cittecla 51:a98ffbd41e76 109
cittecla 53:1c61cadbcb35 110 /*//Format the card
cittecla 53:1c61cadbcb35 111 printf("Formatting SD card...");
cittecla 53:1c61cadbcb35 112 if (sd.format() != 0) {
cittecla 53:1c61cadbcb35 113 printf("failed!\n\r");
cittecla 53:1c61cadbcb35 114 continue;
cittecla 53:1c61cadbcb35 115 }
cittecla 53:1c61cadbcb35 116 printf("success!\n\r");*/
cittecla 51:a98ffbd41e76 117
cittecla 53:1c61cadbcb35 118 //Perform a read/write test
cittecla 53:1c61cadbcb35 119 writeTest();
cittecla 53:1c61cadbcb35 120 readTest();
cittecla 51:a98ffbd41e76 121
cittecla 51:a98ffbd41e76 122
cittecla 51:a98ffbd41e76 123
cittecla 53:1c61cadbcb35 124 printf("write to SD card on a txt file:\n\r");
cittecla 53:1c61cadbcb35 125 printf("\tHello World!\n\r");
cittecla 53:1c61cadbcb35 126
cittecla 53:1c61cadbcb35 127 FILE *fp = fopen("/sd/medication/sdtest.txt", "a");
cittecla 53:1c61cadbcb35 128 if(fp == NULL) {
cittecla 53:1c61cadbcb35 129 printf("\tCould not open file for write\n\r");
cittecla 53:1c61cadbcb35 130 } else {
cittecla 53:1c61cadbcb35 131 printf("\tfile opened\n\r");
cittecla 53:1c61cadbcb35 132 }
cittecla 53:1c61cadbcb35 133
cittecla 53:1c61cadbcb35 134 fprintf(fp, "Hello fun SD Card World!\n\r");
cittecla 53:1c61cadbcb35 135 fclose(fp);
cittecla 53:1c61cadbcb35 136
cittecla 53:1c61cadbcb35 137 printf("\tText written to SD card\n\r");
cittecla 53:1c61cadbcb35 138 printf("\tGoodbye World!\r\n");
cittecla 53:1c61cadbcb35 139
cittecla 53:1c61cadbcb35 140 //Unmount the SD card
cittecla 53:1c61cadbcb35 141 sd.unmount();
cittecla 53:1c61cadbcb35 142 }
cittecla 53:1c61cadbcb35 143
cittecla 53:1c61cadbcb35 144 s_user readMedication(int user)
cittecla 53:1c61cadbcb35 145 {
cittecla 53:1c61cadbcb35 146 s_user userfile;
cittecla 53:1c61cadbcb35 147 userfile.valid = false;
cittecla 53:1c61cadbcb35 148 char filepath[] = "/sd/medication/medicationUser2.txt";
cittecla 53:1c61cadbcb35 149 if(user==0) {
cittecla 53:1c61cadbcb35 150 strcpy(filepath, "/sd/medication/medicationUser1.txt");
cittecla 53:1c61cadbcb35 151 }
cittecla 53:1c61cadbcb35 152
cittecla 53:1c61cadbcb35 153 //Configure CRC, large frames, and write validation
cittecla 53:1c61cadbcb35 154 sd.crc(true);
cittecla 53:1c61cadbcb35 155 sd.large_frames(true);
cittecla 53:1c61cadbcb35 156 sd.write_validation(true);
cittecla 51:a98ffbd41e76 157
cittecla 53:1c61cadbcb35 158 //mount SD card
cittecla 53:1c61cadbcb35 159 printf("\n\rMounting SD card...");
cittecla 53:1c61cadbcb35 160 if (sd.mount() != 0) {
cittecla 53:1c61cadbcb35 161 printf("failed!\n\r");
cittecla 53:1c61cadbcb35 162 return userfile;
cittecla 53:1c61cadbcb35 163 }
cittecla 53:1c61cadbcb35 164 printf("success!\n\r");
cittecla 53:1c61cadbcb35 165
cittecla 53:1c61cadbcb35 166 //open file for read
cittecla 53:1c61cadbcb35 167 printf("open file %s...",filepath);
cittecla 53:1c61cadbcb35 168 FILE *fp = fopen(filepath, "r");
cittecla 53:1c61cadbcb35 169 if(fp == NULL) {
cittecla 53:1c61cadbcb35 170 printf("failed!\n\r");
cittecla 53:1c61cadbcb35 171 return userfile;
cittecla 53:1c61cadbcb35 172 }
cittecla 53:1c61cadbcb35 173 printf("success!\n\r");
cittecla 53:1c61cadbcb35 174
cittecla 53:1c61cadbcb35 175 char string[STR_LEN];
cittecla 53:1c61cadbcb35 176 int linecounter = 1;
cittecla 53:1c61cadbcb35 177 while (fgets(string, STR_LEN, fp) != NULL) {
cittecla 53:1c61cadbcb35 178
cittecla 53:1c61cadbcb35 179 switch (linecounter) {
cittecla 53:1c61cadbcb35 180 case 1:
cittecla 53:1c61cadbcb35 181 printf("Reading Line 1...");
cittecla 53:1c61cadbcb35 182 userfile.side = atoi(strtok(string, TOKEN));
cittecla 53:1c61cadbcb35 183 strcpy(userfile.firstName, strtok(NULL, TOKEN));
cittecla 53:1c61cadbcb35 184 strcpy(userfile.secondName, strtok(NULL, TOKEN));
cittecla 53:1c61cadbcb35 185 printf("done!\r\n");
cittecla 53:1c61cadbcb35 186 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);
cittecla 53:1c61cadbcb35 187 break;
cittecla 53:1c61cadbcb35 188 case 2:
cittecla 53:1c61cadbcb35 189 printf("Reading Line 2...");
cittecla 53:1c61cadbcb35 190 int mo = atoi(strtok(string, TOKEN));
cittecla 53:1c61cadbcb35 191 int lu = atoi(strtok(NULL, TOKEN));
cittecla 53:1c61cadbcb35 192 int di = atoi(strtok(NULL, TOKEN));
cittecla 53:1c61cadbcb35 193 int ev = atoi(strtok(NULL, TOKEN));
cittecla 53:1c61cadbcb35 194 printf("done!\r\n");
cittecla 53:1c61cadbcb35 195 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);
cittecla 53:1c61cadbcb35 196 break;
cittecla 53:1c61cadbcb35 197 default:
cittecla 53:1c61cadbcb35 198 break;
cittecla 51:a98ffbd41e76 199 }
cittecla 53:1c61cadbcb35 200 linecounter++;
cittecla 53:1c61cadbcb35 201 }
cittecla 53:1c61cadbcb35 202 userfile.valid = true;
cittecla 53:1c61cadbcb35 203 return userfile;
cittecla 53:1c61cadbcb35 204 }