Asteroids for MBED game with Bluetooth LE UART Friend Controller, uLCD, speaker, and uSD.

Dependencies:   4DGL-uLCD-SE mbed-rtos mbed

Asteroids for MBED game with Bluetooth LE UART Friend Controller

Revision:
5:f41d3bfc6701
Parent:
4:563648944ecc
Child:
6:7d1d8945b2f7
diff -r 563648944ecc -r f41d3bfc6701 main.cpp
--- a/main.cpp	Sat Mar 12 03:57:01 2016 +0000
+++ b/main.cpp	Sat Mar 12 16:11:05 2016 +0000
@@ -8,10 +8,21 @@
 //constants
 #define ACCEL .5
 #define ROTTA .2
+#define MAXSP 10
 
 //devices
-DigitalOut myled(LED1);
+//DigitalOut myled(LED1);
 uLCD_4DGL ulcd(p28,p27,p29);
+BusOut myled(LED1,LED2,LED3,LED4);
+Serial blue(p13,p14);
+
+//Globals
+int game_speed = 25;
+bool up = 0;
+bool left = 0;
+bool right = 0;
+bool four = 0;
+
 
 class Movement
 {
@@ -60,9 +71,9 @@
 {
     x_vel += ACCEL * cos(angle);
     y_vel += ACCEL * sin(angle);
-    if(sqrt(pow(x_vel,2) + pow(y_vel,2)) > 10) {
-        x_vel = x_vel * 10 / sqrt(pow(x_vel,2) + pow(y_vel,2));
-        y_vel = y_vel * 10 / sqrt(pow(x_vel,2) + pow(y_vel,2));
+    if(sqrt(pow(x_vel,2) + pow(y_vel,2)) > MAXSP) {
+        x_vel = x_vel * MAXSP / sqrt(pow(x_vel,2) + pow(y_vel,2));
+        y_vel = y_vel * MAXSP / sqrt(pow(x_vel,2) + pow(y_vel,2));
     }
 }
 
@@ -73,6 +84,93 @@
 void Movement::turn_right(){
     angle += ROTTA;
 }
+
+//Threads
+//Controls
+int read_getc(Serial &blue)
+{
+    if(!blue.readable()) {
+        Thread::yield();
+    }
+    return blue.getc();
+}
+
+void controls(void const *args)
+{
+    char bnum=0;
+    char bhit=0;
+    while(1) {
+        if (read_getc(blue)=='!') {
+            if (read_getc(blue)=='B') { //button data packet
+                bnum = read_getc(blue); //button number
+                bhit = read_getc(blue); //1=hit, 0=release
+                if (read_getc(blue)==char(~('!' + 'B' + bnum + bhit))) { //checksum OK?
+                    myled = bnum - '0'; //current button number will appear on LEDs
+                    switch (bnum) {
+                        case '1': //number button 1
+                            if (bhit=='1') {
+                                //add hit code here
+                            } else {
+                                //add release code here
+                            }
+                            break;
+                        case '2': //number button 2
+                            if (bhit=='1') {
+                                //add hit code here
+                            } else {
+                                //add release code here
+                            }
+                            break;
+                        case '3': //number button 3
+                            if (bhit=='1') {
+                                //add hit code here
+                            } else {
+                                //add release code here
+                            }
+                            break;
+                        case '4': //number button 4
+                            if (bhit=='1') {
+                                //add hit code here
+                            } else {
+                                //add release code here
+                            }
+                            break;
+                        case '5': //button 5 up arrow
+                            if (bhit=='1') {
+                                //add hit code here
+                            } else {
+                                //add release code here
+                            }
+                            break;
+                            //case '6': //button 6 down arrow
+                            //  if (bhit=='1') {
+                            //add release code here
+                            //} else {
+                            //add release code here
+                            //}
+                            //break;
+                        case '7': //button 7 left arrow
+                            if (bhit=='1') {
+                                //add hit code here
+                            } else {
+                                //add release code here
+                            }
+                            break;
+                        case '8': //button 8 right arrow
+                            if (bhit=='1') {
+                                //add hit code here
+                            } else {
+                                //add release code here
+                            }
+                            break;
+                        default:
+                            break;
+                    }
+                }
+            }
+        }
+    }
+}
     
 //collisions
 bool collision(Movement p1, Movement p2)
@@ -105,6 +203,7 @@
 
 int main()
 {
+    Thread buttons(controls);
     Movement player(50, 100, 0, -5, 0, 5, 1);
     list<Movement> asteroids();
     list<Movement> bullets();
@@ -112,6 +211,6 @@
         player.move();
         //calc_collisions();
         //draw_objs(player, asteroids, bullets);
-
+        wait(game_speed);
     }
 }
\ No newline at end of file