Advertisement
Guest User

Untitled

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