int main(void) { SystemInit(); GLCD_Init(); clock_init(); #ifndef BUSY_WAIT timer0_init(); #endif /* ifndef BUSY_WAIT */ while(1) { GLCD_Clear(White); GLCD_DisplayString(0, 0, 1, (unsigned char*) clock.text); // Delay by 1000 milliseconds #ifdef BUSY_WAIT delay_busy_wait(1000); clock_increment(); clock_print(); #else // incrementing clock done within interrupt handlers clock_print(); #endif /* ifdef BUSY_WAIT */ } }
/* this program illustrates how to use void* as a generic pointer, how to use pointer casts to get the generic pointer interpreted according to what type of value it points to, and the danger of making an incorrect cast */ int main (int argc, char ** argv) { struct tm now; /* standard struct for storing a time value */ double lightspeed; Person * albert; /* pointer to our custom struct person */ void * generic_pointer; /* let's read the clock, it gets stored in a standard struct tm (from the time.h header) */ if (0 != clock_read (&now)) { printf ("failed to read the clock\n"); return 1; } /* the speed of light as a double-precision floating point number */ lightspeed = 299792458.0; /* Albert Einstein was born on March 14, 1879. His age depends on today's date... */ albert = person_create ("Albert Einstein", (now.tm_mon >= 3 && now.tm_mday >= 14) ? now.tm_year + 1901 - 1879 : now.tm_year + 1900 - 1879); /* ************************************************** * NOW FOR THE FUN PART ***************************** * **************************************************/ /* the generic pointer can store the address of any of our variables */ generic_pointer = &now; printf ("the date and time is\n"); clock_print (&now); generic_pointer = &lightspeed; printf ("the speed of light is\n %g m/s\n", *(double*)generic_pointer); generic_pointer = albert; printf ("the person is\n"); person_print (generic_pointer); return 0; }
/////////////////////////////////////////////////////////////////////// // Main function // The code works interrupt based, so the main loop just goes over // the state variables to check if action is required. /////////////////////////////////////////////////////////////////////// void main() { char input; char uart_hour; char uart_min; char uart_sec; char uart_day; char report_clock_was_set = 0; char switch_updated; char new_incoming; char new_update; char settings_retrieved = 0; char update_eeprom = 0; char i; switch_point swpoint; // Some hardware init first init(); // Main loop while (1){ // Set output LED indicator output_led = !output; // Act depending on the UART state if (uart_state == STRING_RECEIVED){ uart_state = WAIT_FOR_DISCONNECT; if (rx_buffer[1] == '-' && rx_buffer[4] == ':') { // We have received a valid string with time information, parse it //uart_state = WAIT_FOR_DISCONNECT; // Get the info uart_day = rx_buffer[0] - 0x30; uart_hour = (rx_buffer[2] - 0x30) * 10 + (rx_buffer[3] - 0x30); uart_min = (rx_buffer[5] - 0x30) * 10 + (rx_buffer[6] - 0x30); uart_sec = (rx_buffer[8] - 0x30) * 10 + (rx_buffer[9] - 0x30); clock_set(uart_day, uart_hour, uart_min, uart_sec); report_clock_was_set = 1; check_timer_table = 1; } else if (rx_buffer[1] == '*') { update_eeprom = 1; } } // Report status to incoming connection // When a user telnets into the XPORT, current internal time and // switch points are reported. if (uart_state == INCOMING && new_incoming){ clock_print(); print_switch_list(); new_incoming = 0; } if (uart_state == IDLE) { new_incoming = 1; } // Update the switch status if there is need to, based on the current time // and the rules defined in the EEPROM if (check_timer_table){ check_timer_table = 0; switch_updated |= update_switch_state(clock_get_day(), clock_get_hours(), clock_get_minutes()); } // Check if we need to sync our internal clock to the web server. // This is done one minute after the odd hour mark when the UART is IDLE if ( (uart_state == IDLE) && (clock_get_minutes() == 0x01) && /*(clock_get_hours() & 0x01 == 0) &&*/ new_update) { new_update = 0; web_php_interface(REQUEST_TIME); } if (clock_get_minutes() == 0x00) { new_update = 1; // Set at minute 0 so that we only request time once after the one minute mark } // Report to the web that the clock was set if (uart_state == IDLE && report_clock_was_set) { report_clock_was_set = 0; web_php_interface(REPORT_CLOCK_SET); } // Report to the web that the switch state changed if (uart_state == IDLE && switch_updated) { switch_updated = 0; web_php_interface(REPORT_SWITCH_STATE); } // Request the new settings from the web server if (uart_state == IDLE && request_settings) { request_settings = 0; web_php_interface(REQUEST_SETTINGS); } // We can only update the EEPROM after we're in IDLE state because interrupts get disabled during // EEPROM write and we would otherwise miss UART RX interrupts. if (uart_state == IDLE && update_eeprom) { for (i=0; i<rx_buffer[0]; i++){ swpoint.hour = rx_buffer[2+(i*4)]; swpoint.minute = rx_buffer[3+(i*4)]; swpoint.mask = rx_buffer[4+(i*4)]; swpoint.action = rx_buffer[5+(i*4)]; swpoint.position = i; put_switch_point(swpoint); } eeprom_write(POINT_COUNT_ADDRESS, rx_buffer[0]); settings_retrieved = 1; update_eeprom = 0; } // Report that new settings were loaded if (uart_state == IDLE && settings_retrieved) { settings_retrieved = 0; web_php_interface(SETTINGS_RETRIEVED); } } }
static void ascii_showboard(void) { int i, j; char letterbar[64]; int last_pos_was_move; int pos_is_move; int dead; int last_move = get_last_move(); make_letterbar(board_size, letterbar); set_handicap_spots(board_size); printf("\n"); printf(" White (O) has captured %d stone%s\n", black_captured, black_captured == 1 ? "" : "s"); printf(" Black (X) has captured %d stone%s\n", white_captured, white_captured == 1 ? "" : "s"); if (showscore) { if (current_score_estimate == NO_SCORE) printf(" No score estimate is available yet.\n"); else if (current_score_estimate < 0) printf(" Estimated score: Black is ahead by %d\n", -current_score_estimate); else if (current_score_estimate > 0) printf(" Estimated score: White is ahead by %d\n", current_score_estimate); else printf(" Estimated score: Even!\n"); } printf("\n"); fflush(stdout); printf("%s", letterbar); if (get_last_player() != EMPTY) { gfprintf(stdout, " Last move: %s %1m", get_last_player() == WHITE ? "White" : "Black", last_move); } printf("\n"); fflush(stdout); for (i = 0; i < board_size; i++) { printf(" %2d", board_size - i); last_pos_was_move = 0; for (j = 0; j < board_size; j++) { if (POS(i, j) == last_move) pos_is_move = 128; else pos_is_move = 0; dead = (dragon_status(POS(i, j)) == DEAD) && showdead; switch (BOARD(i, j) + pos_is_move + last_pos_was_move) { case EMPTY+128: case EMPTY: printf(" %c", hspots[i][j]); last_pos_was_move = 0; break; case BLACK: printf(" %c", dead ? 'x' : 'X'); last_pos_was_move = 0; break; case WHITE: printf(" %c", dead ? 'o' : 'O'); last_pos_was_move = 0; break; case BLACK+128: printf("(%c)", 'X'); last_pos_was_move = 256; break; case WHITE+128: printf("(%c)", 'O'); last_pos_was_move = 256; break; case EMPTY+256: printf("%c", hspots[i][j]); last_pos_was_move = 0; break; case BLACK+256: printf("%c", dead ? 'x' : 'X'); last_pos_was_move = 0; break; case WHITE+256: printf("%c", dead ? 'o' : 'O'); last_pos_was_move = 0; break; default: fprintf(stderr, "Illegal board value %d\n", (int) BOARD(i, j)); exit(EXIT_FAILURE); break; } } if (last_pos_was_move == 0) { if (board_size > 10) printf(" %2d", board_size - i); else printf(" %1d", board_size - i); } else { if (board_size > 10) printf("%2d", board_size - i); else printf("%1d", board_size - i); } printf("\n"); } fflush(stdout); printf("%s\n\n", letterbar); fflush(stdout); if (clock_on) { clock_print(WHITE); clock_print(BLACK); } } /* end ascii_showboard */