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 17:22:42 2020 +0000
Parent:
3:f3e1ee4aa5ec
Commit message:
Updated GPS distance calculation and fixed saving dates

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:59:19 2020 +0000
+++ b/main.cpp	Wed Apr 22 17:22:42 2020 +0000
@@ -23,13 +23,6 @@
 
 #define FSNAME "msc"
 MSCFileSystem msc(FSNAME); 
-
-Ticker display;
-Ticker LCD_clock;
-Ticker Barometer;
-Ticker GPS;
-Ticker HR;
-Ticker setup;
  
 int bpm;
 int steps = 0;
@@ -68,8 +61,7 @@
 Thread thread3;
 Thread thread4;
 Thread thread5;
-Mutex lcd_mtx;
-Mutex gps_mtx;
+Mutex serial_mtx;
 Mutex usb_mtx;
  
 // when the pushbotton is pressed the run flag is set to false and the main 
@@ -115,7 +107,7 @@
 //Display the time on the top
 void display_time() {
     while(1) {
-        lcd_mtx.lock();
+        serial_mtx.lock();
         uLCD.locate(1, 1);
         uLCD.color(WHITE);
         uLCD.text_width(2);
@@ -124,14 +116,14 @@
         char timeBuffer[32];
         strftime(timeBuffer, 32, "%I:%M %p\r\n", localtime(&seconds));
         uLCD.printf("%s", timeBuffer);
-        lcd_mtx.unlock();
+        serial_mtx.unlock();
         Thread::wait(700);
     }
 }
 
 void setup_screen(void) {
     while(1) {
-        lcd_mtx.lock();
+        serial_mtx.lock();
         if (oldScreen != screen) {
             uLCD.filled_rectangle(0,0, 128, 128, BLACK);
             oldScreen++;
@@ -202,7 +194,7 @@
                 uLCD.line(40, 100, 90, 100, WHITE);
                 break;
         }
-        lcd_mtx.unlock();
+        serial_mtx.unlock();
         Thread::wait(100);
     }
 }
@@ -210,7 +202,7 @@
 void update_screen(void) {
     while(1) {
         read_pot();
-        lcd_mtx.lock();
+        serial_mtx.lock();
         if (oldMode != mode) {
             uLCD.filled_rectangle(0,0, 128, 128, BLACK);
         }
@@ -274,7 +266,7 @@
                 uLCD.printf("%2d floors", flights);
                 break;
         }
-        lcd_mtx.unlock();
+        serial_mtx.unlock();
         Thread::wait(100);
     }
 }
@@ -319,7 +311,7 @@
         
 void readGPS(){
     while(1) {
-        gps_mtx.lock();
+        serial_mtx.lock();
         if(gps.connected()) {
             if(gps.sample()) {
                 if(gps.ns == 'S') {
@@ -333,14 +325,16 @@
                     latitude = -gps.latitude*PI/180;
                 }
                 if(latitude != 0 && longitude != 0 && old_lat != 0 && old_lon != 0) {
-                    distance = distance + (3963*acosf(sinf(old_lat)*sinf(latitude)+cosf(old_lat)*cosf(latitude)*cosf(longitude-old_lon)));
+                    float a = sinf(old_lat)*sinf(latitude)+cosf(old_lat)*cosf(latitude)*cosf(longitude-old_lon);
+                    if(a > 1) a = 1;
+                    distance = distance + (.75*acosf(a));
                 }
                 old_lat = latitude;
                 old_lon = longitude;
                 //pc.printf("%f, %f, %f\r\n", latitude, longitude, distance);
             }
         }
-        gps_mtx.unlock();
+        serial_mtx.unlock();
         Thread::wait(10000);
     }
 }
@@ -353,9 +347,9 @@
     }
     time_t seconds = time(NULL);
     char date[32];
-    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);
+    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);
 }
 
@@ -374,7 +368,6 @@
     uLCD.background_color(BLACK);
     uLCD.cls();
     thread1.start(setup_screen);
-    //setup.attach(&setup_screen, 0.3);
     
     while(setup_state) {
         pc.printf("%d\r\n", screen);
@@ -385,12 +378,9 @@
     pb.attach_deasserted(&button);
     
     // set up the display
-    //setup.detach();
     uLCD.cls();
     thread1.start(update_screen);
     thread2.start(display_time);
-    //display.attach(&update_screen, 0.5);
-    //LCD_clock.attach(&display_time, 0.7);
     
     // LED indicates whether or not data is being collected
     one = 0;
@@ -412,20 +402,24 @@
     
     // Initialize data file on usb flash drive
     usb_mtx.lock();
-    FILE *fp = fopen( "/msc/data.txt", "w");
+    FILE *fp = fopen( "/msc/data.txt", "r+");
     if(fp == NULL) {
         error("Could not open file for write\n");
     }
-    fprintf(fp, "Date\tSteps\tFloors\tCalories\tDistance (ft)\r\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);
-    //Barometer.attach(&readBarometer, 2);
-    //GPS.attach(&readGPS, 10);
-    //HR.attach(&readHR, 0.5);
+
     usb_timer.start();
 
     while(run) {
@@ -458,7 +452,7 @@
         one = !one;
         // Sampling rate of ~200 Hz
         if(usb_timer.read() >= 30) {
-            pc.printf("Starting USB Write\r\n");
+            //pc.printf("Starting USB Write\r\n");
             usb_mtx.lock();
             saveData();
             usb_mtx.unlock();
@@ -468,6 +462,5 @@
         }
         Thread::wait(200);
     }
-    //fclose(fp);
     one = 0;
 }
\ No newline at end of file