Effects added :-)

This commit is contained in:
StefansE
2026-03-25 19:06:44 +01:00
parent 7aea97a648
commit 5a4d46bbba
6 changed files with 104 additions and 21 deletions

28
Core/Inc/ws2812_drv.h Normal file
View File

@@ -0,0 +1,28 @@
/*
* ws2812.h
*
* Created on: Mar 25, 2026
* Author: ewars
*/
#ifndef INC_WS2812_DRV_H_
#define INC_WS2812_DRV_H_
#include "stdint-gcc.h"
#define LED_N 24
#define RESET_LEN 50
#define PWM_ZERO 29
#define PWM_ONE 58
void ws2812_Init(void);
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);
#endif /* INC_WS2812_DRV_H_ */

18
Core/Inc/ws2812_effect.h Normal file
View File

@@ -0,0 +1,18 @@
/*
* ws2812_effect.h
*
* Created on: Mar 25, 2026
* Author: ewars
*/
#ifndef INC_WS2812_EFFECT_H_
#define INC_WS2812_EFFECT_H_
void ws_Effect_RoudTheClock(void);
void ws_Effect_RandRound(void);
void ws_Effect_RandRand(void);
#endif /* INC_WS2812_EFFECT_H_ */

View File

@@ -24,8 +24,7 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdlib.h>
#include "ws2812.h"
#include "ws2812_effect.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
@@ -98,6 +97,8 @@ int main(void)
ws2812_Init();
uint32_t mytimer = 0;
/* USER CODE END 2 */
@@ -109,24 +110,27 @@ int main(void)
/* USER CODE BEGIN 3 */
uint8_t r = rand();
uint8_t g = rand();
uint8_t b = rand();
#define EFFECT_TIME 5000
for (uint8_t led = 0; led < 24; led++) {
mytimer = HAL_GetTick();
while(mytimer + EFFECT_TIME > HAL_GetTick()){
ws_Effect_RoudTheClock();
}
uint8_t r = rand();
uint8_t g = rand();
uint8_t b = rand();
ws2812_reset();
ws2812_set_colour(led, r, g, b);
ws2812_update();
HAL_Delay(20);
mytimer = HAL_GetTick();
while(mytimer + EFFECT_TIME > HAL_GetTick()){
ws_Effect_RandRound();
}
HAL_GPIO_TogglePin(LED_Green_GPIO_Port, LED_Green_Pin);
mytimer = HAL_GetTick();
while(mytimer + EFFECT_TIME > HAL_GetTick()){
ws_Effect_RandRand();
}
//HAL_GPIO_TogglePin(LED_Green_GPIO_Port, LED_Green_Pin);
}
/* USER CODE END 3 */

72
Core/Src/ws2812_drv.c Normal file
View File

@@ -0,0 +1,72 @@
/*
* ws2812.c
*
* Created on: Mar 25, 2026
* Author: ewars
*/
#include <ws2812_drv.h>
#include "stm32f1xx_hal.h"
extern htim2;
static void set_byte(uint32_t pos, uint8_t value);
uint8_t pwm_reset[RESET_LEN] = {0};
uint8_t pwm_full = 90;
uint8_t ledStripBuffer[RESET_LEN + LED_N * 24 + 1] = {0};
void ws2812_Init(void){
for(uint32_t i = 0; i < LED_N * 24; i++){
ledStripBuffer[RESET_LEN + i] = PWM_ONE;
}
ledStripBuffer[RESET_LEN + LED_N * 24] = pwm_full;
ws2812_update();
}
void ws2812_update(void){
HAL_TIM_PWM_Start_DMA(&htim2, TIM_CHANNEL_1, ledStripBuffer, sizeof(ledStripBuffer));
}
void ws2812_set_colour(uint8_t led, uint8_t red, uint8_t green, uint8_t blue){
if(led < LED_N){
set_byte(RESET_LEN + 24 * led, green);
set_byte(RESET_LEN + 24 * led + 8, red);
set_byte(RESET_LEN + 24 * led + 16, blue);
}
}
void ws2812_reset(void){
for(uint8_t i = 0; i < LED_N; i++){
ws2812_set_colour(i, 0, 0, 0);
}
}
static void set_byte(uint32_t pos, uint8_t value)
{
int i;
for (i = 0; i < 8; i++) {
if (value & 0x80) {
ledStripBuffer[pos + i] = PWM_ONE;
} else {
ledStripBuffer[pos + i] = PWM_ZERO;
}
value <<= 1;
}
}
void ws2812_wait(void){
while(HAL_TIM_GetChannelState(&htim2, TIM_CHANNEL_1)){}
}

60
Core/Src/ws2812_effect.c Normal file
View File

@@ -0,0 +1,60 @@
/*
* ws2812_effect.c
*
* Created on: Mar 25, 2026
* Author: ewars
*/
#include "ws2812_effect.h"
#include "ws2812_drv.h"
#include <stdlib.h>
void ws_Effect_RoudTheClock(void){
uint8_t r = rand();
uint8_t g = rand();
uint8_t b = rand();
for (uint8_t led = 0; led < 24; led++) {
ws2812_reset();
ws2812_set_colour(led, r, g, b);
ws2812_update();
HAL_Delay(20);
}
}
void ws_Effect_RandRound(void){
uint8_t r = rand();
uint8_t g = rand();
uint8_t b = rand();
for (uint8_t led = 0; led < 24; led++) {
ws2812_set_colour(led, r, g, b);
ws2812_update();
HAL_Delay(20);
}
}
void ws_Effect_RandRand(void){
#define RAND_MAX 255
uint8_t r = rand();
uint8_t g = rand();
uint8_t b = rand();
#define RAND_MAX 24
uint8_t led = rand();
ws2812_set_colour(led, r, g, b);
ws2812_update();
HAL_Delay(1);
}