maze game

Dependencies:   MMA8451Q mbed

Fork of mbed_max2719 by David Wynn

Revision:
1:ac65e35a9196
Parent:
0:6be425a138b2
--- a/main.cpp	Sun Apr 10 19:02:41 2016 +0000
+++ b/main.cpp	Mon Apr 11 17:41:17 2016 +0000
@@ -4,25 +4,27 @@
  * After initialisation two characters (H and W) are displayed alternatively.
  * The MAX7219 IC is driven by hardware SPI: SPI0 module at PTD1, PTD2, PTD3.
  */
- 
+
 #include "mbed.h"
 #include "MMA8451Q.h"
 
 PinName const SDA = PTE25;
 PinName const SCL = PTE24;
- 
+
 SPI spi(PTD2, PTD3, PTD1);          // Arduino compatible MOSI, MISO, SCLK
 DigitalOut cs(PTD0);                // Chip select
+int  point []= {0,2};
 
 #define MMA8451_I2C_ADDRESS (0x1d<<1)
- 
-const unsigned char led1[]= {
-    0xFF,0x18,0x18,0x18,0x18,0x18,0x18,0xFF
-};  //H
+
 const unsigned char led2[]= {
-    0x1F,0x60,0x80,0x40,0x40,0x80,0x60,0x1F
-};  //W
- 
+   0xFF,0xB4,0x15,0xD1,0x85,0xB5,0x95,0xFF};//maze wall
+
+//0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//no maze wall
+unsigned char led1[]= {
+    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
+};  //
+
 /// Send two bytes to SPI bus
 void SPI_Write2(unsigned char MSB, unsigned char LSB)
 {
@@ -32,20 +34,65 @@
     cs = 1;                         // Set CS High
 }
 
-void direction(float x, float y){
-    if(x > 0.5){
-        printf("Left \n");
-    }else if(x < -0.5){
-        printf("Right \n");
-    }else if(y > 0.5){
-        printf("Up \n");
-    }else if(y < -0.5){
-        printf("Down \n");
-    }else{
-        printf("Neutral Position \n");    
-      }
+bool isNotWall(int x,int y)
+{
+    bool result=false;
+    if(x>=0&&x<8&&y>=0&&y<8) {
+        int wallline =led2[y];
+        int pointline=(unsigned char)(1<<(7-x));
+        printf("%d %d\n",wallline,pointline);
+
+        if((wallline|pointline)!=wallline) {
+            result=true;
+
+        }
+        
+        /*
+        101
+        100
+        
+       |101
+       &100
+
+        101
+        010
+        
+       |111
+       &000
+       */
+    }
+    return result;
 }
- 
+void direction(float x, float y)
+{
+    
+    if(x > 0.5) {
+        if(isNotWall(point[0]+1,point[1])) {
+            point[0]++;
+        }
+        printf("Left %d %d\n",point[0],point[1]);
+    } else if(x < -0.5) {
+        if(isNotWall(point[0]-1,point[1])) {
+            point[0]--;
+        }
+        printf("Right %d %d\n",point[0],point[1]);
+    } else if(y > 0.5) {
+        if(isNotWall(point[0],point[1]-1)) {
+            point[1]--;
+        }
+        printf("Down %d %d\n",point[0],point[1]);
+    } else if(y < -0.5) {
+        if(isNotWall(point[0],point[1]+1)) {
+            point[1]++;
+        }
+        printf("Up %d %d\n",point[0],point[1]);
+    } else {
+        printf("Neutral Position \n");
+    }
+}
+
+
+
 /// MAX7219 initialisation
 void Init_MAX7219(void)
 {
@@ -66,39 +113,52 @@
     SPI_Write2(0x0F, 0x00);         // Disable display test
     wait_ms(500);                   // 500 ms delay
 }
- 
+
+
+void clearMap(unsigned char *map )
+{
+    for (int i=0; i<8; i++) {
+        map[i]=0x00;
+    }
+}
+
+void updateMap( )
+{
+    clearMap(led1);
+    led1[point[1]]=(unsigned char)(1<<(7-point[0]));
+
+}
+
+
 int main()
 {
-    
+
     cs = 1;                         // CS initially High
     spi.format(8,0);                // 8-bit format, mode 0,0
     spi.frequency(1000000);         // SCLK = 1 MHz
     Init_MAX7219();                 // Initialize the LED controller
     MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
-    PwmOut rled(LED1);
-    PwmOut gled(LED2);
-    PwmOut bled(LED3);
-    
-    
+
+
+
     while (1) {
-        
-        float x, y, z;
+
+        float x, y;
         x = acc.getAccX();
         y = acc.getAccY();
-        z = acc.getAccZ();
-        rled = 1.0f - abs(x);
-        gled = 1.0f - abs(y);
-        bled = 1.0f - abs(z);
+
         direction(x,y);
-        wait(0.5f);
+        updateMap();
+
+
+        wait(0.5);
         
         for(int i=1; i<9; i++)      // Write first character (8 rows)
-            SPI_Write2(i,led1[i-1]);
-        wait(1);                    // 1 sec delay
-        for(int i=1; i<9; i++)      // Write second character
+            SPI_Write2(i,led1[i-1]|led2[i-1]);
+        wait(0.5);
+        for(int i=1; i<9; i++)      // Write first character (8 rows)
             SPI_Write2(i,led2[i-1]);
-        wait(1);                    // 1 sec delay */
     }
-    
-    
+
+
 }
\ No newline at end of file