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: mbed PulseSensor2 SCP1000 mbed-rtos 4DGL-uLCD-SE LSM9DS1_Library_cal PinDetect FatFileSystemCpp GP-20U7
Diff: main.cpp
- Revision:
- 33:48b2c87faecc
- Parent:
- 32:cdbd7de8ae10
- Child:
- 34:cf9e58eceba4
--- a/main.cpp Thu Apr 23 00:27:32 2020 +0000
+++ b/main.cpp Thu Apr 23 04:49:48 2020 +0000
@@ -78,6 +78,39 @@
setup_state = false;
}
}
+//Saves current configuration in a text file
+void save_config() {
+ usb_mtx.lock();
+ FILE *fp = fopen( "/msc/config.txt", "w");
+ if(fp == NULL) {
+ error("Could not open file for write\n");
+ }
+ fprintf(fp, "Gender (M/F)\tAge\tWeight (lb)\tStep Goal\tSteps\tFlights\tCalories\tDistance (ft)\r\n");
+ fprintf(fp, "%d\t%d\t%d\t%d\t%d\t%d\t%0.2f\t%0.2f\n\r", gender, age, weight, stepGoal, steps, flights, calories, distance);
+ pc.printf("Config saved\r\n");
+ fclose(fp);
+ usb_mtx.unlock();
+}
+
+//Reads configuration file and sets up all the values
+void read_config() {
+ usb_mtx.lock();
+ FILE *fp = fopen( "/msc/config.txt", "r");
+ if(fp == NULL) {
+ error("Could not open file for write\n");
+ }
+ char buf[128];
+ //Check if config file is empty
+ if ((fscanf(fp, "%s", buf)) != EOF) {
+ fscanf(fp, "%d\t%d\t%d\t%d\t%d\t%d\t%f\t%f", &gender, &age, &weight, &stepGoal, &steps, &flights, &calories, &distance);
+ pc.printf("Config loaded\r\n");
+ } else {
+ pc.printf("No config file found\r\n");
+ }
+ fclose(fp);
+ usb_mtx.unlock();
+}
+
// Reads the value of the potentiometer and averages over 3 readings to get rid
// of random spikes/zero values. Returns either a 1, 2 or 3 based on which 3rd
@@ -129,6 +162,9 @@
oldScreen++;
}
switch(screen) {
+ //Add additional case for Use Previous setup
+ //If yes, then set set_up to false and call read_config()
+ //If no, then continue. After setup, save_config() is called
case 1:
//Gender
uLCD.locate(2, 1);
@@ -371,18 +407,21 @@
}
}
-void saveData() {
+void save_data() {
// Save the data to the usb flash drive and print to the terminal
- FILE *fp = fopen( "/msc/data.txt", "a");
+ usb_mtx.lock();
+ FILE *fp = fopen( "/msc/data.txt", "w");
if(fp == NULL) {
error("Could not open file for write\n");
}
+ fprintf(fp, "Date\tSteps\tFloors\tCalories\tDistance (ft)\r\n");
time_t seconds = time(NULL);
char date[32];
strftime(date, 32, "%m/%d/%y", localtime(&seconds));
fprintf(fp, "%s\t%d\t%d\t%0.2f\t%0.2f\n\r", date, steps, flights, calories, distance);
pc.printf("%s\t%d\t%d\t%0.2f\t%0.2f\n\r", date, steps, flights, calories, distance);
fclose(fp);
+ usb_mtx.unlock();
}
@@ -405,6 +444,7 @@
pc.printf("%d\r\n", screen);
}
thread1.terminate();
+ save_config();
// Off button
pb.attach_deasserted(&button);
@@ -432,22 +472,6 @@
float avg_buffer[2] = {0};
float avg;
- // Initialize data file on usb flash drive
- usb_mtx.lock();
- FILE *fp = fopen( "/msc/data.txt", "r+");
- if(fp == NULL) {
- error("Could not open file for write\n");
- }
- //Check to see if file is empty, not working right now
- fseek (fp, 0, SEEK_END);
- int size = ftell(fp);
- if (0 == size) {
- fprintf(fp, "Date\tSteps\tFloors\tCalories\tDistance (ft)\r\n");
- pc.printf("Data.txt rewritten\r\n");
- }
- fclose(fp);
- usb_mtx.unlock();
-
thread3.start(readBarometer);
thread4.start(readGPS);
thread5.start(readHR);
@@ -485,9 +509,7 @@
// Sampling rate of ~200 Hz
if(usb_timer.read() >= 30) {
//pc.printf("Starting USB Write\r\n");
- usb_mtx.lock();
- saveData();
- usb_mtx.unlock();
+ save_data();
usb_timer.stop();
usb_timer.reset();
usb_timer.start();

