Advertisement
Guest User

Untitled

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