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:
slicht
Date:
Tue Nov 07 12:09:39 2017 +0000
Revision:
3:a7f02754bc99
Parent:
2:552e6feb8709
Child:
5:93f88deda5ba
Comments added.

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
slicht 2:552e6feb8709 21 //Function prototype for color selection function:
slicht 2:552e6feb8709 22 int get_LCD_color(int color_integer);
slicht 1:6b99ffa62cc8 23
slicht 1:6b99ffa62cc8 24 // Graphic LCD - TX, RX, and RES pins
slicht 2:552e6feb8709 25 uLCD_4DGL uLCD(p9,p10,p11); //initialize a driver object for an LCD connected on pins 9-11
slicht 1:6b99ffa62cc8 26
slicht 1:6b99ffa62cc8 27 // Accelerometer - SDA, SCL, and I2C address
slicht 2:552e6feb8709 28 MMA8452Q accel(p28, p27, 0x1D); //initialize a driver object for an accelerometer connected on pins 27-28.
slicht 1:6b99ffa62cc8 29
slicht 2:552e6feb8709 30 physics_ball ball1; //initialize two balls for bouncing
slicht 2:552e6feb8709 31 physics_ball ball2; //the default states from the library will be used initially
slicht 0:8d3812068c6c 32
slicht 2:552e6feb8709 33 int main()
slicht 2:552e6feb8709 34 {
slicht 1:6b99ffa62cc8 35 // Initialize uLCD
slicht 1:6b99ffa62cc8 36 uLCD.baudrate(115200);
slicht 1:6b99ffa62cc8 37 uLCD.background_color(BLACK);
slicht 1:6b99ffa62cc8 38 uLCD.cls();
slicht 1:6b99ffa62cc8 39
slicht 1:6b99ffa62cc8 40 // Initialize accelerometer
slicht 1:6b99ffa62cc8 41 accel.init();
slicht 1:6b99ffa62cc8 42
slicht 2:552e6feb8709 43 //Initialize balls:
slicht 2:552e6feb8709 44 ball1.set_state(START_X_1,START_Y_1,0,0); //speeds are set to zero
slicht 2:552e6feb8709 45 ball1.set_state(START_X_2,START_Y_2,0,0);
slicht 2:552e6feb8709 46
slicht 2:552e6feb8709 47 //Set ball radius and color:
slicht 2:552e6feb8709 48 ball1.set_param(RADIUS_1,0); //color is unimportant
slicht 2:552e6feb8709 49 ball2.set_param(RADIUS_2,1); //just making sure the colors are different
slicht 2:552e6feb8709 50
slicht 2:552e6feb8709 51 /* Make the balls "fall" in direction of accelerometer by forcing the ball objects
slicht 2:552e6feb8709 52 to update themselves at regular intervals, and te drawing the locations reported
slicht 2:552e6feb8709 53 by the ball objects on the LCD screen: */
slicht 2:552e6feb8709 54 while (1) { //execute 'forever'
slicht 3:a7f02754bc99 55 // Draw circles in the x and y positions stored by the ball objects:
slicht 2:552e6feb8709 56 uLCD.filled_circle(ball1.posx, ball1.posy, ball1.radius, get_LCD_color(ball1.color));
slicht 2:552e6feb8709 57 uLCD.filled_circle(ball2.posx, ball2.posy, ball2.radius, get_LCD_color(ball2.color));
slicht 2:552e6feb8709 58
slicht 2:552e6feb8709 59 // Wait before erasing old circles:
slicht 2:552e6feb8709 60 wait(UPDATE_TIME_S); // In seconds
slicht 2:552e6feb8709 61
slicht 2:552e6feb8709 62 // Erase old circles by writing over there locations using the screen color:
slicht 2:552e6feb8709 63 uLCD.filled_circle(ball1.posx, ball1.posy, ball1.radius, BLACK);
slicht 2:552e6feb8709 64 uLCD.filled_circle(ball2.posx, ball2.posy, ball2.radius, BLACK);
slicht 1:6b99ffa62cc8 65
slicht 2:552e6feb8709 66 // Force the objects 'ball1' and 'ball2' to update their stored positions
slicht 2:552e6feb8709 67 // and velocities:
slicht 2:552e6feb8709 68 ball1.update(UPDATE_TIME_S,accel);
slicht 2:552e6feb8709 69 ball2.update(UPDATE_TIME_S,accel);
slicht 2:552e6feb8709 70
slicht 2:552e6feb8709 71 if (DEBUG_MODE) {
slicht 3:a7f02754bc99 72 //If compiled with DEBUG_MODE flag raised, print values to screen.
slicht 2:552e6feb8709 73 uLCD.locate(0,4);
slicht 2:552e6feb8709 74 uLCD.printf("X: %d.1\nY: %.1d",ball1.posx,ball1.posy);
slicht 2:552e6feb8709 75
slicht 2:552e6feb8709 76 uLCD.locate(0,6);
slicht 2:552e6feb8709 77 uLCD.printf("VX: %f.1\nVY: %.1f",ball1.speedx,ball1.speedy);
slicht 1:6b99ffa62cc8 78
slicht 2:552e6feb8709 79 uLCD.locate(0,10);
slicht 2:552e6feb8709 80 uLCD.printf("AX: %f.1\nAY: %.1f\nAZ: %0.1f",accel.readX(),accel.readY(),accel.readZ());
slicht 2:552e6feb8709 81 }
slicht 2:552e6feb8709 82 }
slicht 2:552e6feb8709 83 }
slicht 1:6b99ffa62cc8 84
slicht 3:a7f02754bc99 85 //Interpret LCD colors.
slicht 2:552e6feb8709 86 int get_LCD_color(int color_integer)
slicht 2:552e6feb8709 87 {
slicht 2:552e6feb8709 88 switch (color_integer) {
slicht 2:552e6feb8709 89 case 0:
slicht 2:552e6feb8709 90 return(RED);
slicht 2:552e6feb8709 91 case 1:
slicht 2:552e6feb8709 92 return(BLUE);
slicht 2:552e6feb8709 93 case 2:
slicht 2:552e6feb8709 94 return(GREEN);
slicht 2:552e6feb8709 95 default:
slicht 2:552e6feb8709 96 return(WHITE);
slicht 0:8d3812068c6c 97 }
slicht 1:6b99ffa62cc8 98 }