Fitbit code using RTOS

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:
Wed Apr 22 15:59:19 2020 +0000
Parent:
2:f96f7a311486
Child:
4:158ea0c5531c
Commit message:
Added USB writing functionality and some minor additions

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Apr 22 15:18:28 2020 +0000
+++ b/main.cpp	Wed Apr 22 15:59:19 2020 +0000
@@ -61,6 +61,8 @@
 int oldScreen = 1;
 bool setup_state = true;
 
+Timer usb_timer;
+
 Thread thread1;
 Thread thread2;
 Thread thread3;
@@ -68,6 +70,7 @@
 Thread thread5;
 Mutex lcd_mtx;
 Mutex gps_mtx;
+Mutex usb_mtx;
  
 // when the pushbotton is pressed the run flag is set to false and the main 
 // function while loop exits so that the data file can be closed 
@@ -342,7 +345,6 @@
     }
 }
 
-/*
 void saveData() {
     // Save the data to the usb flash drive and print to the terminal
     FILE *fp = fopen( "/msc/data.txt", "a");
@@ -350,15 +352,13 @@
         error("Could not open file for write\n");
     }
     time_t seconds = time(NULL);
-    char hour[2];
     char date[32];
-    strftime(hour, 2, "%H", localtime(&seconds));
-    strftime(date, 32, "%D", localtime(&seconds));
-    fprintf(fp, "%c-%c\t%d\t%d\t%f\%f\n\r", hour, date, steps, flights, calories, distance);
-    pc.printf("%c-%c\t%d\t%d\t%f\%f\n\r", hour, date, steps, flights, calories, distance);
+    strftime(date, 32, "%m %d %y", localtime(&seconds));
+    fprintf(fp, "%c\t%d\t%d\t%0.2f\t%0.2f\n\r", date, steps, flights, calories, distance);
+    pc.printf("%c\t%d\t%d\t%0.2f\t%0.2f\n\r", date, steps, flights, calories, distance);
     fclose(fp);
 }
-*/
+
     
 int main() {
     //Set RTC time
@@ -411,11 +411,14 @@
     float avg;
     
     // Initialize data file on usb flash drive
+    usb_mtx.lock();
     FILE *fp = fopen( "/msc/data.txt", "w");
     if(fp == NULL) {
         error("Could not open file for write\n");
     }
-    //fprintf(fp, "Sample Number, Pressure (Pa), Acceleration Magnitude (Gs), Heart Rate (bpm), Latitude (degrees), Longitude (degrees)\n");
+    fprintf(fp, "Date\tSteps\tFloors\tCalories\tDistance (ft)\r\n");
+    fclose(fp);
+    usb_mtx.unlock();
     
     thread3.start(readBarometer);
     thread4.start(readGPS);
@@ -423,6 +426,7 @@
     //Barometer.attach(&readBarometer, 2);
     //GPS.attach(&readGPS, 10);
     //HR.attach(&readHR, 0.5);
+    usb_timer.start();
 
     while(run) {
         // Read Sensors
@@ -450,14 +454,20 @@
         avg_buffer[0] = avg_buffer[1];
         avg_buffer[1] = avg;
         
-        // Save the data to the usb flash drive and print to the terminal
-        //fprintf(fp, "%d, %lu, %f, %d, %f, %f\r\n", sample_num, pressure, avg, bpm, latitude, longitude); 
-        //pc.printf("%d, %lu, %f, %d, %f, %f\r\n", sample_num, pressure, avg, bpm, latitude, longitude); 
         sample_num++;
         one = !one;
         // Sampling rate of ~200 Hz
+        if(usb_timer.read() >= 30) {
+            pc.printf("Starting USB Write\r\n");
+            usb_mtx.lock();
+            saveData();
+            usb_mtx.unlock();
+            usb_timer.stop();
+            usb_timer.reset();
+            usb_timer.start();
+        }
         Thread::wait(200);
     }
-    fclose(fp);
+    //fclose(fp);
     one = 0;
 }
\ No newline at end of file