]> git.friedersdorff.com Git - max/lanthanide_pdu.git/blobdiff - pdu.c
Way simplify logic
[max/lanthanide_pdu.git] / pdu.c
diff --git a/pdu.c b/pdu.c
index 4e33d185675f53eefa7567b5f5cfcd5e549a5577..e95a0ad03d2e0589e4905ddf7875d91cf0491b75 100644 (file)
--- a/pdu.c
+++ b/pdu.c
@@ -1,7 +1,6 @@
 #include <stdio.h>
 #include <string.h>
 
-#include "uart.h"
 #include "pico/stdlib.h"
 #include "hardware/uart.h"
 
 #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;
         }
     }
 }