Way simplify logic
This commit is contained in:
parent
0d4b50a7c0
commit
5a2963d3ba
4 changed files with 33 additions and 104 deletions
|
|
@ -9,8 +9,6 @@ pico_sdk_init()
|
|||
|
||||
add_executable(pdu
|
||||
pdu.c
|
||||
uart.h
|
||||
uart.c
|
||||
)
|
||||
|
||||
target_link_libraries(pdu pico_stdlib)
|
||||
|
|
|
|||
88
pdu.c
88
pdu.c
|
|
@ -1,7 +1,6 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
36
uart.c
36
uart.c
|
|
@ -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');
|
||||
}
|
||||
11
uart.h
11
uart.h
|
|
@ -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
|
||||
Loading…
Add table
Reference in a new issue