refactor(Code Standard):

Modified code according to standards
This commit is contained in:
Devoalda 2023-09-06 21:31:16 +08:00
parent 59fa7829da
commit b90fe403f5
1 changed files with 36 additions and 25 deletions

View File

@ -16,39 +16,50 @@
#define UART_RX_PIN 17 // GPIO for UART RX 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 #define SLEEP_TIME 1000 // 1000ms is based on the lab requirements
// 1000ms is based on the lab requirements
/** /**
* Sends a character through UART0 based on the state of GPIO15 (Pseudo button) * 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() * @return void Prints the character in uart_receive()
*/ */
static void uart_transmit(char *uart_input) { static void uart_transmit(u_char *uart_input) {
if (gpio_get(PSEUDO_BTN)) { // If GPIO15 is high if (gpio_get(PSEUDO_BTN))
{
uart_putc(UART_ID, '1'); uart_putc(UART_ID, '1');
} else { // If GPIO15 is low }
else
{
uart_putc(UART_ID, *uart_input); uart_putc(UART_ID, *uart_input);
*uart_input = (*uart_input - 'A' + 1) % 26 + 'A'; // Next character and wrap around back to A *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 * Receives a character through UART0 and prints it out
* NOTE: Flipping bit 5 effectively converts a character to lowercase * 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 * @return void Character is read and printed
*/ */
static void uart_receive(char *uart_output) { static void uart_receive(u_char *uart_output) {
if (uart_is_readable(UART_ID)) { if (uart_is_readable(UART_ID))
{
*uart_output = uart_getc(UART_ID); *uart_output = uart_getc(UART_ID);
if (*uart_output >= 'A' && *uart_output <= 'Z') { if ((*uart_output >= 'A') && (*uart_output <= 'Z'))
*uart_output ^= (1 << 5); // XOR with 32 to convert to lowercase {
// This operation flips bit 5 *uart_output ^= (1 << 5); // This operation flips bit 5
} else if (*uart_output == '1') { }
*uart_output = '2'; // If '1' is received, print '2' else if (*uart_output == '1')
{
*uart_output = '2';
}
else
{
return;
} }
printf("%c\n", *uart_output); printf("%c\n", *uart_output);
@ -77,20 +88,20 @@ static void pin_init() {
} }
int main() { int main() {
// Initialize stdio and pins first
stdio_init_all(); stdio_init_all();
pin_init(); pin_init();
u_char uart_input = 'A'; // Initial value of A u_char uart_input = 'A';
u_char uart_output; // To store received character u_char uart_output;
while (true) { while (1) {
uart_transmit(&uart_input); // Pass by reference to change uart_transmit(&uart_input);
// value of uart_input during transmission
uart_receive(&uart_output); // Pass by reference to change uart_receive(&uart_output);
// value of uart_output during reception
sleep_ms(SLEEP_TIME); // Sleep based on SLEEP_TIME sleep_ms(SLEEP_TIME);
} }
} }