Initial commit

This commit is contained in:
2025-10-14 19:37:46 +03:00
commit ec2f7f033a
12 changed files with 5569 additions and 0 deletions

4454
include/clay.h Normal file

File diff suppressed because it is too large Load Diff

151
include/shared_layout.c Normal file
View File

@@ -0,0 +1,151 @@
#ifndef UART_CLAY_COMMON_LAYOUT_H
#define UART_CLAY_COMMON_LAYOUT_H
#include "uart_func.c"
typedef enum { MAIN_FONT, SMALL_FONT } FONT_ID;
const Clay_Color COLOR_RED = {255, 45, 0, 255};
const Clay_Color COLOR_GREEN = {25, 255, 25, 255};
const Clay_Color COLOR_BLUE = {0, 120, 255, 255};
const Clay_Color COLOR_ORANGE = {225, 127, 0, 255};
const Clay_Color COLOR_LIGHT = {224, 215, 210, 255};
const Clay_Color COLOR_BLACK = {0, 0, 0, 255};
const Clay_Sizing EXPAND = { CLAY_SIZING_GROW(0), CLAY_SIZING_GROW(0) };
const Clay_ChildAlignment CENTER_X_Y = { .x = CLAY_ALIGN_X_CENTER, .y = CLAY_ALIGN_Y_CENTER };
const Clay_LayoutConfig BUTTON_CONFIG = {
.sizing = EXPAND,
.childAlignment = CENTER_X_Y
};
const Clay_TextElementConfig LARGE_TEXT = { .textColor = COLOR_BLACK, .fontId = MAIN_FONT, .fontSize = 30 };
Clay_String modal_message;
Clay_Color darken(const Clay_Color color, const int scale) {
return (Clay_Color){
color.r * (100 - scale) / 100,
color.g * (100 - scale) / 100,
color.b * (100 - scale) / 100,
color.a
};
}
void toggle_red(Clay_ElementId elementId, Clay_PointerData pointerInfo, intptr_t userData) {
const uart_data *data = (uart_data *)userData;
if (pointerInfo.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) {
constexpr size_t response_length = 15;
handle_uart(data, 'r', response_length);
modal_message.length = response_length - 2;
UART_modal_show = true;
}
}
void toggle_green(Clay_ElementId elementId, Clay_PointerData pointerInfo, intptr_t userData) {
const uart_data *data = (uart_data *)userData;
if (pointerInfo.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) {
constexpr size_t response_length = 17;
handle_uart(data, 'g', response_length);
modal_message.length = response_length - 2;
UART_modal_show = true;
}
}
void toggle_blue(Clay_ElementId elementId, Clay_PointerData pointerInfo, intptr_t userData) {
const uart_data *data = (uart_data *)userData;
if (pointerInfo.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) {
constexpr size_t response_length = 16;
handle_uart(data, 'b', response_length);
modal_message.length = response_length - 2;
UART_modal_show = true;
}
}
void toggle_orange(Clay_ElementId elementId, Clay_PointerData pointerInfo, intptr_t userData) {
const uart_data *data = (uart_data *)userData;
if (pointerInfo.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) {
constexpr size_t response_length = 18;
handle_uart(data, 'o', response_length);
modal_message.length = response_length - 2;
UART_modal_show = true;
}
}
void hide_modal(Clay_ElementId elementId, Clay_PointerData pointerInfo, intptr_t userData) {
if (pointerInfo.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) {
UART_modal_show = false;
}
}
void Button(const Clay_String label, const Clay_Color color, void (*onHoverFunction)(Clay_ElementId elementId, Clay_PointerData pointerInfo, intptr_t userData), const intptr_t data) {
CLAY_AUTO_ID({
.layout = BUTTON_CONFIG, .backgroundColor = color, .cornerRadius = CLAY_CORNER_RADIUS(16)
}) {
Clay_OnHover(onHoverFunction, data);
CLAY_TEXT(label, (void *)&LARGE_TEXT);
}
}
void Modal() {
CLAY_AUTO_ID({
.layout = {
.layoutDirection = CLAY_TOP_TO_BOTTOM,
.sizing = { .height = CLAY_SIZING_PERCENT(0.25f), .width = CLAY_SIZING_PERCENT(0.55f) },
.childAlignment = CENTER_X_Y,
.padding = CLAY_PADDING_ALL(16),
.childGap = 8,
},
.cornerRadius = CLAY_CORNER_RADIUS(16),
.floating = {
.attachTo = CLAY_ATTACH_TO_PARENT,
.attachPoints = { .element = CLAY_ATTACH_POINT_CENTER_CENTER, .parent = CLAY_ATTACH_POINT_CENTER_CENTER }
},
.backgroundColor = darken(COLOR_LIGHT, 10)
}) {
CLAY_TEXT(modal_message, (void *)&LARGE_TEXT);
CLAY_AUTO_ID({ .layout = { .sizing = EXPAND } });
CLAY_AUTO_ID({
.layout = {
.sizing = { .height = CLAY_SIZING_FIT(), .width = CLAY_SIZING_GROW() },
.childAlignment = CENTER_X_Y
}, .backgroundColor = COLOR_RED, .cornerRadius = CLAY_CORNER_RADIUS(16)
}) {
Clay_OnHover(hide_modal, 0);
CLAY_TEXT(CLAY_STRING("X"), (void *)&LARGE_TEXT);
}
}
}
Clay_RenderCommandArray Clay_CreateLayout(uart_data *data) {
Clay_BeginLayout();
Clay__debugViewWidth = 700;
CLAY(CLAY_ID("Outer"),{
.layout = {
.layoutDirection = CLAY_TOP_TO_BOTTOM,
.sizing = EXPAND,
.padding = CLAY_PADDING_ALL(16),
.childGap = 16
},
.backgroundColor = COLOR_LIGHT,
}) {
if (UART_modal_show) {
Modal();
}
Button(CLAY_STRING("Red"), COLOR_RED, UART_modal_show ? nullptr : toggle_red, (intptr_t)data);
Button(CLAY_STRING("Green"), COLOR_GREEN, UART_modal_show ? nullptr : toggle_green, (intptr_t)data);
Button(CLAY_STRING("Blue"), COLOR_BLUE, UART_modal_show ? nullptr : toggle_blue, (intptr_t)data);
Button(CLAY_STRING("Orange"), COLOR_ORANGE, UART_modal_show ? nullptr : toggle_orange, (intptr_t)data);
}
return Clay_EndLayout();
}
#endif //UART_CLAY_COMMON_LAYOUT_H

79
include/uart_func.c Normal file
View File

@@ -0,0 +1,79 @@
#ifndef UART_CLAY_UART_FUNC_H
#define UART_CLAY_UART_FUNC_H
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include "clay.h"
bool UART_modal_show = false;
typedef struct {
int serial_port;
Clay_String buffer;
} uart_data;
static void trim_message(const Clay_String message, const size_t length) {
memset((void *)message.chars + length - 2, 0, message.length - length + 2);
}
int open_uart_port(const char *file_path) {
const int serial_port = open(file_path, O_RDWR);
if (serial_port < 0) {
printf("Error %i from open: %s\n", errno, strerror(errno));
return -1;
}
struct termios tty;
if (tcgetattr(serial_port, &tty) < 0) {
printf("Error %i from tcgetattr: %s\n", errno, strerror(errno));
return -1;
}
tty.c_cflag &= ~PARENB; // Clear parity bit, disabling parity
tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication
tty.c_cflag &= ~CSIZE; // Clear all the size bits
tty.c_cflag |= CS8; // 8 bits per byte
tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control
tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1)
tty.c_lflag &= ~ICANON; // Non-canonical mode
tty.c_lflag &= ~ECHO; // Disable echo
tty.c_lflag &= ~ECHOE; // Disable erasure
tty.c_lflag &= ~ECHONL; // Disable new-line echo
tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL);
// Disable any special handling of received bytes
tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed
tty.c_cc[VTIME] = 0;
tty.c_cc[VMIN] = 1;
// Set baud rate to be 9600
cfsetspeed(&tty, B9600);
// Save tty settings, also checking for error
if (tcsetattr(serial_port, TCSANOW, &tty) != 0) {
printf("Can't set fd state. Error %d, %s", errno, strerror(errno));
return -1;
}
return serial_port;
}
void handle_uart(const uart_data *data, const char msg, const int response_length) {
write(data->serial_port, &msg, 1);
memset((void *)data->buffer.chars, 0, data->buffer.length);
read(data->serial_port, (void *)data->buffer.chars, response_length);
}
#endif //UART_CLAY_UART_FUNC_H