#include <stdio.h>
#include <string.h>
#include "pico/stdlib.h"
#include "hardware/uart.h"
#include "hardware/gpio.h"
// Define the UART and pins
#define UART_ID uart0
#define BAUD_RATE 115200
#define DATA_BITS 8
#define STOP_BITS 1
#define PARITY UART_PARITY_NONE
#define UART_TX_PIN 0
#define UART_RX_PIN 1
// Define the led pins
#define led_vermelho_pin 14
int main() {
// Initialize standard I/O over USB for console output
stdio_init_all();
gpio_init( led_vermelho_pin );
gpio_set_dir( led_vermelho_pin, GPIO_OUT);
// Set up our UART with a basic baud rate
uart_init(UART_ID, BAUD_RATE);
// Set the TX and RX pins function to UART
gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
// Set UART flow control, we don't want these, so turn them off
uart_set_hw_flow(UART_ID, false, false);
// Set our data format
uart_set_format(UART_ID, DATA_BITS, STOP_BITS, PARITY);
// Enable the FIFO
uart_set_fifo_enabled(UART_ID, true);
char comando[255];
uint8_t comando_index=0;
gpio_put( led_vermelho_pin, 0 );
while (true) {
// Check if data is available in the RX buffer
if (uart_is_readable(UART_ID)) {
// Read a single character
uint8_t ch = uart_getc(UART_ID);
if ( ch != '\n'){
comando[ comando_index] = ch;
comando_index++;
}else{
comando[ comando_index] = '\0';
uart_puts(UART_ID, "Comando:");
uart_puts(UART_ID, comando);
uart_puts(UART_ID, "\n");
comando_index = 0;
if ( strcmp(comando,"acende") == 0 ){
uart_puts(UART_ID,"Acende Led. \n");
gpio_put( led_vermelho_pin, 1 );
} else if ( strcmp(comando,"apaga" ) ==0 ) {
uart_puts(UART_ID,"Apaga Led. \n");
gpio_put( led_vermelho_pin, 0 );
}
}
}
}
}