 /* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    yunhorn_sts_sensors.h											   *
  * @author  Yunhorn Technology Limited Application Team					   *
  * @brief   Header for Yunhorn SmarToilets Product sensors application		   *
  ******************************************************************************
  * @attention
  * V2022-0823
  * Copyright (c) 2023 Yunhorn Technology Limited.
  * Copyright (c) 2023 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 */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __YUNHORN_STS_SENSORS_H__
#define __YUNHORN_STS_SENSORS_H__

#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/

/* USER CODE BEGIN Includes */
#include "stdint.h"
#include <stddef.h>
#include "yunhorn_sts_prd_conf.h"


/* USER CODE END Includes */

/* Exported types ------------------------------------------------------------*/
/**
  * Sensor data parameters
  */
	

enum cfg_cmd_order{
	CFG_CMD1=0,		//'Y'
	CFG_CMD2,			//'D' 'Z' 'V' 'O' 'F' 'H' 'M' 'L'
	CFG_CMD3,			// # Z {H, S, C}  M {0,1,2,3,4]
	CFG_CMD4,			// # 
	CFG_CMD5,			// 'S','M','H'
	CFG_CMD6
};
enum p_cmd_order{
	P_CMD=0,			//'P'  //P_MTM_CODE,		// #1
	P_MTM_VER,			// #1
	P_MEMS_NO,			// #2
	//P_MEM_CFG,			// #3
	P_WORK_MODE,			// #4 work mode
	P_NET_COLOR			// #5 cloud net color
};
#if	(defined(YUNHORN_STS_M7_ENABLED) || defined(YUNHORN_STS_O6_ENABLED))
enum sts_sensor_result_t {
	STS_RESULT_NO_MOTION = 0,
	STS_RESULT_MOTION,
	STS_RESULT_NO_WAVE,
	STS_RESULT_WAVE,
	STS_RESULT_NO_PRESENCE,
	STS_RESULT_PRESENCE,
	STS_RESULT_WATER_LEAKAGE_YES,
	STS_RESULT_WATER_LEAKAGE_NO,
	STS_RESULT_SOAP_LEVEL_ABOVE,
	STS_RESULT_SOAP_LEVEL_BELOW
};
enum RSS_CFG_order{
										// FOR SIMPLE CONFIG .... 8 BYTES
	RSS_CFG_START_M_H=0,
	RSS_CFG_START_M_L,

	RSS_CFG_LENGTH_M_H,
	RSS_CFG_LENGTH_M_L,

	RSS_CFG_THRESHOLD_H,
	RSS_CFG_THRESHOLD_L,

	RSS_CFG_RECEIVER_GAIN_H,
	RSS_CFG_RECEIVER_GAIN_L,
										// FOR SIMPLE CONFIG .... 8 BYTES

	RSS_CFG_PROFILE,

	RSS_CFG_RATE_TRACKING_H,
	RSS_CFG_RATE_TRACKING_L,

	RSS_CFG_RATE_PRESENCE_H,
	RSS_CFG_RATE_PRESENCE_L,

	RSS_CFG_HWAAS_H,
	RSS_CFG_HWAAS_L,

	RSS_CFG_NBR_REMOVED_PC,

	RSS_CFG_ITE_DEVIATION_H,
	RSS_CFG_ITE_DEVIATION_L,

	RSS_CFG_ITE_FAST_CUTOFF_H,
	RSS_CFG_ITE_FAST_CUTOFF_L,

	RSS_CFG_ITE_SLOW_CUTOFF_H,
	RSS_CFG_ITE_SLOW_CUTOFF_L,

	RSS_CFG_ITR_TIME_H,
	RSS_CFG_ITR_TIME_L,

	RSS_CFG_ITR_WEIGHT_H,
	RSS_CFG_ITR_WEIGHT_L,

	RSS_CFG_OUTPUT_TIME_H,
	RSS_CFG_OUTPUT_TIME_L,

	RSS_CFG_DOWNSAMPLING_FACTOR,

	RSS_CFG_POWER_MODE
};
#endif

//"P 0 0 "+"1 2 3 1 2 3 1 2 3 1 2"
// P 
// P 1 SENSOR HEAD 0
// P 1 0  SENSOR HEAD 0, VERSION 0
// P 1 0 1 2  SENSOR HEAD 0, VERSION 0, VALUE 12, 1*10+2

#ifdef YUNHORN_STS_O6_ENABLED
enum sts_ctrl_cmd_type {
	STS_CTRL_CMD_TYPE_DUTYCYCLE_LEVEL_CHANGE,
	STS_CTRL_CMD_MEMS_TUNE_TOF_VL53L0X=0x00,
	STS_CTRL_CMD_MEMS_TUNE_TOF_VL53L1X,  //0X01
	STS_CTRL_CMD_MEMS_TUNE_TOF_VL53L2X,	 //0X02
	STS_CTRL_CMD_MEMS_TUNE_TOF_VL53L3X,	 //0X03	
	STS_CTRL_CMD_MEMS_TUNE_TOF_VL53L4X,  //0X04 
	STS_CTRL_CMD_MEMS_TUNE_TOF_VL53L5X,  //0X05 
	
	STS_CTRL_CMD_MEMS_TUNE_MV_ADXL345,	 //0X06
	STS_CTRL_CMD_MEMS_TUNE_MV_ADXL365,	 //0X07
	STS_CTRL_CMD_MEMS_TUNE_MV_ADXL362,   //0X08
	STS_CTRL_CMD_MEMS_TUNE_MV_ADXL110X,  //0X09
	
	STS_CTRL_CMD_MEMS_TUNE_FMCW_TI16X,
	STS_CTRL_CMD_MEMS_TUNE_FMCW_TI68X,
	
	STS_CTRL_CMD_MEMS_TUNE_PCR_A11X,
	STS_CTRL_CMD_MEMS_TUNE_PCR_A12X,
	
	STS_CTRL_CMD_PRESENCE_SETTING,
	STS_CTRL_CMD_PRESENCE_TUNE_LED_COLOR,
	STS_CTRL_CMD_PRESENCE_TUNE_WORK_MODE,
	
	STS_CTRL_CMD_MEMS_TUNE_SMOKING_MUSIC,
	STS_CTRL_CMD_MEMS_TUNE_SMOKING_ENABLE,
	
	STS_CTRL_CMD_MEMS_TUNE_WATER_FLOW_DN,
	
	STS_CTRL_CMD_MEMS_TUNE_DIGITAL_RELAY,
	
	STS_CTRL_CMD_MEMS_TUNE_FAN_CONTROL,
	STS_CTRL_CMD_MEMS_TUNE_VENTILATION_FAN_SPEED,
	STS_CTRL_CMD_MEMS_TUNE_VENTILATION_FAN_SWING,
	
	
	STS_CTRL_CMD_MEMS_TUNE_DEVICE_COMPONENT
};			// sensor_mems_type

enum sts_ctrl_cmd_tune_para{
	STS_CTRL_CMD_MEMS_TUNE_TOF_RANGE_START=0,
	STS_CTRL_CMD_MEMS_TUNE_TOF_RANGE_END,
	STS_CTRL_CMD_MEMS_TUNE_TOF_THRESHOLD,
	STS_CTRL_CMD_MEMS_TUNE_TOF_GAIN,
	STS_CTRL_CMD_MEMS_TUNE_TOF_MAX_DISTANCE
};			// sensor_mems_tof_tune_type
#endif
	

typedef struct
{
	uint16_t distance_mm;			/*Default distance sensor measured distance in mm, min=0mm, max=2500mm. */
	uint16_t battery_mV;			/*mV, 1000mv-5000mv, regular 3300mV - 3600mV --4200mV */
	
	uint8_t on_off_event;			/* 1: liquid sensed, 0: no liquid sensed */
	uint8_t measure_tech;     /* 0: capacit, 1:dTof, 2: ultrasonic  */
	
	uint8_t distance_mm_h;		/*MSB  */
	uint8_t distance_mm_l;		/*LSB   max=255 mm for short range measure */
	uint16_t distance1_mm;		/* Additional #1 distance sensor */
	uint16_t distance2_mm;		/* Additional #2 distance sensor */

	uint8_t battery_Pct;  		/* % of battery two digits, 88% (00-99)%   */
	uint8_t	dutycycletimelevel; /* level=0,255   */
} STS_R0_SensorDataTypeDef;


#if (defined(YUNHORN_STS_O0_ENABLED) || defined(YUNHORN_STS_O1_ENABLED) || defined(YUNHORN_STS_O2_ENABLED) || defined(YUNHORN_STS_O3_ENABLED) 	|| defined(YUNHORN_STS_O4_ENABLED) || defined(YUNHORN_STS_O5_ENABLED)|| defined(YUNHORN_STS_O6_ENABLED))
typedef struct
{
	uint8_t lamp_bar_color;				/*measured color ID, 0,1,2,3,4,5,6,7,8,9 */
	uint8_t workmode;					/*work mode, 0,1,2,3,4*/
	uint8_t state_sensor1_on_off;  	/* reedswitch or hall element 0: open, 1: closed */
	uint8_t state_sensor2_on_off;  /* pcr_sensor_on_off;  1: occupancy, 0: no occupancy */
	uint8_t state_sensor3_on_off;  /* urinal_sensor_on_off  urinal sensor state */
	uint8_t state_sensor4_on_off;  /* reserved_sensor_on_off  sensor state */
	
	uint8_t battery_Pct;  				/* % of battery two digits, 88% (00-99)%   */
	uint8_t	dutycycletimelevel; 	/* level=0,255   */
	uint8_t sts_service_mask;		/* sts service mask */
} STS_OO_SensorDataTypeDef;
#endif

#ifdef YUNHORN_STS_O6_ENABLED
typedef struct
{
	float start_m;
	float length_m;
	uint8_t acc_profile;
	uint8_t hwaas;
	uint8_t downsampling;
	float sweep_average;
	float gain;
	float threshold;
	float data_output_time;
} distance_measure_cfg_t;

#define DEFAULT_START_M_2             (0.2f)
#define DEFAULT_LENGTH_M_2            (1.4f)
#define DEFAULT_UPDATE_RATE_2         (10)
#define DEFAULT_POWER_SAVE_MODE_2     ACC_POWER_SAVE_MODE_SLEEP
#define DEFAULT_DETECTION_THRESHOLD_2 (2.0f)
#define DEFAULT_NBR_REMOVED_PC_2      (0)
#define DEFAULT_ZONE_LENGTH          				(0.4f)		//default 0.4 unit(meter)

#endif

typedef struct
{
  uint8_t WakeUpStatus;
  uint8_t SleepStatus;
} STS_PRESENCE_SENSOR_Event_Status_t;

#ifdef YUNHORN_STS_O6_ENABLED

typedef struct STS_OO_RSS_SensorTuneDataTypeDef
{
	uint8_t	default_workmode;
	uint8_t	default_lamp_bar_color;

	float	default_profile;						//---[4] (4)			// 1,2,3,4,5

	float	default_start_m;						//[08](0.80f)		//default 0.2 unit(meter)			[1]
	float 	default_length_m;						//[20](2.00f)		//default 2.5
	float	default_zone_length_m;					//---[04](0.4f)		almost fixed, no need to change

	float	default_update_rate_wake_up;			//---[02](2.0f)		//default 80 unit(hz)
	float 	default_update_rate_tracking;			//[10](10.0f)		//default 80 unit(hz)				[7]
	float	default_update_rate_presence;			//[65](65.0F)		//(65.0f)		//default 80 unit(hz)
	float	default_hwaas;							//[63](63)			//default 10 unit(hz)

	float	default_threshold;						//[15](1.5f)		//default 1.5 level float			[3]

	float	default_nbr_removed_pc;					//(0)			//default (0-2)
	float 	default_power_save_mode;				// 0-4

	//filters
	float	default_inter_frame_deviation_time_const;	//[05](0.5f)		//default 0.5 unit(seconds)			[6]
	float	default_inter_frame_fast_cutoff;			//[10](10.0f)		//default 20.0 unit(hz)				[8]
	float   default_inter_frame_slow_cutoff;			//[001](0.01f)		//(0.01f) 0.2 hz unit(hz)			[9]
	float	default_intra_frame_time_const;				//[00](00)			//default 0.0 unit(seconds)
	float	default_intra_frame_weight;					//[06](00)			//default 0.6
	float	default_output_time_const;					//[05](0.5f)		//default 0.5 unit(seconds)			[5]
	//filters

	float	default_downsampling_factor;				//[2](2)			//default 1
	float	default_receiver_gain;						//[45](0.45f)		//default 0.9 gain mdB				[4]
} STS_OO_RSS_SensorTuneDataTypeDef;

#endif


#if (defined(YUNHORN_STS_M0_ENABLED) || defined(YUNHORN_STS_M1_ENABLED) || defined(YUNHORN_STS_M2_ENABLED) || defined(YUNHORN_STS_M5_ENABLED))

typedef struct
{
	uint16_t distance_mm;										/*measured distance in mm, min=0mm, max=2500mm. */
	uint16_t battery_mV;										/*mV, 1000mv-5000mv, regular 3300mV - 3600mV --4200mV */
	
	/* leakage detection   */
	uint8_t leakage_event;									/* 1: leakage detected, 0: no leakage detected */
	uint8_t leakage_cable_position;					/* distance level 0-255  from starting point to length of cable */
	uint8_t leakage_measure_mode;     			/* 0: point, 1: cable/distance/level */
	
	/*   closestool clogging detection */
	uint8_t clogging_state; 								/* 0: no clogging, 1: clogging */
	uint8_t clogging_duration_level; 				/* 0-255  duration level time in min. */
	uint16_t clogging_level_distance_mm; 		/* from measure point to measured clogging level, in mm*/
	
	/*  water flow measure */
	uint16_t water_pulse_count; 						/* water flow count within counting interval 0-255 */
	uint8_t water_pipe_diameter_dn; 				/* DN number  DN20--4", DN25--6", DN30--8"    */
	uint8_t water_count_interval; 					/* in minutes 10 min by default */
	
	/*     power relay   */
	uint8_t break_on_off;  									/* 0: off, 1: on */
	uint8_t channel_num_mask;   						/* 0000 0000 = 0x00, 0xff, 0-7, 8 channels max */
	
	/*     DI, DO digital input output */
	
	uint8_t di_do_channel_mask;   					/* 0000 0000 = 0x00, 0xff, 0-7, 8 channels max */
	
	/*    Vibration sensor */
	uint8_t vibration_on_off; 							/* 0: off, 1: on */
	uint8_t vibration_threshold_level; 			/* 0 -255 */
	
	uint16_t vb_x_freq;   									/* max vibration freq  in x axis */
	uint16_t vb_y_freq;											/* max vibration freq  in y axis */
	uint16_t vb_z_freq;											/* max vibration freq  in z axis */
	
	uint16_t vb_x_rms;   										/* max vibration rms  in x axis */
	uint16_t vb_y_rms;											/* max vibration rms  in y axis */
	uint16_t vb_z_rms;											/* max vibration rms  in z axis */
	
	
	/* ventilation fan  */
	uint8_t swing_state; 										/* 1: swing, 0: no swing */
	uint8_t speed_level;  									/* 0: off, speed 1, 2, 3 min  max , 4:local remote control released */
	
	
	uint8_t distance_mm_h;									/*MSB  */
	uint8_t distance_mm_l;									/*LSB   max=255 mm for short range measure */
	uint8_t battery_Pct;  									/* % of battery two digits, 88% (00-99)%   */
	uint8_t	dutycycletimelevel; 						/* level=0,255   */
} STS_M0_SensorDataTypeDef;

#endif
#ifdef YUNHORN_STS_O1_ENABLED
typedef enum
{
  REED_SWITCH_PIN_RESET = 0U,
  REED_SWITCH_PIN_SET
} REED_SWITCH_State;

typedef enum
{
  DOOR_CONTACT_PIN_RESET = 0U,
  DOOR_CONTACT_PIN_SET
} DOOR_CONTACT_State;

typedef enum
{
  DRY_CONTACT_PIN_RESET = 0U,
  DRY_CONTACT_PIN_SET
} DRY_CONTACT_State;
#endif
#ifdef YUNHORN_STS_M1_ENABLED
typedef enum
{
  WATER_LEAKAGE_PIN_RESET = 0U,
  WATER_LEAKAGE_PIN_SET
} WATER_LEAKAGE_State;

typedef enum
{
  HALL_ELEMENT_PIN_RESET = 0U,
  HALL_ELEMENT_PIN_SET
} HALL_ELEMENT_State;
#endif

#if (defined(YUNHORN_STS_E0_ENABLED) || defined(YUNHORN_STS_E1_ENABLED) || defined(YUNHORN_STS_E2_ENABLED) || defined(YUNHORN_STS_E3_ENABLED) || defined(YUNHORN_STS_E4_ENABLED) || defined(YUNHORN_STS_E5_ENABLED) || defined(YUNHORN_STS_E6_ENABLED) || defined(YUNHORN_STS_E7_ENABLED))

typedef struct
{
	float pressure;         /*!< in mbar */
  float temperature;      /*!< in degC - 40C ~ 125C , 0.05 C */
  float humidity;         /*!< in %  0-100 RH% */
	float distance;         /*!< in mm */
	int16_t co2_ppm;            /*!< 400 - 5200 ppm */
	int16_t tvoc_ppb;           /*!< 0 - 60000 ppb */
  uint32_t latitude;       /*!< latitude converted to binary */
  uint32_t longitude ;     /*!< longitude converted to binary */
  uint16_t altitudeGps;    /*!< in m */
  uint16_t altitudeBar ;   /*!< in m * 10 */

	float nh3_ppb;  /*  NH3 */
	float h2s_ppb;	/*  H2S */
	float ch2o_ppb;     /* CH2O */
	float no2_ppb;		/* NO2 */
	float o3_ugm3;    /* O3 ug/m3 */
	float battery;		
	uint16_t co2_ugm3;			/* ug/m3 */
	uint16_t tvoc_ppb2;      /* TVOC */
	uint16_t pm_dot5_ugm3;      
	uint16_t pm_1_ugm3;
	uint16_t pm_2dot5_ugm3;
	uint16_t pm_10_ugm3;
	/*  FOR CIGGARATE SMOKING DETECTION */
	uint16_t mq2;       /* combined smoking sensor */
	uint16_t co;				/* CO                      */ 
	uint8_t iaq;        /* IAQ index following EPD standards, HKSAR */
	
	uint8_t symbol;
	uint8_t temperature1;
	uint8_t temperature2;
	uint8_t humidity1;
	uint8_t humidity2;
	uint8_t nh31;
	uint8_t nh32;
	uint8_t h2s1;
	uint8_t h2s2;
	uint8_t battery1;
	uint8_t battery2;
	uint8_t ch2o1;
	uint8_t ch2o2;
		
	uint8_t co21;
	uint8_t co22;
		
	uint8_t tvoc1;
	uint8_t tvoc2;
		
	uint8_t pm251;
	uint8_t pm252;
		
	uint8_t pm101;
	uint8_t pm102;

	
	uint8_t battery_Pct;  				/* % of battery two digits, 88% (00-99)%   */
	uint8_t	dutycycletimelevel; 	/* level=0,255   */
} STS_E0_SensorDataTypeDef;
#endif

#if (defined(YUNHORN_STS_P0_ENABLED) || defined(YUNHORN_STS_P1_ENABLED) || defined(YUNHORN_STS_P2_ENABLED) || defined(YUNHORN_STS_P3_ENABLED))
typedef struct
{
	uint8_t people_count_num;				/*measured people number in area, max 30 */
	
	/*     bi-direction              */
	uint16_t people_in_num;						/* people in out count  in given duration */
	uint16_t people_out_num;					/* people in out count  in given duration */
	uint16_t people_in_out_interval;	/* people in out count interval level */
	
	/*     occupancy                */
	uint8_t people_in_out_state_;			/* people in or leave  state 0: leave, 1: stay */
	
	/*    queueing                       */
	uint8_t workmode; 							/* 0-5, 0:tst, 1:sensor reset, 2:power on, 3: power off */
	uint8_t tune_param1_left_x_m;		/* left range in meter   0.5 - 9.9, 1.5 */
	uint8_t tune_param2_right_x_m;		/* right range in meter   0.5 - 9.9 , 1.5 */
	uint8_t tune_param1_far_y_m;		/* far range in meter   2.5 - 9.9, 6.0*/
	uint8_t tune_param1_top_z_m;		/* top range in meter   0.5 - 9.9, 3.0 */
	uint8_t tune_param1_height_m;		/* installation height in meter   0.5 - 9.9, 2.0 */
	uint8_t tune_param1_tilt_degree;		/* tilt up/down in degree 0 - 45, 15 */
	
	uint8_t battery_Pct;  				/* % of battery two digits, 88% (00-99)%   */
	uint8_t	dutycycletimelevel; 	/* level=0,255   */
} STS_P0_SensorDataTypeDef;

#endif

/* USER CODE BEGIN ET */

/* USER CODE END ET */

/* Exported constants --------------------------------------------------------*/

/* USER CODE BEGIN EC */

/* USER CODE END EC */

/* External variables --------------------------------------------------------*/
/* USER CODE BEGIN EV */

/* USER CODE END EV */

/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */

/* USER CODE END EM */

/* Exported functions prototypes ---------------------------------------------*/
//  0 --- 10  11   12 -- 39    40 41 42 43         44-63
//            LEN  P RSS       FALL_DETECTION      AC_CODE
enum nvm_order {
	NVM_MTM1=0,
	NVM_MTM2,
	NVM_VER,
	NVM_HWV,
	NVM_PERIODICITY,
	NVM_UNIT,
	NVM_SAMPLING,
	NVM_S_UNIT,
	NVM_WORK_MODE,
	NVM_SERVICE_MASK,
	NVM_RESERVE01,								//10
	NVM_LEN,									//11, 32=0x20
		NVM_CFG_START,							//12, p[0] bytes for configs,
												//13, p[1]
												//14, p[2]
												// ...
												//39, P[27]
		NVM_FALL_DETECTION_ACC_THRESHOLD=40,	//40
		NVM_FALL_DETECTION_DEPTH_THRESHOLD,		//41
		NVM_FALL_DETECTION_RESERVE,				//42
		NVM_OCCUPANCY_OVERTIME_THRESHOLD,		//43
	NVM_AC_CODE_START=44		//STORED, NO UPLOAD
								//63,  20 bytes for AC code
};

typedef	struct sts_cfg_nvm {
	uint8_t mtmcode1;
	uint8_t mtmcode2;
	uint8_t version;
	uint8_t hardware_ver;
	uint8_t periodicity;		//count of uplink duty cycle duration high {99}
	uint8_t unit;				// uplink time unit of duty cycle duration, in 'S', 'M','H' seconds, minutes, hours
	uint8_t sampling;			// heart-beat interval or count of sampling duty cycle duration high {99}
	uint8_t s_unit;				// Heart-beat interval or sampling time unit of duty cycle duration, in 'S', 'M','H' seconds, minutes, hours
	uint8_t work_mode;
	uint8_t sts_service_mask;
	uint8_t reseve01;
	uint8_t length;				// length of following parameters except  AC CODE(20bytes)
	uint8_t p[STS_CFG_PCFG_SIZE];
	uint8_t fall_detection_acc_threshold;			// 0 - 9: 0:disable: 1-9 accelaration mg/s2
	uint8_t fall_detection_depth_threshold;				// 0 - 9: 0:disable: 1-9 fall down depth * 10 cm
	uint8_t fall_detection_reserve;
	uint8_t occupancy_overtime_threshold;		// 0 - 9： 0：disable, 1-9 occupy over time threshold * 10 min
	uint8_t ac[YUNHORN_STS_AC_CODE_SIZE];		// authorization code, 20 bytes MCU UUID coded
} sts_cfg_nvm_t;

#define	STS_SERVICE_MASK_L0		(0)		// Service normal , no mask off
#define	STS_SERVICE_MASK_L1		(1)		// service mask level 1, sensing data upload in silence mode, node appearance silence (no LED, No display, no sound, no vibration)
#define	STS_SERVICE_MASK_L2		(2)		// service mask level 2, NO sensing data upload (event or periodicity)
#define	STS_SERVICE_MASK_L3		(3)		// service mask level 3, NO responsing to cloud control command, or config change/responding, EXCEPT STS_SERVICE_MASK_ON_OFF cmd


/**
  * @brief  Store/Write/Flash Configuration in RW RAM 	
  */
void OnStoreSTSCFGContextRequest(void);

/**
  * @brief  Read config from flash
  */
void OnRestoreSTSCFGContextRequest(uint8_t *cfg_in_nvm);

void STS_REBOOT_CONFIG_Init(void);

/**
  * @brief  Apply/Process config to board
  */
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_SENSOR_Power_ON(uint8_t cnt);
void STS_SENSOR_Power_OFF(uint8_t cnt);
void STS_SENSOR_MEMS_Reset(uint8_t cnt);

void STS_PRESENCE_SENSOR_NVM_CFG(void);
void STS_PRESENCE_SENSOR_NVM_CFG_SIMPLE(void);

void STS_SENSOR_NVM_CFG_SIMPLE(void);
void STS_SENSOR_NVM_CFG(void);



#if	defined(SOAP_LEVEL)
void YUNHORN_STS_R4_SENSOR_Read(STS_R0_SensorDataTypeDef *r4_data);
void YUNHORN_STS_R4_SENSOR_Init(void);
#endif

#ifdef    YUNHORN_STS_O6_ENABLED
void STS_PRESENCE_SENSOR_Read(STS_OO_SensorDataTypeDef *o6_data);
void STS_PRESENCE_SENSOR_Prepare_Send_Data(void);

void STS_PRESENCE_SENSOR_Init(void);
void STS_PRESENCE_SENSOR_RSS_Init(void);
void STS_PRESENCE_SENSOR_REEDSWITCH_HALL_Init(void);
void STS_PRESENCE_SENSOR_TOF_Init(void);
#endif

#ifdef		YUNHORN_STS_O5_ENABLED

void STS_O5_SENSOR_Read(STS_OO_SensorDataTypeDef *oo_data);

#endif
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_YunhornSTSEventRFAC_Process(void);
void STS_YunhornAuthenticationCode_Process(void);

void STS_Reed_Hall_Presence_Detection(void);

void STS_SENSOR_Upload_Config_Invalid_Message(void);
void STS_SENSOR_Upload_Message(uint8_t appDataPort, uint8_t appBufferSize, char *appDataBuffer);
void STS_SENSOR_Auto_Responder_Process(uint8_t tlv_ver,uint8_t tlv_type, uint8_t tlv_length, uint8_t *tlv_content);
uint8_t STS_SENSOR_MEMS_Get_ID(uint8_t *devID);


#ifdef    YUNHORN_STS_O6_ENABLED
int sts_presence_rss_presence_detection(void);
int sts_distance_rss_detector_distance(void);
int acc_example_detector_distance(int argc, char *argv[]);
int acc_example_service_sparse(int argc, char *argv[]);
int acc_example_detector_distance_recorded(int argc, char *argv[]);
int sts_presence_rss_bring_up_test(uint8_t *rss_self_test_result);
int acc_ref_app_tank_level(int argc, char *argv[]);
#endif
/*
void STS_MOTION_SENSOR_Disable_Wake_Up_Detection(void);

int32_t STS_MOTION_SENSOR_Set_Wake_Up_Threshold(uint8_t Threshold);
int32_t STS_MOTION_SENSOR_Set_Wake_Up_Duration(uint8_t Duration);
int32_t STS_MOTION_SENSOR_Enable_Inactivity_Detection(STS_MOTION_SENSOR_IntPin_t IntPin);
int32_t STS_MOTION_SENSOR_Disable_Inactivity_Detection(void);
int32_t STS_MOTION_SENSOR_Set_Sleep_Duration(uint8_t Duration);

void STS_MOTION_SENSOR_Get_Event_Status(STS_MOTION_SENSOR_Event_Status_t *Status);
void STS_MOTION_SENSOR_Enable_Wake_Up_Detection(void);
void STS_MOTION_SENSOR_Initialization(void);
*/

void USER_APP_AUTO_RESPONDER_Parse(char *tlv_buf, size_t tlv_buf_size);
void STS_SENSOR_Function_Test_Process(void);
void STS_SENSOR_Distance_Test_Process(void);
void STS_PRESENCE_SENSOR_Function_Test_Process(uint8_t *self_test_result, uint8_t count);
void STS_PRESENCE_SENSOR_Distance_Measure_Process(void);


/* USER CODE BEGIN Private defines */
/* 
    In this example TIM2 input clock (TIM2CLK)  is set to APB1 clock (PCLK1),
    since APB1 prescaler is equal to 1.
      TIM2CLK = PCLK1
      PCLK1 = HCLK
      => TIM2CLK = HCLK = SystemCoreClock
    To get TIM2 counter clock at 10 KHz, the Prescaler is computed as following:
    Prescaler = (TIM2CLK / TIM2 counter clock) - 1
    Prescaler = (SystemCoreClock /10 KHz) - 1

    Note:
     SystemCoreClock variable holds HCLK frequency and is defined in system_stm32wlxx.c file.
     Each time the core clock (HCLK) changes, user had to update SystemCoreClock
     variable value. Otherwise, any configuration based on this variable will be incorrect.
     This variable is updated in three ways:
      1) by calling CMSIS function SystemCoreClockUpdate()
      2) by calling HAL API function HAL_RCC_GetSysClockFreq()
      3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  ----------------------------------------------------------------------- */

/* Compute the prescaler value to have TIMx counter clock equal to 10000 Hz */

//#define PRESCALER_VALUE (uint32_t)(((SystemCoreClock) / (10,000)) - 1)

#define PRESCALER_VALUE (uint32_t)(((SystemCoreClock) / (1000000)) - 1)   
// STS-M7 == 2,000,000 == 0.5us ==> 2 Ghz             
// STS-M7 == 1,000,000 == 1.0us ==> 1 GHZ                **************
// STS-M7 == 0,010,000 ==  10ms ==> 100 hz

  /* Initialize TIMx peripheral as follows:
       + Period = 10000 - 1
       + Prescaler = (SystemCoreClock/10000) - 1
       + ClockDivision = 0
       + Counter direction = Up
  */

#define PERIOD_VALUE (1000 - 1);     //(1/PERIOD_VALUE) ms, for 1024hz it's about 0.976ms
//#define PERIOD_VALUE (1000 - 1);   // 1000 hz, 1ms
//#define PERIOD_VALUE 	(320 - 1);
// STS-M7 == 1,000 * 1.0us == 1000 us ==> 1.0 ms ==> 1000 ms /1.0ms ==>1000 hz sample rate
// STS-M7 == 1,000 * 0.5us == 500 us ==> 0.5 ms ==> 1000 ms /0.5ms ==>2000 hz sample rate

/* USER CODE END Private defines */


//=============================================================================
void DelayMicroSeconds(uint32_t nbrOfUs);
//=============================================================================
// Wait function for small delays.
//-----------------------------------------------------------------------------
// input:  nbrOfUs   wait x times approx. one micro second (fcpu = 8MHz)
// return: -
// remark: smallest delay is approx. 15us due to function call



//#define SYSTEM_CORE_CLOCK 48000000 
#define DBG_TIME
#define DELAY_MS_CLOCK    (SYSTEM_CORE_CLOCK - 18) / 11000
#define DELAY_100US_CLOCK    (SYSTEM_CORE_CLOCK) / 120600
#define DELAY_10US_CLOCK    (SYSTEM_CORE_CLOCK) / 1200000
#define DELAY_1US_CLOCK    (SYSTEM_CORE_CLOCK) / 12000000
#define NOP() __ASM volatile ("nop")

#define I2C_TIMING 0x20303E5D
#define I2C_TIMING_TOF250		0x20303E5D
#define I2C_TIMING_ADXL345_STANDARD_MODE			0x20303E5D  //STANDARD MODE 100Khz *******
#define I2C_TIMING_ADXL345_FAST_MODE			0x2010091A  //FAST MODE 400Khz
#define I2C_TIMING_ADXL345_FAST_PLUS_MODE			0x20000209  //FAST PLUS MODE 1000Khz

void Delay(uint32_t nTime);
void DelayMs(uint32_t mS);
void Delay1Us(uint32_t time);
void Delay10Us(uint32_t time);
void Delay100Us(uint32_t time);

/* USER CODE BEGIN EFP */

/* USER CODE END EFP */

#ifdef __cplusplus
}
#endif

#endif /* __YUNHORN_STS_SENSORS_H__ */
