A system to help you ride your bike better than you do right now.

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed LSM9DS1_Library_cal

Revision:
3:26e0c0d7984f
Parent:
2:d08643ff3c62
Child:
4:1928bf053958
--- a/main.cpp	Fri Dec 02 23:03:00 2016 +0000
+++ b/main.cpp	Sun Dec 04 21:42:06 2016 +0000
@@ -1,31 +1,42 @@
 #include "mbed.h"
 #include "SDFileSystem.h"
 #include "uLCD_4DGL.h"
+#include <fstream>
+#include <iomanip>
+using namespace std;
 
-DigitalOut myled(LED1);
+#define START_S 1480719111
 
-SDFileSystem sd(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name);  //mosi -> DI, miso <- DO, slck -> sclck, CS -> CS
-uLCD_4DGL lcd(PinName tx, PinName rx, PinName rst);
+DigitalOut led(LED1);
+SDFileSystem sd(p11, p12, p13, p14, "sd");  //mosi -> DI, miso <- DO, slck -> sclck, CS -> CS
+uLCD_4DGL lcd(p28, p27, p29);
 
-float distance;
-float speed;
-float maxSpeed;
-time_t start = 1480719111;
+float miles = 0;
+float speed = 0;
+float maxSpeed = 0;
+time_t seconds;
+
+void store_trip();
+void recall_trips();
 
 int main() {
-    set_time(start);  // Set RTC time
+    set_time(START_S);  // Set RTC time
     
     // open the file for reading and appending records
-    
     // recall last trip
-    recall_trip(&fp);
-    wait(10);
+    recall_trips();
+    wait(5);
     lcd.cls();
     
     // normal operation loop here
-    while(true) {
-        time_t current = time(NULL);
+    bool going = true;
+    while(going) {
+        seconds = time(NULL) - START_S; // return the seconds passed since start
         
+        
+        led = 1;
+        wait(0.5);
+        led = 0;
     // check current speed
     // check curret distance
     // check time so far
@@ -36,57 +47,92 @@
     
     // store this trip
     lcd.cls();
-    store_trip(&fp);
+    store_trip();
     
-    // close the file at the end of the program
-    fclose(fp);
     // end everything
 }
 
-void store_trip(FILE * fp) {
-    
-    FILE *fp = fopen("/sd/records/recent.txt", "w");
-    
-    // write trip results to the SD card
-    if (fp == NULL) {
-        lcd.locate(0, 1);
-        lcd.printf("Could not open file for write\n");
-    } else {
-        fprintf(fp, "%d %d %d", 
-        
-    }
-}
-
-void recall_trip(FILE * fp) {
+void recall_trips(void) {
     // display the most recent trip made on the screen
     // display the most impressive trip (longest distance, best speed, least time)
     
-    int lineCount;
-    char str[12];
-    char split;
+    float f_miles;
+    float f_maxSpeed;
+    float f_hours;
+    ifstream file;
     
     // read out the most recent trip
-    FILE *fp = fopen("/sd/records/recent.txt", "r");
+    file.open("/sd/records/recent.txt");
     
-    if (fp == NULL) {
+    if (!file.is_open()) {
         lcd.locate(0, 1);
-        lcd.printf("Could not open file for write\n");
+        lcd.printf("Could not open file\n");
     } else {
-        std::getline(file, str);
+        file >> f_miles >> f_maxSpeed >> f_hours;
         lcd.locate(0, 1);
-        lcd.printf("Most recent trip\n%s", str);
+        lcd.printf("Your last trip\n\n");
+        lcd.printf("Distance : %3.1f\n\n", f_miles);
+        lcd.printf("Top speed : %2.1f\n\n", f_maxSpeed);
+        lcd.printf("Time : %1.1f\n\n", f_hours);
     }
     
-    // read out the most impressive trip so far
-    FILE *fp = fopen("/sd/records/best_of.txt", "r");
+    file.close();
+    wait(0.5);
+    
+    // display the best trip
+    
+    file.open("/sd/records/best-of.txt");
+
+    if(!file.is_open()) {
+        lcd.printf("Could not open file\n");
+    } else {
+        // show the best trip
+        file >> f_miles >> f_maxSpeed >> f_hours;
+        lcd.printf("Your best trip\n\n");
+        lcd.printf("Distance : %3.1f\n\n", f_miles);
+        lcd.printf("Top speed : %2.1f\n\n", f_maxSpeed);
+        lcd.printf("Time : %1.1f\n\n", f_hours);
+    }
+
+    file.close();
+    wait(0.5);
+}
 
-    if(fp == NULL) {
+void store_trip(void) {
+    // store the most recent trip completed
+    // determine whether this trip was a record, and indicate if so
+    
+    float hours;
+    fstream file;
+    
+    file.open("/sd/records/recent.txt");
+    
+    if (!file.is_open()) {
+        lcd.locate(0, 1);
+        lcd.printf("Could not open file\n");
+    } else {
+        hours = (float)seconds / 3600;
         lcd.locate(0, 1);
-        lcd.printf("Could not open file for write\n");
+        lcd.printf("This trip\n\n");
+        lcd.printf("Distance : %3.1f\n\n", miles);
+        lcd.printf("Top speed : %2.1f\n\n", maxSpeed);
+        lcd.printf("Time : %1.1f\n\n", hours);
+        // overwrite most recent
+        file << fixed << setprecision(1) << miles << " " << maxSpeed << " " << hours << endl;
+    }
+    
+    file.close();
+    wait(0.5);
+    
+    file.open("/sd/records/best-of.txt");
+    
+    if (!file.is_open()) {
+        lcd.locate(0, 1);
+        lcd.printf("Could not open file\n");
     } else {
         lcd.locate(0, 1);
-        while (std::getline(file, str)) {
-        
-        }
+        // check if you beat your best
     }
+    
+    file.close();
 }
\ No newline at end of file