Assignment 5 for OCE360, timers, tickers, and interrupts.

Dependencies:   4DGL-uLCD-SE MMA8452Q SDFileSystem bouncing_ball mbed

Fork of OCE360_4 by OCE_360

Revision:
9:7a577d790538
Parent:
8:2617100c441d
Child:
10:1fe988d4e61e
--- a/main.cpp	Tue Nov 14 14:20:06 2017 +0000
+++ b/main.cpp	Wed Nov 15 20:59:21 2017 +0000
@@ -20,7 +20,9 @@
 #define DEBUG_MODE 0
 
 #define LCD_UPDATE .091         //11 Hz
-#define DRAW_UPDATE 0.075
+
+// Analog input (pin 15)
+AnalogIn tempin(p15);
 
 //Initialize Serial communication
 Serial pc(USBTX, USBRX);
@@ -28,6 +30,12 @@
 // SD card (SPI pins)
 SDFileSystem sd(p5, p6, p7, p8, "sd");
 
+//Drawing positions
+int lastx1;
+int lasty1;
+int lastx2; 
+int lasty2;
+
 //Led Initialization
 DigitalOut led1(LED1); //update leds
 DigitalOut led2(LED2);
@@ -41,15 +49,22 @@
 
 void toggle(){
     if(debounce.read_ms()>175){
-        led3=!led3;
+        led3= !led3;
         recorder = !recorder;
         debounce.reset(); //restart timer after toggle occurs
         }
     }
 
 //Position recorder
-Timer logtime;
+Timer logtimer;
+Ticker logticker;
 
+void datarecord(){
+    logtimer.start();
+    fprintf(file, "Ball 1: %.3g   X Position   Y Position   X Accleration   Y Acceleration   Temperature\n\r",logtimer.read(),
+    ball1.posx,ball1.posy,ball1.speedx,ball1.speedy,);
+    logtimer.reset();
+    }
 
 //Function prototype for color selection function:
 int get_LCD_color(int color_integer);
@@ -81,25 +96,24 @@
 Ticker displayup;   //ticker for displaying ball on lcd
 
 void display(){
+    
+    // Erase old circles by writing over there locations using the screen color:
+    uLCD.filled_circle(lastx1, lasty1, ball1.radius, BLACK);
+    uLCD.filled_circle(lastx2, lasty2, ball2.radius, BLACK);
+    
     // Draw circles in the x and y positions stored by the ball objects:
     uLCD.filled_circle(ball1.posx, ball1.posy, ball1.radius, get_LCD_color(ball1.color));
     uLCD.filled_circle(ball2.posx, ball2.posy, ball2.radius, get_LCD_color(ball2.color));
-
-    // Wait before erasing old circles:
-    wait(UPDATE_TIME_S);         // In seconds
-
-    // Erase old circles by writing over there locations using the screen color:
-    uLCD.filled_circle(ball1.posx, ball1.posy, ball1.radius, BLACK);
-    uLCD.filled_circle(ball2.posx, ball2.posy, ball2.radius, BLACK);
+    
+    //Sets previous ball position to be erased in next iteration
+    lastx1 = ball1.posx;
+    lasty1 = ball1.posy;
+    lastx2 = ball2.posx;
+    lasty2 = ball2.posy;
     }
 
 int main(){
-    
-    /*
-    logger1.attach(&pos_log1, LCD_UPDATE);
-    logger2.attach(&pos_log2, LCD_UPDATE);
-    */
-    
+
     // Initialize uLCD
     uLCD.baudrate(115200);
     uLCD.background_color(BLACK);
@@ -124,17 +138,34 @@
     debounce.start();       //starts debounce timer
     button.rise(&toggle);   //toggles on button press
     
+    while (1) {  //execute 'forever'
     
-    while (1) {  //execute 'forever'
+        if (recorder == 1){
+                //File Writing
+                FILE *file;
+                file = fopen("/sd/ball_data.txt", "w");
+                if ( file == NULL ) {
+                    error("ERROR: Could not open file for writing!\n\r");
+                    return -1;
+                }
+                // Tell the user we need to wait while we collect some data
+                pc.printf("\nCollecting data (Do not remove SD Card!) ...\n\r");
+                fprintf(file, "       Time   X Position   Y Position   X Accleration   Y Acceleration   Temperature\n\r");
+                
+                pc.printf("\nData Recorded Sucessfully! \n\r");
+                
+                // Close file
+                fclose(file);
+                }
         
         if (DEBUG_MODE) {
             //If compiled with DEBUG_MODE flag raised, print values to screen.
             uLCD.locate(0,4);
             uLCD.printf("X: %d.1\nY: %.1d",ball1.posx,ball1.posy);
-
+    
             uLCD.locate(0,6);
             uLCD.printf("VX: %f.1\nVY: %.1f",ball1.speedx,ball1.speedy);
-
+    
             uLCD.locate(0,10);
             uLCD.printf("AX: %f.1\nAY: %.1f\nAZ: %0.1f",accel.readX(),accel.readY(),accel.readZ());
         }