O7/Core/Src/sts_lamp_bar.c

306 lines
8.1 KiB
C
Raw Normal View History

2024-04-08 14:23:48 +08:00
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file yunhorn_sts_lamp_bar.c *
* @author Yunhorn (r) Technology Limited Application Team *
* @brief Yunhorn (r) SmarToilets (r) Product configuration file. *
******************************************************************************
* @attention
*
* Copyright (c) 2022 Yunhorn Technology Limited.
* Copyright (c) 2022 Shenzhen Yunhorn Technology Co., Ltd.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
#include "main.h"
#include "dma.h"
#include "tim.h"
#include "string.h"
#include "sys_app.h"
#include "stm32_systime.h"
#include "sts_lamp_bar.h"
#include "yunhorn_sts_sensors.h"
2024-07-23 16:38:16 +08:00
#define ONE_PULSE (40) //36
2024-04-08 14:23:48 +08:00
#define ZERO_PULSE (20)
2024-07-18 18:08:16 +08:00
#define LED_DATA_LEN 24
2024-07-25 10:44:49 +08:00
#define WS2812B_DATA_LEN (LED_DATA_LEN * (STS_LAMP_BAR_LED_NUM+1))
#define RESET_PULSE (16) //(80) TO FIX DARK_COLOR AND SM2
2024-04-08 14:23:48 +08:00
typedef struct ws2812b_e {
2024-07-25 10:44:49 +08:00
//uint16_t head[3];
2024-07-23 16:01:23 +08:00
uint16_t GRB[WS2812B_DATA_LEN];
2024-04-08 14:23:48 +08:00
uint16_t tail;
} WS2812B_FrameTypeDef;
volatile WS2812B_FrameTypeDef rgb_buf = {
2024-07-25 10:44:49 +08:00
// .head[0] = 0,
// .head[1] = 0,
// .head[2] = 0,
2024-04-08 14:23:48 +08:00
.tail = 0
};
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}
};
2024-05-08 15:00:50 +08:00
extern volatile uint8_t sts_service_mask;
extern volatile uint8_t sts_work_mode;
2024-04-08 14:23:48 +08:00
volatile uint8_t sts_reed_hall_ext_int = 0;
volatile uint8_t sts_status_color = STS_GREEN;
2024-04-08 14:23:48 +08:00
volatile uint8_t sts_lamp_bar_color = STS_GREEN; //puColor
volatile uint8_t sts_lamp_bar_flashing_color = 0x23; // RED_BLUE;
2024-04-08 14:23:48 +08:00
volatile uint8_t sts_cloud_netcolor = STS_GREEN; //netColor
2024-05-08 15:00:50 +08:00
extern volatile uint8_t sts_occupancy_status;
2024-04-28 19:21:09 +08:00
2024-05-08 15:00:50 +08:00
extern volatile uint8_t sts_reed_hall_result, sts_emergency_button_pushed; // inital 0 = close
volatile uint8_t sts_hall1_read=STS_Status_Door_Open,sts_hall2_read=STS_Status_SOS_Release; // Above hall1_read == reed_hall_result, hall2_read == emergency_button
2024-05-08 17:48:00 +08:00
extern volatile uint8_t sts_reed_hall_1_result, sts_reed_hall_2_result;
2024-05-08 15:00:50 +08:00
extern volatile uint8_t sts_tof_result_changed_flag;
extern volatile uint8_t sts_rss_result_changed_flag, sts_hall1_changed_flag, sts_hall2_changed_flag, sts_reed_hall_changed_flag;
extern volatile uint8_t sts_rss_result;
extern volatile uint8_t sts_rss_2nd_result; //2nd RSS sensor status
extern volatile uint8_t sts_tof_result;
//extern volatile uint8_t last_sts_reed_hall_result = 2; //Initial state, not 0, not 1
volatile uint8_t last_lamp_bar_color=STS_GREEN;
2024-04-08 14:23:48 +08:00
extern volatile uint8_t sts_presence_fall_detection;
extern volatile uint8_t sts_fall_rising_detected_result;
2024-04-08 14:23:48 +08:00
extern volatile float sts_presence_rss_distance;
extern volatile uint8_t sensor_data_ready;
extern SysTime_t mems_event_time;
extern volatile uint32_t event_start_time, event_stop_time;
2024-05-14 12:29:17 +08:00
extern volatile uint32_t event_door_lock_start_time, event_door_lock_stop_time;
volatile uint8_t luminance_level = DEFAULT_LUMINANCE_LEVEL;
2024-04-08 14:23:48 +08:00
void STS_Lamp_Bar_Set_Dark(void)
{
for (uint8_t i=0; i< STS_LAMP_BAR_LED_NUM; i++)
{
STS_WS2812B_Set_RGB(0x00,0x00,0x00,i);
2024-07-23 16:01:23 +08:00
2024-04-08 14:23:48 +08:00
}
STS_WS2812B_Refresh();
2024-04-08 14:23:48 +08:00
}
void STS_WS2812B_Refresh(void)
{
2024-07-25 10:44:49 +08:00
//__disable_irq();
HAL_TIM_PWM_Start_DMA(&STS_LAMP_BAR_HTIM, STS_LAMP_BAR_TIM_CHANNEL, (uint32_t *)&rgb_buf, (WS2812B_DATA_LEN+1));
//__enable_irq();
2024-07-23 16:01:23 +08:00
//HAL_TIM_PWM_Start_IT(&STS_LAMP_BAR_HTIM, STS_LAMP_BAR_TIM_CHANNEL);
2024-04-08 14:23:48 +08:00
}
void STS_Lamp_Bar_Init(void)
{
if (sts_service_mask == STS_SERVICE_MASK_L0)
{
STS_Lamp_Bar_Set_STS_RGB_Color(STS_GREEN, luminance_level);
2024-07-23 16:01:23 +08:00
2024-04-08 14:23:48 +08:00
HAL_Delay(200);
STS_Lamp_Bar_Set_STS_RGB_Color(STS_RED, luminance_level);
2024-07-23 16:01:23 +08:00
2024-04-08 14:23:48 +08:00
HAL_Delay(200);
STS_Lamp_Bar_Set_STS_RGB_Color(STS_BLUE, luminance_level);
2024-07-23 16:01:23 +08:00
2024-04-08 14:23:48 +08:00
HAL_Delay(200);
}
}
//marquee scoller
void STS_Lamp_Bar_Scoller(uint8_t color, uint8_t lum_level)
2024-04-08 14:23:48 +08:00
{
STS_Lamp_Bar_Set_Dark();
for(uint8_t i = 0; i<STS_LAMP_BAR_LED_NUM; i++)
2024-04-08 14:23:48 +08:00
{
HAL_Delay(20); //MAKE THIS LESS THAN 10 NOT TO BLOCK JOIN THE LORAWAN
2024-07-23 16:01:23 +08:00
STS_WS2812B_Set_RGB(color_rgb[color][0]*lum_level,color_rgb[color][1]*lum_level, color_rgb[color][2]*lum_level, i);
2024-07-23 16:01:23 +08:00
STS_WS2812B_Refresh();
2024-04-08 14:23:48 +08:00
}
HAL_Delay(10);
2024-04-08 14:23:48 +08:00
}
2024-07-25 10:44:49 +08:00
void STS_WS2812B_Set_RGB(uint8_t red, uint8_t green, uint8_t blue, uint8_t idx)
2024-04-08 14:23:48 +08:00
{
2024-07-25 10:44:49 +08:00
//if (idx < STS_LAMP_BAR_LED_NUM)
2024-04-08 14:23:48 +08:00
{
for (uint8_t j = 0; j < 8; j ++)
{
2024-07-25 10:44:49 +08:00
rgb_buf.GRB[idx*24+j] = (uint16_t)(((green<<j)&0x80)? ONE_PULSE : ZERO_PULSE);
rgb_buf.GRB[idx*24+8+j] = (uint16_t)(((red<<j)&0x80)? ONE_PULSE : ZERO_PULSE);
rgb_buf.GRB[idx*24+16+j] = (uint16_t)(((blue<<j)&0x80)? ONE_PULSE : ZERO_PULSE);
2024-04-08 14:23:48 +08:00
}
}
}
void STS_Lamp_Bar_Set_RGB_Color(uint8_t red, uint8_t green, uint8_t blue )
{
2024-07-23 16:01:23 +08:00
uint8_t i =0;
2024-07-25 10:44:49 +08:00
//HAL_Delay(1);
2024-07-25 10:44:49 +08:00
//__disable_irq();
//UTIL_MEM_set_8((void*)rgb_buf.GRB,0x0,(WS2812B_DATA_LEN));
//__enable_irq();
2024-07-23 16:01:23 +08:00
for(i = 0; i < STS_LAMP_BAR_LED_NUM; i++)
2024-04-08 14:23:48 +08:00
{
2024-07-25 10:44:49 +08:00
//STS_WS2812B_Set_RGB(red, green, blue, i);
for (uint8_t j = 0; j < 8; j ++)
{
rgb_buf.GRB[i*24+j] = (uint16_t)(((green<<j)&0x80)? ONE_PULSE : ZERO_PULSE);
rgb_buf.GRB[i*24+8+j] = (uint16_t)(((red<<j)&0x80)? ONE_PULSE : ZERO_PULSE);
rgb_buf.GRB[i*24+16+j] = (uint16_t)(((blue<<j)&0x80)? ONE_PULSE : ZERO_PULSE);
}
2024-04-08 14:23:48 +08:00
}
2024-07-25 10:44:49 +08:00
//STS_WS2812B_Set_RGB(0,0,0, i);
2024-07-23 16:01:23 +08:00
2024-07-25 10:44:49 +08:00
//if (sts_service_mask == STS_SERVICE_MASK_L0) {
2024-04-08 14:23:48 +08:00
STS_WS2812B_Refresh();
2024-07-25 10:44:49 +08:00
//}
2024-04-08 14:23:48 +08:00
}
void STS_Lamp_Bar_Refresh(void)
{
STS_Lamp_Bar_Set_STS_RGB_Color(sts_lamp_bar_color, luminance_level);
}
2024-07-23 16:01:23 +08:00
void STS_Lamp_Bar_Set_STS_RGB_Color(uint8_t sts_lamp_color, uint8_t lum)
2024-04-08 14:23:48 +08:00
{
2024-07-25 10:44:49 +08:00
switch (sts_lamp_color&0x0f)
2024-04-08 14:23:48 +08:00
{
case STS_DARK:
STS_Lamp_Bar_Set_RGB_Color(0x0, 0x0, 0x0);
break;
case STS_GREEN:
STS_Lamp_Bar_Set_RGB_Color(0x0, lum, 0x0);
break;
case STS_RED:
STS_Lamp_Bar_Set_RGB_Color(lum, 0x0, 0x0);
break;
case STS_BLUE:
STS_Lamp_Bar_Set_RGB_Color(0x0, 0x0, lum);
break;
case STS_YELLOW:
STS_Lamp_Bar_Set_RGB_Color(lum, lum, 0x0);
break;
case STS_PINK:
STS_Lamp_Bar_Set_RGB_Color(lum, 0x0, lum);
break;
case STS_CYAN:
STS_Lamp_Bar_Set_RGB_Color(0x0, lum, lum);
break;
case STS_WHITE:
STS_Lamp_Bar_Set_RGB_Color(lum, lum, lum);
break;
}
}
void STS_Reed_Hall_Working(void)
{
}
2024-04-08 14:23:48 +08:00
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
{
__HAL_TIM_SetCompare(&STS_LAMP_BAR_HTIM, STS_LAMP_BAR_TIM_CHANNEL,0);
2024-07-23 16:01:23 +08:00
HAL_TIM_PWM_Stop_DMA(&STS_LAMP_BAR_HTIM, STS_LAMP_BAR_TIM_CHANNEL);
2024-04-08 14:23:48 +08:00
}
void STS_Lamp_Bar_Self_Test_Simple(void)
{
uint8_t color=0, lum_level=DEFAULT_LUMINANCE_LEVEL;
2024-06-11 14:26:25 +08:00
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++)
2024-04-08 14:23:48 +08:00
{
lum_level = 10;
2024-04-08 14:23:48 +08:00
do {
STS_Lamp_Bar_Set_STS_RGB_Color(color, lum_level);
2024-04-09 16:19:00 +08:00
HAL_Delay(50);
lum_level += 20;
} while (lum_level < 99);
STS_Lamp_Bar_Set_Dark();
2024-04-08 14:23:48 +08:00
}
2024-06-11 14:26:25 +08:00
APP_LOG(TS_OFF, VLEVEL_H, "\r\n [#1] RGB Space Lumianance Level Testing Finished\r\n");
2024-04-08 14:23:48 +08:00
}
2024-07-23 16:01:23 +08:00
2024-04-08 14:23:48 +08:00
void STS_Lamp_Bar_Self_Test(void)
{
uint8_t color=0, lum_level=DEFAULT_LUMINANCE_LEVEL;
2024-04-08 14:23:48 +08:00
2024-06-11 14:26:25 +08:00
APP_LOG(TS_OFF, VLEVEL_H, "\r\n YunHorn STS Indicative Lamp Self Test\r\n");
2024-04-08 14:23:48 +08:00
STS_Lamp_Bar_Self_Test_Simple();
APP_LOG(TS_OFF, VLEVEL_H, "\r\n [#2] Scoller Testing\r\n");
for (color = STS_GREEN; color < STS_COLOR_MAX; color++)
2024-04-08 14:23:48 +08:00
{
STS_Lamp_Bar_Scoller(color, lum_level);
2024-04-08 14:23:48 +08:00
}
STS_Lamp_Bar_Set_Dark();
2024-06-11 14:26:25 +08:00
APP_LOG(TS_OFF, VLEVEL_H, "\r\n [##] YunHorn STS Indicative Lamp Self Test Finished\r\n");
2024-04-08 14:23:48 +08:00
if ((sts_work_mode == STS_WIRED_MODE) )
{
STS_Lamp_Bar_Set_Dark();
} else
{
STS_Lamp_Bar_Set_STS_RGB_Color(STS_GREEN, lum_level);
2024-04-08 14:23:48 +08:00
}
STS_Lamp_Bar_Set_Dark();
2024-04-08 14:23:48 +08:00
}
2024-07-23 16:01:23 +08:00
2024-04-08 14:23:48 +08:00
void sts_rgb_unit_test(void)
{
APP_LOG(TS_OFF, VLEVEL_L, "\r\n STS Lamp Bar Init...\r\n");
STS_Lamp_Bar_Set_Dark();
STS_Lamp_Bar_Full_Color_Gradient();
STS_Lamp_Bar_Self_Test();
do {
for (uint8_t i=0; i<9; i++)
{
APP_LOG(TS_OFF, VLEVEL_L, "\r\n STS Lamp Bar color = %d...\r\n", i);
STS_Lamp_Bar_Set_STS_RGB_Color(i, luminance_level);
STS_Combined_Status_Processing();
HAL_Delay(6000);
STS_Lamp_Bar_Set_Dark();
}
} while(1);
}
2024-07-23 16:01:23 +08:00