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:07:36 2017 +0000
Revision:
2:552e6feb8709
Parent:
1:6b99ffa62cc8
Child:
3:a7f02754bc99
Working version.  Non-deterministic behavior in color cycling, however.

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 1:6b99ffa62cc8 4
slicht 0:8d3812068c6c 5 #include "mbed.h"
slicht 2:552e6feb8709 6 #include "MMA8452Q.h" //acceleromater library
slicht 2:552e6feb8709 7 #include "uLCD_4DGL.h" //LCD library
slicht 2:552e6feb8709 8 #include "bouncing_ball.h" //new ball phyics library
slicht 1:6b99ffa62cc8 9
slicht 2:552e6feb8709 10 #define UPDATE_TIME_S 0.02
slicht 2:552e6feb8709 11 #define START_X_1 10
slicht 2:552e6feb8709 12 #define START_Y_1 10
slicht 2:552e6feb8709 13 #define START_X_2 20
slicht 2:552e6feb8709 14 #define START_Y_2 20
slicht 2:552e6feb8709 15 #define RADIUS_1 6
slicht 2:552e6feb8709 16 #define RADIUS_2 3
slicht 0:8d3812068c6c 17
slicht 2:552e6feb8709 18 #define DEBUG_MODE 0
slicht 2:552e6feb8709 19
slicht 2:552e6feb8709 20 //Function prototype for color selection function:
slicht 2:552e6feb8709 21 int get_LCD_color(int color_integer);
slicht 1:6b99ffa62cc8 22
slicht 1:6b99ffa62cc8 23 // Graphic LCD - TX, RX, and RES pins
slicht 2:552e6feb8709 24 uLCD_4DGL uLCD(p9,p10,p11); //initialize a driver object for an LCD connected on pins 9-11
slicht 1:6b99ffa62cc8 25
slicht 1:6b99ffa62cc8 26 // Accelerometer - SDA, SCL, and I2C address
slicht 2:552e6feb8709 27 MMA8452Q accel(p28, p27, 0x1D); //initialize a driver object for an accelerometer connected on pins 27-28.
slicht 1:6b99ffa62cc8 28
slicht 2:552e6feb8709 29 physics_ball ball1; //initialize two balls for bouncing
slicht 2:552e6feb8709 30 physics_ball ball2; //the default states from the library will be used initially
slicht 0:8d3812068c6c 31
slicht 2:552e6feb8709 32 int main()
slicht 2:552e6feb8709 33 {
slicht 1:6b99ffa62cc8 34 // Initialize uLCD
slicht 1:6b99ffa62cc8 35 uLCD.baudrate(115200);
slicht 1:6b99ffa62cc8 36 uLCD.background_color(BLACK);
slicht 1:6b99ffa62cc8 37 uLCD.cls();
slicht 1:6b99ffa62cc8 38
slicht 1:6b99ffa62cc8 39 // Initialize accelerometer
slicht 1:6b99ffa62cc8 40 accel.init();
slicht 1:6b99ffa62cc8 41
slicht 2:552e6feb8709 42 //Initialize balls:
slicht 2:552e6feb8709 43 ball1.set_state(START_X_1,START_Y_1,0,0); //speeds are set to zero
slicht 2:552e6feb8709 44 ball1.set_state(START_X_2,START_Y_2,0,0);
slicht 2:552e6feb8709 45
slicht 2:552e6feb8709 46 //Set ball radius and color:
slicht 2:552e6feb8709 47 ball1.set_param(RADIUS_1,0); //color is unimportant
slicht 2:552e6feb8709 48 ball2.set_param(RADIUS_2,1); //just making sure the colors are different
slicht 2:552e6feb8709 49
slicht 2:552e6feb8709 50 /* Make the balls "fall" in direction of accelerometer by forcing the ball objects
slicht 2:552e6feb8709 51 to update themselves at regular intervals, and te drawing the locations reported
slicht 2:552e6feb8709 52 by the ball objects on the LCD screen: */
slicht 2:552e6feb8709 53 while (1) { //execute 'forever'
slicht 2:552e6feb8709 54 // Draw a red circle using the x and y positions stored by the object 'ball1':
slicht 2:552e6feb8709 55 uLCD.filled_circle(ball1.posx, ball1.posy, ball1.radius, get_LCD_color(ball1.color));
slicht 2:552e6feb8709 56 // Draw a blue circle using the x and y positions stored by the object 'ball2':
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 2:552e6feb8709 72 uLCD.locate(0,4);
slicht 2:552e6feb8709 73 uLCD.printf("X: %d.1\nY: %.1d",ball1.posx,ball1.posy);
slicht 2:552e6feb8709 74
slicht 2:552e6feb8709 75 uLCD.locate(0,6);
slicht 2:552e6feb8709 76 uLCD.printf("VX: %f.1\nVY: %.1f",ball1.speedx,ball1.speedy);
slicht 1:6b99ffa62cc8 77
slicht 2:552e6feb8709 78 uLCD.locate(0,10);
slicht 2:552e6feb8709 79 uLCD.printf("AX: %f.1\nAY: %.1f\nAZ: %0.1f",accel.readX(),accel.readY(),accel.readZ());
slicht 2:552e6feb8709 80 }
slicht 2:552e6feb8709 81 }
slicht 2:552e6feb8709 82 }
slicht 1:6b99ffa62cc8 83
slicht 2:552e6feb8709 84 int get_LCD_color(int color_integer)
slicht 2:552e6feb8709 85 {
slicht 2:552e6feb8709 86 switch (color_integer) {
slicht 2:552e6feb8709 87 case 0:
slicht 2:552e6feb8709 88 return(RED);
slicht 2:552e6feb8709 89 case 1:
slicht 2:552e6feb8709 90 return(BLUE);
slicht 2:552e6feb8709 91 case 2:
slicht 2:552e6feb8709 92 return(GREEN);
slicht 2:552e6feb8709 93 default:
slicht 2:552e6feb8709 94 return(WHITE);
slicht 0:8d3812068c6c 95 }
slicht 1:6b99ffa62cc8 96 }