From 2bf4134f7e8ebc9568848ff779b78fa723391862 Mon Sep 17 00:00:00 2001 From: YunHorn Technology Date: Mon, 15 Jul 2024 15:19:33 +0800 Subject: [PATCH] --- good over stay state and duration display and upload --- Core/Inc/sts_lamp_bar.h | 4 +- Core/Inc/sys_conf.h | 4 +- Core/Src/yunhorn_sts_process.c | 9 +++- LoRaWAN/App/lora_app.c | 78 ++++++++++++++++++++++++---------- 4 files changed, 67 insertions(+), 28 deletions(-) diff --git a/Core/Inc/sts_lamp_bar.h b/Core/Inc/sts_lamp_bar.h index c6eeaf6..574639b 100644 --- a/Core/Inc/sts_lamp_bar.h +++ b/Core/Inc/sts_lamp_bar.h @@ -30,8 +30,8 @@ extern "C" { #define DEFAULT_SATURATION 50 // 0 - 100 S:Saturation, 0~100 #define DEFAULT_BRIGHTNESS 50 // 0 - 100 V:Value of lightness, 0~100 #define DEFAULT_LUMINANCE_LEVEL (30) -#define STS_Status_Door_Close (1) //Normal Close NC:Open -#define STS_Status_Door_Open (0) //Normal Close NC:Close +#define STS_Status_Door_Close (0) //Normal Close NC:Open **2024-07-15 changed +#define STS_Status_Door_Open (1) //Normal Close NC:Close **2024-07-15 changed #define STS_Status_SOS_Pushdown (0) //Normal Open NO:Open #define STS_Status_SOS_Release (1) //Normal Open NO:Close diff --git a/Core/Inc/sys_conf.h b/Core/Inc/sys_conf.h index 6059cf0..9a4f819 100644 --- a/Core/Inc/sys_conf.h +++ b/Core/Inc/sys_conf.h @@ -50,7 +50,7 @@ extern "C" { * #define VLEVEL_M 2 functional traces * #define VLEVEL_H 3 all traces */ -#define VERBOSE_LEVEL VLEVEL_M +#define VERBOSE_LEVEL VLEVEL_L /** * @brief Enable trace logs @@ -78,7 +78,7 @@ extern "C" { * @brief Enable/Disable MCU Debugger pins (dbg serial wires) * @note by HW serial wires are ON by default, need to put them OFF to save power */ -#define DEBUGGER_ENABLED 1 +#define DEBUGGER_ENABLED 0 /** * @brief Disable Low Power mode diff --git a/Core/Src/yunhorn_sts_process.c b/Core/Src/yunhorn_sts_process.c index 58e5d29..2023cc2 100644 --- a/Core/Src/yunhorn_sts_process.c +++ b/Core/Src/yunhorn_sts_process.c @@ -547,7 +547,9 @@ void STS_PRESENCE_SENSOR_Prepare_Send_Data(STS_OO_SensorStatusDataTypeDef *senso } sensor_data->unconscious_state=(sts_fall_rising_detected_result == STS_PRESENCE_UNCONSCIOUS)? 1:0; sensor_data->over_stay_state = sts_o7_sensorData.over_stay_state; - sensor_data->over_stay_duration = sts_o7_sensorData.over_stay_duration; + sensor_data->over_stay_duration = sts_o7_sensorData.event_sensor1_duration; + + APP_LOG(TS_OFF,VLEVEL_L,"\r\n ... Over Stay Duration=%u Sec\r\n", sensor_data->over_stay_duration); sensor_data->fall_state = sts_fall_rising_detected_result; if (sts_fall_rising_detected_result == STS_PRESENCE_FALL) @@ -857,12 +859,14 @@ void STS_O5_SENSOR_Read(STS_OO_SensorDataTypeDef *oo_data) void OnSensor1StateChanged(void) { SysTime_t sensor_event_time = SysTimeGetMcuTime(); - if (sts_hall1_read==STS_Status_Door_Close) + if (sts_hall1_read == STS_Status_Door_Close) { sts_o7_sensorData.event_sensor1_start_time = sensor_event_time.Seconds; sts_o7_sensorData.event_sensor1_duration = 0; + APP_LOG(TS_OFF, VLEVEL_L, "Door Closed ---Timer start: %u\r\n",sts_o7_sensorData.event_sensor1_start_time ); } else if (sts_hall1_read==STS_Status_Door_Open) { + sts_o7_sensorData.event_sensor1_start_time = 0; sts_o7_sensorData.event_sensor1_stop_time = sensor_event_time.Seconds; //sts_o7_sensorData.event_sensor1_duration = 0; } @@ -879,6 +883,7 @@ void OnSensor2StateChanged(void) sts_o7_sensorData.event_sensor2_duration = 0; }else if (sts_hall2_read==STS_Status_SOS_Release) { + sts_o7_sensorData.event_sensor2_start_time =0; sts_o7_sensorData.event_sensor2_stop_time = sensor_event_time.Seconds; //sts_o7_sensorData.event_sensor2_duration = 0; } diff --git a/LoRaWAN/App/lora_app.c b/LoRaWAN/App/lora_app.c index baaa861..9c85142 100644 --- a/LoRaWAN/App/lora_app.c +++ b/LoRaWAN/App/lora_app.c @@ -140,6 +140,17 @@ extern volatile uint8_t sensor_data_ready; extern volatile STS_OO_SensorStatusDataTypeDef sts_o7_sensorData; extern volatile float sts_distance_rss_distance, sts_sensor_install_height; +//#define STS_Status_Door_Close (1) //Normal Close NC:Open **2024-07-15 changed +//#define STS_Status_Door_Open (0) //Normal Close NC:Close **2024-07-15 changed + +char sts_door_status_code[2][10]={ + "Close", + "Open " +}; +char sts_sos_status_code[2][10]={ + "PushDown", + "No_Push" +}; char sts_lamp_color_code[15][15]={ "Dark", "Green", @@ -670,6 +681,7 @@ void LoRaWAN_Init(void) UTIL_TIMER_PERIODIC, OnYunhornSTSHeartBeatTimerEvent, NULL); UTIL_TIMER_Start(&YunhornSTSHeartBeatTimer); + UTIL_TIMER_Start(&STSDurationCheckTimer); #else UTIL_TIMER_Create(&YunhornSTSSamplingCheckTimer, @@ -690,17 +702,19 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) case HALL1_Pin: if (sts_reed_hall_1_changed) { - HAL_Delay(30); //de-bouncing sts_hall1_read = HALL1_STATE; + HAL_Delay(30); //de-bouncing + if (sts_hall1_read == HALL1_STATE) { SysTime_t current_event_time = SysTimeGetMcuTime(); - if ((last_sts_hall1_read !=sts_hall1_read) && ((current_event_time.Seconds - sts_o7_sensorData.event_sensor1_start_time)>5)&&((current_event_time.Seconds - sts_o7_sensorData.event_sensor1_stop_time)>5)) + if ((last_sts_hall1_read !=sts_hall1_read) && ((current_event_time.Seconds - sts_o7_sensorData.event_sensor1_start_time)>5) + &&((current_event_time.Seconds - sts_o7_sensorData.event_sensor1_stop_time)>5)) { - if (((current_event_time.Seconds - sts_o7_sensorData.event_sensor1_start_time) > 5) || ((current_event_time.Seconds - sts_o7_sensorData.event_sensor1_stop_time) > 5)) - // if (last_sts_hall1_read !=sts_hall1_read) + //if (((current_event_time.Seconds - sts_o7_sensorData.event_sensor1_start_time) > 5) || ((current_event_time.Seconds - sts_o7_sensorData.event_sensor1_stop_time) > 5)) + // if (last_sts_hall1_read !=sts_hall1_read) { - APP_LOG(TS_OFF, VLEVEL_L, "\n\n Door Contact Read = %02x --%20s\r\n", HALL1_STATE, (HALL1_STATE==STS_Status_Door_Close)?"Door Closed":"Door Opened"); + APP_LOG(TS_OFF, VLEVEL_L, "\n\n Door Contact Read = %02x --%s\r\n", sts_hall1_read, sts_door_status_code[sts_hall1_read]); OnSensor1StateChanged(); /* Note: when "EventType == TX_ON_TIMER" this GPIO is not initialized */ // if (EventType == TX_ON_EVENT) @@ -724,12 +738,12 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) HAL_Delay(30); //de-bouncing if (sts_hall2_read == HALL2_STATE) { - SysTime_t current_event_time = SysTimeGetMcuTime(); + //SysTime_t current_event_time = SysTimeGetMcuTime(); if (last_sts_hall2_read != sts_hall2_read) { - if (((current_event_time.Seconds - sts_o7_sensorData.event_sensor2_start_time) > 2) || ((current_event_time.Seconds - sts_o7_sensorData.event_sensor2_stop_time) > 2)) + //if (((current_event_time.Seconds - sts_o7_sensorData.event_sensor2_start_time) > 2) || ((current_event_time.Seconds - sts_o7_sensorData.event_sensor2_stop_time) > 2)) { - APP_LOG(TS_OFF, VLEVEL_L, "\n\n SOS Button Read = %02x --%20s\r\n", HALL2_STATE, (HALL2_STATE==STS_Status_SOS_Pushdown)?"SOS Pushdown":"SOS Released"); + APP_LOG(TS_OFF, VLEVEL_L, "\n\n SOS Button Read = %02x --%s\r\n", sts_hall2_read, sts_sos_status_code[sts_hall2_read]); OnSensor2StateChanged(); //sensor_data_ready =1; UTIL_SEQ_SetTask((1 << CFG_SEQ_Task_YunhornSTSEventP1), CFG_SEQ_Prio_0); @@ -949,22 +963,22 @@ static void SendTxData(void) char colorshow[30]=""; strcpy(colorshow, (ich==0)?"":sts_lamp_color_code[ich]); - APP_LOG(TS_OFF, VLEVEL_M, + APP_LOG(TS_OFF, VLEVEL_L, "\r\n######| Color = %s%s | Mode = %5s |\r\n",(char *)colorshow, sts_lamp_color_code[icl], (char*)sts_work_mode_code[sensorData.workmode]); if (sts_work_mode == STS_UNI_MODE) { - APP_LOG(TS_OFF, VLEVEL_M, - "\r\n######| S1-DoorOpen | S2-Motion | S3-SOS | S4 |Distance(mm) | MotionScore| Unconscious | Over_Stay_(min) | Fall Detected|" - "\r\n######| %1d | %1d | %1d | %1d | %04d | %04d | %1d | %4d | %1d |\r\n", - sensorData.state_sensor1_on_off, sensorData.state_sensor2_on_off,sensorData.state_sensor3_on_off, sensorData.state_sensor4_on_off, + APP_LOG(TS_OFF, VLEVEL_L, + "\r\n######| S1-Door | S2-Motion | S3-SOS | S4 |Distance(mm) | MotionScore| Unconscious | Over_Stay_(min) | Fall Detected|" + "\r\n######| %s | %1d | %s | %1d | %04d | %04d | %1d | %4d | %1d |\r\n", + sts_door_status_code[sensorData.state_sensor1_on_off], sensorData.state_sensor2_on_off,sts_sos_status_code[sensorData.state_sensor3_on_off], sensorData.state_sensor4_on_off, (uint16_t)sensorData.rss_presence_distance,(uint16_t)sensorData.rss_presence_score, - sensorData.unconscious_state, sensorData.over_stay_duration/60, sensorData.fall_state ); + sensorData.unconscious_state, sensorData.over_stay_duration, sensorData.fall_state ); } else if (sts_work_mode == STS_DUAL_MODE) { - APP_LOG(TS_OFF, VLEVEL_M, - "\r\n######| S1-DoorOpen | S2-Motion | S3-SOS |\r\n" - "\r\n######| %1d | %1d | %1d |\r\n", - sensorData.state_sensor1_on_off, sensorData.state_sensor2_on_off,sensorData.state_sensor3_on_off); + APP_LOG(TS_OFF, VLEVEL_L, + "\r\n######| S1-Door | S2-Motion | S3-SOS |\r\n" + "\r\n######| %s | %1d | %s |\r\n", + sts_door_status_code[sensorData.state_sensor1_on_off], sensorData.state_sensor2_on_off,sts_sos_status_code[sensorData.state_sensor3_on_off]); } } @@ -1125,27 +1139,47 @@ static void OnYunhornSTSDurationCheckTimerEvent(void *context) if (STS_Status_Door_Close==sts_hall1_read) + { sts_o7_sensorData.event_sensor1_duration = current_time.Seconds - sts_o7_sensorData.event_sensor1_start_time; - if (sts_o7_sensorData.event_sensor1_duration > sts_occupancy_overtime_threshold_in_10min*600) + } else { + sts_o7_sensorData.event_sensor1_duration = 0; + } + //if (sts_o7_sensorData.event_sensor1_duration > sts_occupancy_overtime_threshold_in_10min*600) + if (sts_o7_sensorData.event_sensor1_duration > sts_occupancy_overtime_threshold_in_10min*60) //for debug { sts_o7_sensorData.over_stay_state = 1; sts_o7_sensorData.over_stay_duration = sts_o7_sensorData.event_sensor1_duration; + + APP_LOG(TS_OFF, VLEVEL_L, "\r\nSensor 1 Over Stay = %d Sec \r\n", (sts_o7_sensorData.over_stay_duration)); + } // to be defiend later for SOS threshold TODO XXXX if (STS_Status_SOS_Pushdown==sts_hall2_read) + { sts_o7_sensorData.event_sensor2_duration = current_time.Seconds - sts_o7_sensorData.event_sensor2_start_time; - if (sts_o7_sensorData.event_sensor2_duration > sts_occupancy_overtime_threshold_in_10min*600) + } else { + sts_o7_sensorData.event_sensor2_duration =0; + } + //if (sts_o7_sensorData.event_sensor2_duration > sts_occupancy_overtime_threshold_in_10min*600) + if (sts_o7_sensorData.event_sensor2_duration > sts_occupancy_overtime_threshold_in_10min*60) //for debug { sts_o7_sensorData.over_stay_state = 1; - sts_o7_sensorData.over_stay_duration = sts_o7_sensorData.event_sensor2_duration; + // 2024-07-15 update, no overwrite sensor1 duration value + //sts_o7_sensorData.over_stay_duration = sts_o7_sensorData.event_sensor2_duration; + // } // to be defiend later for SOS threshold TODO XXXX if (sts_rss_result==STS_RESULT_MOTION) + { sts_o7_sensorData.event_sensor3_motion_duration = current_time.Seconds - sts_o7_sensorData.event_sensor3_motion_start_time; - if (sts_o7_sensorData.event_sensor3_motion_duration > sts_occupancy_overtime_threshold_in_10min*600) + } else { + sts_o7_sensorData.event_sensor3_motion_duration =0; + } + //if (sts_o7_sensorData.event_sensor3_motion_duration > sts_occupancy_overtime_threshold_in_10min*600) + if (sts_o7_sensorData.event_sensor3_motion_duration > sts_occupancy_overtime_threshold_in_10min) //for debug { sts_o7_sensorData.occupancy_over_stay_state = 1; sts_o7_sensorData.occupancy_duration =sts_o7_sensorData.event_sensor3_motion_duration;