void main(void) { Sys_Init(); // System Initialization Port_Init(); // Initialize ports 2 and 3 Interrupt_Init(); //Initialize Interrupts Timer_Init(); //Initialize timer 0 putchar(' '); // the quote fonts may not copy correctly into SiLabs IDE BILED0=0; //Turn OFF the BILED BILED1=0; printf("\r\nStart"); while(1) { printf("\r\n\n Press any key to begin"); //get a number to randomize the random number even more keyinput=getchar(); previous_num=keyinput%3; printf("\r\n This game will light 1 or both leds. Press the corresponding button to win."); while (i<=9) { if(!SS) { TR0 = 1; //start timer0 TL0 = 0; //clear low bits TH0 = 0; //clear high bits Counts = 0; //set the counts to 0 random_num = random(); //create a random number. where random_num is a while (random_num==previous_num) { random_num = random(); } //check to see if the random number was used before if (random_num==0) { //light LED0 for 1 second Counts=0; LED0=0; //turn it on while (Counts<338); //wait 1 second LED0=1; //turn it off if (PB1==0 && PB2==1) { correct_answer(); //store correct answer; } else { incorrect_answer(); } } else if (random_num==1) { //light LED1 for 1 second Counts=0; LED1=0; //turn it on while (Counts<338); //wait 1 second LED1=1; //turn it off if (PB1==1 && PB2==0) { correct_answer(); } else { incorrect_answer(); } } else //this means that the random number is 2 { //light LED0 and LED1 for 1 second Counts=0; LED0=0; LED1=0; while (Counts<338); LED0=1; LED1=1; if (PB1==0 && PB2==0) { correct_answer(); } else { incorrect_answer(); } } previous_num=random_num; i++; //increment i } else { TR0=0; //END } } TR0 = 0; //turn off timer printf("\r\n Number of correct answers = %d", number_correct); //display results while (!SS); //wait until the switch is turned off and back on again to loop } }
/** * @brief Step through the main stages in the code. * @return Void */ void md14_main() { switch(md14_next_state) { case MD14_STATE_INTRO: PRINTF("In intro state\n\r"); play_mp3(MODE_FILESET, "WELC"); lang_fileset = "ENG_"; mode_fileset = "MD14"; md14_next_state = MD14_STATE_LVLSEL; srand(timer_rand()); break; case MD14_STATE_LVLSEL: md14_last_dot = create_dialog("LVLS", (DOT_1 | DOT_2)); switch (md14_last_dot) { case NO_DOTS: break; case '1': play_mp3(LANGUAGE, "EASY"); strings_to_wordlist(easy, ARRAYLEN(easy), &md14_dict); print_words_in_list(&md14_dict); play_mp3(MODE_FILESET, "INST"); md14_next_state = MD14_STATE_GENQUES; break; case '2': play_mp3(LANGUAGE, "HARD"); strings_to_wordlist(medium, ARRAYLEN(medium), &md14_dict); print_words_in_list(&md14_dict); play_mp3(MODE_FILESET, "INST"); md14_next_state = MD14_STATE_GENQUES; break; // todo: add case 3; figure out too-long words default: play_mp3(LANGUAGE, "INVP"); PRINTF("Invalid entry."); break; } break; case MD14_STATE_GENQUES: PRINTF("In genques state\n\r"); md14_curr_mistakes = 0; get_next_word_in_wordlist(&md14_dict, &md14_chosen_word); sprintf(dbgstr, "[MD14] Next word: %s\n\r", md14_chosen_word->name); PRINTF(dbgstr); md14_next_state = MD14_STATE_PROMPT; break; case MD14_STATE_PROMPT: PRINTF("In prompt state\n\r"); play_mp3(LANGUAGE, "SPEL"); speak_word(md14_chosen_word); md14_next_state = MD14_STATE_INPUT; break; case MD14_STATE_INPUT: md14_cell = get_cell(); if (md14_cell == NO_DOTS) { break; } md14_cell_pattern = GET_CELL_PATTERN(md14_cell); md14_cell_control = GET_CELL_CONTROL(md14_cell); switch (md14_cell_control) { case WITH_ENTER: md14_user_cell.pattern = md14_cell_pattern; md14_next_state = MD14_STATE_CHECK; PRINTF("[MD14] Checking answer\n\r"); break; case WITH_LEFT: md14_next_state = MD14_STATE_REPROMPT; break; case WITH_RIGHT: stats(); md14_next_state = MD14_STATE_GENQUES; break; case WITH_CANCEL: break; } break; case MD14_STATE_CHECK: get_next_cell_in_word(md14_chosen_word, &md14_curr_cell); sprintf(dbgstr, "In check state. Current cell: %x, user cell: %x.\n\r", md14_curr_cell.pattern, md14_user_cell.pattern); PRINTF(dbgstr); if (cell_equals(&md14_curr_cell, &md14_user_cell)) { md14_curr_mistakes = 0; if (md14_chosen_word->curr_letter == md14_chosen_word->num_letters - 1) { // done correct_answer(); md14_next_state = MD14_STATE_GENQUES; } else {// correct but not done play_mp3(LANGUAGE, "GOOD"); play_mp3(LANGUAGE, "NLET"); md14_next_state = MD14_STATE_INPUT; } } else {// incorrect letter incorrect_answer(); // @todo mark invalid letters invalid md14_next_state = MD14_STATE_REPROMPT; } break; case MD14_STATE_REPROMPT: PRINTF("In reprompt state\n\r"); if (md14_curr_mistakes >= MAX_INCORRECT_GUESS) { play_mp3(LANGUAGE, "PRSS"); play_pattern(md14_curr_cell.pattern); } else { play_mp3(LANGUAGE, "SPEL"); speak_word(md14_chosen_word); if (md14_chosen_word->curr_glyph > -1) {// not at beginning of word play_mp3(MODE_FILESET, "SPLS"); speak_letters_so_far(md14_chosen_word); } } md14_next_state = MD14_STATE_INPUT; break; default: break; } }