Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**Author: Jerry Chen and Liam Pribis
- * Date: 17 October 2018
- * Description: A collectoin of self-contained states that block and
- * wait for a condition to exit and continue on to the next state.
- */
- #include "intermediate_states.h"
- #include "../fonts/font3x5_1.h"
- #include "extension_board.h"
- #include "ir_uart.h"
- #include "navswitch.h"
- #include "pacer.h"
- #include "ship_placer.h"
- #include "system.h"
- #include "tinygl.h"
- #include "util.h"
- #include <stdbool.h>
- /** Default pacer Hz for all intermediate states */
- #define INITIAL_PACER_HZ 2000
- #define ESTABLISH_PLAYER_NUMBER_LED_DIVISOR 300
- /** Text that is displayed when in the wait_player_start state */
- #define PLAYER_0_TEXT "P0"
- #define PLAYER_1_TEXT "P1"
- /** Signals the board recieving this signal to become player 0 or 1.
- * Note this is the ASCII code 0/1 rather than integer 0/1 to attempt to
- * mitigate interference with other projects.
- */
- #define IR_BECOME_PLAYER_0_SIGNAL '0'
- #define IR_BECOME_PLAYER_1_SIGNAL '1'
- /** Text dislayed while waiting for the other player to place their ships */
- #define WAITING_TEXT " WAITING "
- #define WAITING_TEXT_SPEED 30
- /** Signal sent to signify this board is done placing their ships
- * and is ready to begin gameplay.
- */
- #define IR_READY_SIGNAL 'R'
- /** How long to buzz the piezo for each note of the winning note sequence*/
- #define WIN_NOTE_BUZZ_TIME 0.2f
- /** The pattern of leds: 0 = Green, 1 = Red, 2 = Blue */
- const int INITIAL_LED_PATTERN[] = { 0, 1, 2, 1 };
- /** The frequencies that will be repeatedly played while in the win state */
- const int WIN_STATE_FREQUENCIES[] = { 110, 220, 440, 220 };
- // NOTE all public function doc is in intermediate_states.h
- void game_init(void)
- {
- system_init();
- ir_uart_init();
- navswitch_init();
- extension_board_init();
- tinygl_init(INITIAL_PACER_HZ);
- tinygl_font_set(&font3x5_1);
- tinygl_text_dir_set(TINYGL_TEXT_DIR_ROTATE);
- pacer_init(INITIAL_PACER_HZ);
- }
- bool establish_player_number(void)
- {
- int iteration = 0;
- int led_num_index = 0;
- int initial_led_pattern_length = sizeof(INITIAL_LED_PATTERN) / sizeof(INITIAL_LED_PATTERN[0]);
- bool wait_for_establish = true;
- bool is_player_1 = false;
- while (wait_for_establish) {
- pacer_wait();
- navswitch_update();
- // Cycle through LEDs while waiting
- if (iteration % ESTABLISH_PLAYER_NUMBER_LED_DIVISOR == 0) {
- // Increment led_num_index and modulo it to keep with array bounds.
- // The result from array is bit shifted so it works as a bitmask for set_led_state()
- set_led_state(1 << INITIAL_LED_PATTERN[(led_num_index++ % initial_led_pattern_length)]);
- }
- iteration++;
- if (ir_uart_read_ready_p()) {
- switch (ir_uart_getc()) {
- case IR_BECOME_PLAYER_0_SIGNAL:
- // If 0 message is recieved (ie this board is player 0)
- // Send a 1 message back to indicate the other board is player 1
- ir_uart_putc(IR_BECOME_PLAYER_1_SIGNAL);
- is_player_1 = false;
- wait_for_establish = false;
- break;
- case IR_BECOME_PLAYER_1_SIGNAL:
- is_player_1 = true;
- wait_for_establish = false;
- break;
- // Ignore all other messages
- }
- }
- if (navswitch_push_event_p(NAVSWITCH_PUSH)) {
- ir_uart_putc(IR_BECOME_PLAYER_0_SIGNAL);
- }
- }
- return is_player_1;
- }
- void wait_player_start(bool is_player_1)
- {
- tinygl_clear();
- set_led_state(LED_GREEN);
- char* text = is_player_1 ? PLAYER_1_TEXT : PLAYER_0_TEXT;
- tinygl_text(text);
- bool wait = true;
- while (wait) {
- pacer_wait();
- tinygl_update();
- navswitch_update();
- if (navswitch_push_event_p(NAVSWITCH_PUSH)) {
- wait = false;
- }
- }
- tinygl_clear();
- set_led_state(LED_NONE);
- }
- void wait_both_ready(void)
- {
- tinygl_clear();
- clear_display_and_update();
- tinygl_text_mode_set(TINYGL_TEXT_MODE_SCROLL);
- tinygl_text_speed_set(WAITING_TEXT_SPEED);
- tinygl_text(WAITING_TEXT);
- bool listening = false;
- bool other_player_ready = false;
- while (!other_player_ready) {
- pacer_wait();
- tinygl_update();
- if (listening) {
- // Wait for ready signal from other board, then continue to next state (break)
- if (ir_uart_read_ready_p()) {
- if (ir_uart_getc() == IR_READY_SIGNAL) {
- other_player_ready = true;
- }
- }
- } else {
- // If not listening, we are trying to send ready signal
- // When possible, notify the other board that we are ready
- if (ir_uart_write_ready_p()) {
- ir_uart_putc(IR_READY_SIGNAL);
- }
- // When ready signal sent, listen for reply
- listening = true;
- }
- }
- }
- void win_state()
- {
- set_led_state(LED_GREEN | LED_YELLOW);
- int freqs_length = sizeof(WIN_STATE_FREQUENCIES) / sizeof(WIN_STATE_FREQUENCIES[0]);
- int freqs_iter = 0;
- // Use while true because this is the final state of the game.
- while (true) {
- // Increment freqs_iter and modulo it to ensure it stays within the bounds of frequency array
- buzz_hz_time(WIN_NOTE_BUZZ_TIME, WIN_STATE_FREQUENCIES[freqs_iter++ % freqs_length]);
- }
- }
- void loss_state()
- {
- set_led_state(LED_RED | LED_YELLOW);
- // Use while true because this is the final state of the game.
- while (true) {
- // Do nothing
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement