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:
Tue Nov 07 15:24:31 2017 +0000
Revision:
5:93f88deda5ba
Parent:
3:a7f02754bc99
Child:
6:050104c0dc75

        

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