Game of life.
Dependencies: N5110 NOKIA_5110 mbed
main.cpp@0:978b9f226721, 2016-05-06 (annotated)
- Committer:
- bonnyngangu
- Date:
- Fri May 06 17:15:30 2016 +0000
- Revision:
- 0:978b9f226721
Implementing random regeneration cell on N5110.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bonnyngangu | 0:978b9f226721 | 1 | // PROGRAM'S LIBRARIES |
bonnyngangu | 0:978b9f226721 | 2 | #include "mbed.h" |
bonnyngangu | 0:978b9f226721 | 3 | #include "N5110.h" |
bonnyngangu | 0:978b9f226721 | 4 | #define PI 3.14159265359 |
bonnyngangu | 0:978b9f226721 | 5 | #define CMD_DC_INVERT_VIDEO 0x0D |
bonnyngangu | 0:978b9f226721 | 6 | |
bonnyngangu | 0:978b9f226721 | 7 | // VARIABLES DEFINED |
bonnyngangu | 0:978b9f226721 | 8 | #define WIDTH 84 |
bonnyngangu | 0:978b9f226721 | 9 | #define HEIGH 48 |
bonnyngangu | 0:978b9f226721 | 10 | #define BANKS 6 |
bonnyngangu | 0:978b9f226721 | 11 | |
bonnyngangu | 0:978b9f226721 | 12 | // mbed API OBJECTS |
bonnyngangu | 0:978b9f226721 | 13 | AnalogIn pot(p17); // DAC on pin 18 for analogue input to control the timer |
bonnyngangu | 0:978b9f226721 | 14 | DigitalIn button(p5); // SPI on pin 5 for digital input to control the radom distribution |
bonnyngangu | 0:978b9f226721 | 15 | DigitalIn stop(p6); // SPI on pin 6 |
bonnyngangu | 0:978b9f226721 | 16 | |
bonnyngangu | 0:978b9f226721 | 17 | // VCC,SCE,RST,D/C,MOSI,SCLK,LED |
bonnyngangu | 0:978b9f226721 | 18 | N5110 lcd(p7,p8,p9,p10,p11,p13,p21); // N5110 lcd pin connection |
bonnyngangu | 0:978b9f226721 | 19 | // VCC powered directly from VOUT (3.3 V) - |
bonnyngangu | 0:978b9f226721 | 20 | // Can give better performance due to current limitation from GPIO pin |
bonnyngangu | 0:978b9f226721 | 21 | BusOut myleds(LED4, LED3, LED2, LED1);// the LEDs are flashing in opposite direction - used as speed control |
bonnyngangu | 0:978b9f226721 | 22 | |
bonnyngangu | 0:978b9f226721 | 23 | // GLOBAL VARIABLES |
bonnyngangu | 0:978b9f226721 | 24 | bool Cells[84][48]; // array to hold samples |
bonnyngangu | 0:978b9f226721 | 25 | int checkNeigbour (int i, int j); // to check cell within the sample |
bonnyngangu | 0:978b9f226721 | 26 | |
bonnyngangu | 0:978b9f226721 | 27 | // FUNCTION PROTOTYPES |
bonnyngangu | 0:978b9f226721 | 28 | void init(); |
bonnyngangu | 0:978b9f226721 | 29 | void setPixel(); |
bonnyngangu | 0:978b9f226721 | 30 | void displayNext(); |
bonnyngangu | 0:978b9f226721 | 31 | void clearBuffer(); |
bonnyngangu | 0:978b9f226721 | 32 | void gameSetUp(); |
bonnyngangu | 0:978b9f226721 | 33 | void checkBoard(); |
bonnyngangu | 0:978b9f226721 | 34 | void countkNeigbour (int i, int j);// to count surrounding cells |
bonnyngangu | 0:978b9f226721 | 35 | void glider(); |
bonnyngangu | 0:978b9f226721 | 36 | void turnOff(); |
bonnyngangu | 0:978b9f226721 | 37 | |
bonnyngangu | 0:978b9f226721 | 38 | // START OF MAIN() |
bonnyngangu | 0:978b9f226721 | 39 | int main() |
bonnyngangu | 0:978b9f226721 | 40 | { |
bonnyngangu | 0:978b9f226721 | 41 | // INITIALISATION CODE |
bonnyngangu | 0:978b9f226721 | 42 | lcd.init(); |
bonnyngangu | 0:978b9f226721 | 43 | // drawing rectangles frame |
bonnyngangu | 0:978b9f226721 | 44 | // origin x,y,width,height,type |
bonnyngangu | 0:978b9f226721 | 45 | lcd.drawRect(0,0,83,47,0); // transparent, just outline rectangle |
bonnyngangu | 0:978b9f226721 | 46 | lcd.refresh(); |
bonnyngangu | 0:978b9f226721 | 47 | // printing strings at specified co-ordinates |
bonnyngangu | 0:978b9f226721 | 48 | lcd.printString("Hello, World!",6,3); |
bonnyngangu | 0:978b9f226721 | 49 | // refreshing display after setting pixels |
bonnyngangu | 0:978b9f226721 | 50 | lcd.refresh(); |
bonnyngangu | 0:978b9f226721 | 51 | wait(1); |
bonnyngangu | 0:978b9f226721 | 52 | void clearCells(); |
bonnyngangu | 0:978b9f226721 | 53 | // loop throught cells, and clear |
bonnyngangu | 0:978b9f226721 | 54 | for (int i = 1; i < 83 ; i++) { |
bonnyngangu | 0:978b9f226721 | 55 | for (int j = 1; j < 47 ; j++) { |
bonnyngangu | 0:978b9f226721 | 56 | lcd.clearPixel(i,j); |
bonnyngangu | 0:978b9f226721 | 57 | } |
bonnyngangu | 0:978b9f226721 | 58 | } |
bonnyngangu | 0:978b9f226721 | 59 | lcd.refresh(); // refreshing lcd to write buffer to display |
bonnyngangu | 0:978b9f226721 | 60 | |
bonnyngangu | 0:978b9f226721 | 61 | lcd.printString("Welcome to",15,1); // splash screen |
bonnyngangu | 0:978b9f226721 | 62 | lcd.printString("Conway's",20,2); |
bonnyngangu | 0:978b9f226721 | 63 | lcd.printString("Game of Life",6,3); |
bonnyngangu | 0:978b9f226721 | 64 | lcd.printString("Bonny Ngangu",6,4); |
bonnyngangu | 0:978b9f226721 | 65 | // drawing a line across the display at y = 40 pixels (6 pixel offser from left, and toward right) |
bonnyngangu | 0:978b9f226721 | 66 | for (int i = 6; i < 78; i++) { |
bonnyngangu | 0:978b9f226721 | 67 | lcd.setPixel(i,40); |
bonnyngangu | 0:978b9f226721 | 68 | } |
bonnyngangu | 0:978b9f226721 | 69 | lcd.refresh(); // refreshing LCD after setting pixels |
bonnyngangu | 0:978b9f226721 | 70 | wait(3); |
bonnyngangu | 0:978b9f226721 | 71 | void clearCells(); |
bonnyngangu | 0:978b9f226721 | 72 | // loop throught cells, and clear |
bonnyngangu | 0:978b9f226721 | 73 | for (int i = 1; i < 83 ; i++) { |
bonnyngangu | 0:978b9f226721 | 74 | for (int j = 1; j < 47 ; j++) { |
bonnyngangu | 0:978b9f226721 | 75 | lcd.clearPixel(i,j); |
bonnyngangu | 0:978b9f226721 | 76 | } |
bonnyngangu | 0:978b9f226721 | 77 | } |
bonnyngangu | 0:978b9f226721 | 78 | lcd.refresh(); // refreshing to write buffer to display |
bonnyngangu | 0:978b9f226721 | 79 | // printing strings at specified co-ordinates |
bonnyngangu | 0:978b9f226721 | 80 | lcd.printString("Two!",20,2); |
bonnyngangu | 0:978b9f226721 | 81 | // refreshing display after setting pixels |
bonnyngangu | 0:978b9f226721 | 82 | lcd.refresh(); |
bonnyngangu | 0:978b9f226721 | 83 | wait(1); |
bonnyngangu | 0:978b9f226721 | 84 | void clearCells(); |
bonnyngangu | 0:978b9f226721 | 85 | // loop throught cells, and clear |
bonnyngangu | 0:978b9f226721 | 86 | for (int i = 1; i < 83 ; i++) { |
bonnyngangu | 0:978b9f226721 | 87 | for (int j = 1; j < 47 ; j++) { |
bonnyngangu | 0:978b9f226721 | 88 | lcd.clearPixel(i,j); |
bonnyngangu | 0:978b9f226721 | 89 | } |
bonnyngangu | 0:978b9f226721 | 90 | } |
bonnyngangu | 0:978b9f226721 | 91 | lcd.refresh(); // refreshing to write buffer to display |
bonnyngangu | 0:978b9f226721 | 92 | |
bonnyngangu | 0:978b9f226721 | 93 | // printing strings at specified co-ordinates |
bonnyngangu | 0:978b9f226721 | 94 | lcd.printString("One!",20,2); |
bonnyngangu | 0:978b9f226721 | 95 | // need to refresh display after setting pixels |
bonnyngangu | 0:978b9f226721 | 96 | lcd.refresh(); |
bonnyngangu | 0:978b9f226721 | 97 | wait(1); |
bonnyngangu | 0:978b9f226721 | 98 | void clearCells(); |
bonnyngangu | 0:978b9f226721 | 99 | // loop throught cells, and clear |
bonnyngangu | 0:978b9f226721 | 100 | for (int i = 1; i < 83 ; i++) { |
bonnyngangu | 0:978b9f226721 | 101 | for (int j = 1; j < 47 ; j++) { |
bonnyngangu | 0:978b9f226721 | 102 | lcd.clearPixel(i,j); |
bonnyngangu | 0:978b9f226721 | 103 | } |
bonnyngangu | 0:978b9f226721 | 104 | } |
bonnyngangu | 0:978b9f226721 | 105 | lcd.refresh(); // refreshing to write buffer to display |
bonnyngangu | 0:978b9f226721 | 106 | |
bonnyngangu | 0:978b9f226721 | 107 | // printing strings at specified co-ordinates |
bonnyngangu | 0:978b9f226721 | 108 | lcd.printString("Have Fun!",20,2); |
bonnyngangu | 0:978b9f226721 | 109 | // need to refresh display after setting pixels |
bonnyngangu | 0:978b9f226721 | 110 | lcd.refresh(); |
bonnyngangu | 0:978b9f226721 | 111 | wait(1); |
bonnyngangu | 0:978b9f226721 | 112 | void clearCells(); |
bonnyngangu | 0:978b9f226721 | 113 | // loop throught cells, and clear |
bonnyngangu | 0:978b9f226721 | 114 | for (int i = 1; i < 83 ; i++) { |
bonnyngangu | 0:978b9f226721 | 115 | for (int j = 1; j < 47 ; j++) { |
bonnyngangu | 0:978b9f226721 | 116 | lcd.clearPixel(i,j); |
bonnyngangu | 0:978b9f226721 | 117 | } |
bonnyngangu | 0:978b9f226721 | 118 | } |
bonnyngangu | 0:978b9f226721 | 119 | lcd.refresh(); // refreshing to write buffer to display |
bonnyngangu | 0:978b9f226721 | 120 | |
bonnyngangu | 0:978b9f226721 | 121 | // TESTING USING GLIDER AND STRUCTRES |
bonnyngangu | 0:978b9f226721 | 122 | glider(); // glicer testing |
bonnyngangu | 0:978b9f226721 | 123 | lcd.refresh(); |
bonnyngangu | 0:978b9f226721 | 124 | wait(1); |
bonnyngangu | 0:978b9f226721 | 125 | |
bonnyngangu | 0:978b9f226721 | 126 | // INFINITE LOOP |
bonnyngangu | 0:978b9f226721 | 127 | while(1) { |
bonnyngangu | 0:978b9f226721 | 128 | displayNext(); |
bonnyngangu | 0:978b9f226721 | 129 | wait(pot); |
bonnyngangu | 0:978b9f226721 | 130 | for(int i=0; i<16; i++) { |
bonnyngangu | 0:978b9f226721 | 131 | myleds = i; |
bonnyngangu | 0:978b9f226721 | 132 | wait(pot); |
bonnyngangu | 0:978b9f226721 | 133 | } |
bonnyngangu | 0:978b9f226721 | 134 | } |
bonnyngangu | 0:978b9f226721 | 135 | } |
bonnyngangu | 0:978b9f226721 | 136 | |
bonnyngangu | 0:978b9f226721 | 137 | // SURROUNDING LIVING CELLS CHECK |
bonnyngangu | 0:978b9f226721 | 138 | int checkNeigbour (int i, int j) // number of neighbours |
bonnyngangu | 0:978b9f226721 | 139 | { |
bonnyngangu | 0:978b9f226721 | 140 | int n = 0; // number of neighbours |
bonnyngangu | 0:978b9f226721 | 141 | |
bonnyngangu | 0:978b9f226721 | 142 | if (lcd.getPixel(i-1,j-1)) // pixel to top-left |
bonnyngangu | 0:978b9f226721 | 143 | n++; |
bonnyngangu | 0:978b9f226721 | 144 | if (lcd.getPixel(i-1,j)) // pixel to left |
bonnyngangu | 0:978b9f226721 | 145 | n++; |
bonnyngangu | 0:978b9f226721 | 146 | if (lcd.getPixel(i-1,j+1)) // pixel to bottom-left |
bonnyngangu | 0:978b9f226721 | 147 | n++; |
bonnyngangu | 0:978b9f226721 | 148 | if (lcd.getPixel(i,j-1)) // pixel to top |
bonnyngangu | 0:978b9f226721 | 149 | n++; |
bonnyngangu | 0:978b9f226721 | 150 | if (lcd.getPixel(i,j+1)) // pixel to bottom |
bonnyngangu | 0:978b9f226721 | 151 | n++; |
bonnyngangu | 0:978b9f226721 | 152 | if (lcd.getPixel(i+1,j-1)) // pixel to top-right |
bonnyngangu | 0:978b9f226721 | 153 | n++; |
bonnyngangu | 0:978b9f226721 | 154 | if (lcd.getPixel(i+1,j)) // pixel to right |
bonnyngangu | 0:978b9f226721 | 155 | n++; |
bonnyngangu | 0:978b9f226721 | 156 | if (lcd.getPixel(i+1,j+1)) // pixel to bottom-right |
bonnyngangu | 0:978b9f226721 | 157 | n++; |
bonnyngangu | 0:978b9f226721 | 158 | return n; |
bonnyngangu | 0:978b9f226721 | 159 | } |
bonnyngangu | 0:978b9f226721 | 160 | |
bonnyngangu | 0:978b9f226721 | 161 | /* APPLING RULES: |
bonnyngangu | 0:978b9f226721 | 162 | 1. Any live cell with fewer than two live neighbours dies, due to under-population. |
bonnyngangu | 0:978b9f226721 | 163 | 2. Any live cell with two or three live neighbours lives on to the next generation. |
bonnyngangu | 0:978b9f226721 | 164 | 3. Any live cell with more than three live neighbours dies, due to over-population. |
bonnyngangu | 0:978b9f226721 | 165 | 4. Any dead cell with exactly three live neighbours becomes a live cell, due to reproduction.*/ |
bonnyngangu | 0:978b9f226721 | 166 | |
bonnyngangu | 0:978b9f226721 | 167 | void gameSetUp() // appliying rules of the game |
bonnyngangu | 0:978b9f226721 | 168 | { |
bonnyngangu | 0:978b9f226721 | 169 | for (int i = 0; i < 84 ; i++) { |
bonnyngangu | 0:978b9f226721 | 170 | for (int j = 0; j < 48 ; j++) { |
bonnyngangu | 0:978b9f226721 | 171 | int n = checkNeigbour(i,j); |
bonnyngangu | 0:978b9f226721 | 172 | |
bonnyngangu | 0:978b9f226721 | 173 | if (lcd.getPixel(i,j)) {// this means that if a pixel is already on |
bonnyngangu | 0:978b9f226721 | 174 | if (n==2) // cell with 2 neighbours live on in the next generation |
bonnyngangu | 0:978b9f226721 | 175 | Cells[i][j]=1; |
bonnyngangu | 0:978b9f226721 | 176 | else if (n==3) // cell with 3 neighbours live on in the next generation |
bonnyngangu | 0:978b9f226721 | 177 | Cells[i][j]=1; |
bonnyngangu | 0:978b9f226721 | 178 | else if (n>3) // cell with > 3 neighbours dies - overpopulation |
bonnyngangu | 0:978b9f226721 | 179 | Cells[i][j]=0; |
bonnyngangu | 0:978b9f226721 | 180 | else if (n<2) // cell with < 2 neighbours dies - underpopulation |
bonnyngangu | 0:978b9f226721 | 181 | Cells[i][j]=0; |
bonnyngangu | 0:978b9f226721 | 182 | } else { // if no pixel is set on yet |
bonnyngangu | 0:978b9f226721 | 183 | if (n==3) // cell with exactly 3 neighbours becomes alive - reproduction |
bonnyngangu | 0:978b9f226721 | 184 | Cells[i][j]=1; |
bonnyngangu | 0:978b9f226721 | 185 | } |
bonnyngangu | 0:978b9f226721 | 186 | } |
bonnyngangu | 0:978b9f226721 | 187 | } |
bonnyngangu | 0:978b9f226721 | 188 | lcd.refresh(); |
bonnyngangu | 0:978b9f226721 | 189 | } |
bonnyngangu | 0:978b9f226721 | 190 | |
bonnyngangu | 0:978b9f226721 | 191 | // NEW GENERATION CELLS |
bonnyngangu | 0:978b9f226721 | 192 | void displayNext() |
bonnyngangu | 0:978b9f226721 | 193 | { |
bonnyngangu | 0:978b9f226721 | 194 | gameSetUp(); |
bonnyngangu | 0:978b9f226721 | 195 | for (int i = 0; i < 84 ; i++) { |
bonnyngangu | 0:978b9f226721 | 196 | for (int j = 0; j < 48 ; j++) { |
bonnyngangu | 0:978b9f226721 | 197 | if (Cells[i][j]==1) |
bonnyngangu | 0:978b9f226721 | 198 | lcd.setPixel(i,j); |
bonnyngangu | 0:978b9f226721 | 199 | else |
bonnyngangu | 0:978b9f226721 | 200 | lcd.clearPixel(i,j); |
bonnyngangu | 0:978b9f226721 | 201 | } |
bonnyngangu | 0:978b9f226721 | 202 | } |
bonnyngangu | 0:978b9f226721 | 203 | |
bonnyngangu | 0:978b9f226721 | 204 | // RANDON CELLS DISTRIBUTION WHEN BUTTON A IS PRESSED AND HOLDED FOR 0.5 MS |
bonnyngangu | 0:978b9f226721 | 205 | if(button==1) { |
bonnyngangu | 0:978b9f226721 | 206 | lcd.randomiseBuffer(); |
bonnyngangu | 0:978b9f226721 | 207 | lcd.refresh(); |
bonnyngangu | 0:978b9f226721 | 208 | wait_ms(0.5); |
bonnyngangu | 0:978b9f226721 | 209 | } |
bonnyngangu | 0:978b9f226721 | 210 | |
bonnyngangu | 0:978b9f226721 | 211 | // TURNING OFF THE PROCESS |
bonnyngangu | 0:978b9f226721 | 212 | if(stop==1) { |
bonnyngangu | 0:978b9f226721 | 213 | lcd.turnOff(); |
bonnyngangu | 0:978b9f226721 | 214 | lcd.refresh(); |
bonnyngangu | 0:978b9f226721 | 215 | wait_ms(0.1); |
bonnyngangu | 0:978b9f226721 | 216 | } |
bonnyngangu | 0:978b9f226721 | 217 | } |
bonnyngangu | 0:978b9f226721 | 218 | void glider() // glider testing |
bonnyngangu | 0:978b9f226721 | 219 | { |
bonnyngangu | 0:978b9f226721 | 220 | lcd.setPixel(17,17); |
bonnyngangu | 0:978b9f226721 | 221 | wait(pot); |
bonnyngangu | 0:978b9f226721 | 222 | lcd.setPixel(17,18); |
bonnyngangu | 0:978b9f226721 | 223 | wait(pot); |
bonnyngangu | 0:978b9f226721 | 224 | lcd.setPixel(17,19); |
bonnyngangu | 0:978b9f226721 | 225 | wait(pot); |
bonnyngangu | 0:978b9f226721 | 226 | lcd.setPixel(16,19); |
bonnyngangu | 0:978b9f226721 | 227 | wait(pot); |
bonnyngangu | 0:978b9f226721 | 228 | lcd.setPixel(15,18); |
bonnyngangu | 0:978b9f226721 | 229 | wait(pot); |
bonnyngangu | 0:978b9f226721 | 230 | } |
bonnyngangu | 0:978b9f226721 | 231 | |
bonnyngangu | 0:978b9f226721 | 232 |