Snake game for a 5x5 LED matrix
Embed:
(wiki syntax)
Show/hide line numbers
ledCube.cpp
00001 #include "mbed.h" 00002 #include "ledCube.h" 00003 #include "ledValues.h" 00004 #include "MCP23S17.h" 00005 00006 // Set up the ledArray output pins 00007 ledCube::ledCube() 00008 { 00009 spi = new SPI(p11, p12, p13); 00010 char Opcode = 0x40; // Used to start the spi communication with the DIP 00011 chip = new MCP23S17(*spi, p20, Opcode); 00012 00013 ledArray[0][0] = new DigitalOut(P000); 00014 ledArray[0][1] = new DigitalOut(P001); 00015 ledArray[0][2] = new DigitalOut(P002); 00016 ledArray[0][3] = new DigitalOut(P003); 00017 ledArray[0][4] = new DigitalOut(P004); 00018 ledArray[1][0] = new DigitalOut(P010); 00019 ledArray[1][1] = new DigitalOut(P011); 00020 ledArray[1][2] = new DigitalOut(P012); 00021 ledArray[1][3] = NULL; 00022 ledArray[1][4] = NULL; 00023 ledArray[2][0] = NULL; 00024 ledArray[2][1] = NULL; 00025 ledArray[2][2] = NULL; 00026 ledArray[2][3] = NULL; 00027 ledArray[2][4] = NULL; 00028 ledArray[3][0] = NULL; 00029 ledArray[3][1] = new DigitalOut(P031); 00030 ledArray[3][2] = new DigitalOut(P032); 00031 ledArray[3][3] = new DigitalOut(P033); 00032 ledArray[3][4] = new DigitalOut(P034); 00033 ledArray[4][0] = new DigitalOut(P040); 00034 ledArray[4][1] = new DigitalOut(P041); 00035 ledArray[4][2] = new DigitalOut(P042); 00036 ledArray[4][3] = new DigitalOut(P043); 00037 ledArray[4][4] = new DigitalOut(P044); 00038 spiCurrentlyLit = 0; 00039 // Set all 8 Port A bits to output direction 00040 (*chip).direction(PORT_A, 0x00); 00041 // Set all 8 Port B bits to output direction 00042 (*chip).direction(PORT_B, 0x00); 00043 lastLedLit = NULL; 00044 (*chip).write(PORT_A, spiCurrentlyLit); 00045 } 00046 // Light the LED at the specified row, column, layer coordinate 00047 void ledCube::turnOnLed(char row, char column, char layer) 00048 { 00049 if(ledArray[row][column] != NULL){ 00050 *ledArray[row][column] = 1; 00051 } 00052 // This else handles cases where the LEDs are handled by the I/O extender 00053 else{ 00054 //printf("before on-spiCurrentlyLit: %d turn on Row: %d Col: %d \n", spiCurrentlyLit, row, column); 00055 if( row == 1 && column == 3){ 00056 spiCurrentlyLit |= 0x01; 00057 } 00058 else if(row == 1 && column == 4){ 00059 spiCurrentlyLit |= 0x02; 00060 } 00061 else if(row == 2 && column == 0){ 00062 spiCurrentlyLit |= 0x04; 00063 } 00064 else if(row == 2 && column == 1){ 00065 spiCurrentlyLit |= 0x08; 00066 } 00067 else if(row == 2 && column == 2){ 00068 spiCurrentlyLit |= 0x10; 00069 } 00070 else if(row == 2 && column == 3){ 00071 spiCurrentlyLit |= 0x20; 00072 } 00073 else if(row == 2 && column == 4){ 00074 spiCurrentlyLit |= 0x40; 00075 } 00076 else if(row == 3 && column == 0){ 00077 spiCurrentlyLit |= 0x80; 00078 } 00079 (*chip).write(PORT_A, spiCurrentlyLit); 00080 } 00081 } 00082 00083 // Turns off the LED at the specified row and column 00084 void ledCube::turnOffLed(char row, char column, char layer) 00085 { 00086 if(ledArray[row][column] != NULL){ 00087 *ledArray[row][column] = 0; 00088 } 00089 // This else handles cases where the LEDs are handled by the I/O extender 00090 else{ 00091 //printf("before: off-spiCurrentlyLit: %d turn off Row: %d Col: %d \n", spiCurrentlyLit, row, column); 00092 if( row == 1 && column == 3){ 00093 spiCurrentlyLit &= ~(0x01); 00094 } 00095 else if(row == 1 && column == 4){ 00096 spiCurrentlyLit &= ~(0x02); 00097 } 00098 else if(row == 2 && column == 0){ 00099 spiCurrentlyLit &= ~(0x04); 00100 } 00101 else if(row == 2 && column == 1){ 00102 spiCurrentlyLit &= ~(0x08); 00103 } 00104 else if(row == 2 && column == 2){ 00105 spiCurrentlyLit &= ~(0x10); 00106 } 00107 else if(row == 2 && column == 3){ 00108 spiCurrentlyLit &= ~(0x20); 00109 } 00110 else if(row == 2 && column == 4){ 00111 spiCurrentlyLit &= ~(0x40); 00112 } 00113 else if(row == 3 && column == 0){ 00114 spiCurrentlyLit &= ~(0x80); 00115 } 00116 (*chip).write(PORT_A, spiCurrentlyLit); 00117 } 00118 } 00119 00120 // Blinks all the LEDs to indicate losing 00121 void ledCube::blink() 00122 { 00123 for(char i = 0; i < 5; i++) { 00124 for(char j = 0; j < 5; j++) { 00125 turnOnLed(i,j,0); 00126 } 00127 } 00128 wait(.3); 00129 for(char i = 0; i < 5; i++) { 00130 for(char j = 0; j < 5; j++) { 00131 turnOffLed(i,j,0); 00132 } 00133 } 00134 wait(.3); 00135 }
Generated on Wed Jul 13 2022 02:48:01 by 1.7.2