//main code int main(void) { //init stuff //init the ht1632c LED matrix driver chip ht1632c_init(); //init the ADC init_ADC(); //init srand() with a somewhat random number from ADC9's low bits init_srand(); //init button stuff for input and pullup //and setup INT0 for button if you set DO_YOU_WANT_BUTTON_INT0 init_button(); //init timer1 for use in triggering an interrupt //on overflow, which updates the display and calculates new generation. init_timer1(); //init the I/O for the 7 segment display control init_digit_pins(); init_segment_pins(); //reset the display with a "random" array using rand() reset_grid(); //test glider //fb[29] = 0b00100000; //fb[30] = 0b00101000; //fb[31] = 0b00110000; //variable to store generation_count for display on 7 segment displays uint16_t g_count=0; //enable global interrupts sei(); //infinite loop while(1){ //check if the update generation count flag has been set //by within the timer1 overflow interrupt. //if set put the updated value into g_count and reset the flag. if(update_gen_flag){ g_count = generation_count; update_gen_flag=0; } write_number(g_count); //write the g_count to 7 segment displays //write_number(generation_count); //if the seven_seg_error flag is set (output is over 999 for 3 digits) //then reset the grid if(seven_seg_error_flag){ reset_grid(); } } }
void screen_dirty_callback(Sprite *toolbox) { // new on 250105 reset_grid(); if (tt_screen != NULL) { tt_screen->screen_dirty(); tt_screen->remove_current_subtitle(); }; };
void init() { INIT.init(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0,GRIDW,0,GRIDH); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); reset_grid(&GRID_FRONT); reset_grid(&GRID_BACK); init_react(); init_diffuse(); randomize_field(); }
int main() { prepare(); reset_grid(); while (read_command()); return 0; }
void do_eop() { for (int i = 0; i < 60; ++i) puts(grid[i]); putchar('\n'); for (int i = 0; i < 60; ++i) putchar('-'); puts("\n"); reset_grid(); }
void step() { reset_grid(&GRID_BACK); // diffuse for (int i = 1; i < GRIDW-1; i++) { for (int j = 1; j < GRIDH-1; j++) { for (int k = 0; k < COEFS; k++) { GRID_BACK[i][j].value[k] += GRID_FRONT[i][j].value[k] * (1 - DT * DIFFUSE[k].sum); GRID_BACK[i+1][j].value[k] += DT * DIFFUSE[k].dir[0] * GRID_FRONT[i][j].value[k]; GRID_BACK[i-1][j].value[k] += DT * DIFFUSE[k].dir[1] * GRID_FRONT[i][j].value[k]; GRID_BACK[i][j+1].value[k] += DT * DIFFUSE[k].dir[2] * GRID_FRONT[i][j].value[k]; GRID_BACK[i][j-1].value[k] += DT * DIFFUSE[k].dir[3] * GRID_FRONT[i][j].value[k]; } } } for (int i = 0; i < GRIDW; i++) { for (int k = 0; k < COEFS; k++) { GRID_BACK[i][0].value[k] = 1; GRID_BACK[i][GRIDH-1].value[k] = 1; } } for (int j = 0; j < GRIDH; j++) { for (int k = 0; k < COEFS; k++) { GRID_BACK[0][j].value[k] = 1; GRID_BACK[GRIDW-1][j].value[k] = 1; } } for (int i = 1; i < GRIDW-1; i++) { for (int j = 1; j < GRIDH-1; j++) { int a = int(GRID_BACK[i][j].value[0] * GRAN + 0.5); GRANCLAMP(a); int b = int(GRID_BACK[i][j].value[1] * GRAN + 0.5); GRANCLAMP(b); int c = int(GRID_BACK[i][j].value[2] * GRAN + 0.5); GRANCLAMP(c); for (int k = 0; k < COEFS; k++) { GRID_FRONT[i][j].value[k] = REACT[a][b][c][k]; } } } }
//interactively read initial conditons from the user void setup_initial_conditons(world *w, WINDOW *win){ keypad(win, 1); int c,y,x,ymax,xmax; getyx(win, y ,x); getmaxyx(win, ymax, xmax); wmove(win, 0, 0); while(c = wgetch(win)){ switch(c){ case KEY_UP: // case KEY_SUP: y = y > 0 ? y-1 : ymax; break; case KEY_DOWN: // case KEY_SDOWN: y = y < ymax ? y+1 : 0; break; case KEY_LEFT: // case KEY_SLEFT: x = x > 0 ? x-1 : xmax; break; case KEY_RIGHT: // case KEY_SRIGHT: x = x < xmax ? x+1 : 0; break; case KEY_BACKSPACE: reset_grid(w); draw_world(w, win); x = y = 0; break; case '+': wait_time += 0.05; break; case '-': wait_time -= 0.05; break; case 'q': exit(0); case 'r': randomize_grid(w); draw_world(w, win); x = y = 0; break; case 's': step_world(w); draw_world(w, win); break; case '\r': case '\n': waddch(win, '#'); w->grid[y*w->cols + x] = 1; break; case ' ': return; } wmove(win, y ,x); // if(c == KEY_SUP || c == KEY_SDOWN | c == KEY_SLEFT || c = KEY_SRIGHT){ // waddch(win, '#'); // wmove(win, y ,x); // } } }
void get_new_states(void){ //find all the new states and put them in the buffer //copy the current stuff into storage int8_t x=X_AXIS_LEN; while(x--){ int8_t y=Y_AXIS_LEN; while(y--){ if(get_new_pixel_state(fb, x, y)==1){ state_storage[x] |= (1<<y); } else { state_storage[x] &= ~(1<<y); } } } //store the difference between the two generations in diff_val //to be used in finding when to reset. uint8_t diff_val= get_difference(state_storage,fb); if((diff_val <= 4)){ //if diff_val is a low difference then increment it's counter low_diff_count++; } else if((diff_val<=8)){ //if diff_val is a medium difference then increment that counter med_diff_count++; } else{ //if neither, then decrement their counters to stay longer before reset if(low_diff_count > 0){ low_diff_count--; } if(med_diff_count >0){ med_diff_count--; } } #if DO_YOU_WANT_A_GENERATION_RESET_BUTTON==1 #if DO_YOU_WANT_BUTTON_INT0==0 //if you don't want to use INT0 for button //then this "if" statement will compile //which just checks the button pin's state whenever //this function runs if(bit_is_clear(BUTTON_PIN, BUTTON_BIT)){ reset_grid(); } else #endif #endif if(low_diff_count > LOW_DIFF_THRESHOLD){ //if low_diff_count is above threshold, reset low_diff_count=0; reset_grid(); } else if(med_diff_count > MED_DIFF_THRESHOLD){ //if med_diff_count is above threshold, reset med_diff_count=0; reset_grid(); } else{ //if it is interesting enough so far then just add the new generation //to the framebuffer. for(x=0;x<X_AXIS_LEN;x++){ //put the new values into the framebuffer fb[x] = state_storage[x]; } } }