diff --git a/lab_2/lab_2.c b/lab_2/lab_2.c index 82af630..adc70b6 100644 --- a/lab_2/lab_2.c +++ b/lab_2/lab_2.c @@ -9,46 +9,57 @@ #include "pico/stdlib.h" #include "hardware/uart.h" -#define UART_ID uart0 -#define BAUD_RATE 115200 // Baud rate of UART +#define UART_ID uart0 +#define BAUD_RATE 115200 // Baud rate of UART #define UART_TX_PIN 16 // GPIO for UART TX pin #define UART_RX_PIN 17 // GPIO for UART RX pin -#define PSEUDO_BTN 15 // GPIO for pseudo button +#define PSEUDO_BTN 15 // GPIO for pseudo button -#define SLEEP_TIME 1000 // For testing can use a smaller value - // 1000ms is based on the lab requirements +#define SLEEP_TIME 1000 // 1000ms is based on the lab requirements /** * Sends a character through UART0 based on the state of GPIO15 (Pseudo button) - * @param uart_input Pointer to a character to be sent through UART0 (Space optimisation) + * @param uart_input Pointer to a character to be sent through UART0 * @return void Prints the character in uart_receive() */ -static void uart_transmit(char *uart_input) { - if (gpio_get(PSEUDO_BTN)) { // If GPIO15 is high +static void uart_transmit(u_char *uart_input) { + if (gpio_get(PSEUDO_BTN)) + { uart_putc(UART_ID, '1'); - } else { // If GPIO15 is low + } + else + { uart_putc(UART_ID, *uart_input); *uart_input = (*uart_input - 'A' + 1) % 26 + 'A'; // Next character and wrap around back to A } + } /** * Receives a character through UART0 and prints it out * NOTE: Flipping bit 5 effectively converts a character to lowercase - * @param uart_output Pointer to a location to store the received character (Space optimisation) + * EXAMPLE: 'A' = 0b01000001, 'a' = 0b01100001 + * @param uart_output Pointer to a location to store the received character * @return void Character is read and printed */ -static void uart_receive(char *uart_output) { - if (uart_is_readable(UART_ID)) { +static void uart_receive(u_char *uart_output) { + if (uart_is_readable(UART_ID)) + { *uart_output = uart_getc(UART_ID); - if (*uart_output >= 'A' && *uart_output <= 'Z') { - *uart_output ^= (1 << 5); // XOR with 32 to convert to lowercase - // This operation flips bit 5 - } else if (*uart_output == '1') { - *uart_output = '2'; // If '1' is received, print '2' + if ((*uart_output >= 'A') && (*uart_output <= 'Z')) + { + *uart_output ^= (1 << 5); // This operation flips bit 5 + } + else if (*uart_output == '1') + { + *uart_output = '2'; + } + else + { + return; } printf("%c\n", *uart_output); @@ -77,20 +88,20 @@ static void pin_init() { } int main() { + // Initialize stdio and pins first + stdio_init_all(); pin_init(); - u_char uart_input = 'A'; // Initial value of A - u_char uart_output; // To store received character + u_char uart_input = 'A'; + u_char uart_output; - while (true) { - uart_transmit(&uart_input); // Pass by reference to change - // value of uart_input during transmission + while (1) { + uart_transmit(&uart_input); - uart_receive(&uart_output); // Pass by reference to change - // value of uart_output during reception + uart_receive(&uart_output); - sleep_ms(SLEEP_TIME); // Sleep based on SLEEP_TIME + sleep_ms(SLEEP_TIME); } }