overall good lamp bar color setting

This commit is contained in:
Yunhorn 2025-07-28 19:11:16 +08:00
parent d9659fd92b
commit c90656b238
3 changed files with 161 additions and 22 deletions

View File

@ -141,7 +141,7 @@ void Error_Handler(void);
/* /*
* STM32WL55, STM32WLE5 Clock=48Mhz 48,000,000/800khz = 60 * STM32WL55, STM32WLE5 Clock=48Mhz 48,000,000/800khz = 60
*/ */
#define STS_LAMP_BAR_PWM_TIM_PERIOD (120 - 1) #define STS_LAMP_BAR_PWM_TIM_PERIOD (240 - 1)
#define STS_LAMP_BAR_HTIM htim1 #define STS_LAMP_BAR_HTIM htim1
#define STS_LAMP_BAR_LED_NUM (60) //60 for 46CM length LED strip #define STS_LAMP_BAR_LED_NUM (60) //60 for 46CM length LED strip
#define LED_COUNT 18 // for 30 cm lamp bar #define LED_COUNT 18 // for 30 cm lamp bar

View File

@ -28,12 +28,13 @@
#include "yunhorn_sts_sensors.h" #include "yunhorn_sts_sensors.h"
#define ONE_PULSE (40) //36 #define ONE_PULSE (36) //(36) //36
#define ZERO_PULSE (20) #define ZERO_PULSE (12) //(12)
#define LED_DATA_LEN 24 #define LED_DATA_LEN 24
#define WS2812B_DATA_LEN (LED_DATA_LEN * (STS_LAMP_BAR_LED_NUM)) //#define WS2812B_DATA_LEN (LED_DATA_LEN * (STS_LAMP_BAR_LED_NUM))
#define RESET_PULSE (150) //(80) TO FIX DARK_COLOR AND SM2 #define WS2812B_DATA_LEN (LED_DATA_LEN * (LED_COUNT))
#define RESET_PULSE (24) //(80) TO FIX DARK_COLOR AND SM2
typedef struct ws2812b_e { typedef struct ws2812b_e {
@ -49,6 +50,9 @@ volatile WS2812B_FrameTypeDef rgb_buf = {
.tail = 0 .tail = 0
}; };
uint8_t ws2812b_crr_value[LED_COUNT*24+RESET_PULSE]={00};
uint8_t ws2812b_data[LED_COUNT*3]={0};
uint8_t color_rgb[8][3] = { //STS_COLOR R G B MAPPING TABLE uint8_t color_rgb[8][3] = { //STS_COLOR R G B MAPPING TABLE
{0,0,0},{0,1,0},{1,0,0},{0,0,1},{1,1,0},{1,0,1},{0,1,1},{1,1,1} {0,0,0},{0,1,0},{1,0,0},{0,0,1},{1,1,0},{1,0,1},{0,1,1},{1,1,1}
}; };
@ -92,16 +96,93 @@ extern SysTime_t mems_event_time;
extern volatile uint32_t event_start_time, event_stop_time; extern volatile uint32_t event_start_time, event_stop_time;
extern volatile uint32_t event_door_lock_start_time, event_door_lock_stop_time; extern volatile uint32_t event_door_lock_start_time, event_door_lock_stop_time;
volatile uint8_t luminance_level = DEFAULT_LUMINANCE_LEVEL; volatile uint8_t luminance_level = DEFAULT_LUMINANCE_LEVEL;
void ws2812b_data_transfer(uint8_t *src, uint32_t len);
void ws2812b_light_custom(uint8_t red, uint8_t green, uint8_t blue);
void ws2812b_light_all_with_lum_level(uint8_t color, uint8_t lum_level);
void ws2812b_light_all(uint8_t color);
void ws2812b_data_transfer(uint8_t *src, uint32_t len)
{
uint8_t i =0;
uint32_t bits = 0;
uint32_t bytes = 0;
while(len--)
{
for (i=0; i < 8; i++)
{
ws2812b_crr_value[bits] = (((*(src+bytes))<<i)&0x80)? ONE_PULSE : ZERO_PULSE;
bits++;
}
bytes++;
}
for (i=0;i<RESET_PULSE;i++)
{
ws2812b_crr_value[bits++] = 0;
}
HAL_TIM_PWM_Start_DMA(&STS_LAMP_BAR_HTIM, STS_LAMP_BAR_TIM_CHANNEL, (uint32_t *)ws2812b_crr_value, bits); //(WS2812B_DATA_LEN+RESET_PULSE));
//HAL_TIM_PWM_Start_IT(&STS_LAMP_BAR_HTIM, STS_LAMP_BAR_TIM_CHANNEL);
}
void ws2812b_light_custom(uint8_t red, uint8_t green, uint8_t blue)
{
uint8_t i = 0;
memset(ws2812b_data, 0, sizeof(ws2812b_data));
for (i=0; i<(LED_COUNT); i++)
{
ws2812b_data[3*i] = green;
ws2812b_data[3*i+1] = red;
ws2812b_data[3*i+2] = blue;
}
ws2812b_data_transfer(ws2812b_data, sizeof(ws2812b_data));
}
void ws2812b_light_one_rgb(uint8_t red, uint8_t green, uint8_t blue, uint8_t idx)
{
uint8_t i = 0;
memset(ws2812b_data, 0, sizeof(ws2812b_data));
for (i=0; i<(LED_COUNT); i++)
{
ws2812b_data[3*i] = green;
ws2812b_data[3*i+1] = red;
ws2812b_data[3*i+2] = blue;
}
ws2812b_data_transfer(ws2812b_data, sizeof(ws2812b_data));
}
void ws2812b_light_all_with_lum_level(uint8_t color, uint8_t lum_level)
{
luminance_level = lum_level;
ws2812b_light_all(color);
}
void ws2812b_light_all(uint8_t color)
{
switch(color)
{
case STS_DARK : ws2812b_light_custom(0, 0, 0); break;
case STS_GREEN : ws2812b_light_custom(0, luminance_level, 0 ); break;
case STS_RED : ws2812b_light_custom(luminance_level, 0, 0 ); break;
case STS_BLUE : ws2812b_light_custom(0, 0, luminance_level); break;
case STS_YELLOW : ws2812b_light_custom(luminance_level, luminance_level, 0 ); break;
case STS_PINK : ws2812b_light_custom(luminance_level, 0, luminance_level ); break;
case STS_CYAN : ws2812b_light_custom(0, luminance_level, luminance_level ); break;
case STS_WHITE : ws2812b_light_custom(luminance_level, luminance_level, luminance_level ); break;
}
}
void STS_Lamp_Bar_Set_Dark(void) void STS_Lamp_Bar_Set_Dark(void)
{ {
for (uint8_t i=0; i< STS_LAMP_BAR_LED_NUM; i++) ws2812b_light_all(STS_DARK);
/*
//for (uint8_t i=0; i< STS_LAMP_BAR_LED_NUM; i++)
for (uint8_t i=0; i< LED_COUNT+1; i++)
{ {
STS_WS2812B_Set_RGB(0x00,0x00,0x00,i); STS_WS2812B_Set_RGB(0x00,0x00,0x00,i);
} }
STS_WS2812B_Refresh(); STS_WS2812B_Refresh();
*/
} }
void STS_WS2812B_Refresh(void) void STS_WS2812B_Refresh(void)
@ -130,6 +211,18 @@ void STS_Lamp_Bar_Init(void)
//marquee scoller //marquee scoller
void STS_Lamp_Bar_Scoller(uint8_t color, uint8_t lum_level) void STS_Lamp_Bar_Scoller(uint8_t color, uint8_t lum_level)
{ {
uint8_t i =0;
memset(ws2812b_data, 0, sizeof(ws2812b_data));
for(i=0; i < (LED_COUNT+1); i++)
{
ws2812b_data_transfer(ws2812b_data, sizeof(ws2812b_data));
HAL_Delay(20);
if (i<LED_COUNT){
ws2812b_data[i*3 + color] = lum_level;
}
}
/*
STS_Lamp_Bar_Set_Dark(); STS_Lamp_Bar_Set_Dark();
//for(uint8_t i = 0; i<STS_LAMP_BAR_LED_NUM; i++) //for(uint8_t i = 0; i<STS_LAMP_BAR_LED_NUM; i++)
@ -140,7 +233,7 @@ void STS_Lamp_Bar_Scoller(uint8_t color, uint8_t lum_level)
STS_WS2812B_Refresh(); STS_WS2812B_Refresh();
} }
*/
} }
@ -156,10 +249,13 @@ void STS_WS2812B_Set_RGB(uint8_t red, uint8_t green, uint8_t blue, uint8_t idx)
void STS_Lamp_Bar_Set_RGB_Color(uint8_t red, uint8_t green, uint8_t blue ) void STS_Lamp_Bar_Set_RGB_Color(uint8_t red, uint8_t green, uint8_t blue )
{ {
//ws2812b_light_all();
/*
uint8_t i =0; uint8_t i =0;
UTIL_MEM_set_8((void*)rgb_buf.GRB,0x00,(WS2812B_DATA_LEN+RESET_PULSE)); UTIL_MEM_set_8((void*)rgb_buf.GRB,0x00,(WS2812B_DATA_LEN+RESET_PULSE));
for(i = 0; i < STS_LAMP_BAR_LED_NUM; i++) //for(i = 0; i < STS_LAMP_BAR_LED_NUM; i++)
for(i = 0; i < LED_COUNT; i++)
{ {
for (uint8_t j = 0; j < 8; j ++) for (uint8_t j = 0; j < 8; j ++)
{ {
@ -169,6 +265,7 @@ void STS_Lamp_Bar_Set_RGB_Color(uint8_t red, uint8_t green, uint8_t blue )
} }
} }
STS_WS2812B_Refresh(); STS_WS2812B_Refresh();
*/
} }
void STS_Lamp_Bar_Set_RGB_Color_Flow(void) void STS_Lamp_Bar_Set_RGB_Color_Flow(void)
{ {
@ -180,6 +277,9 @@ void STS_Lamp_Bar_Set_RGB_Color_Flow(void)
static void STS_Lamp_Bar_Set_RGB_Color_FadeIn(uint8_t red, uint8_t green, uint8_t blue, uint8_t start_position, uint8_t fade_length) static void STS_Lamp_Bar_Set_RGB_Color_FadeIn(uint8_t red, uint8_t green, uint8_t blue, uint8_t start_position, uint8_t fade_length)
{ {
uint8_t fade_len = fade_length; uint8_t fade_len = fade_length;
memset(ws2812b_data, 0, sizeof(ws2812b_data));
//UTIL_MEM_set_8((void*)rgb_buf.GRB,0x00,(WS2812B_DATA_LEN+RESET_PULSE));
//STS_WS2812B_Refresh();
for (uint8_t idx = 0; idx < LED_COUNT ; idx++) for (uint8_t idx = 0; idx < LED_COUNT ; idx++)
{ {
if (idx >= start_position) { if (idx >= start_position) {
@ -189,19 +289,32 @@ static void STS_Lamp_Bar_Set_RGB_Color_FadeIn(uint8_t red, uint8_t green, uint8_
uint8_t lin = (rev * 0xFF / fade_len); uint8_t lin = (rev * 0xFF / fade_len);
uint8_t bri = (lin * lin) >> 8; uint8_t bri = (lin * lin) >> 8;
STS_WS2812B_Set_RGB((red*bri)>>8, (green*bri)>>8, (blue*bri)>>8, ((idx))); //STS_WS2812B_Set_RGB((red*bri)>>8, (green*bri)>>8, (blue*bri)>>8, ((idx)));
//ws2812b_light_one_rgb((red*bri)>>8, (green*bri)>>8, (blue*bri)>>8, ((idx)));
{
ws2812b_data[3*idx] = (green*bri)>>8;
ws2812b_data[3*idx+1] = (red*bri)>>8;
ws2812b_data[3*idx+2] = (blue*bri)>>8;
}
} else { } else {
// shorter than tail, set to dark // shorter than tail, set to dark
//STS_WS2812B_Set_RGB(red, green, blue, idx); //STS_WS2812B_Set_RGB(red, green, blue, idx);
STS_WS2812B_Set_RGB(0, 0, 0, idx); //STS_WS2812B_Set_RGB(0, 0, 0, idx);
ws2812b_data[3*idx] = 0;
ws2812b_data[3*idx+1] = 0;
ws2812b_data[3*idx+2] = 0;
} }
} }
ws2812b_data_transfer(ws2812b_data, sizeof(ws2812b_data));
} }
static void STS_Lamp_Bar_Set_RGB_Color_FadeOut(uint8_t red, uint8_t green, uint8_t blue, uint8_t start_position, uint8_t fade_length) static void STS_Lamp_Bar_Set_RGB_Color_FadeOut(uint8_t red, uint8_t green, uint8_t blue, uint8_t start_position, uint8_t fade_length)
{ {
uint8_t fade_len = fade_length; uint8_t fade_len = fade_length;
//UTIL_MEM_set_8((void*)rgb_buf.GRB,0x00,(WS2812B_DATA_LEN+RESET_PULSE));
memset(ws2812b_data, 0, sizeof(ws2812b_data));
for (uint8_t idx = 0; idx < LED_COUNT; idx++) for (uint8_t idx = 0; idx < LED_COUNT; idx++)
{ {
if ((idx >= start_position) && (idx <= (start_position+fade_len))) { if ((idx >= start_position) && (idx <= (start_position+fade_len))) {
@ -212,15 +325,28 @@ static void STS_Lamp_Bar_Set_RGB_Color_FadeOut(uint8_t red, uint8_t green, uint8
//uint8_t lin = (idx/(2*tail_len)); //uint8_t lin = (idx/(2*tail_len));
//uint8_t bri = lin; //uint8_t bri = lin;
STS_WS2812B_Set_RGB((red*bri)>>8, (green*bri)>>8, (blue*bri)>>8, ((idx))); //STS_WS2812B_Set_RGB((red*bri)>>8, (green*bri)>>8, (blue*bri)>>8, ((idx)));
//ws2812b_light_one_rgb((red*bri)>>8, (green*bri)>>8, (blue*bri)>>8, ((idx)));
{
ws2812b_data[3*idx] = (green*bri)>>8;
ws2812b_data[3*idx+1] = (red*bri)>>8;
ws2812b_data[3*idx+2] = (blue*bri)>>8;
}
//ws2812b_light_one_rgb(uint8_t red, uint8_t green, uint8_t blue, uint8_t idx)
//STS_WS2812B_Set_RGB((red*bri), (green*bri), (blue*bri), ((idx + start_position)%LED_COUNT)); //STS_WS2812B_Set_RGB((red*bri), (green*bri), (blue*bri), ((idx + start_position)%LED_COUNT));
} else if ((idx < start_position)||(idx > fade_len)) { } else if ((idx < start_position)||(idx > fade_len)) {
// shorter than start position or longer than fade length, set to dark // shorter than start position or longer than fade length, set to dark
//STS_WS2812B_Set_RGB(red, green, blue, idx); //STS_WS2812B_Set_RGB(red, green, blue, idx);
STS_WS2812B_Set_RGB(0, 0, 0, idx); //STS_WS2812B_Set_RGB(0, 0, 0, idx);
//ws2812b_light_one_rgb(0, 0, 0, (idx));
ws2812b_data[3*idx] = 0;
ws2812b_data[3*idx+1] = 0;
ws2812b_data[3*idx+2] = 0;
} }
} }
ws2812b_data_transfer(ws2812b_data, sizeof(ws2812b_data));
} }
void STS_Lamp_Bar_Refresh(void) void STS_Lamp_Bar_Refresh(void)
@ -235,7 +361,7 @@ void STS_Lamp_Bar_Set_STS_RGB_Color_FadeIn(uint8_t sts_lamp_color, uint8_t max_l
switch (sts_lamp_color) switch (sts_lamp_color)
{ {
case STS_DARK: case STS_DARK:
STS_Lamp_Bar_Set_RGB_Color(0x0, 0x0, 0x0); // STS_Lamp_Bar_Set_RGB_Color(0x0, 0x0, 0x0);
break; break;
case STS_GREEN: case STS_GREEN:
STS_Lamp_Bar_Set_RGB_Color_FadeIn(0x0, max_lum_level, 0x0, LED_COUNT/2, fade_length); STS_Lamp_Bar_Set_RGB_Color_FadeIn(0x0, max_lum_level, 0x0, LED_COUNT/2, fade_length);
@ -270,7 +396,7 @@ void STS_Lamp_Bar_Set_STS_RGB_Color_FadeOut(uint8_t sts_lamp_color, uint8_t max_
switch (sts_lamp_color) switch (sts_lamp_color)
{ {
case STS_DARK: case STS_DARK:
STS_Lamp_Bar_Set_RGB_Color(0x0, 0x0, 0x0); // STS_Lamp_Bar_Set_RGB_Color(0x0, 0x0, 0x0);
break; break;
case STS_GREEN: case STS_GREEN:
STS_Lamp_Bar_Set_RGB_Color_FadeOut(0x0, max_lum_level, 0x0, 0, fade_length); STS_Lamp_Bar_Set_RGB_Color_FadeOut(0x0, max_lum_level, 0x0, 0, fade_length);
@ -300,6 +426,8 @@ void STS_Lamp_Bar_Set_STS_RGB_Color_FadeOut(uint8_t sts_lamp_color, uint8_t max_
} }
void STS_Lamp_Bar_Set_STS_RGB_Color(uint8_t sts_lamp_color, uint8_t lum) void STS_Lamp_Bar_Set_STS_RGB_Color(uint8_t sts_lamp_color, uint8_t lum)
{ {
ws2812b_light_all_with_lum_level(sts_lamp_color, lum);
/*
if (sts_lamp_color <=8) if (sts_lamp_color <=8)
{ {
switch (sts_lamp_color) switch (sts_lamp_color)
@ -330,7 +458,7 @@ void STS_Lamp_Bar_Set_STS_RGB_Color(uint8_t sts_lamp_color, uint8_t lum)
break; break;
} }
} }
*/
} }
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim) void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
@ -369,11 +497,11 @@ void STS_Lamp_Bar_Self_Test_Fade(void)
APP_LOG(TS_OFF, VLEVEL_M, "\rFade Out %04d\r", lum_level); APP_LOG(TS_OFF, VLEVEL_M, "\rFade Out %04d\r", lum_level);
STS_Lamp_Bar_Set_STS_RGB_Color_FadeOut(color, lum_level, LED_COUNT/2); STS_Lamp_Bar_Set_STS_RGB_Color_FadeOut(color, lum_level, LED_COUNT/2);
HAL_Delay(2000); HAL_Delay(1000);
//lum_level += 5; //lum_level += 5;
//} while (lum_level < 200); //} while (lum_level < 200);
//STS_Lamp_Bar_Set_Dark(); STS_Lamp_Bar_Set_Dark();
STS_Lamp_Bar_Refresh(); //STS_Lamp_Bar_Refresh();
} }
APP_LOG(TS_OFF, VLEVEL_M, "\r\n [#1] RGB Space Fade In Testing Start\r\n"); APP_LOG(TS_OFF, VLEVEL_M, "\r\n [#1] RGB Space Fade In Testing Start\r\n");
for (color=STS_GREEN; color < STS_COLOR_MAX; color++) for (color=STS_GREEN; color < STS_COLOR_MAX; color++)
@ -383,11 +511,11 @@ void STS_Lamp_Bar_Self_Test_Fade(void)
APP_LOG(TS_OFF, VLEVEL_M, "\rFade In %04d\r", lum_level); APP_LOG(TS_OFF, VLEVEL_M, "\rFade In %04d\r", lum_level);
STS_Lamp_Bar_Set_STS_RGB_Color_FadeIn(color, lum_level, LED_COUNT/2); STS_Lamp_Bar_Set_STS_RGB_Color_FadeIn(color, lum_level, LED_COUNT/2);
HAL_Delay(2000); HAL_Delay(1000);
//lum_level += 5; //lum_level += 5;
//} while (lum_level < 200); //} while (lum_level < 200);
//STS_Lamp_Bar_Set_Dark(); STS_Lamp_Bar_Set_Dark();
STS_Lamp_Bar_Refresh(); //STS_Lamp_Bar_Refresh();
} }
APP_LOG(TS_OFF, VLEVEL_M, "\r\n [#1] RGB Space Fade Out Fade In Testing Finished\r\n"); APP_LOG(TS_OFF, VLEVEL_M, "\r\n [#1] RGB Space Fade Out Fade In Testing Finished\r\n");
@ -397,7 +525,7 @@ void STS_Lamp_Bar_Self_Test_Fade(void)
void STS_Lamp_Bar_Self_Test(void) void STS_Lamp_Bar_Self_Test(void)
{ {
uint8_t color=0, lum_level=DEFAULT_LUMINANCE_LEVEL; uint8_t color=0, lum_level=DEFAULT_LUMINANCE_LEVEL;
/*
APP_LOG(TS_OFF, VLEVEL_H, "\r\n YunHorn STS Indicative Lamp Self Test\r\n"); APP_LOG(TS_OFF, VLEVEL_H, "\r\n YunHorn STS Indicative Lamp Self Test\r\n");
STS_Lamp_Bar_Self_Test_Simple(); STS_Lamp_Bar_Self_Test_Simple();
@ -410,7 +538,18 @@ void STS_Lamp_Bar_Self_Test(void)
STS_Lamp_Bar_Scoller(color, lum_level); STS_Lamp_Bar_Scoller(color, lum_level);
} }
} }
*/
for (color = STS_GREEN; color < STS_COLOR_MAX; color++)
{
//for (lum_level=1; lum_level<250; lum_level+=50)
{
ws2812b_light_all(color);
}
HAL_Delay(1000);
}
STS_Lamp_Bar_Set_Dark();
HAL_Delay(1000);
STS_Lamp_Bar_Self_Test_Fade(); STS_Lamp_Bar_Self_Test_Fade();
APP_LOG(TS_OFF, VLEVEL_H, "\r\n [##] YunHorn STS Indicative Lamp Self Test Finished\r\n"); APP_LOG(TS_OFF, VLEVEL_H, "\r\n [##] YunHorn STS Indicative Lamp Self Test Finished\r\n");

Binary file not shown.