Yulou Yang
/
mbed_max2719
maze game
Fork of mbed_max2719 by
main.cpp@1:ac65e35a9196, 2016-04-11 (annotated)
- Committer:
- yangyulounk
- Date:
- Mon Apr 11 17:41:17 2016 +0000
- Revision:
- 1:ac65e35a9196
- Parent:
- 0:6be425a138b2
maze game
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
alexwynn41 | 0:6be425a138b2 | 1 | /* 06_spi_max7219_led8x8 |
alexwynn41 | 0:6be425a138b2 | 2 | * |
alexwynn41 | 0:6be425a138b2 | 3 | * Simple demo to drive a 8x8-as LED matrix by a MAX7219 LED driver IC |
alexwynn41 | 0:6be425a138b2 | 4 | * After initialisation two characters (H and W) are displayed alternatively. |
alexwynn41 | 0:6be425a138b2 | 5 | * The MAX7219 IC is driven by hardware SPI: SPI0 module at PTD1, PTD2, PTD3. |
alexwynn41 | 0:6be425a138b2 | 6 | */ |
yangyulounk | 1:ac65e35a9196 | 7 | |
alexwynn41 | 0:6be425a138b2 | 8 | #include "mbed.h" |
alexwynn41 | 0:6be425a138b2 | 9 | #include "MMA8451Q.h" |
alexwynn41 | 0:6be425a138b2 | 10 | |
alexwynn41 | 0:6be425a138b2 | 11 | PinName const SDA = PTE25; |
alexwynn41 | 0:6be425a138b2 | 12 | PinName const SCL = PTE24; |
yangyulounk | 1:ac65e35a9196 | 13 | |
alexwynn41 | 0:6be425a138b2 | 14 | SPI spi(PTD2, PTD3, PTD1); // Arduino compatible MOSI, MISO, SCLK |
alexwynn41 | 0:6be425a138b2 | 15 | DigitalOut cs(PTD0); // Chip select |
yangyulounk | 1:ac65e35a9196 | 16 | int point []= {0,2}; |
alexwynn41 | 0:6be425a138b2 | 17 | |
alexwynn41 | 0:6be425a138b2 | 18 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
yangyulounk | 1:ac65e35a9196 | 19 | |
alexwynn41 | 0:6be425a138b2 | 20 | const unsigned char led2[]= { |
yangyulounk | 1:ac65e35a9196 | 21 | 0xFF,0xB4,0x15,0xD1,0x85,0xB5,0x95,0xFF};//maze wall |
yangyulounk | 1:ac65e35a9196 | 22 | |
yangyulounk | 1:ac65e35a9196 | 23 | //0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//no maze wall |
yangyulounk | 1:ac65e35a9196 | 24 | unsigned char led1[]= { |
yangyulounk | 1:ac65e35a9196 | 25 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 |
yangyulounk | 1:ac65e35a9196 | 26 | }; // |
yangyulounk | 1:ac65e35a9196 | 27 | |
alexwynn41 | 0:6be425a138b2 | 28 | /// Send two bytes to SPI bus |
alexwynn41 | 0:6be425a138b2 | 29 | void SPI_Write2(unsigned char MSB, unsigned char LSB) |
alexwynn41 | 0:6be425a138b2 | 30 | { |
alexwynn41 | 0:6be425a138b2 | 31 | cs = 0; // Set CS Low |
alexwynn41 | 0:6be425a138b2 | 32 | spi.write(MSB); // Send two bytes |
alexwynn41 | 0:6be425a138b2 | 33 | spi.write(LSB); |
alexwynn41 | 0:6be425a138b2 | 34 | cs = 1; // Set CS High |
alexwynn41 | 0:6be425a138b2 | 35 | } |
alexwynn41 | 0:6be425a138b2 | 36 | |
yangyulounk | 1:ac65e35a9196 | 37 | bool isNotWall(int x,int y) |
yangyulounk | 1:ac65e35a9196 | 38 | { |
yangyulounk | 1:ac65e35a9196 | 39 | bool result=false; |
yangyulounk | 1:ac65e35a9196 | 40 | if(x>=0&&x<8&&y>=0&&y<8) { |
yangyulounk | 1:ac65e35a9196 | 41 | int wallline =led2[y]; |
yangyulounk | 1:ac65e35a9196 | 42 | int pointline=(unsigned char)(1<<(7-x)); |
yangyulounk | 1:ac65e35a9196 | 43 | printf("%d %d\n",wallline,pointline); |
yangyulounk | 1:ac65e35a9196 | 44 | |
yangyulounk | 1:ac65e35a9196 | 45 | if((wallline|pointline)!=wallline) { |
yangyulounk | 1:ac65e35a9196 | 46 | result=true; |
yangyulounk | 1:ac65e35a9196 | 47 | |
yangyulounk | 1:ac65e35a9196 | 48 | } |
yangyulounk | 1:ac65e35a9196 | 49 | |
yangyulounk | 1:ac65e35a9196 | 50 | /* |
yangyulounk | 1:ac65e35a9196 | 51 | 101 |
yangyulounk | 1:ac65e35a9196 | 52 | 100 |
yangyulounk | 1:ac65e35a9196 | 53 | |
yangyulounk | 1:ac65e35a9196 | 54 | |101 |
yangyulounk | 1:ac65e35a9196 | 55 | &100 |
yangyulounk | 1:ac65e35a9196 | 56 | |
yangyulounk | 1:ac65e35a9196 | 57 | 101 |
yangyulounk | 1:ac65e35a9196 | 58 | 010 |
yangyulounk | 1:ac65e35a9196 | 59 | |
yangyulounk | 1:ac65e35a9196 | 60 | |111 |
yangyulounk | 1:ac65e35a9196 | 61 | &000 |
yangyulounk | 1:ac65e35a9196 | 62 | */ |
yangyulounk | 1:ac65e35a9196 | 63 | } |
yangyulounk | 1:ac65e35a9196 | 64 | return result; |
alexwynn41 | 0:6be425a138b2 | 65 | } |
yangyulounk | 1:ac65e35a9196 | 66 | void direction(float x, float y) |
yangyulounk | 1:ac65e35a9196 | 67 | { |
yangyulounk | 1:ac65e35a9196 | 68 | |
yangyulounk | 1:ac65e35a9196 | 69 | if(x > 0.5) { |
yangyulounk | 1:ac65e35a9196 | 70 | if(isNotWall(point[0]+1,point[1])) { |
yangyulounk | 1:ac65e35a9196 | 71 | point[0]++; |
yangyulounk | 1:ac65e35a9196 | 72 | } |
yangyulounk | 1:ac65e35a9196 | 73 | printf("Left %d %d\n",point[0],point[1]); |
yangyulounk | 1:ac65e35a9196 | 74 | } else if(x < -0.5) { |
yangyulounk | 1:ac65e35a9196 | 75 | if(isNotWall(point[0]-1,point[1])) { |
yangyulounk | 1:ac65e35a9196 | 76 | point[0]--; |
yangyulounk | 1:ac65e35a9196 | 77 | } |
yangyulounk | 1:ac65e35a9196 | 78 | printf("Right %d %d\n",point[0],point[1]); |
yangyulounk | 1:ac65e35a9196 | 79 | } else if(y > 0.5) { |
yangyulounk | 1:ac65e35a9196 | 80 | if(isNotWall(point[0],point[1]-1)) { |
yangyulounk | 1:ac65e35a9196 | 81 | point[1]--; |
yangyulounk | 1:ac65e35a9196 | 82 | } |
yangyulounk | 1:ac65e35a9196 | 83 | printf("Down %d %d\n",point[0],point[1]); |
yangyulounk | 1:ac65e35a9196 | 84 | } else if(y < -0.5) { |
yangyulounk | 1:ac65e35a9196 | 85 | if(isNotWall(point[0],point[1]+1)) { |
yangyulounk | 1:ac65e35a9196 | 86 | point[1]++; |
yangyulounk | 1:ac65e35a9196 | 87 | } |
yangyulounk | 1:ac65e35a9196 | 88 | printf("Up %d %d\n",point[0],point[1]); |
yangyulounk | 1:ac65e35a9196 | 89 | } else { |
yangyulounk | 1:ac65e35a9196 | 90 | printf("Neutral Position \n"); |
yangyulounk | 1:ac65e35a9196 | 91 | } |
yangyulounk | 1:ac65e35a9196 | 92 | } |
yangyulounk | 1:ac65e35a9196 | 93 | |
yangyulounk | 1:ac65e35a9196 | 94 | |
yangyulounk | 1:ac65e35a9196 | 95 | |
alexwynn41 | 0:6be425a138b2 | 96 | /// MAX7219 initialisation |
alexwynn41 | 0:6be425a138b2 | 97 | void Init_MAX7219(void) |
alexwynn41 | 0:6be425a138b2 | 98 | { |
alexwynn41 | 0:6be425a138b2 | 99 | SPI_Write2(0x09, 0x00); // Decoding off |
alexwynn41 | 0:6be425a138b2 | 100 | SPI_Write2(0x0A, 0x08); // Brightness to intermediate |
alexwynn41 | 0:6be425a138b2 | 101 | SPI_Write2(0x0B, 0x07); // Scan limit = 7 |
alexwynn41 | 0:6be425a138b2 | 102 | SPI_Write2(0x0C, 0x01); // Normal operation mode |
alexwynn41 | 0:6be425a138b2 | 103 | SPI_Write2(0x0F, 0x0F); // Enable display test |
alexwynn41 | 0:6be425a138b2 | 104 | wait_ms(500); // 500 ms delay |
alexwynn41 | 0:6be425a138b2 | 105 | SPI_Write2(0x01, 0x00); // Clear row 0. |
alexwynn41 | 0:6be425a138b2 | 106 | SPI_Write2(0x02, 0x00); // Clear row 1. |
alexwynn41 | 0:6be425a138b2 | 107 | SPI_Write2(0x03, 0x00); // Clear row 2. |
alexwynn41 | 0:6be425a138b2 | 108 | SPI_Write2(0x04, 0x00); // Clear row 3. |
alexwynn41 | 0:6be425a138b2 | 109 | SPI_Write2(0x05, 0x00); // Clear row 4. |
alexwynn41 | 0:6be425a138b2 | 110 | SPI_Write2(0x06, 0x00); // Clear row 5. |
alexwynn41 | 0:6be425a138b2 | 111 | SPI_Write2(0x07, 0x00); // Clear row 6. |
alexwynn41 | 0:6be425a138b2 | 112 | SPI_Write2(0x08, 0x00); // Clear row 7. |
alexwynn41 | 0:6be425a138b2 | 113 | SPI_Write2(0x0F, 0x00); // Disable display test |
alexwynn41 | 0:6be425a138b2 | 114 | wait_ms(500); // 500 ms delay |
alexwynn41 | 0:6be425a138b2 | 115 | } |
yangyulounk | 1:ac65e35a9196 | 116 | |
yangyulounk | 1:ac65e35a9196 | 117 | |
yangyulounk | 1:ac65e35a9196 | 118 | void clearMap(unsigned char *map ) |
yangyulounk | 1:ac65e35a9196 | 119 | { |
yangyulounk | 1:ac65e35a9196 | 120 | for (int i=0; i<8; i++) { |
yangyulounk | 1:ac65e35a9196 | 121 | map[i]=0x00; |
yangyulounk | 1:ac65e35a9196 | 122 | } |
yangyulounk | 1:ac65e35a9196 | 123 | } |
yangyulounk | 1:ac65e35a9196 | 124 | |
yangyulounk | 1:ac65e35a9196 | 125 | void updateMap( ) |
yangyulounk | 1:ac65e35a9196 | 126 | { |
yangyulounk | 1:ac65e35a9196 | 127 | clearMap(led1); |
yangyulounk | 1:ac65e35a9196 | 128 | led1[point[1]]=(unsigned char)(1<<(7-point[0])); |
yangyulounk | 1:ac65e35a9196 | 129 | |
yangyulounk | 1:ac65e35a9196 | 130 | } |
yangyulounk | 1:ac65e35a9196 | 131 | |
yangyulounk | 1:ac65e35a9196 | 132 | |
alexwynn41 | 0:6be425a138b2 | 133 | int main() |
alexwynn41 | 0:6be425a138b2 | 134 | { |
yangyulounk | 1:ac65e35a9196 | 135 | |
alexwynn41 | 0:6be425a138b2 | 136 | cs = 1; // CS initially High |
alexwynn41 | 0:6be425a138b2 | 137 | spi.format(8,0); // 8-bit format, mode 0,0 |
alexwynn41 | 0:6be425a138b2 | 138 | spi.frequency(1000000); // SCLK = 1 MHz |
alexwynn41 | 0:6be425a138b2 | 139 | Init_MAX7219(); // Initialize the LED controller |
alexwynn41 | 0:6be425a138b2 | 140 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
yangyulounk | 1:ac65e35a9196 | 141 | |
yangyulounk | 1:ac65e35a9196 | 142 | |
yangyulounk | 1:ac65e35a9196 | 143 | |
alexwynn41 | 0:6be425a138b2 | 144 | while (1) { |
yangyulounk | 1:ac65e35a9196 | 145 | |
yangyulounk | 1:ac65e35a9196 | 146 | float x, y; |
alexwynn41 | 0:6be425a138b2 | 147 | x = acc.getAccX(); |
alexwynn41 | 0:6be425a138b2 | 148 | y = acc.getAccY(); |
yangyulounk | 1:ac65e35a9196 | 149 | |
alexwynn41 | 0:6be425a138b2 | 150 | direction(x,y); |
yangyulounk | 1:ac65e35a9196 | 151 | updateMap(); |
yangyulounk | 1:ac65e35a9196 | 152 | |
yangyulounk | 1:ac65e35a9196 | 153 | |
yangyulounk | 1:ac65e35a9196 | 154 | wait(0.5); |
alexwynn41 | 0:6be425a138b2 | 155 | |
alexwynn41 | 0:6be425a138b2 | 156 | for(int i=1; i<9; i++) // Write first character (8 rows) |
yangyulounk | 1:ac65e35a9196 | 157 | SPI_Write2(i,led1[i-1]|led2[i-1]); |
yangyulounk | 1:ac65e35a9196 | 158 | wait(0.5); |
yangyulounk | 1:ac65e35a9196 | 159 | for(int i=1; i<9; i++) // Write first character (8 rows) |
alexwynn41 | 0:6be425a138b2 | 160 | SPI_Write2(i,led2[i-1]); |
alexwynn41 | 0:6be425a138b2 | 161 | } |
yangyulounk | 1:ac65e35a9196 | 162 | |
yangyulounk | 1:ac65e35a9196 | 163 | |
alexwynn41 | 0:6be425a138b2 | 164 | } |