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:
6:050104c0dc75
Parent:
5:93f88deda5ba
Child:
7:614b40b85579
--- a/main.cpp	Tue Nov 07 15:24:31 2017 +0000
+++ b/main.cpp	Thu Nov 09 15:14:23 2017 +0000
@@ -18,11 +18,19 @@
 
 #define DEBUG_MODE 0
 
-#define LOG_UPDATE .091         //11 Hz
+#define LCD_UPDATE .091         //11 Hz
+#define DRAW_UPDATE 0.075
 
 //Initialize Serial communication
 Serial pc(USBTX, USBRX);
 
+//Led Initialization
+DigitalOut led1(LED1); //update leds
+DigitalOut led2(LED2);
+
+//Button Initialization
+DigitalIn button(p13);
+
 //Function prototype for color selection function:
 int get_LCD_color(int color_integer);
 
@@ -36,15 +44,47 @@
 physics_ball ball2;  //the default states from the library will be used initially
 
 //Ticker for logging
-Ticker logger1;
-Ticker logger2;
+Ticker logger1; //ball 1 ticker (position log)
+Ticker logger2; //ball 2 ticker (position log)
 
 void pos_log1(){
     pc.printf("Ball 1 (x): %i     Ball 1 (y): %i     \n\r",ball1.posx,ball1.posy);
+    //prints ball 1 position to terminal
     }
     
 void pos_log2(){
     pc.printf("Ball 2 (x): %i     Ball 2 (y): %i     \n\r",ball2.posx,ball2.posy);
+    //prints ball 2 position to terminal
+    }
+
+//Ticker for update
+Ticker update1; //ball 1 ticker for update position
+Ticker update2; //ball 2 ticker for update position
+
+void ball1up(){
+    ball1.update(UPDATE_TIME_S,accel);  //updates position
+    led1 = !led1;                       //changes led state every update
+    }
+
+void ball2up(){
+    ball2.update(UPDATE_TIME_S,accel);  //updates position
+    led2 = !led2;                       //changes led state every update
+    }
+
+//Ticker for display
+Ticker displayup;   //ticker for displaying ball on lcd
+
+void display(){
+    // 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(DRAW_UPDATE);         // 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);
     }
 
 int main(){
@@ -68,26 +108,13 @@
     ball1.set_param(RADIUS_1,0); //color is unimportant
     ball2.set_param(RADIUS_2,1); //just making sure the colors are different
 
-    /* Make the balls "fall" in direction of accelerometer by forcing the ball objects
-     to update themselves at regular intervals, and te drawing the locations reported
-     by the ball objects on the LCD screen: */
+    update1.attach(&ball1up,UPDATE_TIME_S); //ticker force position update ball 1
+    update2.attach(&ball2up,UPDATE_TIME_S); //ticker force position update ball 2
+    
+    displayup.attach(&display,LCD_UPDATE);   //ticker force lcd update
+    
     while (1) {  //execute 'forever'
-        // 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);
-
-        // Force the objects 'ball1' and 'ball2' to update their stored positions
-        // and velocities:
-        ball1.update(UPDATE_TIME_S,accel);
-        ball2.update(UPDATE_TIME_S,accel);
-
+        
         if (DEBUG_MODE) {
             //If compiled with DEBUG_MODE flag raised, print values to screen.
             uLCD.locate(0,4);