/**
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h>
#include <string.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/uart.h"
#include "hardware/irq.h"
#include "char_fifo_buffer.h"
#define UART_ID uart0
#define BAUD_RATE 9600
#define DATA_BITS 8
#define STOP_BITS 1
#define PARITY UART_PARITY_NONE
// We are using pins 0 and 1, but see the GPIO function select table in the
// datasheet for information on which other pins can be used.
#define UART_TX_PIN 0
#define UART_RX_PIN 1
#define LED1_FARM1_PIN 6
#define LED2_FARM1_PIN 7
#define LED3_FARM1_PIN 8
#define LED4_FARM1_PIN 9
#define LED5_FARM1_PIN 10
#define LED6_FARM1_PIN 11
#define LED7_FARM1_PIN 12
#define LED8_FARM1_PIN 13
#define LED1_FARM2_PIN 28
#define LED2_FARM2_PIN 27
#define LED3_FARM2_PIN 26
#define LED4_FARM2_PIN 22
#define LED5_FARM2_PIN 21
#define LED6_FARM2_PIN 20
#define LED7_FARM2_PIN 19
#define LED8_FARM2_PIN 18
enum SENSOR_TYPE {
FAN,
TEMP
};
struct SensorData{
char domain[10];
char farm[10];
uint32_t sensor_type;
uint32_t values[10]; //A maximum of 10 values, adjust as needed
};
struct CharFIFO char_buffer;
struct SensorData sensor_data;
// Initialize uart
void uart_setup(void);
// Initialize gpio led pin.
void gpio_led_init(void);
void parse_data(const char *data, struct SensorData *sensor_data);
// RX interrupt handler
void on_uart_rx(void) {
while (uart_is_readable(UART_ID)) {
uint8_t ch = uart_getc(UART_ID);
enqueueChar(&char_buffer, ch);
}
}
int main() {
stdio_init_all();
gpio_led_init();
uart_setup();
initCharFIFO(&char_buffer);
while(true){
// Introduce a delay of 1000 milliseconds (1 second)
initCharFIFO(&char_buffer);
sleep_ms(500);
if (!isCharFIFOEmpty(&char_buffer)){
sleep_ms(200); //wait to make sure that data is recieved.
displayCharFIFO(&char_buffer);
parse_data(char_buffer.buffer, &sensor_data);
if( sensor_data.sensor_type == FAN){
if( strcmp(sensor_data.farm, "FARM_01") == 0 ){
gpio_put(LED1_FARM1_PIN, sensor_data.values[0]);
gpio_put(LED2_FARM1_PIN, sensor_data.values[1]);
gpio_put(LED3_FARM1_PIN, sensor_data.values[2]);
gpio_put(LED4_FARM1_PIN, sensor_data.values[3]);
gpio_put(LED5_FARM1_PIN, sensor_data.values[4]);
gpio_put(LED6_FARM1_PIN, sensor_data.values[5]);
gpio_put(LED7_FARM1_PIN, sensor_data.values[6]);
gpio_put(LED8_FARM1_PIN, sensor_data.values[7]);
}
else if( strcmp(sensor_data.farm, "FARM_02") == 0 ){
gpio_put(LED1_FARM2_PIN, sensor_data.values[0]);
gpio_put(LED2_FARM2_PIN, sensor_data.values[1]);
gpio_put(LED3_FARM2_PIN, sensor_data.values[2]);
gpio_put(LED4_FARM2_PIN, sensor_data.values[3]);
gpio_put(LED5_FARM2_PIN, sensor_data.values[4]);
gpio_put(LED6_FARM2_PIN, sensor_data.values[5]);
gpio_put(LED7_FARM2_PIN, sensor_data.values[6]);
gpio_put(LED8_FARM2_PIN, sensor_data.values[7]);
}
}
freeCharFIFO(&char_buffer);
}
}
return 0;
}
void uart_setup(void){
// Set up our UART with a basic baud rate.
uart_init(UART_ID, BAUD_RATE);
// Set the TX and RX pins by using the function select on the GPIO
// Set datasheet for more information on function select
gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
// Set UART flow control CTS/RTS, 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);
// Turn off FIFO's - we want to do this character by character
uart_set_fifo_enabled(UART_ID, false);
// Set up a RX interrupt
// We need to set up the handler first
// Select correct interrupt for the UART we are using
int UART_IRQ = UART_ID == uart0 ? UART0_IRQ : UART1_IRQ;
// And set up and enable the interrupt handlers
irq_set_exclusive_handler(UART_IRQ, on_uart_rx);
irq_set_enabled(UART_IRQ, true);
// Now enable the UART to send interrupts - RX only
uart_set_irq_enables(UART_ID, true, false);
}
void parse_data(const char *data, struct SensorData *sensor_data) {
sscanf(data, "%9[^|]|%9[^|]|%u|%u|%u|%u|%u|%u|%u|%u|%u",
sensor_data->domain,
sensor_data->farm,
sensor_data->sensor_type,
&sensor_data->values[0],
&sensor_data->values[1],
&sensor_data->values[2],
&sensor_data->values[3],
&sensor_data->values[4],
&sensor_data->values[5],
&sensor_data->values[6],
&sensor_data->values[7]
);
}
// Initialize gpio for LED pin.
void gpio_led_init(void){
gpio_init(LED1_FARM1_PIN);
gpio_set_dir(LED1_FARM1_PIN, GPIO_OUT);
gpio_init(LED2_FARM1_PIN);
gpio_set_dir(LED2_FARM1_PIN, GPIO_OUT);
gpio_init(LED3_FARM1_PIN);
gpio_set_dir(LED3_FARM1_PIN, GPIO_OUT);
gpio_init(LED4_FARM1_PIN);
gpio_set_dir(LED4_FARM1_PIN, GPIO_OUT);
gpio_init(LED5_FARM1_PIN);
gpio_set_dir(LED5_FARM1_PIN, GPIO_OUT);
gpio_init(LED6_FARM1_PIN);
gpio_set_dir(LED6_FARM1_PIN, GPIO_OUT);
gpio_init(LED7_FARM1_PIN);
gpio_set_dir(LED7_FARM1_PIN, GPIO_OUT);
gpio_init(LED8_FARM1_PIN);
gpio_set_dir(LED8_FARM1_PIN, GPIO_OUT);
gpio_init(LED1_FARM2_PIN);
gpio_set_dir(LED1_FARM2_PIN, GPIO_OUT);
gpio_init(LED2_FARM2_PIN);
gpio_set_dir(LED2_FARM2_PIN, GPIO_OUT);
gpio_init(LED3_FARM2_PIN);
gpio_set_dir(LED3_FARM2_PIN, GPIO_OUT);
gpio_init(LED4_FARM2_PIN);
gpio_set_dir(LED4_FARM2_PIN, GPIO_OUT);
gpio_init(LED5_FARM2_PIN);
gpio_set_dir(LED5_FARM2_PIN, GPIO_OUT);
gpio_init(LED6_FARM2_PIN);
gpio_set_dir(LED6_FARM2_PIN, GPIO_OUT);
gpio_init(LED7_FARM2_PIN);
gpio_set_dir(LED7_FARM2_PIN, GPIO_OUT);
gpio_init(LED8_FARM2_PIN);
gpio_set_dir(LED8_FARM2_PIN, GPIO_OUT);
}