From e6e97f04125d29786980ce7f8ad864b7d9d88c90 Mon Sep 17 00:00:00 2001 From: StefansE Date: Fri, 27 Mar 2026 19:40:53 +0100 Subject: [PATCH] UART commands work --- Core/Inc/clock.h | 7 ++ Core/Inc/myComms.h | 43 +++++++++++ Core/Inc/ws2812_drv.h | 1 + Core/Inc/ws2812_effect.h | 1 - Core/Src/clock.c | 12 ++- Core/Src/ds1307.c | 7 +- Core/Src/main.c | 7 +- Core/Src/myComms.c | 157 +++++++++++++++++++++++++++++++++++++++ Core/Src/ws2812_drv.c | 8 ++ Core/Src/ws2812_effect.c | 35 ++++++++- 10 files changed, 267 insertions(+), 11 deletions(-) create mode 100644 Core/Inc/myComms.h create mode 100644 Core/Src/myComms.c diff --git a/Core/Inc/clock.h b/Core/Inc/clock.h index ce1343b..f679ebb 100644 --- a/Core/Inc/clock.h +++ b/Core/Inc/clock.h @@ -13,6 +13,13 @@ uint8_t clock_convert_hours(uint8_t hours); uint8_t clock_convert_min_sec(uint8_t min_sec); +void clock_LedOffset(uint8_t offset); + +void clock_setColourHours(uint8_t r, uint8_t g, uint8_t b); +void clock_setColourMinutes(uint8_t r, uint8_t g, uint8_t b); +void clock_setColourSeconds(uint8_t r, uint8_t g, uint8_t b); +void clock_setColourBackground(uint8_t r, uint8_t g, uint8_t b); + #endif /* INC_CLOCK_H_ */ diff --git a/Core/Inc/myComms.h b/Core/Inc/myComms.h new file mode 100644 index 0000000..15541f4 --- /dev/null +++ b/Core/Inc/myComms.h @@ -0,0 +1,43 @@ +/* + * muComms.h + * + * Created on: Mar 27, 2026 + * Author: ewars + */ + +#ifndef INC_MYCOMMS_H_ +#define INC_MYCOMMS_H_ + +#include "stdint-gcc.h" + +typedef enum { + com_WhoAmI, + com_setTime, + com_getTime, + com_setLedOffset, + com_setColour_hour, + com_setColour_minute, + com_setColour_second, + com_setColour_background, + com_MAX_command + +} commands_e; + + +void cmd_UART_Request(void); + +void cmd_RunCommand(commands_e cmd, uint32_t * input); + + +void cmd_WhoAmI(uint32_t); +void cmd_SetTime(uint32_t time); +void cmd_SetOffsetDiode(uint32_t diode); +void cmd_GetTime(uint32_t time); +void cmd_setColour_hour(uint32_t colour); +void cmd_setColour_minute(uint32_t colour); +void cmd_setColour_second(uint32_t colour); +void cmd_setColour_background(uint32_t colour); + + + +#endif /* INC_MYCOMMS_H_ */ diff --git a/Core/Inc/ws2812_drv.h b/Core/Inc/ws2812_drv.h index 8fb5226..3815ac4 100644 --- a/Core/Inc/ws2812_drv.h +++ b/Core/Inc/ws2812_drv.h @@ -23,6 +23,7 @@ void ws2812_update(void); void ws2812_set_colour(uint8_t led, uint8_t red, uint8_t green, uint8_t blue); void ws2812_wait(void); void ws2812_reset(void); +void ws2812_resetColour(uint8_t r, uint8_t g, uint8_t b); #endif /* INC_WS2812_DRV_H_ */ diff --git a/Core/Inc/ws2812_effect.h b/Core/Inc/ws2812_effect.h index ead7ee2..7825208 100644 --- a/Core/Inc/ws2812_effect.h +++ b/Core/Inc/ws2812_effect.h @@ -16,7 +16,6 @@ void ws_Effect_RandRound(void); void ws_Effect_RandRand(void); void ws_effect_display_time(uint8_t hour, uint8_t minute, uint8_t second); - void ws_effect_display_hour(uint8_t hour); diff --git a/Core/Src/clock.c b/Core/Src/clock.c index aed428e..18486e1 100644 --- a/Core/Src/clock.c +++ b/Core/Src/clock.c @@ -9,7 +9,13 @@ #include "clock.h" // define LED marking 12:00 or 00:00 -#define LED_ZERO_OFFSET 10 + +__attribute__((section(".noinit"))) volatile uint8_t ledOffset = 0; + + +void clock_LedOffset(uint8_t offset){ + ledOffset = offset; +} uint8_t clock_convert_hours(uint8_t hours){ @@ -26,7 +32,7 @@ uint8_t clock_convert_hours(uint8_t hours){ h = hours; } - led = 2 * h + LED_ZERO_OFFSET; + led = 2 * h + ledOffset; if(led > 23){ led -= 24; } @@ -44,7 +50,7 @@ uint8_t clock_convert_min_sec(uint8_t min_sec){ if(min_sec < 60){ led = (float) min_sec/(60/24); - led += LED_ZERO_OFFSET; + led += ledOffset; if(led > 23){ led -= 24; } diff --git a/Core/Src/ds1307.c b/Core/Src/ds1307.c index 970bda8..b5232c6 100644 --- a/Core/Src/ds1307.c +++ b/Core/Src/ds1307.c @@ -296,9 +296,10 @@ void ds1307_update(ds1307_dev_t *ds1307_dev){ } val = ds1307_get_minutes(); - if(val != 255){ - ds1307_dev->minutes = val; - } + if(val != 255){ + ds1307_dev->minutes = val; + } + ds1307_dev->hours = ds1307_get_hour(); ds1307_dev->day = ds1307_get_day(); ds1307_dev->date = ds1307_get_date(); diff --git a/Core/Src/main.c b/Core/Src/main.c index 4d35975..37e0f09 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -50,7 +50,7 @@ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ - +extern uint8_t UART_RxBuf[6]; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ @@ -122,6 +122,7 @@ int main(void) /* USER CODE BEGIN 3 */ +uint8_t UART_Buff[] = "Hello World\n"; #define EFFECT_TIME 10000 @@ -141,10 +142,12 @@ int main(void) // ws_Effect_RoudTheClock(); - + HAL_UART_Receive_IT(&huart2, UART_RxBuf, sizeof(UART_RxBuf)); HAL_GPIO_TogglePin(LED_Green_GPIO_Port, LED_Green_Pin); + HAL_UART_Transmit(&huart2, UART_Buff, sizeof(UART_Buff), 1000); + HAL_Delay(1000); } diff --git a/Core/Src/myComms.c b/Core/Src/myComms.c new file mode 100644 index 0000000..5632f23 --- /dev/null +++ b/Core/Src/myComms.c @@ -0,0 +1,157 @@ +/* + * myComms.c + * + * Created on: Mar 27, 2026 + * Author: ewars + */ + + +#include "myComms.h" +#include "usart.h" +#include "clock.h" + +#include "ds1307.h" + +typedef struct { + uint8_t cmdId; + + struct{ + uint8_t pByte_3; + uint8_t pByte_2; + uint8_t pByte_1; + uint8_t pByte_0; + } bytes; + + uint8_t crc; +} uartMessage_s; + + +static uint8_t ok[] = "OK\n"; +uint8_t myId[] = "Clock\n"; + +uint8_t UART_RxBuf[6] = {0}; + +void (*commands[])(uint32_t) = { + cmd_WhoAmI, + cmd_SetTime, + cmd_GetTime, + cmd_SetOffsetDiode, + cmd_setColour_hour, + cmd_setColour_minute, + cmd_setColour_second +}; + +void cmd_SetOffsetDiode(uint32_t diode){ + + uint8_t offset = (uint8_t) diode; + + clock_LedOffset(offset); + + HAL_UART_Transmit(&huart2, ok, sizeof(ok), 1000); + +} + +void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){ + + cmd_UART_Request(); + +} + +void cmd_UART_Request(void){ + + uartMessage_s* msg = (uartMessage_s*) &UART_RxBuf[0]; + + cmd_RunCommand((uint8_t) msg->cmdId, (uint32_t*) &msg->bytes); + + HAL_UART_Transmit(&huart2, (uint8_t*) msg, sizeof(*msg), 1000); + + HAL_UART_Transmit(&huart2, ok, sizeof(ok), 1000); + +} + + +void cmd_RunCommand(commands_e cmd, uint32_t* input){ + uint32_t i = *input; + if(cmd < com_MAX_command){ + commands[cmd](i); + } + +} + + + + +void cmd_WhoAmI(uint32_t){ + HAL_UART_Transmit(&huart2, myId, sizeof(myId), 1000); +} + + +void cmd_SetTime(uint32_t time){ + + uint8_t h, m, s; + + h = time & 0xFF; + m = time >> 8 & 0xFF; + s = time >> 16 & 0xFF; + + ds1307_set_hour(h); + ds1307_set_minutes(m); + ds1307_set_second(s); + + HAL_UART_Transmit(&huart2, ok, sizeof(ok), 1000); + +} + +void cmd_GetTime(uint32_t time){} + +void cmd_setColour_hour(uint32_t colour){ + + uint8_t r, g, b; + + r = colour & 0xFF; + g = colour >> 8 & 0xFF; + b = colour >> 16 & 0xFF; + + clock_setColourHours(r, g, b); + + HAL_UART_Transmit(&huart2, ok, sizeof(ok), 1000); + +} +void cmd_setColour_minute(uint32_t colour){ + + uint8_t r, g, b; + + r = colour & 0xFF; + g = colour >> 8 & 0xFF; + b = colour >> 16 & 0xFF; + + clock_setColourMinutes(r, g, b); + + HAL_UART_Transmit(&huart2, ok, sizeof(ok), 1000); + +} +void cmd_setColour_second(uint32_t colour){ + + uint8_t r, g, b; + + r = colour & 0xFF; + g = colour >> 8 & 0xFF; + b = colour >> 16 & 0xFF; + + clock_setColourSeconds(r, g, b); + + HAL_UART_Transmit(&huart2, ok, sizeof(ok), 1000); + +} +void cmd_setColour_background(uint32_t colour){ + + uint8_t r, g, b; + + r = colour & 0xFF; + g = colour >> 8 & 0xFF; + b = colour >> 16 & 0xFF; + + clock_setColourBackground(r, g, b); + + HAL_UART_Transmit(&huart2, ok, sizeof(ok), 1000); +} diff --git a/Core/Src/ws2812_drv.c b/Core/Src/ws2812_drv.c index 6f0253a..bd13d22 100644 --- a/Core/Src/ws2812_drv.c +++ b/Core/Src/ws2812_drv.c @@ -43,6 +43,14 @@ void ws2812_set_colour(uint8_t led, uint8_t red, uint8_t green, uint8_t blue){ } +void ws2812_resetColour(uint8_t r, uint8_t g, uint8_t b){ + + for(uint8_t i = 0; i < LED_N; i++){ + ws2812_set_colour(i, r, g, b); + } + +} + void ws2812_reset(void){ for(uint8_t i = 0; i < LED_N; i++){ ws2812_set_colour(i, 0, 0, 0); diff --git a/Core/Src/ws2812_effect.c b/Core/Src/ws2812_effect.c index 66951e2..a092e42 100644 --- a/Core/Src/ws2812_effect.c +++ b/Core/Src/ws2812_effect.c @@ -5,9 +5,9 @@ * Author: ewars */ - -#include "ws2812_effect.h" #include "ws2812_drv.h" +#include "ws2812_effect.h" +#include "clock.h" #include typedef struct { @@ -19,6 +19,35 @@ typedef struct { static colour_s colour_hours = {250, 0, 0}; static colour_s colour_minutes = {0, 250, 0}; static colour_s colour_seconds = {0, 0, 250}; +static colour_s colour_background = {0, 0, 0}; + +void clock_setColourHours(uint8_t r, uint8_t g, uint8_t b){ + colour_hours.r = r; + colour_hours.g = g; + colour_hours.b = b; +} + +void clock_setColourMinutes(uint8_t r, uint8_t g, uint8_t b){ + colour_minutes.r = r; + colour_minutes.g = g; + colour_minutes.b = b; +} + +void clock_setColourSeconds(uint8_t r, uint8_t g, uint8_t b){ + colour_seconds.r = r; + colour_seconds.g = g; + colour_seconds.b = b; +} + + +void clock_setColourBackground(uint8_t r, uint8_t g, uint8_t b){ + + colour_background.r = r; + colour_background.g = g; + colour_background.b = b; +} + + void ws_Effect_RoudTheClock(void){ @@ -60,6 +89,8 @@ void ws_Effect_RandRound(void){ void ws_effect_display_time(uint8_t hour, uint8_t minute, uint8_t second){ ws2812_reset(); + //ws2812_resetColour(colour_background.r, colour_background.g, colour_background.b); + ws2812_update(); ws2812_set_colour(hour, colour_hours.r, colour_hours.g, colour_hours.b); ws2812_update();