Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.68 KB | None | 0 0
  1. /**Author: Jerry Chen and Liam Pribis
  2.  * Date: 17 October 2018
  3.  * Description: A collectoin of self-contained states that block and
  4.  * wait for a condition to exit and continue on to the next state.
  5.  */
  6.  
  7. #include "intermediate_states.h"
  8. #include "../fonts/font3x5_1.h"
  9. #include "extension_board.h"
  10. #include "ir_uart.h"
  11. #include "navswitch.h"
  12. #include "pacer.h"
  13. #include "ship_placer.h"
  14. #include "system.h"
  15. #include "tinygl.h"
  16. #include "util.h"
  17. #include <stdbool.h>
  18.  
  19. /** Default pacer Hz for all intermediate states */
  20. #define INITIAL_PACER_HZ 2000
  21.  
  22. #define ESTABLISH_PLAYER_NUMBER_LED_DIVISOR 300
  23.  
  24. /** Text that is displayed when in the wait_player_start state */
  25. #define PLAYER_0_TEXT "P0"
  26. #define PLAYER_1_TEXT "P1"
  27.  
  28. /** Signals the board recieving this signal to become player 0 or 1.
  29.  * Note this is the ASCII code 0/1 rather than integer 0/1 to attempt to
  30.  * mitigate interference with other projects.
  31. */
  32. #define IR_BECOME_PLAYER_0_SIGNAL '0'
  33. #define IR_BECOME_PLAYER_1_SIGNAL '1'
  34.  
  35. /** Text dislayed while waiting for the other player to place their ships */
  36. #define WAITING_TEXT " WAITING "
  37. #define WAITING_TEXT_SPEED 30
  38.  
  39. /** Signal sent to signify this board is done placing their ships
  40.  * and is ready to begin gameplay.
  41.  */
  42. #define IR_READY_SIGNAL 'R'
  43.  
  44. /** How long to buzz the piezo for each note of the winning note sequence*/
  45. #define WIN_NOTE_BUZZ_TIME 0.2f
  46.  
  47. /** The pattern of leds: 0 = Green, 1 = Red, 2 = Blue */
  48. const int INITIAL_LED_PATTERN[] = { 0, 1, 2, 1 };
  49.  
  50. /** The frequencies that will be repeatedly played while in the win state */
  51. const int WIN_STATE_FREQUENCIES[] = { 110, 220, 440, 220 };
  52.  
  53. // NOTE all public function doc is in intermediate_states.h
  54.  
  55. void game_init(void)
  56. {
  57.     system_init();
  58.     ir_uart_init();
  59.     navswitch_init();
  60.     extension_board_init();
  61.     tinygl_init(INITIAL_PACER_HZ);
  62.     tinygl_font_set(&font3x5_1);
  63.     tinygl_text_dir_set(TINYGL_TEXT_DIR_ROTATE);
  64.     pacer_init(INITIAL_PACER_HZ);
  65. }
  66.  
  67. bool establish_player_number(void)
  68. {
  69.     int iteration = 0;
  70.     int led_num_index = 0;
  71.     int initial_led_pattern_length = sizeof(INITIAL_LED_PATTERN) / sizeof(INITIAL_LED_PATTERN[0]);
  72.  
  73.     bool wait_for_establish = true;
  74.     bool is_player_1 = false;
  75.  
  76.     while (wait_for_establish) {
  77.         pacer_wait();
  78.         navswitch_update();
  79.  
  80.         // Cycle through LEDs while waiting
  81.         if (iteration % ESTABLISH_PLAYER_NUMBER_LED_DIVISOR == 0) {
  82.             // Increment led_num_index and modulo it to keep with array bounds.
  83.             // The result from array is bit shifted so it works as a bitmask for set_led_state()
  84.             set_led_state(1 << INITIAL_LED_PATTERN[(led_num_index++ % initial_led_pattern_length)]);
  85.         }
  86.         iteration++;
  87.  
  88.         if (ir_uart_read_ready_p()) {
  89.             switch (ir_uart_getc()) {
  90.             case IR_BECOME_PLAYER_0_SIGNAL:
  91.  
  92.                 // If 0 message is recieved (ie this board is player 0)
  93.                 // Send a 1 message back to indicate the other board is player 1
  94.                 ir_uart_putc(IR_BECOME_PLAYER_1_SIGNAL);
  95.                 is_player_1 = false;
  96.                 wait_for_establish = false;
  97.                 break;
  98.  
  99.             case IR_BECOME_PLAYER_1_SIGNAL:
  100.                 is_player_1 = true;
  101.                 wait_for_establish = false;
  102.                 break;
  103.                 // Ignore all other messages
  104.             }
  105.         }
  106.         if (navswitch_push_event_p(NAVSWITCH_PUSH)) {
  107.             ir_uart_putc(IR_BECOME_PLAYER_0_SIGNAL);
  108.         }
  109.     }
  110.  
  111.     return is_player_1;
  112. }
  113.  
  114. void wait_player_start(bool is_player_1)
  115. {
  116.     tinygl_clear();
  117.     set_led_state(LED_GREEN);
  118.  
  119.     char* text = is_player_1 ? PLAYER_1_TEXT : PLAYER_0_TEXT;
  120.     tinygl_text(text);
  121.  
  122.     bool wait = true;
  123.  
  124.     while (wait) {
  125.         pacer_wait();
  126.         tinygl_update();
  127.         navswitch_update();
  128.  
  129.         if (navswitch_push_event_p(NAVSWITCH_PUSH)) {
  130.             wait = false;
  131.         }
  132.     }
  133.  
  134.     tinygl_clear();
  135.     set_led_state(LED_NONE);
  136. }
  137.  
  138. void wait_both_ready(void)
  139. {
  140.     tinygl_clear();
  141.     clear_display_and_update();
  142.     tinygl_text_mode_set(TINYGL_TEXT_MODE_SCROLL);
  143.     tinygl_text_speed_set(WAITING_TEXT_SPEED);
  144.     tinygl_text(WAITING_TEXT);
  145.  
  146.     bool listening = false;
  147.  
  148.     bool other_player_ready = false;
  149.  
  150.     while (!other_player_ready) {
  151.         pacer_wait();
  152.         tinygl_update();
  153.  
  154.         if (listening) {
  155.             // Wait for ready signal from other board, then continue to next state (break)
  156.             if (ir_uart_read_ready_p()) {
  157.                 if (ir_uart_getc() == IR_READY_SIGNAL) {
  158.                     other_player_ready = true;
  159.                 }
  160.             }
  161.         } else {
  162.             // If not listening, we are trying to send ready signal
  163.             // When possible, notify the other board that we are ready
  164.             if (ir_uart_write_ready_p()) {
  165.                 ir_uart_putc(IR_READY_SIGNAL);
  166.             }
  167.  
  168.             // When ready signal sent, listen for reply
  169.             listening = true;
  170.         }
  171.     }
  172. }
  173.  
  174. void win_state()
  175. {
  176.     set_led_state(LED_GREEN | LED_YELLOW);
  177.     int freqs_length = sizeof(WIN_STATE_FREQUENCIES) / sizeof(WIN_STATE_FREQUENCIES[0]);
  178.  
  179.     int freqs_iter = 0;
  180.  
  181.     // Use while true because this is the final state of the game.
  182.     while (true) {
  183.         // Increment freqs_iter and modulo it to ensure it stays within the bounds of frequency array
  184.         buzz_hz_time(WIN_NOTE_BUZZ_TIME, WIN_STATE_FREQUENCIES[freqs_iter++ % freqs_length]);
  185.     }
  186. }
  187.  
  188. void loss_state()
  189. {
  190.     set_led_state(LED_RED | LED_YELLOW);
  191.  
  192.     // Use while true because this is the final state of the game.
  193.     while (true) {
  194.         // Do nothing
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement