asdf

Dependencies:   NokiaLCD XMIT_IR mbed

Fork of 4180_mP_WirelessPong_revC by Curtis Mulady

Files at this revision

API Documentation at this revision

Comitter:
mesdoram
Date:
Tue Oct 09 23:28:34 2012 +0000
Parent:
19:5a4be4519de5
Commit message:
asdf

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
pong/pong.cpp Show annotated file Show diff for this revision Revisions of this file
pong/pong.h Show annotated file Show diff for this revision Revisions of this file
diff -r 5a4be4519de5 -r a74fe4d43ec0 main.cpp
--- a/main.cpp	Sat Oct 06 17:45:32 2012 +0000
+++ b/main.cpp	Tue Oct 09 23:28:34 2012 +0000
@@ -2,6 +2,7 @@
 #include "rtos.h"
 #include "NokiaLCD.h"
 #include "XMIT_IR.h"
+#include "pong.h"
 
 #define FPS 15
 #define PKT_RATE 15
@@ -20,19 +21,19 @@
 ****************************************/
 
 //Function Prototypes
-void BlinkAlive(void const* arguments);
+/*void BlinkAlive(void const* arguments);
 void UpdateLCD(void const* arguments);
 void IRStuff(void const* arguments);
 void ISR_UARTRX(void);
 char CheckPacket(char new_data, char* packet_buffer, char* data, int data_len);
 void MakePacket2(char* data,int len);
-
-//Pin Setup
+*/
+/*//Pin Setup
 PwmOut led1(LED1);
 PwmOut led2(LED2);
 DigitalOut led3(LED3);
 DigitalOut led4(LED4);
-NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type
+//NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type
 Serial* devicerx;
 
 //Serial devicetx(p13,NC);
@@ -50,15 +51,32 @@
 Thread* threadptr_irstuff;
 char packet_buff[10];
 Mutex data_update_mutex;
-
+*/
 
-
+AnalogIn paddle(p20);
 
 int main()
 {
-
+    double left = 1.00, right = 0.8;
+    right = paddle;
+    pong pongTest;
+    pongTest.drawObjects();
+    while(1)
+    {
+    pongTest.clear();
+    right = paddle;
+    pongTest.update(left, right);
+    pongTest.drawObjects();
+    /*lcd.cls();
+    lcd.locate(0,1);
+    lcd.printf("%d", counter);
+    */
+    Thread::wait(1000/FPS);
+    }
+}
+/*
     //LCD init
-    lcd.background(0x000000);
+    //lcd.background(0x000000);
 
     //PC serial init
     pc.baud(19200);
@@ -78,8 +96,8 @@
     threadptr_irstuff = &thread_irstuff;
 
 
-
-    while(1) {
+*/
+   /* while(1) {
 
         //Use main loop to set LCD framerate
         thread_updatelcd.signal_set(0x1);
@@ -197,4 +215,4 @@
 }
 
 
-
+*/
diff -r 5a4be4519de5 -r a74fe4d43ec0 pong/pong.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pong/pong.cpp	Tue Oct 09 23:28:34 2012 +0000
@@ -0,0 +1,69 @@
+#include "pong.h"
+using namespace std;
+
+pong::pong()
+{
+    lcd = new NokiaLCD(p5, p7, p8, p9, NokiaLCD::LCD6610);
+    //for all dimensions: x, y, width, height
+    ball[0] = 65; ball[1] = 65; ball[2] = 3; ball[3] = 3;
+    ballDir[0] = -1; ballDir[1] = 1;
+    rightPaddle[0] = 10; rightPaddle[1] = 55; rightPaddle[2] = 5; rightPaddle[3] = 20;
+    leftPaddle[0] = 115; leftPaddle[1] = 55; leftPaddle[2] = 5; leftPaddle[3] = 20;
+    score[0] = 0; score[1] = 0;
+    
+}
+
+void pong::drawObjects()
+{
+    lcd->background(0x000000);
+    lcd->fill(leftPaddle[0], leftPaddle[1], leftPaddle[2], leftPaddle[3], 0xFFFFFF);
+    lcd->fill(rightPaddle[0], rightPaddle[1], rightPaddle[2], rightPaddle[3], 0xFFFFFF);
+    lcd->fill(ball[0], ball[1], ball[2], ball[3], 0xFFFFFF);
+}
+
+void pong::update(double left, double right)
+{
+    leftPaddle[1] = left * 110;
+    rightPaddle[1] = right * 110;
+    
+    //right paddle collision check -- reverse if true
+    if ( (ball[0] + ball[2] -1 + ballDir[0]) == leftPaddle[0] ) {
+        if ( ( (ball[1] + ball[3]-1 + ballDir[1]) >= leftPaddle[1] ) && ( (ball[1] + ballDir[1]) <= (leftPaddle[1] + leftPaddle[3]-1) ) )
+            ballDir[0] = -1;
+    }
+    
+    //left paddle collision check -- reverse if true
+    if ( (ball[0] + ballDir[0]) == (rightPaddle[0] + rightPaddle[2]-1) ) {
+        if ( ( (ball[1] + ball[3]-1 + ballDir[1]) >= rightPaddle[1] ) && ( (ball[1] + ballDir[1]) <= (rightPaddle[1] + rightPaddle[3]-1) ) )
+            ballDir[0] = 1;
+    }
+    
+    ball[0] = ball[0] + ballDir[0];
+    ball[1] = ball[1] + ballDir[1];
+    
+    //if the ball hits the left wall, increment right player's score and reset
+    if ( ball[0] == 0 )
+    {
+        score[1]++;
+        ball[0] = 65; ball[1] = 65; ball[2] = 3; ball[3] = 3;
+        ballDir[0] = -1; ballDir[1] = 1;
+    }
+     //if the ball hits the right wall, increment left player's score and reset
+    else if ( (ball[0] + ball[2]-1) == 130 )
+    {
+        score[0]++;
+        ball[0] = 65; ball[1] = 65; ball[2] = 3; ball[3] = 3;
+        ballDir[0] = 1; ballDir[1] = 1;
+    } 
+    
+    if ( (ball[1] + 2 == 130) && (ballDir[1] == 1) ) ballDir[1] = -1;
+    else if ( (ball[1] - 1 == 0) && (ballDir[1] == -1) ) ballDir[1] = 1;
+    
+}
+
+void pong::clear()
+{
+    lcd->fill(leftPaddle[0], leftPaddle[1], leftPaddle[2], leftPaddle[3], 0x000000);
+    lcd->fill(rightPaddle[0], rightPaddle[1], rightPaddle[2], rightPaddle[3], 0x000000);
+    lcd->fill(ball[0], ball[1], ball[2], ball[3], 0x000000);
+}
\ No newline at end of file
diff -r 5a4be4519de5 -r a74fe4d43ec0 pong/pong.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pong/pong.h	Tue Oct 09 23:28:34 2012 +0000
@@ -0,0 +1,19 @@
+#include "mbed.h"
+#include "NokiaLCD.h"
+
+class pong
+{
+private:
+    NokiaLCD *lcd;
+    int ball[4];
+    int ballDir[2];
+    int leftPaddle[4];
+    int rightPaddle[4];
+    int score[2];
+    
+public:
+    pong();
+    void drawObjects();
+    void update(double, double);
+    void clear();
+};
\ No newline at end of file