Advertisement
Guest User

mini project

a guest
Apr 21st, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 26.51 KB | None | 0 0
  1. #include "stm32f0xx.h"
  2. #include "stm32f0_discovery.h"
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdarg.h>
  8. #include "fifo.h"
  9.  
  10. #define UNK -1
  11. #define NON_INTR 0
  12. #define INTR 1
  13.  
  14. int __io_putchar(int ch);
  15. static int putchar_nonirq(int ch);
  16.  
  17. void step3(void);
  18. void step4(void);
  19. void step5(void);
  20.  
  21. #define COL1 (1 << 0)
  22. #define COL2 (1 << 1)
  23. #define COL3 (1 << 2)
  24. #define COL4 (1 << 3)
  25.  
  26. #define ROW1 (1 << 4)
  27. #define ROW2 (1 << 5)
  28. #define ROW3 (1 << 6)
  29. #define ROW4 (1 << 7)
  30.  
  31. #define SAMPLE_TIME_MS 10
  32. #define SAMPLE_COUNT (SAMPLE_TIME_MS)
  33.  
  34. #define THRESHOLD_TIME_MS 2
  35. #define THRESHOLD (THRESHOLD_TIME_MS)
  36.  
  37. #define KEY_PRESS_MASK  0b11000111
  38. #define KEY_REL_MASK    0b11100011
  39.  
  40. static struct fifo input_fifo;  // input buffer
  41. static struct fifo output_fifo; // output buffer
  42. int interrupt_mode = UNK;   // which version of putchar/getchar to use.
  43. int echo_mode = 0;          // should we echo input characters?
  44. int ships[16][16];
  45. int player_1 = 1;
  46. char input[5];
  47. char X[3];
  48. char Y[3];
  49. int counter =0;
  50.  
  51. char coord[4] = {'\0', '\0', '\0', '\0',};
  52. int t_i = 0;
  53.  
  54. bool win = false;
  55. int opp_hit = 0;
  56. int hit_opp = 0;
  57. int phase = 1;
  58. char player = '\0';
  59. char my_turn = '\0';
  60.  
  61. char opponent_ready = 'N';
  62. char phase2_input[7] = {'\0', '\0', '\0', '\0', '\0', '\0', '\0'};
  63. char phase3_input[5] = {'\0', '\0', '\0', '\0', '\0'};
  64. bool ship5_set = false;
  65. bool ship4_set = false;
  66. bool ship2_set = false;
  67. bool ship3_1_set = false;
  68. bool ship3_2_set = false;
  69. int input_index = 0;
  70.  
  71. int grid[16][16];
  72.  
  73. //=============================================================================
  74.  
  75. //=======================================================================
  76. // Simply write a string one char at a time.
  77. //=======================================================================
  78. static void putstr(const char *s) {
  79.     while(*s)
  80.         __io_putchar(*s++);
  81. }
  82.  
  83. //=======================================================================
  84. // Insert a character and echo it.
  85. // (or, if it's a backspace, remove a char and erase it from the line).
  86. // If echo_mode is turned off, just insert the character and get out.
  87. //=======================================================================
  88. static void insert_echo_char(char ch) {
  89.     if (ch == '\r')
  90.         ch = '\n';
  91.     if (!echo_mode) {
  92.         fifo_insert(&input_fifo, ch);
  93.         return;
  94.     }
  95.     if (ch == '\b' || ch == '\177') {
  96.         if (!fifo_empty(&input_fifo)) {
  97.             char tmp = fifo_uninsert(&input_fifo);
  98.             if (tmp == '\n')
  99.                 fifo_insert(&input_fifo, '\n');
  100.             else if (tmp < 32)
  101.                 putstr("\b\b  \b\b");
  102.             else
  103.                 putstr("\b \b");
  104.         }
  105.         return; // Don't put a backspace into buffer.
  106.     } else if (ch == '\n') {
  107.         __io_putchar('\n');
  108.     } else if (ch == 0){
  109.         putstr("^0");
  110.     } else if (ch == 28) {
  111.         putstr("^\\");
  112.     } else if (ch < 32) {
  113.         __io_putchar('^');
  114.         __io_putchar('A'-1+ch);
  115.     } else {
  116.         __io_putchar(ch);
  117.     }
  118.     fifo_insert(&input_fifo, ch);
  119. }
  120.  
  121.  
  122. //-----------------------------------------------------------------------------
  123. // Section 6.2
  124. //-----------------------------------------------------------------------------
  125. // This should should perform the following
  126. // 1) Enable clock to GPIO port A
  127. // 2) Configure PA9 and PA10 to alternate function to use a USART
  128. //    Note: Configure both MODER and AFRL registers
  129. // 3) Enable clock to the USART module, it is up to you to determine
  130. //    which RCC register to use
  131. // 4) Disable the USART module (hint UE bit in CR1)
  132. // 5) Configure USART for 8 bits, 1 stop bit and no parity bit
  133. // 6) Use 16x oversampling
  134. // 7) Configure for 115200 baud rate
  135. // 8) Enable the USART for both transmit and receive
  136. // 9) Enable the USART
  137. // 10) Wait for TEACK and REACK to be set by hardware in the ISR register
  138. // 11) Set the 'interrupt_mode' variable to NON_INTR
  139. void tty_init(void) {
  140.     // Disable buffers for stdio streams.  Otherwise, the first use of
  141.     // each stream will result in a *malloc* of 2K.  Not good.
  142.     setbuf(stdin,0);
  143.     setbuf(stdout,0);
  144.     setbuf(stderr,0);
  145.     RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
  146.     GPIOA->MODER |= GPIO_MODER_MODER9_1;
  147.     GPIOA->MODER &= ~GPIO_MODER_MODER9_0;
  148.     GPIOA->MODER |= GPIO_MODER_MODER10_1;
  149.     GPIOA->MODER &= ~GPIO_MODER_MODER10_0;
  150.     GPIOA->AFR[1] |= 0x00000110;
  151.     RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
  152.     USART1->CR1 &= ~(USART_CR1_UE);
  153.     USART1->CR1 &= ~(USART_CR1_OVER8);
  154.     USART1->CR2 &= ~(USART_CR2_STOP);
  155.     USART1->CR1 &= ~(USART_CR1_M);
  156.     USART1->CR1 &= ~(USART_CR1_PCE);
  157.     USART1->BRR = 417;
  158.     USART1->CR1 |= USART_CR1_TE;
  159.     USART1->CR1 |= USART_CR1_RE;
  160.     USART1->CR1 |= (USART_CR1_UE);
  161.     while (!(((USART1->ISR & USART_ISR_REACK) == USART_ISR_REACK) && ((USART1->ISR & USART_ISR_TEACK) == USART_ISR_TEACK)));
  162.     interrupt_mode = NON_INTR;
  163.     // Student code goes here...
  164.  
  165. }
  166.  
  167. //=======================================================================
  168. // Enable the USART RXNE interrupt.
  169. // Remember to enable the right bit in the NVIC registers
  170. //=======================================================================
  171. void enable_tty_irq(void) {
  172.     // Student code goes here...
  173.     USART1->CR1 |= USART_CR1_RXNEIE;
  174.     NVIC->ISER[0] |= 1 << USART1_IRQn;
  175.     interrupt_mode = INTR;
  176. }
  177.  
  178. //-----------------------------------------------------------------------------
  179. // Section 6.5
  180. //-----------------------------------------------------------------------------
  181. // See lab document for description
  182. //=======================================================================
  183. // IRQ invoked for USART1 activity.
  184. void USART1_IRQHandler(void) {
  185.     // Student code goes here...
  186.     if((USART1->ISR & USART_ISR_RXNE) == USART_ISR_RXNE) {
  187.         insert_echo_char(USART1->RDR & 0xff);
  188.     }
  189.     if((USART1->ISR & USART_ISR_TXE) == USART_ISR_TXE) {
  190.         if (fifo_empty(&output_fifo)) {
  191.             USART1->CR1 &= ~(USART_CR1_TXEIE);
  192.         }
  193.         else {
  194.             USART1->TDR = fifo_remove(&output_fifo);
  195.         }
  196.     }
  197.     //-----------------------------------------------------------------
  198.     // Leave this checking code here to make sure nothing bad happens.
  199.     if (USART1->ISR & (USART_ISR_RXNE|
  200.             USART_ISR_ORE|USART_ISR_NE|USART_ISR_FE|USART_ISR_PE)) {
  201.    //     safe_printf("Problem in USART1_IRQHandler: ISR = 0x%x\n", USART1->ISR);
  202.     }
  203. }
  204.  
  205. // See lab document for description
  206. static int getchar_irq(void) {
  207.     // Student code goes here...
  208.  
  209. //     while(!fifo_newline(&input_fifo)) {
  210. //        asm("wfi");
  211. //   }
  212.  
  213.     return fifo_remove(&input_fifo);
  214. }
  215.  
  216. // See lab document for description
  217. static int putchar_irq(char ch) {
  218.     // Student code goes here...
  219.     while (fifo_full(&output_fifo)) {
  220.         asm("wfi");
  221.     }
  222.     if (ch == 10) {
  223.         fifo_insert(&output_fifo, '\r');
  224.     }
  225.     else {
  226.         fifo_insert(&output_fifo, ch);
  227.     }
  228.     if ((USART1->CR1 & USART_CR1_TXEIE) != USART_CR1_TXEIE) {
  229.         USART1->CR1 |= USART_CR1_TXEIE;
  230.         USART1_IRQHandler();
  231.     }
  232.     if (ch == 10) {
  233.         while (fifo_full(&output_fifo)) {
  234.             asm("wfi");
  235.         }
  236.         fifo_insert(&output_fifo, '\n');
  237.     }
  238. }
  239.  
  240. //=======================================================================
  241. // Called by the Standard Peripheral library for a write()
  242. int __io_putchar(int ch) {
  243.     if (interrupt_mode == INTR)
  244.         return putchar_irq(ch);
  245.  
  246. }
  247.  
  248. //=======================================================================
  249. // Called by the Standard Peripheral library for a read()
  250. int __io_getchar(void) {
  251.     // Choose the right implementation.
  252.     if (interrupt_mode == INTR)
  253.         return getchar_irq();
  254.  
  255. }
  256.  
  257. void initilize_ships() {
  258.     for (int i =0; i < 16; i++) {
  259.         for (int j =0; j < 16; j++) {
  260.             ships[i][j] = 0;
  261.         }
  262.     }
  263.     for (int i =0; i <2; i++) {
  264.         ships[0][i] = 1;
  265.     }
  266.     for (int i =0; i <3; i++) {
  267.         ships[i+10][5] = 1;
  268.     }
  269.     for (int i =0; i <4; i++) {
  270.         ships[i][9] = 1;
  271.         ships[i][14] = 1;
  272.     }
  273.     for (int i =0; i <5; i++) {
  274.         ships[3][i+2] = 1;
  275.     }
  276.     ships[3][14] = 0;
  277.     return;
  278. }
  279.  
  280. //void TIM3_IRQHandler() {
  281.  
  282. //}
  283.  
  284. /*void game() {
  285.     if (player_1 == 1) { //this means stm 1 is going to be the first one going
  286.        for (int i=0; i < 4;i++) {
  287.  
  288.        }
  289.     }
  290. }
  291. */
  292.  
  293. int col = 0;
  294. int row = -1;
  295. char char_array[4][4] = { {'1', '2', '3', 'A'},
  296.                           {'4', '5', '6', 'B'},
  297.                           {'7', '8', '9', 'C'},
  298.                           {'*', '0', '#', 'D'} };
  299.  
  300.  
  301. int int_array[4][4] =   { {1,2,3,0xa},
  302.                           {4,5,6,0xb},
  303.                           {7,8,9,0xc},
  304.                           {0xf, 0, 0xe, 0xd} };
  305.  
  306. uint8_t key_samples[4][4]  = { {0}, {0}, {0}, {0} };
  307. uint8_t key_pressed[4][4]  = { {0}, {0}, {0}, {0} };
  308. uint8_t key_released[4][4]  = { {0}, {0}, {0}, {0} };
  309.  
  310. void pb9out(void) {
  311.     RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
  312.     GPIOB->MODER &= ~(GPIO_MODER_MODER9);
  313.     GPIOB->MODER |= GPIO_MODER_MODER9_0;
  314.     if ((GPIOB->ODR & GPIO_ODR_9) == 0) {
  315.         GPIOB->ODR |= GPIO_ODR_9;
  316.     }
  317.     else {
  318.         GPIOB->BSRR |= GPIO_BSRR_BR_9;
  319.     }
  320. }
  321.  
  322. void pb8out(void) {
  323.     RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
  324.     GPIOB->MODER &= ~(GPIO_MODER_MODER8);
  325.     GPIOB->MODER |= GPIO_MODER_MODER8_0;
  326.     if ((GPIOB->ODR & GPIO_ODR_8) == 0) {
  327.         GPIOB->ODR |= GPIO_ODR_8;
  328.     }
  329.     else {
  330.         GPIOB->BSRR |= GPIO_BSRR_BR_8;
  331.     }
  332. }
  333.  
  334. void pb7out(void) {
  335.     RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
  336.     GPIOB->MODER &= ~(GPIO_MODER_MODER7);
  337.     GPIOB->MODER |= GPIO_MODER_MODER7_0;
  338.     if ((GPIOB->ODR & GPIO_ODR_7) == 0) {
  339.         GPIOB->ODR |= GPIO_ODR_7;
  340.     }
  341.     else {
  342.         GPIOB->BSRR |= GPIO_BSRR_BR_7;
  343.     }
  344. }
  345.  
  346. void pb6out(void) {
  347.     RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
  348.     GPIOB->MODER &= ~(GPIO_MODER_MODER6);
  349.     GPIOB->MODER |= GPIO_MODER_MODER6_0;
  350.     if ((GPIOB->ODR & GPIO_ODR_6) == 0) {
  351.         GPIOB->ODR |= GPIO_ODR_6;
  352.     }
  353.     else {
  354.         GPIOB->BSRR |= GPIO_BSRR_BR_6;
  355.     }
  356. }
  357.  
  358. void pb5out(void) {
  359.     RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
  360.     GPIOB->MODER &= ~(GPIO_MODER_MODER5);
  361.     GPIOB->MODER |= GPIO_MODER_MODER5_0;
  362.     if ((GPIOB->ODR & GPIO_ODR_5) == 0) {
  363.         GPIOB->ODR |= GPIO_ODR_5;
  364.     }
  365.     else {
  366.         GPIOB->BSRR |= GPIO_BSRR_BR_5;
  367.     }
  368. }
  369.  
  370. void pb4out(void) {
  371.     RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
  372.     GPIOB->MODER &= ~(GPIO_MODER_MODER4);
  373.     GPIOB->MODER |= GPIO_MODER_MODER4_0;
  374.     if ((GPIOB->ODR & GPIO_ODR_4) == 0) {
  375.         GPIOB->ODR |= GPIO_ODR_4;
  376.     }
  377.     else {
  378.         GPIOB->BSRR |= GPIO_BSRR_BR_4;
  379.     }
  380. }
  381.  
  382. void pb3out(void) {
  383.     RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
  384.     GPIOB->MODER &= ~(GPIO_MODER_MODER3);
  385.     GPIOB->MODER |= GPIO_MODER_MODER3_0;
  386.     if ((GPIOB->ODR & GPIO_ODR_3) == 0) {
  387.         GPIOB->ODR |= GPIO_ODR_3;
  388.     }
  389.     else {
  390.         GPIOB->BSRR |= GPIO_BSRR_BR_3;
  391.     }
  392. }
  393.  
  394. void pc12out(void) {
  395.     RCC->AHBENR |= RCC_AHBENR_GPIOCEN;
  396.     GPIOC->MODER &= ~(GPIO_MODER_MODER12);
  397.     GPIOC->MODER |= GPIO_MODER_MODER12_0;
  398.     if ((GPIOC->ODR & GPIO_ODR_12) == 0) {
  399.         GPIOC->ODR |= GPIO_ODR_12;
  400.     }
  401.     else {
  402.         GPIOC->BSRR |= GPIO_BSRR_BR_12;
  403.     }
  404. }
  405.  
  406. void pc11out(void) {
  407.     RCC->AHBENR |= RCC_AHBENR_GPIOCEN;
  408.     GPIOC->MODER &= ~(GPIO_MODER_MODER11);
  409.     GPIOC->MODER |= GPIO_MODER_MODER11_0;
  410.     if ((GPIOC->ODR & GPIO_ODR_11) == 0) {
  411.         GPIOC->ODR |= GPIO_ODR_11;
  412.     }
  413.     else {
  414.         GPIOC->BSRR |= GPIO_BSRR_BR_11;
  415.     }
  416. }
  417.  
  418. void update_key_press() {
  419.     for(int i = 0; i < 4; i++) {
  420.         for(int j = 0; j < 4; j++) {
  421.             if ((key_samples[i][j] & KEY_PRESS_MASK) == 0b00000111) {
  422.                 key_pressed[i][j] = 1;
  423.                 key_samples[i][j] = 0xFF;
  424.             }
  425.  
  426.             if ((key_samples[i][j] & KEY_REL_MASK) == 0b11100000) {
  427.                 key_released[i][j] = 1;
  428.                 key_samples[i][j] = 0x00;
  429.             }
  430.         }
  431.     }
  432. }
  433.  
  434. char get_char_key() {
  435.     for(int i = 0; i < 4; i++) {
  436.         for(int j = 0; j < 4; j++) {
  437.             if(key_released[i][j] == 1 && key_pressed[i][j] == 1) {
  438.                 key_released[i][j] = 0;
  439.                 key_pressed[i][j] = 0;
  440.                 return char_array[i][j];
  441.             }
  442.         }
  443.     }
  444.  
  445.     return '\0';
  446. }
  447.  
  448. int get_key_pressed() {
  449.     for(int i = 0; i < 4; i++) {
  450.         for(int j = 0; j < 4; j++) {
  451.             if(key_released[i][j] == 1 && key_pressed[i][j] == 1) {
  452.                 key_released[i][j] = 0;
  453.                 key_pressed[i][j] = 0;
  454.                 return int_array[i][j];
  455.             }
  456.         }
  457.     }
  458.     return -1;
  459. }
  460.  
  461. void update_samples(int row) {
  462.     // Update the current column with new values
  463.     for(int i = 0; i < 4; i++) {
  464.         uint8_t curr_history = key_samples[i][col];
  465.         curr_history = curr_history << 1;
  466.  
  467.         if(row == i)
  468.             curr_history |= 1;
  469.  
  470.         key_samples[i][col] = curr_history;
  471.     }
  472. }
  473.  
  474. void init_keypad() {
  475.     RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
  476.     GPIOA->MODER &= ~(GPIO_MODER_MODER0 | GPIO_MODER_MODER1 |
  477.                       GPIO_MODER_MODER2 | GPIO_MODER_MODER3 |
  478.                       GPIO_MODER_MODER6 | GPIO_MODER_MODER7 |
  479.                       GPIO_MODER_MODER8 | GPIO_MODER_MODER9);
  480.  
  481. //    Columns set to output:
  482.     GPIOA->MODER |= GPIO_MODER_MODER0_0 | GPIO_MODER_MODER1_0 |
  483.                     GPIO_MODER_MODER2_0 | GPIO_MODER_MODER3_0;
  484.  
  485. //    Pull-down resistors for rows:
  486.     GPIOA->PUPDR &= ~(GPIO_PUPDR_PUPDR4 | GPIO_PUPDR_PUPDR5 |
  487.                       GPIO_PUPDR_PUPDR6 | GPIO_PUPDR_PUPDR7);
  488.     GPIOA->PUPDR |= GPIO_PUPDR_PUPDR4_1 | GPIO_PUPDR_PUPDR5_1 |
  489.                     GPIO_PUPDR_PUPDR6_1 | GPIO_PUPDR_PUPDR7_1;
  490. }
  491.  
  492. void setup_timer3() {
  493.     RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
  494.     TIM3->PSC = 480 - 1;
  495.     TIM3->ARR = 100 - 1;
  496.     TIM3->DIER |= 0x1;
  497.     NVIC -> ISER[0] = 1 << TIM3_IRQn;
  498.     TIM3->CR1 |= TIM_CR1_CEN;
  499. }
  500.  
  501. char key_scan() {
  502.     int current_row = -1;
  503.  
  504.     if (((GPIOA->IDR >> 4) & 0x1) == 0x1) {
  505.         current_row = 0;
  506.     }
  507.     else if (((GPIOA->IDR >> 5) & 0x1) == 0x1) {
  508.         current_row = 1;
  509.     }
  510.     else if (((GPIOA->IDR >> 6) & 0x1) == 0x1) {
  511.         current_row = 2;
  512.     }
  513.     else if (((GPIOA->IDR >> 7) & 0x1) == 0x1) {
  514.         current_row = 3;
  515.     }
  516.  
  517.     update_samples(current_row);
  518.     update_key_press();
  519.     col = (col + 1) % 4;
  520.  
  521.     GPIOA->ODR = 1<<col;
  522.  
  523.     return get_char_key();
  524. }
  525.  
  526. void ack_tim3() {
  527.     if ((TIM3->SR & TIM_SR_UIF) != 0) {
  528.         TIM3->SR &= ~TIM_SR_UIF;
  529.     }
  530. }
  531.  
  532. void reset_grid() {
  533.     for (int i = 0; i < 16; i++) {
  534.         for (int j = 0; j < 16; j++) {
  535.             grid[i][j] = 0;
  536.         }
  537.     }
  538. }
  539.  
  540. void phase1(char key) {
  541.     char input = getchar_irq();
  542.     if (input == 'G') {
  543.         my_turn = 'N';
  544.     }
  545.     if (key == '*') {
  546.         my_turn = 'Y';
  547.         phase = 2;          //Change back to 2 later
  548.         reset_grid();
  549.         putchar_irq('G');
  550.     }
  551.     else if (my_turn == 'N') {
  552.         phase =  2;          // Change back to 2 in real
  553.         reset_grid();
  554.     }
  555. }
  556.  
  557. void input_reset() {
  558.     if (phase == 2) {
  559.         for(int i = 0; i < (sizeof phase2_input / sizeof phase2_input[0]); i++){
  560.             phase2_input[i] = '\0';
  561.         }
  562.     }
  563.     else {
  564.         for(int i = 0; i < (sizeof phase3_input / sizeof phase3_input[0]); i++){
  565.             phase3_input[i] = '\0';
  566.         }
  567.     }
  568. }
  569.  
  570. int get_row(char key1, char key2) {
  571.             int row_calc = 0;
  572.             if (key1 == '1') {
  573.                 row_calc += 10;
  574.             }
  575.             row_calc += (key2 - 48);
  576.             return row_calc;
  577.         }
  578.  
  579. int get_col(char key3, char key4) {
  580.     int col_calc = 0;
  581.     if (key3 == '1') {
  582.         col_calc += 10;
  583.     }
  584.     col_calc += (key4 - 48);
  585.     return col_calc;
  586. }
  587. bool phase2_check_input() {
  588.     char key1 = phase2_input[0];
  589.     char key2 = phase2_input[1];
  590.     char key3 = phase2_input[2];
  591.     char key4 = phase2_input[3];
  592.     char key5 = phase2_input[4];
  593.     char key6 = phase2_input[5];
  594.    // char key7 = phase2_input[6];
  595.  
  596.     if ((key1 != 'A') && (key1 != 'B')) {
  597.         return false;
  598.     }
  599.     else {
  600.         if ((key2 - 48 < 2) || (key2 - 48 > 5)) {
  601.             return false;
  602.         }
  603.     }
  604.  
  605.     if ((key3 != '0') && (key3 != '1')) {
  606.         return false;
  607.     }
  608.     else if (key3 == '0') {
  609.         if ((key4 - 48 <= 0) || (key4 - 48 >= 10)) {
  610.             return false;
  611.         }
  612.     }
  613.     else if (key3 == '1') {
  614.         if ((key4 - 48 < 0) || (key4 - 48 > 6)) {
  615.             return false;
  616.         }
  617.     }
  618.  
  619.     if ((key5 != '0') && (key5 != '1')) {
  620.         return false;
  621.     }
  622.     else if (key5 == '0') {
  623.         if ((key6 - 48 <= 0) || (key6 - 48 >= 10)) {
  624.             return false;
  625.         }
  626.     }
  627.     else if (key5 == '1') {
  628.         if ((key6 - 48 < 0) || (key6 - 48 > 6)) {
  629.             return false;
  630.         }
  631.     }
  632.  
  633.   /*  if (key7 != '#') {
  634.         return false;
  635.     } */
  636.  
  637.     return true;
  638. }
  639.  
  640. void phase2_full_reset() {
  641.     reset_grid();
  642.     input_index = 0;
  643. //    display_phase2(0, 0);
  644.     for(int i = 0; i < (sizeof phase2_input / sizeof phase2_input[0]); i++) {
  645.         phase2_input[i] = '\0';
  646.     }
  647.     ship5_set = false;
  648.     ship4_set = false;
  649.     ship2_set = false;
  650.     ship3_1_set = false;
  651.     ship3_2_set = false;
  652.  
  653. }
  654.  
  655. bool phase2_conflicts() {           // Overlap & Limit-Over Check
  656.     char key1 = phase2_input[0];
  657.     char key2 = phase2_input[1];
  658.     char key3 = phase2_input[2];
  659.     char key4 = phase2_input[3];
  660.     char key5 = phase2_input[4];
  661.     char key6 = phase2_input[5];
  662.  
  663.     int row_calc = get_row(key3, key4) - 1;
  664.     int col_calc = get_col(key5, key6) - 1;
  665.  
  666.     if (key1 == 'A') {
  667.         if ((col_calc + (key2 - 48) - 1) >  16) {
  668.             return true;
  669.         }
  670.         for (int j = (col_calc); j < (col_calc + key2 - 48); j++) {
  671.             if (grid[row_calc][j] == 1) {
  672.                 return true;
  673.             }
  674.         }
  675.     }
  676.     else {
  677.         if ((row_calc + (key2 - 48) - 1) >  16) {
  678.             return true;
  679.         }
  680.         for (int i = (row_calc); i < (row_calc + key2 - 48); i++) {
  681.             if (grid[i][col_calc] == 1) {
  682.                 return true;
  683.             }
  684.         }
  685.     }
  686.  
  687.     return false;
  688. }
  689.  
  690. bool phase2_used_ships() {
  691.     if ((phase2_input[1] == '5') && (ship5_set == true)) {
  692.         return true;
  693.     }
  694.     else if ((phase2_input[1] == '4') && (ship4_set == true)) {
  695.         return true;
  696.     }
  697.     else if ((phase2_input[1] == '2') && (ship2_set == true)) {
  698.         return true;
  699.     }
  700.     else if ((phase2_input[1] == '3') && (ship3_1_set == true) &&
  701.             (ship3_2_set == true)){
  702.         return true;
  703.     }
  704.     return false;
  705. }
  706.  
  707. void phase2_set_ships() {
  708.     char key1 = phase2_input[0];
  709.     char key2 = phase2_input[1];
  710.     char key3 = phase2_input[2];
  711.     char key4 = phase2_input[3];
  712.     char key5 = phase2_input[4];
  713.     char key6 = phase2_input[5];
  714.  
  715.     char row_array[3] = {key3,key4,'\0'};
  716.     char col_array[3] = {key5,key6,'\0'};
  717.     int row_calc = atoi(row_array) - 1;
  718.     int col_calc = atoi(col_array) - 1;
  719.     //int row_calc = get_row(key3, key4);
  720.     //int col_calc = get_col(key5, key6);
  721.  
  722.     if (key1 == 'A') {
  723.         for (int j = col_calc; j < (col_calc + key2 - 48); j++) {
  724.             grid[row_calc][j] = 1;
  725. //            display_phase2(row_calc, j);
  726.         }
  727.     }
  728.     else {
  729.         for (int i = (row_calc); i < (row_calc + key2 - 48); i++) {
  730.             grid[i][col_calc] = 1;
  731. //            display_phase2(i, col_calc);
  732.         }
  733.     }
  734. }
  735.  
  736. void phase2(char key) {
  737.     if (key == 'C') {
  738.         input_index = 0;
  739.         input_reset();
  740.         return;
  741.     }
  742.     else if (key == 'D') {
  743.         phase2_full_reset();
  744.         return;
  745.     }
  746.  
  747.     if ((key == '*') || (key == '#')) {
  748.         return;
  749.     }
  750.  
  751.     if (input_index == 0) {
  752.         input_reset();
  753.         phase2_input[input_index] = key;
  754.     }
  755.     else if (input_index < 5) {
  756.         phase2_input[input_index] = key;
  757.     }
  758.     else {
  759.         phase2_input[input_index] = key;
  760.         if (phase2_check_input() == true) {
  761.             if ((phase2_conflicts() || phase2_used_ships()) == true) {
  762.                 input_reset();
  763.                 input_index = 0;
  764.                 return;
  765.             }
  766.  
  767.             if (phase2_input[1] == '5') {
  768.                 ship5_set = true;
  769.                 phase2_set_ships();
  770. //                pb9out();
  771.             }
  772.             else if (phase2_input[1] == '4') {
  773.                 ship4_set = true;
  774.                 phase2_set_ships();
  775. //                pb8out();
  776.             }
  777.             else if (phase2_input[1] == '3') {
  778.                 if (ship3_1_set == false) {
  779.                     ship3_1_set = true;
  780.                     phase2_set_ships();
  781.                     pb7out();
  782.                 }
  783.                 else {
  784.                     if (ship3_2_set == false) {
  785.                         ship3_2_set = true;
  786.                         phase2_set_ships();
  787.                         pb6out();
  788.                     }
  789.                     else {
  790.                         input_reset();
  791.                         return;
  792.                     }
  793.                 }
  794.             }
  795.             else {
  796.                 ship2_set = true;
  797.                 phase2_set_ships();
  798.                 pb5out();
  799.             }
  800.         }
  801.         else {
  802.             input_reset();
  803.             return;
  804.         }
  805.  
  806.         if ((ship5_set && ship4_set && ship3_1_set && ship3_2_set && ship2_set)
  807.                 == true) {
  808.             putchar_irq('O');
  809.             phase = 3;
  810.         }
  811.     }
  812.     input_index = (input_index + 1) % 6;
  813. }
  814.  
  815. bool phase3_check_input() {
  816. //    char key1 = phase3_input[0];
  817. //    char key2 = phase3_input[1];
  818. //    char key3 = phase3_input[2];
  819. //    char key4 = phase3_input[3];
  820. //    char key5 = phase3_input[4];
  821.     char key1 = coord[0];
  822.     char key2 = coord[1];
  823.     char key3 = coord[2];
  824.     char key4 = coord[3];
  825. //    char key5 = phase3_input[4];
  826.  
  827.     if ((key1 != '0') && (key1 != '1')) {
  828.         return false;
  829.     }
  830.     else if (key1 == '0') {
  831.         if ((key2 - 48 <= 0) || (key2 - 48 >= 10)) {
  832.             return false;
  833.         }
  834.     }
  835.     else if (key1 == '1') {
  836.         if ((key2 - 48 < 0) || (key2 - 48 > 6)) {
  837.             return false;
  838.         }
  839.     }
  840.  
  841.     if ((key3 != '0') && (key3 != '1')) {
  842.         return false;
  843.     }
  844.     else if (key3 == '0') {
  845.         if ((key4 - 48 <= 0) || (key4 - 48 >= 10)) {
  846.             return false;
  847.         }
  848.     }
  849.     else if (key3 == '1') {
  850.         if ((key4 - 48 < 0) || (key4 - 48 > 6)) {
  851.             return false;
  852.         }
  853.     }
  854.  
  855. //    if (key5 != '#') {
  856. //        return false;
  857. //    }
  858.  
  859.     return true;
  860. }
  861.  
  862.  
  863. void phase3_clear() {
  864.     t_i = 0;
  865.     for (int i = 0; i < 4; i++) {
  866.         coord[i] = '\0';
  867.     }
  868. }
  869.  
  870. void phase3(char key) {
  871.     if (hit_opp == 17) {                                    // WIN
  872.         win = true;
  873.         phase = 4;
  874.         return;
  875.     }
  876.     else if (opp_hit == 17) {                               // LOSE
  877.         phase = 4;
  878.         return;
  879.     }
  880.     char result = getchar_irq();
  881.     if ( result == 'L') {
  882.         win = true;
  883.         phase = 4;
  884.         return;
  885.     }
  886.     if (my_turn == 'Y') {
  887.         if ((key - 48 >= 0) && (key - 48 <= 9)) {
  888.             coord[t_i] = key;
  889.             t_i++;
  890.             if (t_i == 4) {
  891.                 if (phase3_check_input() == true) {
  892.                     for (int i = 0; i < 4; i++) {
  893.                         putchar_irq(coord[i]);
  894.                     }
  895.                     my_turn = 'W';
  896.                     pb7out();
  897.                 }
  898.                 else {
  899.                     phase3_clear();
  900.                 }
  901.             }
  902.             t_i = t_i % 4;
  903.         }
  904.         else if ((key == 'C')) {
  905.             phase3_clear();
  906.         }
  907.         else if (key == 'D') {
  908.             putchar_irq('L');
  909.             phase = 4;
  910.             return;
  911.         }
  912.     }
  913.     else if (my_turn == 'W') {
  914.         if (result == 'H') {
  915.             pc12out();
  916.             hit_opp++;
  917.             my_turn = 'N';
  918.         }
  919.         else if (result == 'M') {
  920.             pb3out();
  921.             my_turn = 'N';
  922.         }
  923.     }
  924.     else if (my_turn == 'N') {
  925.         if ((result - 48 >= 0) && (result - 48 <= 9)) {
  926.             coord[t_i] = result;
  927.             t_i = (t_i + 1) % 4;
  928.             if (t_i == 0) {
  929.                 char tempx[3];
  930.                 char tempy[3];
  931.                 tempx[2] = '\0';
  932.                 tempx[0] = coord[0];
  933.                 tempx[1] = coord[1];
  934.                 tempy[2] = '\0';
  935.                 tempy[0] = coord[2];
  936.                 tempy[1] = coord[3];
  937.                 int x = atoi(tempx);
  938.                 int y = atoi(tempy);
  939.                 if (grid[x-1][y-1] == 1) {
  940.                     grid[x-1][y-1] = 0;
  941.                     pc11out();
  942.                     putchar_irq('H');
  943.                     opp_hit++;
  944.                 }
  945.                 else {
  946.                     putchar_irq('M');
  947.                 }
  948.                 my_turn = 'Y';
  949.             }
  950.         }
  951.     }
  952. }
  953.  
  954. void TIM3_IRQHandler() {
  955.     char key = key_scan();
  956.     if (phase == 1) {
  957.         phase1(key);
  958.     }
  959.     else if (phase == 2) {
  960.         if (key != '\0') {
  961.             phase2(key);
  962.         }
  963.     }
  964.     else if (phase == 3) {
  965.         if (opponent_ready != 'Y') {
  966.             char ready = getchar_irq();
  967.             if (ready == 'O') {
  968.                 opponent_ready = 'Y';
  969.             }
  970.         }
  971.         else {
  972.             phase3(key);
  973.         }
  974.     }
  975.     else {
  976.         if (win == true) {
  977.         //      DISPLAY WIN
  978.             printf("win");
  979.             pb8out();
  980.         }
  981.         else {
  982.         //      DISPLAY LOSE
  983.             printf("lose");
  984.             pb9out();
  985.         }
  986.     }
  987.  
  988.     ack_tim3();
  989. }
  990.  
  991. void main() {
  992.     //printf("hhh");
  993.     init_keypad();
  994.     setup_timer3();
  995.     phase = 1;
  996. //    opponent_ready = 'Y';
  997.     tty_init();
  998.     enable_tty_irq();
  999.     //initilize_ships();
  1000.  
  1001.  
  1002.  
  1003.  
  1004.  
  1005.  
  1006.  
  1007.  
  1008.  
  1009.  
  1010.  
  1011.  
  1012. //    counter = 0;
  1013.   //  printf(getchar_irq());
  1014.  //   printf("hey2!");
  1015.  
  1016.        //insert_echo_char('h');
  1017.        //insert_echo_char('e');
  1018.        //insert_echo_char('l');
  1019.        //insert_echo_char('l');
  1020.        //insert_echo_char('o');
  1021. //    getchar_irq();
  1022. //    for (int i=0; i < 5; i++) {
  1023. //      putchar_irq(getchar_irq());
  1024. //    }
  1025. //    putchar_irq('g');
  1026.     for(;;)
  1027.      asm("wfi");
  1028.      //TEST DELETE WHEN RUNNING GAME
  1029.     // DISPLAY/INIT START SCREEN
  1030.     return;
  1031.  
  1032. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement