--- /dev/null
+cmake_minimum_required(VERSION 3.13)
+
+include(pico_sdk_import.cmake)
+
+project(lanthanide_pdu)
+
+pico_sdk_init()
+
+
+add_executable(pdu
+ pdu.c
+ uart.h
+ uart.c
+)
+
+target_link_libraries(pdu pico_stdlib)
--- /dev/null
+#include <stdio.h>
+#include <string.h>
+
+#include "uart.h"
+#include "pico/stdlib.h"
+#include "hardware/uart.h"
+
+#define UART_ID uart0
+#define DATA_BITS 8
+#define STOP_BITS 1
+#define PARITY UART_PARITY_NONE
+
+#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);
+ gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
+
+ uart_set_hw_flow(UART_ID, false, false);
+ uart_set_format(UART_ID, DATA_BITS, STOP_BITS, PARITY);
+
+ gpio_init(PINA);
+ gpio_init(PINB);
+ gpio_init(PINC);
+ gpio_init(PIND);
+
+ gpio_set_dir(PINA, GPIO_OUT);
+ gpio_set_dir(PINB, GPIO_OUT);
+ gpio_set_dir(PINC, GPIO_OUT);
+ gpio_set_dir(PIND, GPIO_OUT);
+
+ gpio_put(PINA, 1);
+ gpio_put(PINB, 1);
+ 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");
+ }
+ }
+}
--- /dev/null
+# This is a copy of <PICO_SDK_PATH>/external/pico_sdk_import.cmake
+
+# This can be dropped into an external project to help locate this SDK
+# It should be include()ed prior to project()
+
+if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH))
+ set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
+ message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')")
+endif ()
+
+if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT))
+ set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT})
+ message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')")
+endif ()
+
+if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH))
+ set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH})
+ message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
+endif ()
+
+set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK")
+set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable")
+set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK")
+
+if (NOT PICO_SDK_PATH)
+ if (PICO_SDK_FETCH_FROM_GIT)
+ include(FetchContent)
+ set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
+ if (PICO_SDK_FETCH_FROM_GIT_PATH)
+ get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
+ endif ()
+ FetchContent_Declare(
+ pico_sdk
+ GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
+ GIT_TAG master
+ )
+ if (NOT pico_sdk)
+ message("Downloading Raspberry Pi Pico SDK")
+ FetchContent_Populate(pico_sdk)
+ set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR})
+ endif ()
+ set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
+ else ()
+ message(FATAL_ERROR
+ "SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git."
+ )
+ endif ()
+endif ()
+
+get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
+if (NOT EXISTS ${PICO_SDK_PATH})
+ message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found")
+endif ()
+
+set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake)
+if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE})
+ message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK")
+endif ()
+
+set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE)
+
+include(${PICO_SDK_INIT_CMAKE_FILE})
--- /dev/null
+#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');
+}
--- /dev/null
+#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