Pong Game Tea Term

Dependencies:   HCSR04 mbed

Fork of contest_IOT by Alexandre HOCHART

Files at this revision

API Documentation at this revision

Comitter:
Alex_Hochart
Date:
Wed Oct 14 08:58:18 2015 +0000
Parent:
2:35466dfc81fe
Child:
4:ed21ec4a79ad
Commit message:
Pong Tera Term

Changed in this revision

HCSR04.lib Show annotated file Show diff for this revision Revisions of this file
contest.lib Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/HCSR04.lib	Sun Oct 11 11:18:29 2015 +0000
+++ b/HCSR04.lib	Wed Oct 14 08:58:18 2015 +0000
@@ -1,1 +1,1 @@
-https://developer.mbed.org/teams/Contest-IOT-GSE5/code/HCSR04/#79e45473cfab
+http://mbed.org/users/aralshukaili/code/HCSR04/#0bda99bb39a4
--- a/contest.lib	Sun Oct 11 11:18:29 2015 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://developer.mbed.org/teams/Contest-IOT-GSE5/code/contest_IOT/#04b305a9368f
--- a/main.cpp	Sun Oct 11 11:18:29 2015 +0000
+++ b/main.cpp	Wed Oct 14 08:58:18 2015 +0000
@@ -1,13 +1,10 @@
 #include "mbed.h"
-#include "hcsr04.h"
-#include "contest.h"
-#include <math.h>
+#include "HCSR04.h"
 
-#define ECHO_1  PB_9
-#define ECHO_2  PA_5
-#define TRIG_1  PB_8
-#define TX      PA_2
-#define RX      PA_3
+#define ECHO_1  PA_8
+#define ECHO_2  PB_4
+#define TRIG_1  PB_10
+#define TRIG_2  PB_5
 #define PUSH    USER_BUTTON //PC_13
 #define LED_1   LED1
 
@@ -17,45 +14,117 @@
 //------------------------------------
 
 
-Serial pc(TX, RX);          //UART
+Serial pc(USBTX, USBRX);    //UART
 DigitalOut led(LED_1);      //Led d'état
 InterruptIn button(PUSH);   //Bouton d'interruption
 
-HCSR04 sensor(TRIG_1, ECHO_1); 
-Control ctrl;
+HCSR04 sensor1(TRIG_1, ECHO_1);
+HCSR04 sensor2(TRIG_2, ECHO_2); 
+
+void pong_init(){
 
-void changeEtat(){
-    pc.printf("\033[2J");   //Efface la console
-    ctrl.marcheArret();
+    pc.printf("\033[2J");       //Efface la console
+    pc.printf("\033[?25l");     //Cache le curseur
+    
+    for(int i=0; i <= 128 ; i++){
+        pc.printf("\033[0;%dH",i);     //Place le curseur à 0:0
+        pc.printf("X");     //Place le curseur à 0:0
+        pc.printf("\033[32;%dH",i);     //Place le curseur à 0:0
+        pc.printf("X");     //Place le curseur à 0:0
+    }
+    
+    for(int i=0; i <= 32 ; i++){
+        pc.printf("\033[%d;0H",i);     //Place le curseur à 0:0
+        pc.printf("X");     //Place le curseur à 0:0
+        pc.printf("\033[%d;128H",i);     //Place le curseur à 0:0
+        pc.printf("X");     //Place le curseur à 0:0
+    }
+    
 }
-float distancePrec=0;
-
-
 
 int main() {
-    float distance;
-    pc.printf("\033[2J");       //Efface la console
-    pc.printf("\033[0;0H");     //Place le curseur à 0:0
-    pc.printf("\033[?25l");     //Cache le curseur
-    //pc.printf("\033[?25h");   //Affiche le curseur
-    pc.printf("Initialisation...   ");
+    int distance1;
+    int distance2;
+    int pos1prec[30];
+    int pos2prec[30];
+    int pos1[30];
+    int pos2[30];
+
+    pc.baud(115200);
+    pong_init();
+    wait(1);
+    //Initialisation de l'interruption : en appuyant sur le bouton bleu de la carte, le programme change d'état
     
-    //Initialisation de l'interruption : en appuyant sur le bouton bleu de la carte, le programme change d'état
-    button.fall(&changeEtat);
-    
-    pc.printf("Ready !\n\r");
     
     //Boucle d'exécution du programme
     while(1) { 
-        if(ctrl.isActive()){
             led=1;
-            distance = sensor.distance();               //Mesure de la distance
-            print_distance(distance, distancePrec);     //Affichage à l'écran
-            distancePrec=distance;                      //Mise en mémoire
-        }else{
-            led=0;
-        }
-    wait(0.001);
+            for(int i=0; i<=29; i++){
+                pos1prec[i]= pos1[i];
+                pos2prec[i]= pos2[i];
+                pos1[i]= 0;
+                pos2[i]= 0;
+            }
+            distance1 = sensor1.distance(1);               //Mesure de la distance
+            distance2 = sensor2.distance(1);               //Mesure de la distance
+            
+            if(distance1 <= 10){
+                for(int i=0;i<=5;i++){
+                    pos1[i] = 1;
+                }
+            }
+            else if(distance1 >= 34){
+                for(int i=24;i<=29;i++){
+                    pos1[i] = 1;
+                }
+            }
+            else{
+                for(int i=(distance1-10);i<=(distance1-4);i++){
+                    pos1[i] = 1;
+                }
+            }
+            
+            
+            if(distance2 <= 10){
+                for(int i=0;i<=5;i++){
+                    pos2[i] = 1;
+                }
+            }
+            else if(distance2 >= 34){
+                for(int i=24;i<=29;i++){
+                    pos2[i] = 1;
+                }
+            }
+            else{
+                for(int i=(distance2-10);i<=(distance2-4);i++){
+                    pos2[i] = 1;
+                }
+            }  
+            
+            for(int i=0;i<=29;i++){
+                if( pos1[i] != pos1prec[i] ){
+                    if(pos1[i] == 1){
+                        pc.printf("\033[%d;3H",i+2);     //Place le curseur à 0:0
+                        pc.printf("X");     //Place le curseur à 0:0  
+                    }
+                    else{
+                        pc.printf("\033[%d;3H",i+2);     //Place le curseur à 0:0
+                        pc.printf(" ");     //Place le curseur à 0:0  
+                    }
+                }
+                if( pos2[i] != pos2prec[i] ){
+                    if(pos2[i] == 1){
+                        pc.printf("\033[%d;126H",i+2);     //Place le curseur à 0:0
+                        pc.printf("X");     //Place le curseur à 0:0  
+                    }
+                    else{
+                        pc.printf("\033[%d;126H",i+2);     //Place le curseur à 0:0
+                        pc.printf(" ");     //Place le curseur à 0:0  
+                    }
+                }   
+            }
+            wait(0.1);
     }
 }
+
  
\ No newline at end of file