From 1d55ed1d7285d45de7ffd27b0f9b892c2aed2ace Mon Sep 17 00:00:00 2001 From: YunHorn Technology Date: Wed, 23 Jul 2025 18:41:51 +0800 Subject: [PATCH] minor improve for fadeOut/In lamp bar --- Core/Inc/main.h | 2 +- Core/Inc/sts_lamp_bar.h | 3 + Core/Src/main.c | 5 +- Core/Src/sts_lamp_bar.c | 172 ++++++++++++++++++++++++-------- LoRaWAN/App/app_lorawan.c | 3 +- LoRaWAN/App/lora_app.c | 11 +- STM32CubeIDE/.cproject | 1 + STM32CubeIDE/Release/STS_O7.bin | Bin 248852 -> 144660 bytes 8 files changed, 150 insertions(+), 47 deletions(-) diff --git a/Core/Inc/main.h b/Core/Inc/main.h index 9438d2f..9916c22 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -144,7 +144,7 @@ void Error_Handler(void); #define STS_LAMP_BAR_PWM_TIM_PERIOD (240 - 1) #define STS_LAMP_BAR_HTIM htim1 #define STS_LAMP_BAR_LED_NUM (60) //60 for 46CM length LED strip - +#define LED_COUNT 15 // for 30 cm lamp bar #define WSDATA_PORT_PIN_PA8 //#define WSDATA_PORT_PIN_PA9 diff --git a/Core/Inc/sts_lamp_bar.h b/Core/Inc/sts_lamp_bar.h index 60e5bda..30a97a2 100644 --- a/Core/Inc/sts_lamp_bar.h +++ b/Core/Inc/sts_lamp_bar.h @@ -99,6 +99,7 @@ void STS_WS2812B_Set_RGB(uint8_t R, uint8_t G, uint8_t B, uint8_t idx); void STS_Reed_Hall_Working(void); void STS_Lamp_Bar_Self_Test_Simple(void); void STS_Lamp_Bar_Self_Test(void); +void STS_Lamp_Bar_Self_Test_Fade(void); void STS_Lamp_Bar_Set_Dark(void); void sts_rgb_unit_test(void); void STS_Reed_Hall_Presence_Detection(void); @@ -113,6 +114,8 @@ void STS_Lamp_Bar_Scoller(uint8_t color, uint8_t luminance_level); // Occupancy Status, Set Color to STS_RED // Vacant Status, Set Color to STS_GREEN void STS_Lamp_Bar_Set_STS_RGB_Color(uint8_t sts_lamp_color, uint8_t luminance_level); +void STS_Lamp_Bar_Set_STS_RGB_Color_FadeOut(uint8_t sts_lamp_color, uint8_t max_lum_level, uint8_t fade_length); +void STS_Lamp_Bar_Set_STS_RGB_Color_FadeIn(uint8_t sts_lamp_color, uint8_t max_lum_level, uint8_t fade_length); void STS_Lamp_Bar_Refresh(void); diff --git a/Core/Src/main.c b/Core/Src/main.c index 43ee6f2..315837d 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -99,7 +99,10 @@ int main(void) /* USER CODE BEGIN 2 */ STS_Sensor_Init(); STS_Sensor_Prepare(); - + while(1) + { + STS_Lamp_Bar_Self_Test_Fade(); + } /* USER CODE END 2 */ #if 0 sts_presence_rss_detection_init(); diff --git a/Core/Src/sts_lamp_bar.c b/Core/Src/sts_lamp_bar.c index 5e17359..acbcb58 100644 --- a/Core/Src/sts_lamp_bar.c +++ b/Core/Src/sts_lamp_bar.c @@ -169,46 +169,102 @@ void STS_Lamp_Bar_Set_RGB_Color(uint8_t red, uint8_t green, uint8_t blue ) } STS_WS2812B_Refresh(); } - -void STS_Lamp_Bar_Set_RGB_Color_HalfTone(uint8_t red, uint8_t green, uint8_t blue, uint8_t fade_length, uint8_t fade_direction ) +void STS_Lamp_Bar_Set_RGB_Color_Flow(void) { - uint8_t i =0, k=0; - UTIL_MEM_set_8((void*)rgb_buf.GRB,0x00,(WS2812B_DATA_LEN+RESET_PULSE)); - uint8_t fade_len = min(fade_length, STS_LAM_BAR_LED_NUM); //ensure it's not over length - uint8_t cc_red=red, cc_green= green, cc_blue=blue; - do - { - for (uint8_t j = 0; j < 8; j ++) - { - rgb_buf.GRB[i*24+j] = (uint16_t)(((cc_green<0 ; idx--) + { + if (idx > tail_len) { + // the most far tail end, the darker, the most head/first, the brighter + uint8_t rev = tail_len + 1 - idx; + + uint8_t lin = 0xFF - (rev * 0xFF / tail_len); + uint8_t bri = (lin * lin) >> 8; + + STS_WS2812B_Set_RGB((red*bri)>>8, (green*bri)>>8, (blue*bri)>>8, ((idx + start_position)%LED_COUNT)); + + } else { + // longer than tail, set to dark + STS_WS2812B_Set_RGB(red, green, blue, idx); + } + } +} + +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) +{ + // set tail length 1/3 + uint8_t tail_len = LED_COUNT /2; + + for (uint8_t idx = 0; idx < LED_COUNT; idx++) + { + if (idx < tail_len) { + // the most far tail end, the darker, the most head/first, the brighter + uint8_t rev = tail_len - 1 - idx; + //uint8_t lin = 0xFF - (rev * 0xFF / tail_len); + //uint8_t bri = (lin * lin) >> 8; + uint8_t lin = (idx/(2*tail_len)); + uint8_t bri = lin; + + //STS_WS2812B_Set_RGB((red*bri)>>8, (green*bri)>>8, (blue*bri)>>8, ((idx + start_position)%LED_COUNT)); + STS_WS2812B_Set_RGB((red*bri), (green*bri), (blue*bri), ((idx + start_position)%LED_COUNT)); + + } else { + // longer than tail, set to dark + STS_WS2812B_Set_RGB(red, green, blue, idx); + } + } +} + void STS_Lamp_Bar_Refresh(void) { STS_Lamp_Bar_Set_STS_RGB_Color(sts_lamp_bar_color, luminance_level); } -void STS_Lamp_Bar_Set_STS_RGB_Color_HalfTone(uint8_t sts_lamp_color, uint8_t max_lum_level) + +void STS_Lamp_Bar_Set_STS_RGB_Color_FadeIn(uint8_t sts_lamp_color, uint8_t max_lum_level, uint8_t fade_length) +{ + + if (sts_lamp_color <=8) + { + switch (sts_lamp_color) + { + case STS_DARK: + STS_Lamp_Bar_Set_RGB_Color(0x0, 0x0, 0x0); + break; + case STS_GREEN: + STS_Lamp_Bar_Set_RGB_Color_FadeIn(0x0, max_lum_level, 0x0, LED_COUNT/2, LED_COUNT/2); + break; + case STS_RED: + STS_Lamp_Bar_Set_RGB_Color_FadeIn(max_lum_level, 0x0, 0x0, LED_COUNT/2, LED_COUNT/2); + break; + case STS_BLUE: + STS_Lamp_Bar_Set_RGB_Color_FadeIn(0x0, 0x0, max_lum_level, LED_COUNT/2, LED_COUNT/2); + break; + case STS_YELLOW: + STS_Lamp_Bar_Set_RGB_Color_FadeIn(max_lum_level, max_lum_level, 0x0, LED_COUNT/2, LED_COUNT/2); + break; + case STS_PINK: + STS_Lamp_Bar_Set_RGB_Color_FadeIn(max_lum_level, 0x0, max_lum_level, LED_COUNT/2, LED_COUNT/2); + break; + case STS_CYAN: + STS_Lamp_Bar_Set_RGB_Color_FadeIn(0x0, max_lum_level, max_lum_level, LED_COUNT/2, LED_COUNT/2); + break; + case STS_WHITE: + STS_Lamp_Bar_Set_RGB_Color_FadeIn(max_lum_level, max_lum_level, max_lum_level, LED_COUNT/2, LED_COUNT/2); + break; + } + } +} + +void STS_Lamp_Bar_Set_STS_RGB_Color_FadeOut(uint8_t sts_lamp_color, uint8_t max_lum_level, uint8_t fade_length) { if (sts_lamp_color <=8) { @@ -218,26 +274,25 @@ void STS_Lamp_Bar_Set_STS_RGB_Color_HalfTone(uint8_t sts_lamp_color, uint8_t max STS_Lamp_Bar_Set_RGB_Color(0x0, 0x0, 0x0); break; case STS_GREEN: - STS_Lamp_Bar_Set_RGB_Color(0x0, lum, 0x0); - STS_Lamp_Bar_Set_RGB_Color_HalfTone(0x0, lum, 0x0); + STS_Lamp_Bar_Set_RGB_Color_FadeOut(0x0, max_lum_level, 0x0, 0, LED_COUNT/2); break; case STS_RED: - STS_Lamp_Bar_Set_RGB_Color(lum, 0x0, 0x0); + STS_Lamp_Bar_Set_RGB_Color_FadeOut(max_lum_level, 0x0, 0x0, 0, LED_COUNT/2); break; case STS_BLUE: - STS_Lamp_Bar_Set_RGB_Color(0x0, 0x0, lum); + STS_Lamp_Bar_Set_RGB_Color_FadeOut( 0x0, 0x0, max_lum_level, 0, LED_COUNT/2); break; case STS_YELLOW: - STS_Lamp_Bar_Set_RGB_Color(lum, lum, 0x0); + STS_Lamp_Bar_Set_RGB_Color_FadeOut(max_lum_level, max_lum_level, 0x0, 0, LED_COUNT/2); break; case STS_PINK: - STS_Lamp_Bar_Set_RGB_Color(lum, 0x0, lum); + STS_Lamp_Bar_Set_RGB_Color_FadeOut(max_lum_level, 0x0, max_lum_level, 0, LED_COUNT/2); break; case STS_CYAN: - STS_Lamp_Bar_Set_RGB_Color(0x0, lum, lum); + STS_Lamp_Bar_Set_RGB_Color_FadeOut(0x0, max_lum_level, max_lum_level, 0, LED_COUNT/2); break; case STS_WHITE: - STS_Lamp_Bar_Set_RGB_Color(lum, lum, lum); + STS_Lamp_Bar_Set_RGB_Color_FadeOut(max_lum_level, max_lum_level, max_lum_level, 0, LED_COUNT/2); break; } } @@ -291,17 +346,48 @@ void STS_Lamp_Bar_Self_Test_Simple(void) APP_LOG(TS_OFF, VLEVEL_H, "\r\n [#1] RGB Space Lumianance Level Testing Start\r\n"); for (color=STS_GREEN; color < STS_COLOR_MAX; color++) { - lum_level = 10; + lum_level = 1; do { STS_Lamp_Bar_Set_STS_RGB_Color(color, lum_level); - HAL_Delay(20); - lum_level += 20; - } while (lum_level < 99); + HAL_Delay(30); + lum_level += 10; + } while (lum_level < 255); STS_Lamp_Bar_Set_Dark(); } APP_LOG(TS_OFF, VLEVEL_H, "\r\n [#1] RGB Space Lumianance Level Testing Finished\r\n"); } +void STS_Lamp_Bar_Self_Test_Fade(void) +{ + uint8_t color=0, lum_level=DEFAULT_LUMINANCE_LEVEL; + APP_LOG(TS_OFF, VLEVEL_H, "\r\n [#1] RGB Space Fade Out Fade In Testing Start\r\n"); + + for (color=STS_GREEN; color < STS_COLOR_MAX; color++) + { + lum_level = 10; + do { + STS_Lamp_Bar_Set_STS_RGB_Color_FadeOut(color, lum_level, 10); + HAL_Delay(1000); + lum_level += 20; + } while (lum_level < 99); + STS_Lamp_Bar_Set_Dark(); + } + + for (color=STS_GREEN; color < STS_COLOR_MAX; color++) + { + lum_level = 10; + do { + STS_Lamp_Bar_Set_STS_RGB_Color_FadeIn(color, lum_level, 10); + HAL_Delay(1000); + lum_level += 20; + } while (lum_level < 99); + STS_Lamp_Bar_Set_Dark(); + } + + APP_LOG(TS_OFF, VLEVEL_H, "\r\n [#1] RGB Space Fade Out Fade In Testing Finished\r\n"); +} + + void STS_Lamp_Bar_Self_Test(void) { uint8_t color=0, lum_level=DEFAULT_LUMINANCE_LEVEL; diff --git a/LoRaWAN/App/app_lorawan.c b/LoRaWAN/App/app_lorawan.c index fa2c740..4325c31 100644 --- a/LoRaWAN/App/app_lorawan.c +++ b/LoRaWAN/App/app_lorawan.c @@ -71,8 +71,9 @@ void MX_LoRaWAN_Init(void) /* USER CODE BEGIN MX_LoRaWAN_Init_2 */ //STS_Lamp_Bar_Self_Test_Simple(); STS_Lamp_Bar_Self_Test(); + STS_Lamp_Bar_Self_Test_Fade(); /* USER CODE END MX_LoRaWAN_Init_2 */ - LoRaWAN_Init(); + //LoRaWAN_Init(); /* USER CODE BEGIN MX_LoRaWAN_Init_3 */ /* USER CODE END MX_LoRaWAN_Init_3 */ diff --git a/LoRaWAN/App/lora_app.c b/LoRaWAN/App/lora_app.c index 0f754c3..53878b5 100644 --- a/LoRaWAN/App/lora_app.c +++ b/LoRaWAN/App/lora_app.c @@ -130,7 +130,7 @@ volatile sts_cfg_nvm_t sts_cfg_nvm = { 0x8C, //P[19] RSS SLIDING WINDOW CFG: 0x08 AS threshold, 0x0C as window size }, // above 20 bytes #ifdef ATAL - 0x20, // 0x20 occupy(red:2) | color vacant (dark:0) for ATAL-HK 20241230 + 0x27, // 0x20 occupy(red:2) | color vacant (dark:0) for ATAL-HK 20241230 #else 0x21, // color occupy (red:2) | color vacant (green:1) or other 0x20 occupy(red:2) | color vacant (dark:0) for ATAL-HK 20241230 #endif @@ -1273,7 +1273,16 @@ static void OnYunhornSTSLampBarColorTimerEvent(void *context) if (high4==0) { + +#if defined(FADEOUT) + if (sts_rss_result == STS_RESULT_MOTION) { + STS_Lamp_Bar_Set_STS_RGB_Color_FadeOut(sts_lamp_bar_color, luminance_level, LED_COUNT/2); + } else if (sts_rss_result == STS_RESULT_NO_MOTION){ + STS_Lamp_Bar_Set_STS_RGB_Color_FadeIn(sts_lamp_bar_color, luminance_level, LED_COUNT/2); + } +#else STS_Lamp_Bar_Set_STS_RGB_Color(sts_lamp_bar_color, luminance_level); +#endif } else { diff --git a/STM32CubeIDE/.cproject b/STM32CubeIDE/.cproject index e7876b0..68a2ceb 100644 --- a/STM32CubeIDE/.cproject +++ b/STM32CubeIDE/.cproject @@ -154,6 +154,7 @@