This is a repository for code relating to mbed Fitness Tracker

Dependencies:   mbed PulseSensor2 SCP1000 mbed-rtos 4DGL-uLCD-SE LSM9DS1_Library_cal PinDetect FatFileSystemCpp GP-20U7

Files at this revision

API Documentation at this revision

Comitter:
dyu2021
Date:
Thu Apr 23 15:57:21 2020 +0000
Parent:
33:48b2c87faecc
Child:
35:c14c5059f5b7
Commit message:
Updated save and read config

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Apr 23 04:49:48 2020 +0000
+++ b/main.cpp	Thu Apr 23 15:57:21 2020 +0000
@@ -85,8 +85,8 @@
     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);
+    fprintf(fp, "Gender (M/F)\tAge\tWeight (lb)\tStep Goal");
+    fprintf(fp, "%d\t%d\t%d\t%d\n\r", gender, age, weight, stepGoal);
     pc.printf("Config saved\r\n");
     fclose(fp);
     usb_mtx.unlock();
@@ -95,19 +95,30 @@
 //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");
+    FILE *fp1 = fopen( "/msc/config.txt", "r");
+    FILE *fp2 = fopen("/msc/data.txt", "r");
+    if(fp1 == NULL) {
+        error("Could not open file for read\n");
+    }
+    if(fp2 == NULL) {
+        error("Could not open file for read\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);
+    if ((fscanf(fp1, "%s", buf)) != EOF) {
+        fscanf(fp1, "%d\t%d\t%d\t%d", &gender, &age, &weight, &stepGoal);
         pc.printf("Config loaded\r\n");
     } else {
         pc.printf("No config file found\r\n");
     }
-    fclose(fp);
+    if ((fscanf(fp2, "%s", buf)) != EOF) {
+        fscanf(fp2, "%d\t%d\t%f\t%f", &steps, &flights, &calories, &distance);
+        pc.printf("Data loaded\r\n");
+    } else {
+        pc.printf("No data file found\r\n");
+    }
+    fclose(fp1);
+    fclose(fp2);
     usb_mtx.unlock();
 }
  
@@ -517,4 +528,20 @@
         Thread::wait(200);
     }
     one = 0;
+    //Turn off all threads reading sensors
+    thread3.terminate();
+    thread4.terminate();
+    thread5.terminate();
+    
+    usb_timer.stop();
+    usb_timer.reset();
+    
+    save_data();
+    two = 1;
+    //Append data from data.txt to prev_data.txt
+    //usb_mtx.lock();
+    //FILE *fp1 = fopen("/msc/prev_data.txt", "a");
+    //FILE *fp2 = fopen("/msc/data.txt", "r");
+    two = 0;
+    return 0;
 }
\ No newline at end of file