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:
- 58:cda5298c9b7f
- Parent:
- 57:79fed71031da
- Child:
- 64:ca667234c845
--- a/source/sdcard.cpp Sat Mar 31 21:45:08 2018 +0000
+++ b/source/sdcard.cpp Thu Apr 05 09:53:43 2018 +0000
@@ -1,6 +1,7 @@
#include "sdcard.h"
#include <stdio.h>
#include <string.h>
+#include <math.h>
Timer timer;
@@ -180,11 +181,10 @@
switch (linecounter) {
case 1:
printf("Reading Line 1...");
- userfile.side = atoi(strtok(string, TOKEN));
- strcpy(userfile.firstName, strtok(NULL, TOKEN));
+ strcpy(userfile.firstName, strtok(string, 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);
+ printf("\tFirst Name:\t%s \n\r\tLast Name:\t%s \n\r", userfile.firstName,userfile.secondName);
break;
case 30:
printf("reading checksum...");
@@ -219,9 +219,77 @@
}
linecounter++;
}
-
+ //close file
+ fclose(fp);
//Unmount the SD card
sd.unmount();
return userfile;
+}
+
+int writeMedication(int user, s_user userfile)
+{
+
+ 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 EXIT_FAILURE;
+ }
+ printf("success!\n\r");
+
+ //delete file
+ //sd.remove(filepath);
+
+ //open file for write
+ printf("open file %s...",filepath);
+ FILE *fp = fopen(filepath, "w");
+ if(fp == NULL) {
+ printf("failed!\n\r");
+ return EXIT_FAILURE;
+ }
+ printf("success!\n\r");
+ printf("writing to SD card...");
+ fprintf(fp,"%s;%s",userfile.firstName,userfile.secondName);
+ int checksum = 0;
+ for(int i = 2; i< 30; i++) {
+ int daycounter = (i-2)/4;
+ int momentcounter = (i-2)%4;
+ int hour = userfile.medication.day[daycounter].moment[momentcounter].time.hour;
+ int minute = userfile.medication.day[daycounter].moment[momentcounter].time.minute;
+ int offsetMinus = userfile.medication.day[daycounter].moment[momentcounter].timeOffsetMinus;
+ int offsetPlus = userfile.medication.day[daycounter].moment[momentcounter].timeOffsetPlus;
+ checksum += hour+minute+offsetMinus+offsetPlus;
+ int medication = 0;
+ for(int ii = 0; ii < 6; ii++) {
+ int devider = pow(10.0f,ii);
+ medication += 100000/devider * userfile.medication.day[daycounter].moment[momentcounter].medContainer.container[ii];
+ checksum+=userfile.medication.day[daycounter].moment[momentcounter].medContainer.container[ii];
+ }
+ fprintf(fp,"%02d;%02d;%02d;%02d;%06d\r\n",hour, minute,offsetMinus, offsetPlus, medication);
+
+ }
+ fprintf(fp,"%d",checksum);
+ printf("done!\r\n");
+
+ //close file
+ printf("closing file %s...",filepath);
+ fclose(fp);
+ printf("done!\n\r");
+ //Unmount the SD card
+ printf("Unmounting SD card...");
+ sd.unmount();
+ printf("done!\n\r");
+
+ return EXIT_SUCCESS;
}
\ No newline at end of file
