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

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

Fork of OCE360_4 by OCE_360

Committer:
rsean10
Date:
Thu Nov 09 15:14:23 2017 +0000
Revision:
6:050104c0dc75
Parent:
5:93f88deda5ba
Child:
7:614b40b85579
Assignment 5, Exercise 1 Complete. Moving on to Exercise 2.;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
slicht 2:552e6feb8709 1 // A solution to OCE360 Homework #4.
slicht 2:552e6feb8709 2 // Objective: Use object oriented programming to create a system that displays
slicht 2:552e6feb8709 3 // multiple balls bouncing around the LCD screen.
slicht 3:a7f02754bc99 4 // Stephen Licht, 11/7/2017
slicht 1:6b99ffa62cc8 5
slicht 0:8d3812068c6c 6 #include "mbed.h"
slicht 2:552e6feb8709 7 #include "MMA8452Q.h" //acceleromater library
slicht 2:552e6feb8709 8 #include "uLCD_4DGL.h" //LCD library
slicht 2:552e6feb8709 9 #include "bouncing_ball.h" //new ball phyics library
slicht 1:6b99ffa62cc8 10
slicht 2:552e6feb8709 11 #define UPDATE_TIME_S 0.02
slicht 2:552e6feb8709 12 #define START_X_1 10
slicht 2:552e6feb8709 13 #define START_Y_1 10
slicht 2:552e6feb8709 14 #define START_X_2 20
slicht 2:552e6feb8709 15 #define START_Y_2 20
slicht 2:552e6feb8709 16 #define RADIUS_1 6
slicht 2:552e6feb8709 17 #define RADIUS_2 3
slicht 0:8d3812068c6c 18
slicht 2:552e6feb8709 19 #define DEBUG_MODE 0
slicht 2:552e6feb8709 20
rsean10 6:050104c0dc75 21 #define LCD_UPDATE .091 //11 Hz
rsean10 6:050104c0dc75 22 #define DRAW_UPDATE 0.075
rsean10 5:93f88deda5ba 23
rsean10 5:93f88deda5ba 24 //Initialize Serial communication
rsean10 5:93f88deda5ba 25 Serial pc(USBTX, USBRX);
rsean10 5:93f88deda5ba 26
rsean10 6:050104c0dc75 27 //Led Initialization
rsean10 6:050104c0dc75 28 DigitalOut led1(LED1); //update leds
rsean10 6:050104c0dc75 29 DigitalOut led2(LED2);
rsean10 6:050104c0dc75 30
rsean10 6:050104c0dc75 31 //Button Initialization
rsean10 6:050104c0dc75 32 DigitalIn button(p13);
rsean10 6:050104c0dc75 33
slicht 2:552e6feb8709 34 //Function prototype for color selection function:
slicht 2:552e6feb8709 35 int get_LCD_color(int color_integer);
slicht 1:6b99ffa62cc8 36
slicht 1:6b99ffa62cc8 37 // Graphic LCD - TX, RX, and RES pins
slicht 2:552e6feb8709 38 uLCD_4DGL uLCD(p9,p10,p11); //initialize a driver object for an LCD connected on pins 9-11
slicht 1:6b99ffa62cc8 39
slicht 1:6b99ffa62cc8 40 // Accelerometer - SDA, SCL, and I2C address
slicht 2:552e6feb8709 41 MMA8452Q accel(p28, p27, 0x1D); //initialize a driver object for an accelerometer connected on pins 27-28.
slicht 1:6b99ffa62cc8 42
slicht 2:552e6feb8709 43 physics_ball ball1; //initialize two balls for bouncing
slicht 2:552e6feb8709 44 physics_ball ball2; //the default states from the library will be used initially
slicht 0:8d3812068c6c 45
rsean10 5:93f88deda5ba 46 //Ticker for logging
rsean10 6:050104c0dc75 47 Ticker logger1; //ball 1 ticker (position log)
rsean10 6:050104c0dc75 48 Ticker logger2; //ball 2 ticker (position log)
rsean10 5:93f88deda5ba 49
rsean10 5:93f88deda5ba 50 void pos_log1(){
rsean10 5:93f88deda5ba 51 pc.printf("Ball 1 (x): %i Ball 1 (y): %i \n\r",ball1.posx,ball1.posy);
rsean10 6:050104c0dc75 52 //prints ball 1 position to terminal
rsean10 5:93f88deda5ba 53 }
rsean10 5:93f88deda5ba 54
rsean10 5:93f88deda5ba 55 void pos_log2(){
rsean10 5:93f88deda5ba 56 pc.printf("Ball 2 (x): %i Ball 2 (y): %i \n\r",ball2.posx,ball2.posy);
rsean10 6:050104c0dc75 57 //prints ball 2 position to terminal
rsean10 6:050104c0dc75 58 }
rsean10 6:050104c0dc75 59
rsean10 6:050104c0dc75 60 //Ticker for update
rsean10 6:050104c0dc75 61 Ticker update1; //ball 1 ticker for update position
rsean10 6:050104c0dc75 62 Ticker update2; //ball 2 ticker for update position
rsean10 6:050104c0dc75 63
rsean10 6:050104c0dc75 64 void ball1up(){
rsean10 6:050104c0dc75 65 ball1.update(UPDATE_TIME_S,accel); //updates position
rsean10 6:050104c0dc75 66 led1 = !led1; //changes led state every update
rsean10 6:050104c0dc75 67 }
rsean10 6:050104c0dc75 68
rsean10 6:050104c0dc75 69 void ball2up(){
rsean10 6:050104c0dc75 70 ball2.update(UPDATE_TIME_S,accel); //updates position
rsean10 6:050104c0dc75 71 led2 = !led2; //changes led state every update
rsean10 6:050104c0dc75 72 }
rsean10 6:050104c0dc75 73
rsean10 6:050104c0dc75 74 //Ticker for display
rsean10 6:050104c0dc75 75 Ticker displayup; //ticker for displaying ball on lcd
rsean10 6:050104c0dc75 76
rsean10 6:050104c0dc75 77 void display(){
rsean10 6:050104c0dc75 78 // Draw circles in the x and y positions stored by the ball objects:
rsean10 6:050104c0dc75 79 uLCD.filled_circle(ball1.posx, ball1.posy, ball1.radius, get_LCD_color(ball1.color));
rsean10 6:050104c0dc75 80 uLCD.filled_circle(ball2.posx, ball2.posy, ball2.radius, get_LCD_color(ball2.color));
rsean10 6:050104c0dc75 81
rsean10 6:050104c0dc75 82 // Wait before erasing old circles:
rsean10 6:050104c0dc75 83 wait(DRAW_UPDATE); // In seconds
rsean10 6:050104c0dc75 84
rsean10 6:050104c0dc75 85 // Erase old circles by writing over there locations using the screen color:
rsean10 6:050104c0dc75 86 uLCD.filled_circle(ball1.posx, ball1.posy, ball1.radius, BLACK);
rsean10 6:050104c0dc75 87 uLCD.filled_circle(ball2.posx, ball2.posy, ball2.radius, BLACK);
rsean10 5:93f88deda5ba 88 }
rsean10 5:93f88deda5ba 89
rsean10 5:93f88deda5ba 90 int main(){
rsean10 5:93f88deda5ba 91 /*
rsean10 5:93f88deda5ba 92 logger1.attach(&pos_log1, LOG_UPDATE);
rsean10 5:93f88deda5ba 93 logger2.attach(&pos_log2, LOG_UPDATE);
rsean10 5:93f88deda5ba 94 */
slicht 1:6b99ffa62cc8 95 // Initialize uLCD
slicht 1:6b99ffa62cc8 96 uLCD.baudrate(115200);
slicht 1:6b99ffa62cc8 97 uLCD.background_color(BLACK);
slicht 1:6b99ffa62cc8 98 uLCD.cls();
slicht 1:6b99ffa62cc8 99
slicht 1:6b99ffa62cc8 100 // Initialize accelerometer
slicht 1:6b99ffa62cc8 101 accel.init();
slicht 1:6b99ffa62cc8 102
slicht 2:552e6feb8709 103 //Initialize balls:
slicht 2:552e6feb8709 104 ball1.set_state(START_X_1,START_Y_1,0,0); //speeds are set to zero
slicht 2:552e6feb8709 105 ball1.set_state(START_X_2,START_Y_2,0,0);
slicht 2:552e6feb8709 106
slicht 2:552e6feb8709 107 //Set ball radius and color:
slicht 2:552e6feb8709 108 ball1.set_param(RADIUS_1,0); //color is unimportant
slicht 2:552e6feb8709 109 ball2.set_param(RADIUS_2,1); //just making sure the colors are different
slicht 2:552e6feb8709 110
rsean10 6:050104c0dc75 111 update1.attach(&ball1up,UPDATE_TIME_S); //ticker force position update ball 1
rsean10 6:050104c0dc75 112 update2.attach(&ball2up,UPDATE_TIME_S); //ticker force position update ball 2
rsean10 6:050104c0dc75 113
rsean10 6:050104c0dc75 114 displayup.attach(&display,LCD_UPDATE); //ticker force lcd update
rsean10 6:050104c0dc75 115
slicht 2:552e6feb8709 116 while (1) { //execute 'forever'
rsean10 6:050104c0dc75 117
slicht 2:552e6feb8709 118 if (DEBUG_MODE) {
slicht 3:a7f02754bc99 119 //If compiled with DEBUG_MODE flag raised, print values to screen.
slicht 2:552e6feb8709 120 uLCD.locate(0,4);
slicht 2:552e6feb8709 121 uLCD.printf("X: %d.1\nY: %.1d",ball1.posx,ball1.posy);
slicht 2:552e6feb8709 122
slicht 2:552e6feb8709 123 uLCD.locate(0,6);
slicht 2:552e6feb8709 124 uLCD.printf("VX: %f.1\nVY: %.1f",ball1.speedx,ball1.speedy);
slicht 1:6b99ffa62cc8 125
slicht 2:552e6feb8709 126 uLCD.locate(0,10);
slicht 2:552e6feb8709 127 uLCD.printf("AX: %f.1\nAY: %.1f\nAZ: %0.1f",accel.readX(),accel.readY(),accel.readZ());
slicht 2:552e6feb8709 128 }
slicht 2:552e6feb8709 129 }
slicht 2:552e6feb8709 130 }
slicht 1:6b99ffa62cc8 131
slicht 3:a7f02754bc99 132 //Interpret LCD colors.
slicht 2:552e6feb8709 133 int get_LCD_color(int color_integer)
slicht 2:552e6feb8709 134 {
slicht 2:552e6feb8709 135 switch (color_integer) {
slicht 2:552e6feb8709 136 case 0:
slicht 2:552e6feb8709 137 return(RED);
slicht 2:552e6feb8709 138 case 1:
slicht 2:552e6feb8709 139 return(BLUE);
slicht 2:552e6feb8709 140 case 2:
slicht 2:552e6feb8709 141 return(GREEN);
slicht 2:552e6feb8709 142 default:
slicht 2:552e6feb8709 143 return(WHITE);
slicht 0:8d3812068c6c 144 }
slicht 1:6b99ffa62cc8 145 }