diff --git a/Core/Inc/sts_lamp_bar.h b/Core/Inc/sts_lamp_bar.h index f368f60..25555b3 100644 --- a/Core/Inc/sts_lamp_bar.h +++ b/Core/Inc/sts_lamp_bar.h @@ -94,14 +94,7 @@ void STS_Lamp_Bar_Scoller(uint8_t color, uint8_t luminance_level); void STS_Lamp_Bar_Set_STS_RGB_Color(uint8_t sts_lamp_color, uint8_t luminance_level); void STS_Lamp_Bar_Refresh(void); -void STS_YunhornSTSEventP1_Process(void); -void STS_YunhornSTSEventP2_Process(void); -void STS_YunhornSTSEventP3_Process(void); -void STS_YunhornSTSEventP4_Process(void); -void STS_YunhornSTSEventP5_Process(void); -void STS_YunhornSTSEventP6_Process(void); -void STS_YunhornSTSEventP7_Process(void); -void STS_YunhornSTSEventP8_Process(void); + #ifdef __cplusplus } diff --git a/Core/Inc/yunhorn_sts_sensors.h b/Core/Inc/yunhorn_sts_sensors.h index 89ffce9..45a921d 100644 --- a/Core/Inc/yunhorn_sts_sensors.h +++ b/Core/Inc/yunhorn_sts_sensors.h @@ -580,6 +580,14 @@ void OnRestoreSTSCFGContextProcess(void); * @brief Read duty cycle level count from flash */ //uint8_t * read_duty_cycle_level_from_flash(uint8_t *flash_code_duty_cycle_level, uint32_t Save_Config_Flash_Addr); +void STS_YunhornSTSEventP1_Process(void); +void STS_YunhornSTSEventP2_Process(void); +void STS_YunhornSTSEventP3_Process(void); +void STS_YunhornSTSEventP4_Process(void); +void STS_YunhornSTSEventP5_Process(void); +void STS_YunhornSTSEventP6_Process(void); +void STS_YunhornSTSEventP7_Process(void); +void STS_YunhornSTSEventP8_Process(void); void STS_SENSOR_Power_ON(uint8_t cnt); void STS_SENSOR_Power_OFF(uint8_t cnt); diff --git a/Core/Src/sts_lamp_bar.c b/Core/Src/sts_lamp_bar.c index 38506a4..135b8c7 100644 --- a/Core/Src/sts_lamp_bar.c +++ b/Core/Src/sts_lamp_bar.c @@ -53,26 +53,26 @@ volatile WS2812B_FrameTypeDef rgb_buf = { 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} }; -volatile uint8_t sts_service_mask; -volatile uint8_t sts_work_mode = STS_DUAL_MODE; +extern volatile uint8_t sts_service_mask; +extern volatile uint8_t sts_work_mode; volatile uint8_t sts_reed_hall_ext_int = 0; volatile uint8_t sts_status_color = STS_DARK; volatile uint8_t sts_lamp_bar_color = STS_GREEN; //puColor volatile uint8_t sts_cloud_netcolor = STS_GREEN; //netColor -volatile uint8_t sts_occupancy_status; +extern volatile uint8_t sts_occupancy_status; -volatile uint8_t sts_reed_hall_result =0, sts_emergency_button_pushed=0; // inital 0 = close +extern volatile uint8_t sts_reed_hall_result, sts_emergency_button_pushed; // inital 0 = close volatile uint8_t sts_hall1_read=0,sts_hall2_read=0; // Above hall1_read == reed_hall_result, hall2_read == emergency_button -volatile uint8_t sts_tof_result_changed_flag = 0; -volatile uint8_t sts_water_leakage_result=0; -volatile uint8_t sts_water_leakage_changed_flag=0; +extern volatile uint8_t sts_tof_result_changed_flag; -volatile uint8_t sts_rss_result_changed_flag = 0, sts_hall1_changed_flag=0, sts_hall2_changed_flag=0, sts_reed_hall_changed_flag = 0; -volatile uint8_t sts_rss_result = STS_RESULT_NO_MOTION; -volatile uint8_t sts_rss_2nd_result = STS_RESULT_NO_MOTION; //2nd RSS sensor status -volatile uint8_t sts_tof_result = STS_RESULT_NO_MOTION; -volatile uint8_t last_sts_rss_result, last_sts_hall1_result, last_sts_hall2_result; -volatile uint8_t last_sts_reed_hall_result = 2; //Initial state, not 0, not 1 + + +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; extern volatile uint8_t sts_presence_fall_detection; extern volatile float sts_presence_rss_distance; @@ -81,187 +81,9 @@ extern SysTime_t mems_event_time; extern volatile uint32_t event_start_time, event_stop_time; uint8_t luminance_level = DEFAULT_LUMINANCE_LEVEL; -void STS_YunhornSTSEventP1_Process(void) -{ - STS_Lamp_Bar_Refresh(); - if ((sts_work_mode == STS_WIRED_MODE) || (sts_work_mode == STS_REEDSWITCH_MODE) || (sts_work_mode == STS_DUAL_MODE)) - { - STS_Reed_Hall_Presence_Detection(); - - if (sts_reed_hall_result == last_sts_reed_hall_result) { - sts_reed_hall_changed_flag = 0; - } else { - sts_reed_hall_changed_flag = 1; - STS_Combined_Status_Processing(); - } - - last_sts_reed_hall_result = sts_reed_hall_result; - } - -} - -void STS_YunhornSTSEventP2_Process(void) -{ - STS_Lamp_Bar_Refresh(); //TODO XXX eliminate refresh every second.... try - if ((sts_work_mode >= STS_RSS_MODE) && (sts_work_mode <= STS_TOF_RSS_MODE)) - { - STS_RSS_Smart_Presence_Detection(); - STS_Reed_Hall_Presence_Detection(); -#if 0 - if (sts_rss_result == last_sts_rss_result) { - sts_rss_result_changed_flag =0; - } else { - sts_rss_result_changed_flag =1; - last_sts_rss_result = sts_rss_result; - } -#endif - sts_rss_result_changed_flag = (sts_rss_result == last_sts_rss_result)? 0:1; - last_sts_rss_result = sts_rss_result; -#if 0 - if (sts_reed_hall_result == last_sts_reed_hall_result) - { - sts_reed_hall_changed_flag = 0; - - } else - { - sts_reed_hall_changed_flag = 1; - } -#endif - sts_hall1_changed_flag = (sts_hall1_read == last_sts_hall1_result)? 0:1; - last_sts_hall1_result = sts_hall1_read; - sts_hall2_changed_flag = (sts_hall2_read == last_sts_hall2_result)? 0:1; - last_sts_hall2_result = sts_hall2_read; - sts_reed_hall_changed_flag = sts_hall1_changed_flag|sts_hall2_changed_flag; - sts_reed_hall_result = last_sts_hall2_result|last_sts_hall1_result; - sts_reed_hall_changed_flag = (sts_reed_hall_result == last_sts_reed_hall_result)? 0:1; - - if (sts_service_mask > 0 ) { - sts_rss_result_changed_flag =0; - sts_reed_hall_changed_flag = 0; - } - - STS_Combined_Status_Processing(); - - last_sts_rss_result = sts_rss_result; - last_sts_reed_hall_result = sts_reed_hall_result; - } - -} - -void STS_Reed_Hall_Presence_Detection(void) -{ -// HAL_Delay(50); // BOUNCING ELIMIATION -#if 0 - if (STS_Reed_Hall_State == STS_Status_Door_Open) - { - sts_reed_hall_result = STS_Status_Door_Open; - - } else if (STS_Reed_Hall_State == STS_Status_Door_Close) - { - sts_reed_hall_result = STS_Status_Door_Close; - - } -#endif - sts_reed_hall_result = (sts_hall1_read==0)?STS_Status_Door_Close:STS_Status_Door_Open; - - sts_emergency_button_pushed = (sts_hall2_read==0)?STS_Status_Door_Close:STS_Status_Door_Open; //(STS_Emergency_Button_State==STS_Status_Door_Open)? 0:1; - - //sts_reed_hall_result = ((STS_Reed_Hall_State)&STS_Status_Door_Open); - -// HAL_Delay(20); // BOUNCING ELIMIATION - - sts_reed_hall_changed_flag = 0; - - sts_reed_hall_ext_int = 0; - -} - -void STS_RSS_Smart_Presence_Detection(void) -{ - STS_Lamp_Bar_Refresh(); - - //sts_presence_rss_presence_detection(); - - sts_presence_rss_fall_rise_detection(); - -// if (sts_presence_fall_detection) { -// STS_YunhornSTSFallDetection(); -// } - -} - -/* - * STS P3 process, Lamp Bar Scoller - */ -void STS_YunhornSTSEventP3_Process(void) -{ - STS_Lamp_Bar_Refresh(); - if (STS_Reed_Hall_State == STS_Status_Door_Open) - { - sts_lamp_bar_color =STS_GREEN; - } - else - { - sts_lamp_bar_color =STS_RED; - } - STS_Lamp_Bar_Scoller(sts_lamp_bar_color, luminance_level); -} - -/* - * STS SOAP Level detection Process, STS_CAP_Sensor_Detection Process - * STS_CAP_SWITCH(ON) Boost Voltage to 5V, then hold for 1000 ms - * HAL_Delay(1000) (ms) - * STS_CAP_Read_Data() Read STS_CAP_DATA state - * STS_CAP_SWITCH(OFF) Switch Off Boosted Voltage - */ -void STS_YunhornSTSEventP4_Process(void) -{ - - APP_LOG(TS_OFF, VLEVEL_L, "\r\n P4 Testing Process\r\n"); - -} - -/* - * STS P5 Process, Detection ToF distance (VL53L0X) - * - */ - -void STS_YunhornSTSEventP5_Process(void) -{ - APP_LOG(TS_OFF, VLEVEL_L, "\r\n P5 Testing Process\r\n"); -} - -/* - * STS P6 Process, Detection ToF IN-OUT PEOPLE COUNT (VL53L3X) - * - */ - -void STS_YunhornSTSEventP6_Process(void) -{ - APP_LOG(TS_OFF, VLEVEL_L, "\r\n P6 Testing Process\r\n"); -} -/* - * STS P7 Process, Detection IAQ Sensors - * air quality and odor level sensors - */ -void STS_YunhornSTSEventP7_Process(void) -{ - APP_LOG(TS_OFF, VLEVEL_L, "\r\n P7 Testing Process\r\n"); -} - - -/* - * STS P8 Process, Detection xxx Sensors - * xxx sensors - */ - -void STS_YunhornSTSEventP8_Process(void) -{ - APP_LOG(TS_OFF, VLEVEL_L, "\r\n P8 Testing Process\r\n"); -} void STS_Lamp_Bar_Set_Dark(void) @@ -438,8 +260,10 @@ void STS_Combined_Status_Processing(void) break; case STS_WIRED_MODE: // NO LAMP BAR FOR THOSE WATER LEAKAGE SENSOR OR SOAP CAPACITY SENSORS sts_status_color = STS_DARK; +#ifdef STS_M1 sts_water_leakage_result = (sts_reed_hall_result == STS_Status_Door_Open )?STS_RESULT_WATER_LEAKAGE_YES:STS_RESULT_WATER_LEAKAGE_NO; sts_water_leakage_changed_flag = 1; +#endif break; case STS_REEDSWITCH_MODE: @@ -560,14 +384,16 @@ void STS_Combined_Status_Processing(void) } } #if 1 - if ((sts_rss_result_changed_flag)|| (sts_reed_hall_changed_flag) || (sts_tof_result_changed_flag) || (sts_water_leakage_changed_flag)) + //if ((sts_rss_result_changed_flag)|| (sts_reed_hall_changed_flag) || (sts_tof_result_changed_flag) || (sts_water_leakage_changed_flag)) { sensor_data_ready = 1; STS_PRESENCE_SENSOR_Prepare_Send_Data(); sts_rss_result_changed_flag =0; sts_reed_hall_changed_flag =0; sts_tof_result_changed_flag =0; +#ifdef STS_M1 sts_water_leakage_changed_flag=0; +#endif } #endif } diff --git a/Core/Src/yunhorn_sts_presence_rss.c b/Core/Src/yunhorn_sts_presence_rss.c index 7c24082..d1c5058 100644 --- a/Core/Src/yunhorn_sts_presence_rss.c +++ b/Core/Src/yunhorn_sts_presence_rss.c @@ -399,7 +399,7 @@ int sts_presence_rss_fall_rise_detection(void) } acc_detector_presence_deactivate(handle); - APP_LOG(TS_OFF, VLEVEL_L,"First Half Presence Detection, Motion Count = %u \r\n", (int)motion_count); + APP_LOG(TS_OFF, VLEVEL_H,"First Half Presence Detection, Motion Count = %u \r\n", (int)motion_count); // ******** Second Half detection of fall down and rise up @@ -457,7 +457,7 @@ int sts_presence_rss_fall_rise_detection(void) acc_integration_sleep_ms(1000 / DEFAULT_UPDATE_RATE_PRESENCE); // DEFAULT_UPDATE_RATE); } - APP_LOG(TS_OFF, VLEVEL_L,"Second Half, Fall Rise Detection, Motion Count = %u \r\n", (int)motion_count); + APP_LOG(TS_OFF, VLEVEL_H,"Second Half, Fall Rise Detection, Motion Count = %u \r\n", (int)motion_count); sts_rss_result = (average_result > 3)? 1: 0; average_distance = (1000.0f*average_distance)/average_result; // in meters @@ -466,7 +466,7 @@ int sts_presence_rss_fall_rise_detection(void) sts_presence_rss_score = average_score; if (sts_rss_result) //if (average_score !=0) //if (sts_rss_result) { - APP_LOG(TS_OFF, VLEVEL_L,"\r\n######## Motion: %u Distance=%u mm, Score=%u Average_result=%u out of %u \r\n", + APP_LOG(TS_OFF, VLEVEL_H,"\r\n######## Motion: %u Distance=%u mm, Score=%u Average_result=%u out of %u \r\n", (uint8_t)sts_rss_result,(int) average_distance, (int)(average_score), (int)average_result, (int)iterations); } diff --git a/Core/Src/yunhorn_sts_process.c b/Core/Src/yunhorn_sts_process.c new file mode 100644 index 0000000..00ca5d5 --- /dev/null +++ b/Core/Src/yunhorn_sts_process.c @@ -0,0 +1,1069 @@ +/* USER CODE BEGIN Header */ +/** + ****************************************************************************** + * @file yunhorn_sts_process.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 */ + +/* Includes ------------------------------------------------------------------*/ +#include "stdint.h" +#include "platform.h" +#include "sys_conf.h" +#include "sys_app.h" +#include "stdio.h" +#include "stm32_systime.h" +#include "adc_if.h" +#include "gpio.h" +#include "LmHandler.h" +//#include "app_tof.h" +#include "lora_app.h" +#include "yunhorn_sts_prd_conf.h" +#include "yunhorn_sts_sensors.h" +#include "sts_cmox_hmac_sha.h" + +/* USER CODE BEGIN Includes */ +extern volatile sts_cfg_nvm_t sts_cfg_nvm; +extern volatile uint8_t sts_ac_code[20]; +volatile uint8_t sts_service_mask; +volatile uint32_t rfac_timer; +volatile uint8_t sensor_data_ready; + +extern volatile uint8_t sts_reed_hall_result; +volatile uint8_t last_sts_reed_hall_result; +volatile uint32_t event_start_time, event_stop_time; +volatile uint8_t sts_soap_level_state; +volatile STS_OO_SensorStatusDataTypeDef sts_o7_sensorData; +volatile STS_PRESENCE_SENSOR_Event_Status_t sts_o7_event_status; +volatile float sts_distance_rss_distance; +extern volatile float sts_presence_rss_distance, sts_presence_rss_score; +extern volatile uint8_t sts_hall1_read,sts_hall2_read; +volatile uint8_t sts_rss_config_updated_flag = 0; +extern volatile uint8_t mems_int1_detected, link_wakeup, link_sleep; +volatile uint32_t event_start_time=0, event_stop_time=0; +extern volatile uint8_t sts_occupancy_overtime_state; +extern volatile STS_OO_RSS_SensorTuneDataTypeDef sts_presence_rss_config; +extern volatile sts_cfg_nvm_t sts_cfg_nvm; +extern volatile uint8_t sts_fall_detection_acc_threshold, sts_fall_detection_depth_threshold, sts_occupancy_overtime_threshold; +extern volatile uint8_t sts_unconcious_state; +extern volatile uint16_t sts_unconcious_threshold, sts_unconcious_duration; +extern volatile uint8_t sts_emergency_button_pushed, sts_rss_2nd_result,sts_tof_result, sts_status_color, sts_lamp_bar_color, sts_service_mask; +extern volatile distance_measure_cfg_t distance_cfg; +extern uint8_t sts_fall_rising_detected_result; +extern volatile uint16_t sts_fall_rising_pattern_factor1; +extern volatile uint16_t sts_roc_acc_standard_variance; +extern uint8_t luminance_level; +SysTime_t mems_event_time; + +extern volatile uint8_t sts_reed_hall_ext_int; +volatile uint8_t sts_occupancy_status; +volatile uint8_t sts_hall1_changed_flag=0, sts_hall2_changed_flag=0, last_sts_hall1_result=0, last_sts_hall2_result=0; +volatile uint8_t sts_reed_hall_changed_flag = 0; +extern volatile uint8_t sts_reed_hall_result; +volatile uint8_t sts_water_leakage_result=0; +volatile uint8_t sts_water_leakage_changed_flag=0; +extern volatile uint8_t sensor_data_ready; +#ifdef YUNHORN_STS_R4_ENABLED +volatile STS_R0_SensorDataTypeDef r4_data; +#endif + +#ifdef YUNHORN_STS_O5_ENABLED + +#endif +#ifdef STS_O7 +extern volatile STS_OO_RSS_SensorTuneDataTypeDef sts_presence_rss_config; + +extern volatile distance_measure_cfg_t distance_cfg; +volatile uint8_t sts_work_mode = STS_DUAL_MODE; +extern uint8_t luminance_level; +extern volatile uint8_t sts_status_color; +extern volatile uint8_t sts_lamp_bar_color; //puColor +extern volatile uint8_t sts_cloud_netcolor; //netColor + +volatile uint8_t sts_tof_result_changed_flag = 0; + +volatile uint8_t sts_rss_result_changed_flag = 0; +volatile uint8_t sts_rss_result = STS_RESULT_NO_MOTION; +volatile uint8_t sts_rss_2nd_result = STS_RESULT_NO_MOTION; //2nd RSS sensor status +volatile uint8_t sts_tof_result = STS_RESULT_NO_MOTION; +volatile uint8_t last_sts_rss_result; +extern volatile uint8_t last_sts_rss_result; +//extern volatile uint8_t last_sts_reed_hall_result; +extern volatile uint8_t last_lamp_bar_color; +extern volatile float sts_presence_rss_distance; + +#endif + + +/* + // UTIL_SEQ_SetTask((1 << CFG_SEQ_Task_YunhornSTSEventP4), CFG_SEQ_Prio_0); + // P1 --- REEDSWITCH, HALL ELEMENT, WATER LEAKAGE + // P2 --- SEE ABOVE, RSS PRESENCE + // P3 ----LAMP BAR SCOLLER PROCESS + // P4 --- TOF DISTANCE VL53L0X simple distance + // P5 --- TOF IN OUT COUNT VL53L3X in out or duration + // P6 --- SOAP Level, capacitive measurement + // P7 --- UltraSonic, 2nd wave UltraSonic range measure + // P8 --- AIR QUALITY AND ODOR LEVEL, SMOKING DETECTION +*/ + + +/* USER CODE END Includes */ + +/* External variables ---------------------------------------------------------*/ +/* USER CODE BEGIN EV */ + +/* USER CODE END EV */ + +/* Private typedef -----------------------------------------------------------*/ +/* USER CODE BEGIN PTD */ + +/* USER CODE END PTD */ + +/* Private define ------------------------------------------------------------*/ + +/* USER CODE BEGIN PD */ + +/* USER CODE END PD */ + +/* Private macro -------------------------------------------------------------*/ +/* USER CODE BEGIN PM */ + +/* USER CODE END PM */ + +/* Private variables ---------------------------------------------------------*/ + +/* USER CODE BEGIN PV */ + +/* USER CODE END PV */ + +/* Private function prototypes -----------------------------------------------*/ +/* USER CODE BEGIN PFP */ +#if (defined(YUNHORN_STS_O6_ENABLED) && defined(USE_ACCONEER_A111)) + +#endif + +/* USER CODE END PFP */ + +/* Exported functions --------------------------------------------------------*/ + +void STS_YunhornAuthenticationCode_Process(void) +{ + if ((sts_ac_code[0] == 0x00) && (sts_ac_code[19]== 0x0)) { + APP_LOG(TS_OFF,VLEVEL_M, "Initial AC CODE blank... \r\n"); + return; + } + + sts_service_mask = (sts_hmac_verify()!= 0)? STS_SERVICE_MASK_L2:STS_SERVICE_MASK_L0; + if (sts_service_mask == STS_SERVICE_MASK_L2) { + sts_ac_code[0] = 0x0; + sts_ac_code[19]= 0x0; + } + APP_LOG(TS_OFF, VLEVEL_H, "STS_SERVICE_MASK:%d \r\n",sts_service_mask); +} + +void STS_YunhornSTSEventRFAC_Process(void) +{ + if ((sts_ac_code[0] ==0x0)&& (sts_ac_code[19]== 0x0)) + { + if ((rfac_timer >= STS_BURN_IN_RFAC) && (rfac_timer < (STS_BURN_IN_RFAC +3))) + { + APP_LOG(TS_OFF, VLEVEL_M, "\r\n -------------------RFAC Process\r\n"); + STS_SENSOR_Upload_Message(LORAWAN_USER_APP_CTRL_REPLY_PORT, 4, "RFAC"); + } + if ((rfac_timer > (STS_BURN_IN_RFAC + 2))) + { + APP_LOG(TS_OFF, VLEVEL_M, "\r\n -------------------Verify RFAC Success or Not\r\n"); + sts_service_mask = (sts_hmac_verify()!= 0)? STS_SERVICE_MASK_L2:STS_SERVICE_MASK_L0; + if (sts_service_mask == STS_SERVICE_MASK_L2) { + sts_ac_code[0] = 0x0; + sts_ac_code[19] = 0x0; + } + } + } + +} + +/* + * STS-P1 --- REEDSWITCH, HALL ELEMENT, WATER LEAKAGE + */ + +void STS_YunhornSTSEventP1_Process(void) +{ + +#if defined(YUNHORN_STS_O5_ENABLED) + + if ((sts_work_mode == STS_WIRED_MODE) || (sts_work_mode == STS_REEDSWITCH_MODE) || (sts_work_mode == STS_DUAL_MODE)) + { + STS_Reed_Hall_Presence_Detection(); + + if (sts_reed_hall_result == last_sts_reed_hall_result) { + sts_reed_hall_changed_flag = 0; + } else { + sts_reed_hall_changed_flag = 1; +#ifdef LED_ONBOARD + STS_Combined_Status_Processing(); +#endif + } + + last_sts_reed_hall_result = sts_reed_hall_result; + } + +#endif +#if (defined(YUNHORN_STS_O6_ENABLED) && defined(USE_ACCONEER_A111)) + if ((sts_work_mode == STS_WIRED_MODE) || (sts_work_mode == STS_REEDSWITCH_MODE) || (sts_work_mode == STS_DUAL_MODE)) + { + STS_Reed_Hall_Presence_Detection(); + + if (sts_reed_hall_result == last_sts_reed_hall_result) { + sts_reed_hall_changed_flag = 0; + } else { + sts_reed_hall_changed_flag = 1; + STS_Combined_Status_Processing(); + } + + last_sts_reed_hall_result = sts_reed_hall_result; + } +#endif + +} + + +/* + * STS-P2 --- SEE ABOVE, RSS PRESENCE + */ +void STS_YunhornSTSEventP2_Process(void) +{ + STS_Lamp_Bar_Refresh(); //TODO XXX eliminate refresh every second.... try + if ((sts_work_mode >= STS_RSS_MODE) && (sts_work_mode <= STS_TOF_RSS_MODE)) + { + STS_RSS_Smart_Presence_Detection(); + STS_Reed_Hall_Presence_Detection(); +#if 0 + if (sts_rss_result == last_sts_rss_result) { + sts_rss_result_changed_flag =0; + } else { + sts_rss_result_changed_flag =1; + last_sts_rss_result = sts_rss_result; + } +#endif + sts_rss_result_changed_flag = (sts_rss_result == last_sts_rss_result)? 0:1; + last_sts_rss_result = sts_rss_result; +#if 0 + if (sts_reed_hall_result == last_sts_reed_hall_result) + { + sts_reed_hall_changed_flag = 0; + + } else + { + sts_reed_hall_changed_flag = 1; + } +#endif + sts_hall1_changed_flag = (sts_hall1_read == last_sts_hall1_result)? 0:1; + last_sts_hall1_result = sts_hall1_read; + sts_hall2_changed_flag = (sts_hall2_read == last_sts_hall2_result)? 0:1; + last_sts_hall2_result = sts_hall2_read; + sts_reed_hall_changed_flag = sts_hall1_changed_flag|sts_hall2_changed_flag; + sts_reed_hall_result = last_sts_hall2_result|last_sts_hall1_result; + sts_reed_hall_changed_flag = (sts_reed_hall_result == last_sts_reed_hall_result)? 0:1; + + if (sts_service_mask > 0 ) { + sts_rss_result_changed_flag =0; + sts_reed_hall_changed_flag = 0; + } + + STS_Combined_Status_Processing(); + + last_sts_rss_result = sts_rss_result; + last_sts_reed_hall_result = sts_reed_hall_result; + } + +} + +void STS_Reed_Hall_Presence_Detection(void) +{ +// HAL_Delay(50); // BOUNCING ELIMIATION +#if 0 + if (STS_Reed_Hall_State == STS_Status_Door_Open) + { + sts_reed_hall_result = STS_Status_Door_Open; + + } else if (STS_Reed_Hall_State == STS_Status_Door_Close) + { + sts_reed_hall_result = STS_Status_Door_Close; + + } +#endif + sts_reed_hall_result = (sts_hall1_read==0)?STS_Status_Door_Close:STS_Status_Door_Open; + + sts_emergency_button_pushed = (sts_hall2_read==0)?STS_Status_Door_Close:STS_Status_Door_Open; //(STS_Emergency_Button_State==STS_Status_Door_Open)? 0:1; + + //sts_reed_hall_result = ((STS_Reed_Hall_State)&STS_Status_Door_Open); + +// HAL_Delay(20); // BOUNCING ELIMIATION + + sts_reed_hall_changed_flag = 0; + + sts_reed_hall_ext_int = 0; + +} + +void STS_RSS_Smart_Presence_Detection(void) +{ + STS_Lamp_Bar_Refresh(); + + //sts_presence_rss_presence_detection(); + + sts_presence_rss_fall_rise_detection(); + +// if (sts_presence_fall_detection) { +// STS_YunhornSTSFallDetection(); +// } + +} + +/* + * STS P3 process, Lamp Bar Scoller + */ +void STS_YunhornSTSEventP3_Process(void) +{ +#if (defined(YUNHORN_STS_O6_ENABLED) && defined(USE_ACCONEER_A111)) + if (STS_Reed_Hall_State == STS_Status_Door_Open) + { + sts_lamp_bar_color =STS_GREEN; + } + else + { + sts_lamp_bar_color =STS_RED; + } + STS_Lamp_Bar_Scoller(sts_lamp_bar_color, luminance_level); +#endif +} + + +/* + * STS SOAP Level detection Process, STS_CAP_Sensor_Detection Process + * STS_CAP_SWITCH(ON) Boost Voltage to 5V, then hold for 1000 ms + * HAL_Delay(1000) (ms) + * STS_CAP_Read_Data() Read STS_CAP_DATA state + * STS_CAP_SWITCH(OFF) Switch Off Boosted Voltage + */ +/* + * STS-P4 Detection ToF distance (VL53L0X) + * + */ +void STS_YunhornSTSEventP4_Process(void) +{ + STS_SENSOR_Power_ON(0); +#ifdef LED_ONBOARD + LED_ON; + HAL_Delay(10); + LED_OFF; +#endif +#if defined(TOF_1)||defined(TOF_2) + APP_LOG(TS_OFF, VLEVEL_H, "\r\n TOF_1, TOF_2 RANGING Process\r\n"); + STS_TOF_VL53L0X_Range_Process(); +#endif + +#ifdef TOF_3 + APP_LOG(TS_OFF, VLEVEL_H, "\r\n TOF 3 RANGING Process ---- TOF250 Ranging \r\n"); + STS_TOF250_Range_Process(); +#endif + + STS_SENSOR_Power_OFF(0); + +} + + +/* + * STS P5 Process, Detection ToF IN-OUT PEOPLE COUNT (VL53L3X) + * + */ +void STS_YunhornSTSEventP5_Process(void) +{ + APP_LOG(TS_OFF, VLEVEL_L, "\r\n P5 Testing Process\r\n"); +} + +/* + * STS-P6 --- SOAP Level, capacitive measurement + * STS Soap Level Detection Process, STS_CAP_Sensor_Detection Process + * STS_CAP_SWITCH(ON) Boost Voltage to 5V, then hold for 1000 ms + * HAL_Delay(1000) (ms) + * STS_CAP_Read_Data() Read STS_CAP_DATA state + * STS_CAP_SWITCH(OFF) Switch Off Boosted Voltage + */ +void STS_YunhornSTSEventP6_Process(void) +{ + APP_LOG(TS_OFF, VLEVEL_H, "\r\n P6 Testing Process\r\n"); + STS_SENSOR_Power_ON(0); + HAL_Delay(50); +#ifdef LED_ONBOARD + LED_ON; + HAL_Delay(20); + LED_OFF; +#endif +/* 1. Sensor Power On */ +#if defined(SOAP_LEVEL_SENSOR) + HAL_GPIO_WritePin(SOAP_SWITCH_GPIO_Port, SOAP_SWITCH_Pin, GPIO_PIN_SET); + + HAL_Delay(1000); + /* 2. Read sensor state */ + + sts_soap_level_state = HAL_GPIO_ReadPin(SOAP_STATUS_GPIO_Port,SOAP_STATUS_Pin); + APP_LOG(TS_OFF, VLEVEL_L, "\r\n Soap State = %s \r\n", (sts_soap_level_state==0x0)?" +++ Liquid Detected":" --- No Liquid"); + + HAL_GPIO_WritePin(SOAP_SWITCH_GPIO_Port, SOAP_SWITCH_Pin, GPIO_PIN_RESET); + + if (sts_soap_level_state == 0x0) + { +#ifdef LED_ONBOARD + LED_ON; + HAL_Delay(20); + LED_OFF; +#endif + } + +#endif +/* 3. Sensor Power Off */ + STS_SENSOR_Power_OFF(0); +} + + +/* + * STS P7 Process, Detection IAQ Sensors + * air quality and odor level sensors + */ +void STS_YunhornSTSEventP7_Process(void) +{ + APP_LOG(TS_OFF, VLEVEL_L, "\r\n P7 Testing Process\r\n"); + STS_SENSOR_Power_ON(0); + + STS_SENSOR_Power_OFF(0); +} + + +/* + * STS P8 Process, Detection ETR Sensors, Water flow sensors, pulse counting + * PA5 sensors , TIM2_ETR, STS-M4 + */ +void STS_YunhornSTSEventP8_Process(void) +{ + APP_LOG(TS_OFF, VLEVEL_L, "\r\n P8 Testing Process\r\n"); + +} + +/* + * STS P_IO_RS485 Process, Send Out Sensor Data Via RS485 interface + * sensors + */ + +void STS_YunhornSTSEventPIORS485_Process(void) +{ + APP_LOG(TS_OFF, VLEVEL_L, "\r\n P_IO_RS485 Process\r\n"); +} + +#if 0 +void STS_Combined_Status_Processing(void) +{ + + switch (sts_work_mode) + { + case STS_NETWORK_MODE: + sts_status_color = sts_cloud_netcolor; + break; + case STS_WIRED_MODE: // NO LAMP BAR FOR THOSE WATER LEAKAGE SENSOR OR SOAP CAPACITY SENSORS + sts_status_color = STS_DARK; + sts_water_leakage_result = sts_reed_hall_result; // == STS_Status_Door_Open )?1U:0U; //STS_RESULT_WATER_LEAKAGE_YES:STS_RESULT_WATER_LEAKAGE_NO; + sts_water_leakage_changed_flag = 1; + break; + case STS_REEDSWITCH_MODE: + + sts_status_color = (sts_reed_hall_result == STS_Status_Door_Open )? STS_GREEN: STS_RED; + + break; + case STS_RSS_MODE: + if (sts_rss_result == STS_RESULT_NO_MOTION){ + sts_status_color = STS_GREEN; + } else if ((sts_rss_result == STS_RESULT_MOTION)) + { + sts_status_color = STS_RED; + } + break; + case STS_DUAL_MODE: + if ((sts_rss_result == STS_RESULT_NO_MOTION) && (sts_reed_hall_result == STS_Status_Door_Open )) + { + sts_status_color = STS_GREEN; + + } else if ((sts_rss_result == STS_RESULT_MOTION) || (sts_reed_hall_result == STS_Status_Door_Close )) + { + sts_status_color = STS_RED; + } + break; + case STS_DUAL_RSS_MODE: + if ((sts_rss_result == STS_RESULT_NO_MOTION) && (sts_rss_2nd_result == STS_RESULT_NO_MOTION)) + { + sts_status_color = STS_GREEN; + } else if ((sts_rss_result == STS_RESULT_MOTION) || (sts_rss_2nd_result == STS_RESULT_MOTION)) + { + sts_status_color = STS_RED; + } + + break; + case STS_TOF_RSS_MODE: + if ((sts_rss_result == STS_RESULT_NO_MOTION) && (sts_tof_result == STS_RESULT_NO_PRESENCE)){ + sts_status_color = STS_GREEN; + + } else if ((sts_rss_result == STS_RESULT_MOTION) || (sts_tof_result == STS_RESULT_PRESENCE)) + { + sts_status_color = STS_RED; + } + + break; +// TO-DO LIST *********************************************************** + case STS_TOF_DISTANCE_MODE: + if ((sts_tof_result == STS_RESULT_NO_PRESENCE)) { + sts_status_color = STS_GREEN; + } else if ((sts_tof_result == STS_RESULT_PRESENCE)) { + sts_status_color = STS_RED; + } + break; + case STS_TOF_PRESENCE_MODE: + if ((sts_tof_result == STS_RESULT_NO_PRESENCE)) { + sts_status_color = STS_GREEN; + } else if ((sts_tof_result == STS_RESULT_PRESENCE)) { + sts_status_color = STS_RED; + } + break; + case STS_TOF_IN_OUT_MODE: + if ((sts_tof_result == STS_RESULT_NO_PRESENCE)) { + sts_status_color = STS_GREEN; + } else if ((sts_tof_result == STS_RESULT_PRESENCE)) { + sts_status_color = STS_RED; + } + break; +// TO-DO LIST *********************************************************** + default: + break; + } + if (sts_work_mode != STS_WIRED_MODE) + { + if ((last_lamp_bar_color != sts_status_color)) + { + sts_lamp_bar_color = ((sts_service_mask == STS_SERVICE_MASK_L0)? STS_DARK: sts_status_color); + + STS_Lamp_Bar_Set_STS_RGB_Color(sts_lamp_bar_color, luminance_level); + if ((sts_service_mask == STS_SERVICE_MASK_L0) || (sts_lamp_bar_color == STS_DARK)) + { + STS_WS2812B_Refresh(); + } + + last_lamp_bar_color = sts_lamp_bar_color; + } + } else STS_Lamp_Bar_Set_Dark(); + + if ((sts_rss_result_changed_flag + sts_reed_hall_changed_flag + sts_tof_result_changed_flag +sts_water_leakage_changed_flag)) + sensor_data_ready = 1U; + STS_PRESENCE_SENSOR_Prepare_Send_Data(); + +} + +#endif + +void STS_PRESENCE_SENSOR_WakeUp_Process_Sampling(void) +{ +#if 0 + if ((sensor_data_ready ==0)) { + STS_PRESENCE_SENSOR_GetValue(); + } +#endif +} + +void STS_PRESENCE_SENSOR_After_Wake_Up() +{ + +} + + +void STS_FallDetection_LampBarProcess(void) +{ + char buf[32]={0x0}; + uint8_t i=0; + switch (sts_fall_rising_detected_result) + { + case STS_PRESENCE_LAYDOWN: + sts_lamp_bar_color = STS_YELLOW; + sts_status_color = STS_YELLOW; + sts_rss_result = STS_RESULT_MOTION; + STS_Lamp_Bar_Set_STS_RGB_Color(sts_lamp_bar_color, luminance_level); + + buf[i++] = (uint8_t)sts_lamp_bar_color; + buf[i++] = (uint8_t)sts_work_mode; + buf[i++] = (uint8_t)sts_fall_rising_detected_result; + + break; + case STS_PRESENCE_FALL: + sts_lamp_bar_color = STS_BLUE; //STS_RED_BLUE; + sts_status_color = STS_BLUE; //STS_RED_BLUE; + sts_rss_result = STS_RESULT_MOTION; + STS_Lamp_Bar_Set_STS_RGB_Color(sts_lamp_bar_color, luminance_level); + + buf[i++] = (uint8_t)sts_lamp_bar_color; + buf[i++] = (uint8_t)sts_work_mode; + buf[i++] = (uint8_t)sts_fall_rising_detected_result; + + buf[i++] = (uint8_t)(sts_fall_rising_pattern_factor1/10 + 0x30)&0xFF; + buf[i++] = (uint8_t)(sts_fall_rising_pattern_factor1%10 + 0x30)&0xFF; + buf[i++] = (uint8_t)(sts_roc_acc_standard_variance/10 + 0x30)&0xFF; + buf[i++] = (uint8_t)(sts_roc_acc_standard_variance%10 + 0x30)&0xFF; + break; + + case STS_PRESENCE_RISING: + sts_lamp_bar_color = STS_RED; // normal occupancy status + sts_status_color = STS_RED; // normal occupancy status + sts_rss_result = STS_RESULT_MOTION; + STS_Lamp_Bar_Set_STS_RGB_Color(sts_lamp_bar_color, luminance_level); + buf[i++] = (uint8_t)sts_lamp_bar_color; + buf[i++] = (uint8_t)sts_work_mode; + buf[i++] = (uint8_t)sts_fall_rising_detected_result; + + // TESTING ONLY TO BE REMOVED + buf[i++] = (uint8_t)(sts_fall_rising_pattern_factor1/10 + 0x30)&0xFF; + buf[i++] = (uint8_t)(sts_fall_rising_pattern_factor1%10 + 0x30)&0xFF; + buf[i++] = (uint8_t)(sts_roc_acc_standard_variance/10 + 0x30)&0xFF; + buf[i++] = (uint8_t)(sts_roc_acc_standard_variance%10 + 0x30)&0xFF; + break; + + default: + break; + } + + APP_LOG(TS_OFF, VLEVEL_L, "\r\n <<<<<<<<<<<<<< Fall Rise state=%d, send buf size = %d \r\n", + sts_fall_rising_detected_result, i ) + + STS_SENSOR_Upload_Message((LORAWAN_USER_APP_PORT+2), i, buf); + + sts_fall_rising_detected_result = STS_PRESENCE_NONE; + +} + +void STS_YunhornSTSFallDetection(void) +{ + //APP_LOG(TS_OFF, VLEVEL_M, "\r\n YUNHORN STS FALL DETECTION \r\n"); + + //STS_YunhornCheckStandardDeviation(); + +} + +void STS_PRESENCE_SENSOR_NVM_CFG(void) +{ + sts_presence_rss_config.default_start_m = (float)((float)sts_cfg_nvm.p[RSS_CFG_START_M]*0.1f); + sts_presence_rss_config.default_length_m = (float)((float)sts_cfg_nvm.p[RSS_CFG_LENGTH_M]*0.1f); + sts_presence_rss_config.default_threshold = (float)((float)sts_cfg_nvm.p[RSS_CFG_THRESHOLD]*0.1f); + sts_presence_rss_config.default_receiver_gain = (float)((float)sts_cfg_nvm.p[RSS_CFG_RECEIVER_GAIN]*0.01f); + + sts_presence_rss_config.default_zone_length_m = DEFAULT_ZONE_LENGTH; + sts_presence_rss_config.default_profile = (float)(sts_cfg_nvm.p[RSS_CFG_PROFILE]); + sts_presence_rss_config.default_update_rate_tracking = (float)(sts_cfg_nvm.p[RSS_CFG_RATE_TRACKING]); + sts_presence_rss_config.default_update_rate_presence = (float)(sts_cfg_nvm.p[RSS_CFG_RATE_PRESENCE]); + sts_presence_rss_config.default_hwaas = (float)(sts_cfg_nvm.p[RSS_CFG_HWAAS]); + + sts_presence_rss_config.default_nbr_removed_pc = (float)(sts_cfg_nvm.p[RSS_CFG_NBR_REMOVED_PC]); + +//filter parameter + sts_presence_rss_config.default_inter_frame_deviation_time_const = (float)(sts_cfg_nvm.p[RSS_CFG_ITE_DEVIATION]*0.1f); + sts_presence_rss_config.default_inter_frame_fast_cutoff = (float)(sts_cfg_nvm.p[RSS_CFG_ITE_FAST_CUTOFF]); + sts_presence_rss_config.default_inter_frame_slow_cutoff = (float)(sts_cfg_nvm.p[RSS_CFG_ITE_SLOW_CUTOFF]*0.01f); + sts_presence_rss_config.default_intra_frame_time_const = (float)(sts_cfg_nvm.p[RSS_CFG_ITR_TIME]); + sts_presence_rss_config.default_intra_frame_weight = (float)(sts_cfg_nvm.p[RSS_CFG_ITR_WEIGHT]*0.1f); + sts_presence_rss_config.default_output_time_const = (float)(sts_cfg_nvm.p[RSS_CFG_OUTPUT_TIME]*0.1f); +//filter parameter + + sts_presence_rss_config.default_downsampling_factor = (float)(sts_cfg_nvm.p[RSS_CFG_DOWNSAMPLING_FACTOR]); + sts_presence_rss_config.default_power_save_mode = (float)(sts_cfg_nvm.p[RSS_CFG_POWER_MODE]); + + sts_rss_config_updated_flag = STS_RSS_CONFIG_FULL; //set to 2 for FULL config effect in next detection +} + +void STS_PRESENCE_SENSOR_NVM_CFG_SIMPLE(void) +{ + sts_presence_rss_config.default_start_m = (float)(sts_cfg_nvm.p[RSS_CFG_START_M]*0.1f); + sts_presence_rss_config.default_length_m = (float)(sts_cfg_nvm.p[RSS_CFG_LENGTH_M]*0.1f); + sts_presence_rss_config.default_threshold = (float)(sts_cfg_nvm.p[RSS_CFG_THRESHOLD]*0.1f); + sts_presence_rss_config.default_receiver_gain = (float)(sts_cfg_nvm.p[RSS_CFG_RECEIVER_GAIN]*0.01f); + + sts_rss_config_updated_flag = STS_RSS_CONFIG_SIMPLE; //set to 1 for simple config effect in next detection +} + +void STS_PRESENCE_SENSOR_Init_Send_Data(void) +{ + sts_o7_sensorData.lamp_bar_color = STS_GREEN; + sts_o7_sensorData.workmode = STS_DUAL_MODE; + + sts_o7_sensorData.state_sensor1_on_off = 0x0; + sts_o7_sensorData.state_sensor2_on_off = 0x0; + sts_o7_sensorData.state_sensor3_on_off = 0x0; + sts_o7_sensorData.state_sensor4_on_off = 0x0; + sts_o7_sensorData.rss_presence_distance = 0x0; + sts_o7_sensorData.rss_presence_score = 0x0; + sts_o7_sensorData.fall_state = STS_PRESENCE_NONE; + sts_o7_sensorData.fall_speed = 0x0; + sts_o7_sensorData.fall_gravity = 0x0; + sts_o7_sensorData.event_start_time = 0x0; + sts_o7_sensorData.event_stop_time = 0x0; + sts_o7_sensorData.overtime = 0x0; + sts_o7_sensorData.over_stay_duration = 0x0; + sts_o7_sensorData.unconcious_state = 0x0; + sts_o7_sensorData.unconcious_duration = 0x0; + + sts_o7_sensorData.battery_Pct = 99; // 99% as init value + sts_o7_sensorData.dutycycletimelevel = 1; + sensor_data_ready = 0; +} +void STS_PRESENCE_SENSOR_Prepare_Send_Data(void) +{ + sts_o7_sensorData.lamp_bar_color = sts_lamp_bar_color; + sts_o7_sensorData.workmode = sts_work_mode; + sts_o7_sensorData.state_sensor1_on_off = sts_reed_hall_result; // sts_hall1_read + sts_o7_sensorData.state_sensor2_on_off = sts_rss_result; + sts_o7_sensorData.state_sensor3_on_off = sts_emergency_button_pushed; //sts_hall2_read + sts_o7_sensorData.state_sensor4_on_off = sts_rss_2nd_result; + if (sts_rss_result == STS_RESULT_MOTION) + { + sts_o7_sensorData.rss_presence_distance = (uint16_t)(sts_presence_rss_distance)&0xFFFF; + sts_o7_sensorData.rss_presence_score = (uint16_t)(sts_presence_rss_score)&0xFFFF; + // uint8_t sts_unconcious_state; + // uint16_t sts_unconcious_threshold, sts_unconcious_threshold_duration; + if (sts_presence_rss_score < sts_unconcious_threshold) + { + sts_o7_sensorData.unconcious_state = sts_presence_rss_score; + } else + { + sts_o7_sensorData.unconcious_state = 0; + } + } else { + sts_o7_sensorData.rss_presence_distance = 0x0; + sts_o7_sensorData.rss_presence_score = 0x0; + } + sts_o7_sensorData.fall_state = sts_fall_rising_detected_result; + if (sts_fall_rising_detected_result != STS_PRESENCE_NONE) + { + sts_o7_sensorData.fall_speed = (uint8_t)sts_fall_rising_pattern_factor1; + sts_o7_sensorData.fall_gravity = (uint8_t)sts_roc_acc_standard_variance; + } + + // For occupancy over time process + SysTime_t occupy_check_time = SysTimeGetMcuTime(); + if ((sts_occupancy_overtime_threshold != 0) && (event_start_time !=0)) + { + uint32_t check_time_tmp = occupy_check_time.Seconds - event_start_time; + + //APP_LOG(TS_OFF, VLEVEL_L, "\r\n Check time at %6u Seconds, time lag =%6u, Started at %6u \r\n", occupy_check_time.Seconds, check_time_tmp, event_start_time); + + if (check_time_tmp > sts_occupancy_overtime_threshold*60) + { + sts_occupancy_overtime_state = 1U; + sts_o7_sensorData.overtime = sts_occupancy_overtime_state; + sts_o7_sensorData.over_stay_duration = check_time_tmp; + + sts_status_color = STS_RED_BLUE; + sts_lamp_bar_color = STS_RED_BLUE; + + STS_Lamp_Bar_Refresh(); + } + } else + { + sts_occupancy_overtime_state = 0U; + } + +} +#if 0 +void STS_PRESENCE_SENSOR_Read(STS_OO_SensorStatusDataTypeDef *oo_data) +{ + oo_data->lamp_bar_color = (uint8_t)sts_o7_sensorData.lamp_bar_color; + oo_data->workmode = (uint8_t)sts_o7_sensorData.workmode; + + oo_data->state_sensor1_on_off = (uint8_t)sts_o7_sensorData.state_sensor1_on_off; + oo_data->state_sensor2_on_off = (uint8_t)sts_o7_sensorData.state_sensor2_on_off; + oo_data->state_sensor3_on_off = (uint8_t)sts_o7_sensorData.state_sensor3_on_off; + oo_data->state_sensor4_on_off = (uint8_t)sts_o7_sensorData.state_sensor4_on_off; + oo_data->rss_presence_distance = (uint16_t)sts_o7_sensorData.rss_presence_distance; + oo_data->rss_presence_score = (uint16_t)sts_o7_sensorData.rss_presence_score; + oo_data->fall_state = (uint8_t)sts_o7_sensorData.fall_state; + oo_data->event_start_time = (uint32_t)sts_o7_sensorData.event_start_time; + oo_data->event_stop_time = (uint32_t)sts_o7_sensorData.event_stop_time; + oo_data->overtime = (uint8_t)sts_o7_sensorData.overtime; + oo_data->over_stay_duration = (uint16_t)sts_o7_sensorData.over_stay_duration; + + oo_data->battery_Pct = (uint8_t)sts_o7_sensorData.battery_Pct; + oo_data->dutycycletimelevel = (uint8_t)sts_o7_sensorData.dutycycletimelevel; + +} +#endif + +/** + * @brief Initializes the motion sensors + * @param Instance Motion sensor instance + * @param Functions Motion sensor functions. Could be : + * - MOTION_ACCELERO for instance + * @retval BSP status + */ +void STS_PRESENCE_SENSOR_Init(void) +{ + APP_LOG(TS_ON, VLEVEL_M, "##### YunHorn SmarToilets(r) Presence Sensor Started\r\n"); + + sts_o7_sensorData.workmode = (uint8_t)STS_DUAL_MODE; + sts_o7_sensorData.lamp_bar_color = (uint8_t)STS_GREEN; + sts_o7_sensorData.battery_Pct = 99; + sts_o7_sensorData.dutycycletimelevel = 1; + sts_o7_sensorData.event_start_time = 0; + sts_o7_sensorData.event_stop_time = 0; + sts_o7_sensorData.overtime = 0; + sts_o7_sensorData.over_stay_duration = 0; + + STS_SENSOR_Power_ON(0); + STS_PRESENCE_SENSOR_REEDSWITCH_HALL_Init(); + STS_PRESENCE_SENSOR_TOF_Init(); + STS_PRESENCE_SENSOR_RSS_Init(); + + mems_int1_detected=0; +} + +void STS_PRESENCE_SENSOR_TOF_Init(void) +{ + APP_LOG(TS_ON, VLEVEL_H, "##### YunHorn SmarToilets(r) MEMS TOF Initializing \r\n"); + +} +void STS_PRESENCE_SENSOR_REEDSWITCH_HALL_Init(void) +{ + + APP_LOG(TS_ON, VLEVEL_H, "##### YunHorn SmarToilets(r) REED SWITCH HALL ELEMENT Initializing \r\n"); + +} +void STS_PRESENCE_SENSOR_RSS_Init(void) +{ + APP_LOG(TS_ON, VLEVEL_H, "##### YunHorn SmarToilets(r) MEMS RSS Initializing \r\n"); + + STS_SENSOR_Power_ON(0); + + STS_PRESENCE_SENSOR_NVM_CFG(); + + sts_rss_config_updated_flag = STS_RSS_CONFIG_DEFAULT; + + mems_int1_detected=0; + +} + + + +#ifdef STS_M7 +void STS_PRESENCE_SENSOR_Get_Event_Status(STS_PRESENCE_SENSOR_Event_Status_t *Status) +{ + uint8_t int_source=0; + SysTime_t mems_event_time; + //int_source = ADXL345_GetRegisterValue(ADXL345_REG_INT_SOURCE); + + if (int_source & 0x10) { + mems_event_time = SysTimeGetMcuTime(); + Status->WakeUpStatus = 1U; + event_start_time = mems_event_time.Seconds; + } + else { + Status->WakeUpStatus =0U; + } + + if (int_source & 0x08) { + mems_event_time = SysTimeGetMcuTime(); + Status->SleepStatus = 1U; + event_stop_time = mems_event_time.Seconds; + } + else { + Status->SleepStatus = 0U; + + } + + +} +#endif + +void STS_PRESENCE_SENSOR_Distance_Measure_Process(void) +{ + uint8_t exit_status = EXIT_SUCCESS, i=0; + + APP_LOG(TS_OFF, VLEVEL_H, "\r\n ****start_m=%u length_m=%u profile=%u hwaas=%u \r\n", + (unsigned int)(distance_cfg.start_m*1000),(unsigned int)(distance_cfg.length_m*1000), + (unsigned int)(distance_cfg.acc_profile),(unsigned int)(distance_cfg.hwaas)); + do + { + exit_status = sts_distance_rss_detector_distance(); + HAL_Delay(100); + i++; + } while ((exit_status == EXIT_FAILURE) && (i < 1)); + +} + + + +void STS_PRESENCE_SENSOR_Function_Test_Process(uint8_t *self_test_result, uint8_t count) +{ + uint8_t bring_up_result[20]; + STS_SENSOR_Power_ON(0); + HAL_Init(); + MX_GPIO_Init(); + HAL_Delay(150); //wait for sensor ready + + for (uint8_t i=0;i < count; i++) //while(1) + { + STS_Lamp_Bar_Self_Test_Simple(); + + sts_presence_rss_bring_up_test(bring_up_result); + + HAL_Delay(5000); + + STS_PRESENCE_SENSOR_Distance_Measure_Process(); + + } + + memcpy(self_test_result,bring_up_result, 10); + mems_int1_detected=0; +} + +uint8_t STS_SENSOR_MEMS_Get_ID(uint8_t *devID) +{ + uint8_t scanned_id[2] = {0x0,0x0}; + +#ifdef YUNHORN_STS_O7_ENABLED + if (hal_test_spi_read_chipid(scanned_id)) + { + devID[0] = scanned_id[0]; + devID[1] = scanned_id[1]; + APP_LOG(TS_OFF, VLEVEL_H,"\r\n--------------devID = %02x %02x \r\n",devID[0], devID[1]); + return true; + } + else + { + return false; + } +#endif + +} + +void STS_SENSOR_Power_ON(uint8_t cnt) +{ + switch (cnt) { + case 0: + case 1: + case 2: + PME_ON; + break; + default: + break; + } + +} +void STS_SENSOR_Power_OFF(uint8_t cnt) +{ + switch (cnt) { + case 0: + case 1: + case 2: + PME_OFF; + break; + default: + break; + } +} + +void STS_SENSOR_MEMS_Reset(uint8_t cnt) +{ + switch (cnt) { + case 0: + case 1: + case 2: + HAL_GPIO_TogglePin(MEMS_POWER_GPIO_Port, MEMS_POWER_Pin); + break; + default: + break; + } + +} + +#ifdef STS_R4 +void YUNHORN_STS_R4_SENSOR_Read(STS_R0_SensorDataTypeDef *r4_data) +{ +#ifdef SOAP_LEVEL_SENSOR + r4_data->on_off_event = (sts_soap_level_state==0x0)? 1:0; + r4_data->measure_tech = 0x0; // capacity measure +#endif + + sensor_data_ready = 0x1; +} + +void YUNHORN_STS_R4_SENSOR_Init(void) +{ + r4_data.on_off_event = 0x0; + r4_data.measure_tech = 0x0; + sensor_data_ready = 0x0; +} +#endif + +#if defined(YUNHORN_STS_R4_ENABLED) +void STS_R0_SENSOR_Read(STS_R0_SensorDataTypeDef *r0_data) +{ +#ifdef SOAP_LEVEL_SENSOR + r0_data->on_off_event = sts_soap_level_state; + r0_data->measure_tech = 0; /* 0: capacit, 1:dTof, 2: ultrasonic */ +#elif defined(TOF_1)||defined(TOF_2)||defined(TOF_3) + r0_data->measure_tech = 1; /* 0: capacit, 1:dTof, 2: ultrasonic */ + r0_data->distance_mm = sts_tof_distance_data[0]; + r0_data->distance1_mm = sts_tof_distance_data[1]; + r0_data->distance2_mm = sts_tof_distance_data[2]; +#elif defined(ROCTEC_R5) + r0_data->distance1_mm = sts_tof_distance_data[0]+sts_tof_distance_data[1]+sts_tof_distance_data[2]; +#endif + + sensor_data_ready = 1; +} +#endif + +void STS_SENSOR_NVM_CFG(void) +{ + +} + +void STS_SENSOR_NVM_CFG_SIMPLE(void) +{ + +} + +#if defined(YUNHORN_STS_O5_ENABLED) +void STS_O5_SENSOR_Read(STS_OO_SensorDataTypeDef *oo_data) +{ + + oo_data->state_sensor1_on_off = sts_reed_hall_result; + + + sensor_data_ready = 1; +} +#endif + +/* USER CODE BEGIN EF */ + +/* USER CODE END EF */ + +/* Private Functions Definition -----------------------------------------------*/ +/* USER CODE BEGIN PrFD */ + +/* USER CODE END PrFD */ diff --git a/LoRaWAN/App/lora_app.c b/LoRaWAN/App/lora_app.c index 1a1cb11..18d3b98 100644 --- a/LoRaWAN/App/lora_app.c +++ b/LoRaWAN/App/lora_app.c @@ -51,7 +51,9 @@ volatile uint8_t sts_ac_code[YUNHORN_STS_AC_CODE_SIZE]={0x0}; extern hmac_result_t hmac_result; extern volatile uint8_t sts_reed_hall_ext_int; extern volatile uint8_t sts_hall1_read, sts_hall2_read; +volatile uint8_t sts_reed_hall_result =0, sts_emergency_button_pushed=0; // inital 0 = close extern volatile uint8_t sts_work_mode, sts_cloud_netcolor, sts_lamp_bar_color, sts_status_color; +extern volatile uint8_t sts_rss_result_changed_flag, sts_hall1_changed_flag, sts_hall2_changed_flag, sts_reed_hall_changed_flag; volatile uint8_t sts_fall_detection_acc_threshold = 10, sts_fall_detection_depth_threshold=30, sts_occupancy_overtime_threshold=20; extern volatile uint32_t event_start_time, event_stop_time; extern volatile uint16_t sts_unconcious_threshold; @@ -631,25 +633,30 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) #ifdef STS_O7 case HALL1_Pin: //sts_reed_hall_ext_int = 1; + HAL_Delay(50); //de-bouncing sts_hall1_read = HALL1_STATE; APP_LOG(TS_OFF, VLEVEL_M, "\n\n Door Contact Read = %02X \r\n", HALL1_STATE); //UTIL_SEQ_SetTask((1 << CFG_SEQ_Task_YunhornSTSEventP1), CFG_SEQ_Prio_0); /* Note: when "EventType == TX_ON_TIMER" this GPIO is not initialized */ // if (EventType == TX_ON_EVENT) { + sensor_data_ready =1; UTIL_SEQ_SetTask((1 << CFG_SEQ_Task_LoRaSendOnTxTimerOrButtonEvent), CFG_SEQ_Prio_0); } break; case HALL2_Pin: + HAL_Delay(50); //de-bouncing sts_hall2_read = HALL2_STATE; APP_LOG(TS_OFF, VLEVEL_M, "\n\n SOS Button Read = %02X \r\n", HALL2_STATE); + sensor_data_ready =1; // UTIL_SEQ_SetTask((1 << CFG_SEQ_Task_YunhornSTSEventP1), CFG_SEQ_Prio_0); UTIL_SEQ_SetTask((1 << CFG_SEQ_Task_LoRaSendOnTxTimerOrButtonEvent), CFG_SEQ_Prio_0); break; #else case BUT1_Pin: + HAL_Delay(20); //de-bouncing //sts_reed_hall_ext_int = 1; UTIL_SEQ_SetTask((1 << CFG_SEQ_Task_YunhornSTSEventP1), CFG_SEQ_Prio_0); //sts_reed_hall_ext_int = 0; diff --git a/STM32CubeIDE/.project b/STM32CubeIDE/.project index d68803d..d85a821 100644 --- a/STM32CubeIDE/.project +++ b/STM32CubeIDE/.project @@ -528,9 +528,9 @@ copy_PARENT/Core/Src/yunhorn_sts_presence_rss_bring_up_test.c - Application/User/Core/yunhorn_sts_presence_sensor.c + Application/User/Core/yunhorn_sts_process.c 1 - copy_PARENT/Core/Src/yunhorn_sts_presence_sensor.c + copy_PARENT/Core/Src/yunhorn_sts_process.c Drivers/BSP/STM32WLxx_Nucleo/stm32wlxx_nucleo.c diff --git a/STM32CubeIDE/Release/STS_O7.bin b/STM32CubeIDE/Release/STS_O7.bin index 2cc8083..0a5b710 100644 Binary files a/STM32CubeIDE/Release/STS_O7.bin and b/STM32CubeIDE/Release/STS_O7.bin differ