/**
  ******************************************************************************
  * @file          : app_tof_peoplecount.h
  * @author        : IMG SW Application Team
  * @brief         : This file provides code for the configuration
  *                  of the STMicroelectronics.X-CUBE-TOF1.3.2.0 instances.
  ******************************************************************************
  *
  * @attention
  *
  * Copyright (c) 2022 STMicroelectronics.
  * 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.
  *
  ******************************************************************************
  */

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

#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/

/* Exported defines ----------------------------------------------------------*/


 // People Counting defines
 #define NOBODY                    0
 #define SOMEONE                   1
 #define LEFT                      0
 #define RIGHT                     1

 #define DOOR_JAM_2000             0
 #define DOOR_JAM_2400             1
 #define DOOR_JAM_3000			   2
 #define DOOR_JAM_3500			   3
 #define DOOR_JAM_4000			   4
 #define DOOR_SIDE_1600            5



 // define here the profile for your case.
 //#define PPC_PROFILE 				DOOR_JAM_2400
 #define PPC_PROFILE				DOOR_JAM_2000
 #define TRACE_PPC				     1

// time budget in ms  20,33,50,100,200,500
#define tbms_20			20
#define tbms_33			33
#define tbms_50			50
#define tbms_100		100
#define tbms_200		200
#define tbms_500		500

 //PPC_PROFILE DOOR_JAM_2400
 #if PPC_PROFILE == DOOR_JAM_2400
 #define PROFILE_STRING                               "DOOR_JAM_2400"
 #define DISTANCES_ARRAY_SIZE                         10   // nb of samples
 #define MAX_DISTANCE                                 2400 // mm   was 2400
 #define MIN_DISTANCE                                 0   // mm
 #define DIST_THRESHOLD                               1600  // mm
 #define ROWS_OF_SPADS                                8 // 8x16 SPADs ROI
 #define TIMING_BUDGET                                33   //33  // was 20 ms, I found 33 ms has better succes rate with lower reflectance target
 #define DISTANCE_MODE                                DISTANCE_MODE_LONG
 #endif

 //PPC_PROFILE DOOR_JAM_2000
  #if PPC_PROFILE == DOOR_JAM_2000
  #define PROFILE_STRING                               "DOOR_JAM_2000"
  #define DISTANCES_ARRAY_SIZE                         10   // nb of samples
  #define MAX_DISTANCE                                 2000 // mm   was 2400
  #define MIN_DISTANCE                                 0   // mm
  #define DIST_THRESHOLD                               1600  // mm
  #define ROWS_OF_SPADS                                16 // 8x16 SPADs ROI
  #define TIMING_BUDGET                                33   //33  // was 20 ms, I found 33 ms has better succes rate with lower reflectance target
  #define DISTANCE_MODE                                DISTANCE_MODE_LONG
  #endif

 #if ROWS_OF_SPADS == 4
 #define FRONT_ZONE_CENTER                            151
 #define BACK_ZONE_CENTER                             247
 #elif ROWS_OF_SPADS == 6
 #define FRONT_ZONE_CENTER                            159
 #define BACK_ZONE_CENTER                             239
 #elif ROWS_OF_SPADS == 8
 #define FRONT_ZONE_CENTER                            175 // was 167, see UM2555 on st.com, centre = 175 has better return signal rate for the ROI #1
 #define BACK_ZONE_CENTER                             231
 #elif ROWS_OF_SPADS == 16
 #define FRONT_ZONE_CENTER                            199
 #define BACK_ZONE_CENTER                             199
 #endif

/* Exported functions --------------------------------------------------------*/
 enum distance_mode {
	 DISTANCE_MODE_SIDE=0,
	 DISTANCE_MODE_SHORT,		// 1: for max distance less than 1.3m
	 DISTANCE_MODE_LONG,		// 2: (default) for max distance less than 4.0m with time buedget 200ms
 };

 typedef struct STS_ZONE_CENTRE_BY_ROWS_OF_SPADS
 {
	 uint8_t rows_of_spads;
	 uint8_t front_zone_center;
	 uint8_t back_zone_center;
 } sts_zone_center_by_rows_of_spads_t;
 typedef struct STS_PPC_CFG_Type {
	 uint8_t profile_name;
	 uint8_t distance_array_size;
	 uint16_t max_distance;
	 uint16_t min_distance;
	 uint16_t dist_threshold;
	 uint8_t rows_of_SPADS;	/* minimum ROI 4,4 */
	 uint16_t timing_budget; /* TIMING_BUDGET, in ms possible values [15, 20, 33, 50, 100, 200, 500] */
	 uint8_t distance_mode;	/* 1=short, 2=long, DISTANCE_MODE */
	 uint8_t front_zone_center;
	 uint8_t back_zone_center;
 } sts_ppc_cfg_type_t;

 typedef struct STS_People_Count_Sensor_Data_Type {
 	uint16_t Walk_In_People_Count;
 	uint16_t Walk_Out_People_Count;
 	uint16_t Walk_Around_People_Count;
 	uint8_t Count_Period;
 	uint8_t Count_Period_Unit;

	uint16_t Sum_Day_Walk_In_People_Count;
	uint16_t Sum_Day_Walk_Out_People_Count;
	uint16_t Sum_Day_Walk_Around_People_Count;

	uint16_t Sum_Week_Walk_In_People_Count;
	uint16_t Sum_Week_Walk_Out_People_Count;
	uint16_t Sum_Week_Walk_Around_People_Count;

	uint16_t Sum_Month_Walk_In_People_Count;
	uint16_t Sum_Month_Walk_Out_People_Count;
	uint16_t Sum_Month_Walk_Around_People_Count;

	uint16_t Sum_Year_Walk_In_People_Count;
	uint16_t Sum_Year_Walk_Out_People_Count;
	uint16_t Sum_Year_Walk_Around_People_Count;

	uint32_t Sum_LifeCycle_Walk_In_People_Count;
	uint32_t Sum_LifeCycle_Walk_Out_People_Count;
	uint32_t Sum_LifeCycle_Walk_Around_People_Count;

	uint8_t Count_Valid;

 } sts_people_count_sensor_data_t;

 typedef struct STS_TOF_Presence_Detection_Sensor_Data_Type {
	 uint8_t 	tof_range_presence_state;				// 1: presence, 0: no presence
	 uint8_t 	gesture_multi_state;					// 0: push, 1: swing left, 2: swing right
	 uint8_t 	tof_motion_presence_state;
	 uint8_t	measure_window_threshold_low_dm;		// dm= 100mm = 10 cm, low = 0 dm
	 uint8_t 	measure_window_threshold_high_dm;		// dm= 100mm = 10 cm, high=40dm
	 uint8_t 	measure_critiera;						// below, 0, in window 1, above 2
	 uint8_t Count_Valid;
 } sts_tof_presence_detection_sensor_data_t;

void STS_TOF_VL53LX_PeopleCounting_Process_Init(void);
void STS_TOF_VL53LX_PeopleCounting_Process_Start(void);

void STS_TOF_VL53LX_PresenceDetection_Process_Start(void);
int sts_tof_vl53lx_presence_detection_start(void);

int sts_tof_vl53lx_peoplecount_init(void);
int sts_tof_vl53lx_peoplecount_start(void);

//int sts_tof_vl53lx_peoplecount(void);
int ProcessPeopleCountingData(int16_t Distance, uint8_t zone, uint8_t RangeStatus);
void STS_people_count_sensor_Read(sts_people_count_sensor_data_t *sts_p2_sensor_data);
void STS_tof_presence_detection_sensor_Read(sts_tof_presence_detection_sensor_data_t *sts_t6_sensor_data);

#ifdef __cplusplus
}
#endif

#endif /* __APP_TOF_PEOPLECOUNT_H */
