From 5a2963d3ba84a68478d34f6e1efd9d1e8fac8123 Mon Sep 17 00:00:00 2001 From: Maximilian Friedersdorff Date: Sun, 31 Oct 2021 16:36:05 +0000 Subject: [PATCH] Way simplify logic --- CMakeLists.txt | 2 -- pdu.c | 88 +++++++++++++++++++------------------------------- uart.c | 36 --------------------- uart.h | 11 ------- 4 files changed, 33 insertions(+), 104 deletions(-) delete mode 100644 uart.c delete mode 100644 uart.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c82e70..0720ff5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,8 +9,6 @@ pico_sdk_init() add_executable(pdu pdu.c - uart.h - uart.c ) target_link_libraries(pdu pico_stdlib) diff --git a/pdu.c b/pdu.c index 4e33d18..e95a0ad 100644 --- a/pdu.c +++ b/pdu.c @@ -1,7 +1,6 @@ #include #include -#include "uart.h" #include "pico/stdlib.h" #include "hardware/uart.h" @@ -13,44 +12,13 @@ #define UART_TX_PIN 16 #define UART_RX_PIN 17 -#define MAX_COMMAND_LENGTH 32 - #define PINA 6 #define PINB 7 #define PINC 8 #define PIND 9 -void set_power_state(char relay, bool on) { - uint state; - if (on) - state = 0; - else - state = 1; - - uint pin; - switch (relay) { - case 'a' : - pin = PINA; - break; - case 'b' : - pin = PINB; - break; - case 'c' : - pin = PINC; - break; - case 'd' : - pin = PIND; - break; - } - - gpio_put(pin, state); -} - int main() { - char command_string[MAX_COMMAND_LENGTH + 1]; - command_string[0] = 0; - uint command_len = 0; uart_init(UART_ID, 9600); gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART); @@ -74,30 +42,40 @@ int main() { gpio_put(PINC, 1); gpio_put(PIND, 1); - bool eol; - char word[9]; - char relay; - while (true) { - read_word(UART_ID, word, 9, &eol); - if (strcmp(word, "on") == 0) { - if (eol) { - uart_puts(UART_ID, "Missing argument: n"); - continue; - } - relay = uart_getc(UART_ID); - finish_line(UART_ID); - set_power_state(relay, true); - } else if (strcmp(word, "off") == 0) { - if (eol) { - uart_puts(UART_ID, "Missing argument: n"); - continue; - } - relay = uart_getc(UART_ID); - finish_line(UART_ID); - set_power_state(relay, false); - } else if (strcmp(word, "temps") == 0) { - uart_puts(UART_ID, "Not implemented yet"); + char last_char; + while (true) { + last_char = uart_getc(UART_ID); + switch (last_char) { + case 'a' : + gpio_put(PINA, 1); + break; + case 'b' : + gpio_put(PINB, 1); + break; + case 'c' : + gpio_put(PINC, 1); + break; + case 'd' : + gpio_put(PIND, 1); + break; + case 'A' : + gpio_put(PINA, 0); + break; + case 'B' : + gpio_put(PINB, 0); + break; + case 'C' : + gpio_put(PINC, 0); + break; + case 'D' : + gpio_put(PIND, 0); + break; + case 't' : + uart_puts(UART_ID, "Not implemented yet \n"); + break; + default : + break; } } } diff --git a/uart.c b/uart.c deleted file mode 100644 index f5d073d..0000000 --- a/uart.c +++ /dev/null @@ -1,36 +0,0 @@ -#include "uart.h" -#include "hardware/uart.h" - -bool is_whitespace(char character) { - switch (character) { - case ' ' : - case '\n' : - case '\t' : - return true; - default : - return false; - } -} - -void read_word(uart_inst_t *uart, char *buffer, int buffersize, bool *eol) { - for (uint i = 0; i < (buffersize - 1); ++i) { - buffer[i] = uart_getc(uart); - if (is_whitespace(buffer[i])) { - buffer[i + 1] = '\0'; - } - } - - char last_char; - do { - last_char = uart_getc(uart); - } while (is_whitespace(last_char)); - - buffer[buffersize - 1] = '\0'; -} - -void finish_line(uart_inst_t *uart) { - char last_char; - do { - last_char = uart_getc(uart); - } while (last_char != '\n'); -} diff --git a/uart.h b/uart.h deleted file mode 100644 index f851a4c..0000000 --- a/uart.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef LANTHANIDE_UART -#define LANTHANIDE_UART - -#include "pico/stdlib.h" - -bool is_whitespace(char character); -void read_word(uart_inst_t *uart, char *buffer, int buffersize, bool *eol); -void finish_line(uart_inst_t *uart); - - -#endif -- 2.46.2