//------------------------------------------------------------------------------ // main ------------------------------------------------------------------------ void main(void) { RBX430_init(_16MHZ); // init board ERROR2(lcd_init()); // init LCD // configure Watchdog WDTCTL = WDT_CTL; // Set Watchdog interval WDT_Sec_Cnt = WDT_1SEC_CNT; // set WD 1 second counter IE1 |= WDTIE; // enable WDT interrupt lcd_clear(); // clear LCD lcd_backlight(ON); // turn on LCD backlight lcd_rectangle(0, 0, NUM_COLS*2, NUM_ROWS*2, 1); // draw border __bis_SR_register(GIE); // enable interrupts // output splash screen & wait for switch lcd_wordImage(life_image, (159-126)/2, 50, 1); lcd_mode(LCD_PROPORTIONAL | LCD_2X_FONT); lcd_cursor(10, 20); printf("Press Any Key"); lcd_mode(0); while (1) if ((P1IN & 0x0f) ^ 0x0f) break; while (1) { uint16 row, col; // for each live row (78 down to 1) for (row = NUM_ROWS-2; row; --row) { // for each live column (78 down to 1) for (col = NUM_COLS-2; col; --col) { cell_death(row, col); } } char str[] = "x = 2, y = 2, rule = B3/S23\n2o$2o!"; char* ptr; int x_number = 0; int y_number = 0; const uint8 pttrn; for (ptr = str; *ptr; ptr++); { if (*ptr != 'x') continue; if ((*ptr < '0') && (*ptr > '9')) continue; while ((*ptr >= '0') && (*ptr <= '9')) { x_number = x_number * 10 + (*ptr++ - '0'); } if (*ptr != 'y') continue; if ((*ptr < '0') && (*ptr > '9')) continue; while ((*ptr >= '0') && (*ptr <= '9')) { y_number = y_number * 10 + (*ptr++ - '0'); } if (*ptr != 'n') continue; } } while (1) // new pattern seed { // load initial seed patterns uint16 generation = 0; // generation counter uint8 pen = BIRTH; // temp variable WDT_Sec_Cnt = WDT_1SEC_CNT; // set WD 1 second counter seconds = 0; // clear second counter while (1) { uint16 row, col; // for each live row (78 down to 1) for (row = NUM_ROWS-2; row; --row) { // for each live column (78 down to 1) for (col = NUM_COLS-2; col; --col) { if (pen == BIRTH) { cell_birth(row, col); } else { cell_death(row, col); } } } lcd_wordImage(life_image, (159-126)/2, 50, (BIRTH == pen) ? 3 : 1); lcd_cursor(4, 1); // output life generation printf("%d/%d", ++generation, seconds); if (seconds == 100) while (1); pen = (pen == BIRTH) ? DEATH : BIRTH; } } } // end main()
//------------------------------------------------------------------------------ // main ------------------------------------------------------------------------ void main(void) { RBX430_init(_16MHZ); // init board ERROR2(lcd_init()); // init LCD //lcd_volume(376); // increase LCD brightness watchdog_init(); // init watchdog port1_init(); // init P1.0-3 switches __bis_SR_register(GIE); // enable interrupts lcd_clear(); memset(life, 0, sizeof(life)); // clear life array lcd_backlight(ON); lcd_wordImage(life_image, (HD_X_MAX - 126) / 2, 50, 1); lcd_cursor(10, 20); printf("\b\tPress Any Key"); switches = 0; // clear switches flag life_pr = life_prev; life_cr = life_cur; life_nr = life_nex; while (!switches); // wait for any switch while (1) // new pattern seed { uint16 generation; // generation counter uint16 row, col; WDT_Sec_Cnt = WDT_1SEC_CNT; // reset WD 1 second counter seconds = 0; // clear second counter generation = 0; // start generation counter int loop; int neighbors; int left; int middle; int current; int right; loop = 1; memset(life, 0, sizeof(life)); // clear life array memset(life_pr, 0, 10 * sizeof(uint8)); // clear slider memset(life_cr, 0, 10 * sizeof(uint8)); // clear slider memset(life_nr, 0, 10 * sizeof(uint8)); // clear slider init_life(switches); // load seed based on switch switches = 0; // reset switches while (loop) // next generation { RED_TOGGLE; memcpy(life_pr, life[79], 10 * sizeof(uint8)); memcpy(life_cr, life[78], 10 * sizeof(uint8)); memcpy(life_nr, life[77], 10 * sizeof(uint8)); // for each life row (78 down to 1) for (row = NUM_ROWS-2; row > 0; row--) { left = 0; neighbors = 0; current = TEST_CELL(life_cr, 1); right = TEST_CELL(life_pr, 2) + TEST_CELL(life_cr, 2) + TEST_CELL(life_nr, 2); middle = TEST_CELL(life_pr, 1) + current + TEST_CELL(life_nr, 1); // for each life column (78 down to 1) for (col = 1; col < 79; col++) { neighbors = left + (middle - current) + right; //neighbors += (TEST_CELL(life_pr, (col - 1)) + TEST_CELL(life_pr, col) + TEST_CELL(life_pr, (col + 1))); // add number of neighbors on row above //neighbors += (TEST_CELL(life_cr, (col - 1)) + TEST_CELL(life_cr, (col + 1))); // add number of neighbors on current row //neighbors += (TEST_CELL(life_nr, (col - 1)) + TEST_CELL(life_nr, col) + TEST_CELL(life_nr, (col + 1))); // add number of neighbors on row below if(current == 1) // if the cell is currently alive { if(neighbors == 2 || neighbors == 3) // the cell has 2 or 3 neighbors { // do nothing, the cell remains alive } else // the cell doesn't have necessary neighbors { CELL_DEATH(row, col); // clear cell bit in life array CELL_DELETE(row, col); // clear LCD 2x2 pixel point } } else // the cell is currently dead { if(neighbors == 3) // the cell has 3 live neighbors { CELL_BIRTH(row, col); // set cell bit in life array CELL_DRAW(row, col); // set LCD 2x2 pixel point } } neighbors = 0; // reset neighbors left = middle; current = TEST_CELL(life_cr, col + 1); middle = right; right = TEST_CELL(life_pr, col + 2) + TEST_CELL(life_cr, col + 2) + TEST_CELL(life_nr, col + 2); } temp = life_pr; life_pr = life_cr; life_cr = life_nr; life_nr = temp; //memcpy(life_pr, life_cr, 10 * sizeof(uint8)); // sets next row //memcpy(life_cr, life_nr, 10 * sizeof(uint8)); // sets next row memcpy(life_nr, life[row - 2], 10 * sizeof(uint8)); // sets next row } // display life generation and generations/second on LCD if (display_results(++generation)) break; if(switches) { loop = 0; // when a switch is pressed, exit the while loop } } } } // end main()