Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f0xx.h"
- #include "stm32f0_discovery.h"
- #include <stdlib.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdarg.h>
- #include "fifo.h"
- #define UNK -1
- #define NON_INTR 0
- #define INTR 1
- int __io_putchar(int ch);
- static int putchar_nonirq(int ch);
- void step3(void);
- void step4(void);
- void step5(void);
- #define COL1 (1 << 0)
- #define COL2 (1 << 1)
- #define COL3 (1 << 2)
- #define COL4 (1 << 3)
- #define ROW1 (1 << 4)
- #define ROW2 (1 << 5)
- #define ROW3 (1 << 6)
- #define ROW4 (1 << 7)
- #define SAMPLE_TIME_MS 10
- #define SAMPLE_COUNT (SAMPLE_TIME_MS)
- #define THRESHOLD_TIME_MS 2
- #define THRESHOLD (THRESHOLD_TIME_MS)
- #define KEY_PRESS_MASK 0b11000111
- #define KEY_REL_MASK 0b11100011
- static struct fifo input_fifo; // input buffer
- static struct fifo output_fifo; // output buffer
- int interrupt_mode = UNK; // which version of putchar/getchar to use.
- int echo_mode = 0; // should we echo input characters?
- int ships[16][16];
- int player_1 = 1;
- char input[5];
- char X[3];
- char Y[3];
- int counter =0;
- //=============================================================================
- //=======================================================================
- // Simply write a string one char at a time.
- //=======================================================================
- static void putstr(const char *s) {
- while(*s)
- __io_putchar(*s++);
- }
- //=======================================================================
- // Insert a character and echo it.
- // (or, if it's a backspace, remove a char and erase it from the line).
- // If echo_mode is turned off, just insert the character and get out.
- //=======================================================================
- static void insert_echo_char(char ch) {
- if (ch == '\r')
- ch = '\n';
- if (!echo_mode) {
- fifo_insert(&input_fifo, ch);
- return;
- }
- if (ch == '\b' || ch == '\177') {
- if (!fifo_empty(&input_fifo)) {
- char tmp = fifo_uninsert(&input_fifo);
- if (tmp == '\n')
- fifo_insert(&input_fifo, '\n');
- else if (tmp < 32)
- putstr("\b\b \b\b");
- else
- putstr("\b \b");
- }
- return; // Don't put a backspace into buffer.
- } else if (ch == '\n') {
- __io_putchar('\n');
- } else if (ch == 0){
- putstr("^0");
- } else if (ch == 28) {
- putstr("^\\");
- } else if (ch < 32) {
- __io_putchar('^');
- __io_putchar('A'-1+ch);
- } else {
- __io_putchar(ch);
- }
- fifo_insert(&input_fifo, ch);
- }
- //-----------------------------------------------------------------------------
- // Section 6.2
- //-----------------------------------------------------------------------------
- // This should should perform the following
- // 1) Enable clock to GPIO port A
- // 2) Configure PA9 and PA10 to alternate function to use a USART
- // Note: Configure both MODER and AFRL registers
- // 3) Enable clock to the USART module, it is up to you to determine
- // which RCC register to use
- // 4) Disable the USART module (hint UE bit in CR1)
- // 5) Configure USART for 8 bits, 1 stop bit and no parity bit
- // 6) Use 16x oversampling
- // 7) Configure for 115200 baud rate
- // 8) Enable the USART for both transmit and receive
- // 9) Enable the USART
- // 10) Wait for TEACK and REACK to be set by hardware in the ISR register
- // 11) Set the 'interrupt_mode' variable to NON_INTR
- void tty_init(void) {
- // Disable buffers for stdio streams. Otherwise, the first use of
- // each stream will result in a *malloc* of 2K. Not good.
- setbuf(stdin,0);
- setbuf(stdout,0);
- setbuf(stderr,0);
- RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
- GPIOA->MODER |= GPIO_MODER_MODER9_1;
- GPIOA->MODER &= ~GPIO_MODER_MODER9_0;
- GPIOA->MODER |= GPIO_MODER_MODER10_1;
- GPIOA->MODER &= ~GPIO_MODER_MODER10_0;
- GPIOA->AFR[1] |= 0x00000110;
- RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
- USART1->CR1 &= ~(USART_CR1_UE);
- USART1->CR1 &= ~(USART_CR1_OVER8);
- USART1->CR2 &= ~(USART_CR2_STOP);
- USART1->CR1 &= ~(USART_CR1_M);
- USART1->CR1 &= ~(USART_CR1_PCE);
- USART1->BRR = 417;
- USART1->CR1 |= USART_CR1_TE;
- USART1->CR1 |= USART_CR1_RE;
- USART1->CR1 |= (USART_CR1_UE);
- while (!(((USART1->ISR & USART_ISR_REACK) == USART_ISR_REACK) && ((USART1->ISR & USART_ISR_TEACK) == USART_ISR_TEACK)));
- interrupt_mode = NON_INTR;
- // Student code goes here...
- }
- //=======================================================================
- // Enable the USART RXNE interrupt.
- // Remember to enable the right bit in the NVIC registers
- //=======================================================================
- void enable_tty_irq(void) {
- // Student code goes here...
- USART1->CR1 |= USART_CR1_RXNEIE;
- NVIC->ISER[0] |= 1 << USART1_IRQn;
- interrupt_mode = INTR;
- }
- //-----------------------------------------------------------------------------
- // Section 6.5
- //-----------------------------------------------------------------------------
- // See lab document for description
- //=======================================================================
- // IRQ invoked for USART1 activity.
- void USART1_IRQHandler(void) {
- // Student code goes here...
- if((USART1->ISR & USART_ISR_RXNE) == USART_ISR_RXNE) {
- insert_echo_char(USART1->RDR & 0xff);
- }
- if((USART1->ISR & USART_ISR_TXE) == USART_ISR_TXE) {
- if (fifo_empty(&output_fifo)) {
- USART1->CR1 &= ~(USART_CR1_TXEIE);
- }
- else {
- USART1->TDR = fifo_remove(&output_fifo);
- }
- }
- //-----------------------------------------------------------------
- // Leave this checking code here to make sure nothing bad happens.
- if (USART1->ISR & (USART_ISR_RXNE|
- USART_ISR_ORE|USART_ISR_NE|USART_ISR_FE|USART_ISR_PE)) {
- // safe_printf("Problem in USART1_IRQHandler: ISR = 0x%x\n", USART1->ISR);
- }
- }
- // See lab document for description
- static int getchar_irq(void) {
- // Student code goes here...
- // while(!fifo_newline(&input_fifo)) {
- // asm("wfi");
- // }
- return fifo_remove(&input_fifo);
- }
- // See lab document for description
- static int putchar_irq(char ch) {
- // Student code goes here...
- while (fifo_full(&output_fifo)) {
- asm("wfi");
- }
- if (ch == 10) {
- fifo_insert(&output_fifo, '\r');
- }
- else {
- fifo_insert(&output_fifo, ch);
- }
- if ((USART1->CR1 & USART_CR1_TXEIE) != USART_CR1_TXEIE) {
- USART1->CR1 |= USART_CR1_TXEIE;
- USART1_IRQHandler();
- }
- if (ch == 10) {
- while (fifo_full(&output_fifo)) {
- asm("wfi");
- }
- fifo_insert(&output_fifo, '\n');
- }
- }
- //=======================================================================
- // Called by the Standard Peripheral library for a write()
- int __io_putchar(int ch) {
- if (interrupt_mode == INTR)
- return putchar_irq(ch);
- }
- //=======================================================================
- // Called by the Standard Peripheral library for a read()
- int __io_getchar(void) {
- // Choose the right implementation.
- if (interrupt_mode == INTR)
- return getchar_irq();
- }
- void initilize_ships() {
- for (int i =0; i < 16; i++) {
- for (int j =0; j < 16; j++) {
- ships[i][j] = 0;
- }
- }
- for (int i =0; i <2; i++) {
- ships[0][i] = 1;
- }
- for (int i =0; i <3; i++) {
- ships[i+10][5] = 1;
- }
- for (int i =0; i <4; i++) {
- ships[i][9] = 1;
- ships[i][14] = 1;
- }
- for (int i =0; i <5; i++) {
- ships[3][i+2] = 1;
- }
- return;
- }
- //void TIM3_IRQHandler() {
- //}
- /*void game() {
- if (player_1 == 1) { //this means stm 1 is going to be the first one going
- for (int i=0; i < 4;i++) {
- }
- }
- }
- */
- char win = 'N';
- int opp_hit = 0;
- int hit_opp = 0;
- int phase = 1;
- char player = '\0';
- char my_turn = '\0';
- char opponent_ready = 'N';
- char phase2_input[7] = {'\0', '\0', '\0', '\0', '\0', '\0', '\0'};
- char phase3_input[5] = {'\0', '\0', '\0', '\0', '\0'};
- bool ship5_set = false;
- bool ship4_set = false;
- bool ship2_set = false;
- bool ship3_1_set = false;
- bool ship3_2_set = false;
- int input_index = 0;
- int grid[16][16];
- int col = 0;
- int row = -1;
- char char_array[4][4] = { {'1', '2', '3', 'A'},
- {'4', '5', '6', 'B'},
- {'7', '8', '9', 'C'},
- {'*', '0', '#', 'D'} };
- int int_array[4][4] = { {1,2,3,0xa},
- {4,5,6,0xb},
- {7,8,9,0xc},
- {0xf, 0, 0xe, 0xd} };
- uint8_t key_samples[4][4] = { {0}, {0}, {0}, {0} };
- uint8_t key_pressed[4][4] = { {0}, {0}, {0}, {0} };
- uint8_t key_released[4][4] = { {0}, {0}, {0}, {0} };
- void pb8out(void) {
- RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
- GPIOB->MODER &= ~(GPIO_MODER_MODER8);
- GPIOB->MODER |= GPIO_MODER_MODER8_0;
- if ((GPIOB->ODR & GPIO_ODR_8) == 0) {
- GPIOB->ODR |= GPIO_ODR_8;
- }
- else {
- GPIOB->BSRR |= GPIO_BSRR_BR_8;
- }
- }
- void pb7out(void) {
- RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
- GPIOB->MODER &= ~(GPIO_MODER_MODER7);
- GPIOB->MODER |= GPIO_MODER_MODER7_0;
- if ((GPIOB->ODR & GPIO_ODR_7) == 0) {
- GPIOB->ODR |= GPIO_ODR_7;
- }
- else {
- GPIOB->BSRR |= GPIO_BSRR_BR_7;
- }
- }
- void pb6out(void) {
- RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
- GPIOB->MODER &= ~(GPIO_MODER_MODER6);
- GPIOB->MODER |= GPIO_MODER_MODER6_0;
- if ((GPIOB->ODR & GPIO_ODR_6) == 0) {
- GPIOB->ODR |= GPIO_ODR_6;
- }
- else {
- GPIOB->BSRR |= GPIO_BSRR_BR_6;
- }
- }
- void pb5out(void) {
- RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
- GPIOB->MODER &= ~(GPIO_MODER_MODER5);
- GPIOB->MODER |= GPIO_MODER_MODER5_0;
- if ((GPIOB->ODR & GPIO_ODR_5) == 0) {
- GPIOB->ODR |= GPIO_ODR_5;
- }
- else {
- GPIOB->BSRR |= GPIO_BSRR_BR_5;
- }
- }
- void pb4out(void) {
- RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
- GPIOB->MODER &= ~(GPIO_MODER_MODER4);
- GPIOB->MODER |= GPIO_MODER_MODER4_0;
- if ((GPIOB->ODR & GPIO_ODR_4) == 0) {
- GPIOB->ODR |= GPIO_ODR_4;
- }
- else {
- GPIOB->BSRR |= GPIO_BSRR_BR_4;
- }
- }
- void pb3out(void) {
- RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
- GPIOB->MODER &= ~(GPIO_MODER_MODER3);
- GPIOB->MODER |= GPIO_MODER_MODER3_0;
- if ((GPIOB->ODR & GPIO_ODR_3) == 0) {
- GPIOB->ODR |= GPIO_ODR_3;
- }
- else {
- GPIOB->BSRR |= GPIO_BSRR_BR_3;
- }
- }
- void pc12out(void) {
- RCC->AHBENR |= RCC_AHBENR_GPIOCEN;
- GPIOC->MODER &= ~(GPIO_MODER_MODER12);
- GPIOC->MODER |= GPIO_MODER_MODER12_0;
- if ((GPIOC->ODR & GPIO_ODR_12) == 0) {
- GPIOC->ODR |= GPIO_ODR_12;
- }
- else {
- GPIOC->BSRR |= GPIO_BSRR_BR_12;
- }
- }
- void pc11out(void) {
- RCC->AHBENR |= RCC_AHBENR_GPIOCEN;
- GPIOC->MODER &= ~(GPIO_MODER_MODER11);
- GPIOC->MODER |= GPIO_MODER_MODER11_0;
- if ((GPIOC->ODR & GPIO_ODR_11) == 0) {
- GPIOC->ODR |= GPIO_ODR_11;
- }
- else {
- GPIOC->BSRR |= GPIO_BSRR_BR_11;
- }
- }
- void update_key_press() {
- for(int i = 0; i < 4; i++) {
- for(int j = 0; j < 4; j++) {
- if ((key_samples[i][j] & KEY_PRESS_MASK) == 0b00000111) {
- key_pressed[i][j] = 1;
- key_samples[i][j] = 0xFF;
- }
- if ((key_samples[i][j] & KEY_REL_MASK) == 0b11100000) {
- key_released[i][j] = 1;
- key_samples[i][j] = 0x00;
- }
- }
- }
- }
- char get_char_key() {
- for(int i = 0; i < 4; i++) {
- for(int j = 0; j < 4; j++) {
- if(key_released[i][j] == 1 && key_pressed[i][j] == 1) {
- key_released[i][j] = 0;
- key_pressed[i][j] = 0;
- return char_array[i][j];
- }
- }
- }
- return '\0';
- }
- int get_key_pressed() {
- for(int i = 0; i < 4; i++) {
- for(int j = 0; j < 4; j++) {
- if(key_released[i][j] == 1 && key_pressed[i][j] == 1) {
- key_released[i][j] = 0;
- key_pressed[i][j] = 0;
- return int_array[i][j];
- }
- }
- }
- return -1;
- }
- void update_samples(int row) {
- // Update the current column with new values
- for(int i = 0; i < 4; i++) {
- uint8_t curr_history = key_samples[i][col];
- curr_history = curr_history << 1;
- if(row == i)
- curr_history |= 1;
- key_samples[i][col] = curr_history;
- }
- }
- void init_keypad() {
- RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
- GPIOA->MODER &= ~(GPIO_MODER_MODER0 | GPIO_MODER_MODER1 |
- GPIO_MODER_MODER2 | GPIO_MODER_MODER3 |
- GPIO_MODER_MODER6 | GPIO_MODER_MODER7 |
- GPIO_MODER_MODER8 | GPIO_MODER_MODER9);
- // Columns set to output:
- GPIOA->MODER |= GPIO_MODER_MODER0_0 | GPIO_MODER_MODER1_0 |
- GPIO_MODER_MODER2_0 | GPIO_MODER_MODER3_0;
- // Pull-down resistors for rows:
- GPIOA->PUPDR &= ~(GPIO_PUPDR_PUPDR4 | GPIO_PUPDR_PUPDR5 |
- GPIO_PUPDR_PUPDR6 | GPIO_PUPDR_PUPDR7);
- GPIOA->PUPDR |= GPIO_PUPDR_PUPDR4_1 | GPIO_PUPDR_PUPDR5_1 |
- GPIO_PUPDR_PUPDR6_1 | GPIO_PUPDR_PUPDR7_1;
- }
- void setup_timer3() {
- RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
- TIM3->PSC = 480 - 1;
- TIM3->ARR = 100 - 1;
- TIM3->DIER |= 0x1;
- NVIC -> ISER[0] = 1 << TIM3_IRQn;
- TIM3->CR1 |= TIM_CR1_CEN;
- }
- char key_scan() {
- int current_row = -1;
- if (((GPIOA->IDR >> 4) & 0x1) == 0x1) {
- current_row = 0;
- }
- else if (((GPIOA->IDR >> 5) & 0x1) == 0x1) {
- current_row = 1;
- }
- else if (((GPIOA->IDR >> 6) & 0x1) == 0x1) {
- current_row = 2;
- }
- else if (((GPIOA->IDR >> 7) & 0x1) == 0x1) {
- current_row = 3;
- }
- update_samples(current_row);
- update_key_press();
- col = (col + 1) % 4;
- GPIOA->ODR = 1<<col;
- return get_char_key();
- }
- void ack_tim3() {
- if ((TIM3->SR & TIM_SR_UIF) != 0) {
- TIM3->SR &= ~TIM_SR_UIF;
- }
- }
- void reset_grid() {
- for (int i = 0; i < 16; i++) {
- for (int j = 0; j < 16; j++) {
- grid[i][j] = 0;
- }
- }
- }
- void phase1(char key) {
- if (key == '*') {
- my_turn = 'Y';
- phase = 2;
- reset_grid();
- // TRANSMIT player='2' / my_turn ='N' to other STM
- }
- else if (my_turn == 'N') {
- phase = 2;
- reset_grid();
- }
- }
- void input_reset() {
- if (phase == 2) {
- for(int i = 0; i < (sizeof phase2_input / sizeof phase2_input[0]); i++){
- phase2_input[i] = '\0';
- }
- }
- else {
- for(int i = 0; i < (sizeof phase3_input / sizeof phase3_input[0]); i++){
- phase3_input[i] = '\0';
- }
- }
- }
- bool phase2_check_input() {
- char key1 = phase2_input[0];
- char key2 = phase2_input[1];
- char key3 = phase2_input[2];
- char key4 = phase2_input[3];
- char key5 = phase2_input[4];
- char key6 = phase2_input[5];
- char key7 = phase2_input[6];
- if ((key1 != 'A') && (key1 != 'B')) {
- return false;
- }
- else {
- if ((key2 - 48 < 2) || (key2 - 48 > 5)) {
- return false;
- }
- }
- if ((key3 != '0') && (key3 != '1')) {
- return false;
- }
- else if (key3 == '0') {
- if ((key4 - 48 <= 0) || (key4 - 48 >= 10)) {
- return false;
- }
- }
- else if (key3 == '1') {
- if ((key4 - 48 < 0) || (key4 - 48 > 6)) {
- return false;
- }
- }
- if ((key5 != '0') && (key5 != '1')) {
- return false;
- }
- else if (key5 == '0') {
- if ((key6 - 48 <= 0) || (key6 - 48 >= 10)) {
- return false;
- }
- }
- else if (key5 == '1') {
- if ((key6 - 48 < 0) || (key6 - 48 > 6)) {
- return false;
- }
- }
- if (key7 != '#') {
- return false;
- }
- return true;
- }
- void phase2_full_reset() {
- reset_grid();
- for(int i = 0; i < (sizeof phase2_input / sizeof phase2_input[0]); i++) {
- phase2_input[i] = '\0';
- }
- ship5_set = false;
- ship4_set = false;
- ship2_set = false;
- ship3_1_set = false;
- ship3_2_set = false;
- input_index = 0;
- }
- bool phase2_conflicts() {
- return false;
- }
- bool phase2_used_ships() {
- if ((phase2_input[1] == '5') && (ship5_set == true)) {
- return true;
- }
- else if ((phase2_input[1] == '4') && (ship4_set == true)) {
- return true;
- }
- else if ((phase2_input[1] == '2') && (ship2_set == true)) {
- return true;
- }
- else if ((phase2_input[1] == '3') && (ship3_1_set == true) &&
- (ship3_2_set == true)){
- return true;
- }
- return false;
- }
- void phase2(char key) {
- if (key == 'C') {
- input_reset();
- return;
- }
- else if (key == 'D') {
- phase2_full_reset();
- return;
- }
- if (input_index == 0) {
- input_reset();
- phase2_input[input_index] = key;
- }
- else if (input_index < 6) {
- phase2_input[input_index] = key;
- }
- else {
- phase2_input[input_index] = key;
- if (phase2_check_input() == true) {
- if ((phase2_conflicts() || phase2_used_ships()) == true) {
- input_reset();
- return;
- }
- if (phase2_input[1] == '5') {
- ship5_set = true;
- // Update Array
- // DISPLAY to 1st screen
- }
- else if (phase2_input[1] == '4') {
- ship4_set = true;
- //Update Array
- // DISPLAY to 1st screen
- }
- else if (phase2_input[1] == '3') {
- if (ship3_1_set == false) {
- ship3_1_set = true;
- //Update Array
- // DISPLAY to 1st screen
- }
- else {
- if (ship3_2_set == false) {
- ship3_2_set = true;
- //Update Array
- // DISPLAY to 1st screen
- }
- else {
- input_reset();
- return;
- }
- }
- }
- else {
- ship2_set = true;
- //Update Array
- // DISPLAY to 1st screen
- }
- }
- else {
- input_reset();
- return;
- }
- if ((ship5_set && ship4_set && ship3_1_set && ship3_2_set && ship2_set)
- == true) {
- // TRANSMIT READY
- phase = 3;
- }
- }
- input_index = (input_index + 1) % 7;
- }
- bool phase3_check_input() {
- char key1 = phase3_input[0];
- char key2 = phase3_input[1];
- char key3 = phase3_input[2];
- char key4 = phase3_input[3];
- char key5 = phase3_input[4];
- if ((key1 != '0') && (key1 != '1')) {
- return false;
- }
- else if (key1 == '0') {
- if ((key2 - 48 <= 0) || (key2 - 48 >= 10)) {
- return false;
- }
- }
- else if (key1 == '1') {
- if ((key2 - 48 < 0) || (key2 - 48 > 6)) {
- return false;
- }
- }
- if ((key3 != '0') && (key3 != '1')) {
- return false;
- }
- else if (key3 == '0') {
- if ((key4 - 48 <= 0) || (key4 - 48 >= 10)) {
- return false;
- }
- }
- else if (key3 == '1') {
- if ((key4 - 48 < 0) || (key4 - 48 > 6)) {
- return false;
- }
- }
- if (key5 != '#') {
- return false;
- }
- return true;
- }
- int counter2 =0;
- void phase3_attack(char key) {
- //printf("%d",counter2);
- // printf("hey!");
- if (key == 'C') {
- input_reset();
- return;
- }
- else if (key == 'D') {
- // SURRENDER: I LOSE
- }
- if (input_index == 0) {
- input_reset();
- phase3_input[input_index] = key;
- putchar_irq(phase3_input[input_index]);
- }
- else if (input_index < 4) {
- phase3_input[input_index] = key;
- putchar_irq(phase3_input[input_index]);
- }
- else {
- //printf("hello");
- // printf("%s",phase3_input[2]);
- phase3_input[input_index] = key;
- if (phase3_input[input_index] == '#') { // FOR EDGE CASE CHECKS, COME BACK LATER!!!
- /* for (int i =0; i < 4; i++) {
- putchar_irq(phase3_input[i]);
- } */
- // counter2++;
- //printf("hello");
- char result = getchar_irq();
- // printf("%s",result);
- while (result != 'H' || 'M') {
- result = getchar_irq();
- }
- if (result == 'H') {
- hit_opp++;
- // DISPLAY result 2nd screen *********
- }
- // TRANSMIT coordinates (phase_input3 string contains valid coords)
- // RECEIVE HIT or MISS (Did you hit or miss)
- // if HIT
- // hit_opp++;
- // DISPLAY result 2nd screen
- if (hit_opp == 17) {
- phase = 4;
- win = 'Y';
- }
- my_turn = 'N';
- }
- else {
- input_reset();
- input_index = 0;
- return;
- }
- }
- input_index = (input_index + 1) % 5;
- }
- int count=0;
- void TIM3_IRQHandler() {
- char key = key_scan();
- char input;
- if (phase == 0) {
- // printf("hi");
- char result = 'n';
- result = getchar_irq();
- if (result == 'a') {
- printf("%c",result);
- if (result == 'a') {
- pc12out();
- }
- }
- }
- //printf("Hello");
- if (phase == 1) {
- phase1(key);
- }
- else if (phase == 2) {
- if (key != '\0') {
- phase2(key);
- }
- }
- else if (phase == 3) {
- if (opponent_ready == 'Y') {
- if (my_turn == 'Y') {
- if (key != '\0')
- phase3_attack(key);
- }
- else if (my_turn == 'N') {
- // printf("hello" );
- /* while(counter < 4) {
- printf("hello2" );
- if((USART1->ISR & USART_ISR_RXNE) == USART_ISR_RXNE) {
- insert_echo_char(USART1->RDR & 0xff);
- counter++;
- }
- } */
- // if (counter == 4) {
- //printf("Hello1");
- input = getchar_irq();
- printf(input);
- if (input >= 48 && input <= 57) {
- if (count < 2) {
- X[count] = input;
- }
- else {
- Y[count-2] = input;
- }
- count++;
- count = count % 4;
- X[2] = '\0';
- Y[2] = '\0';
- printf("%c",X[1]);
- printf("%c",Y[1]);
- int X_coordinates = atoi(X);
- int Y_coordinates = atoi(Y);
- //printf("%d",X_coordinates);
- //printf("%d",Y_coordinates);
- counter = 0;
- if (ships[X_coordinates][Y_coordinates] == 1) {
- putchar_irq('H');
- ships[X_coordinates][Y_coordinates] = 0;
- opp_hit++;
- }
- else {
- putchar_irq('M');
- }
- // if RECEIVED coordinates
- // TRANSMIT HIT OR MISS (Did opponent hit or miss)
- // if HIT
- // opp_hit++;
- // UPDATE "your" screen
- if (opp_hit == 17) {
- phase = 4;
- }
- my_turn = 'Y';
- }
- }
- //}
- }
- }
- else {
- if (win == 'Y') {
- // DISPLAY WIN
- }
- else {
- // DISPLAY LOSE
- }
- }
- ack_tim3();
- }
- bool phase3_got_hit() {
- char key1 = phase3_input[0];
- char key2 = phase3_input[1];
- char key3 = phase3_input[2];
- char key4 = phase3_input[3];
- int row_calc = 0;
- int col_calc = 0;
- if (key1 == '1') {
- row_calc += 10;
- }
- row_calc += (key2 - 48);
- if (key3 == '1') {
- col_calc += 10;
- }
- col_calc += (key4 - 48);
- if (grid[row_calc][col_calc] == 1) {
- return true;
- }
- return false;
- }
- void main() {
- init_keypad();
- setup_timer3();
- phase = 3;
- opponent_ready = 'Y';
- my_turn = 'N';
- tty_init();
- enable_tty_irq();
- counter = 0;
- // printf(getchar_irq());
- // printf("hey2!");
- //insert_echo_char('h');
- //insert_echo_char('e');
- //insert_echo_char('l');
- //insert_echo_char('l');
- //insert_echo_char('o');
- // getchar_irq();
- // for (int i=0; i < 5; i++) {
- // putchar_irq(getchar_irq());
- // }
- // putchar_irq('g');
- for(;;)
- asm("wfi");
- //TEST DELETE WHEN RUNNING GAME
- // DISPLAY/INIT START SCREEN
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement