/*****************************************************************
;Project:   
;MCU:       
;Date:     
;File:      
;Function:  
******************************************************************/
#include "lis2dw.h"
#include "gpio.h"
#include "i2c.h"
#include "lis2dw12_reg.h"
#include "delay.h"
#include "DebugLog.h"

stmdev_ctx_t dev_ctx1;

static void platform1_delay(uint16_t ms);
static int32_t platform1_write(uint8_t I2C_addr, uint8_t addr, uint8_t* value, uint16_t len);
static int32_t platform1_read(uint8_t I2C_addr,uint8_t regAddress, uint8_t * data, uint16_t len);

/*---------------------------------------------------------------*/
//Lis2dw IO 初始化
void Lis2dw_IO_Init(void)
{
  //IIC IO初始化
  i2c_Init(I2C1,U32BIT(I2C1_SCLK),U32BIT(I2C1_SDA));
}

/*---------------------------------------------------------------*/
//Lis2dw 初始化
void Lis2dw_Init(void)
{
  uint8_t temp = 0;
  lis2dw12_ctrl4_int1_pad_ctrl_t int_route;
  
  dev_ctx1.write_reg = platform1_write;
  dev_ctx1.read_reg = platform1_read;
  dev_ctx1.mdelay = platform1_delay;
  dev_ctx1.master_addr = LIS2DW_ADDRESS;
  
  Lis2dw_IO_Init();
  
  //获取设置Id
  lis2dw12_device_id_get(&dev_ctx1, &temp);
  
  if(temp == LIS2DW_ID)
  {
    dbg_printf("device exist\r\n");
  }
  else
  {
    dbg_printf("device not exist\r\n");
  }
  
  //软件复位, 寄存器到默认配值
  lis2dw12_reset_set(&dev_ctx1, PROPERTY_ENABLE);

  do {
    lis2dw12_reset_get(&dev_ctx1, &temp);
  } while (temp);
  
  //设置满量程范围
  lis2dw12_full_scale_set(&dev_ctx1, LIS2DW12_2g);
  
  //设置低通滤波和滤波器带宽
  lis2dw12_filter_path_set(&dev_ctx1, LIS2DW12_LPF_ON_OUT);
  lis2dw12_filter_bandwidth_set(&dev_ctx1,LIS2DW12_ODR_DIV_4);
 
  //配置电源模式
  lis2dw12_power_mode_set(&dev_ctx1, LIS2DW12_CONT_LOW_PWR_12bit); 

  //设置加速度计的唤醒持续时间
  lis2dw12_wkup_dur_set(&dev_ctx1,2); 
  
  //设置进入睡眠模式前的持续时间
  lis2dw12_act_sleep_dur_set(&dev_ctx1,2); 
  
  //设置唤醒加速度计所需的活动阈值
  lis2dw12_wkup_threshold_set(&dev_ctx1,2);
  
  lis2dw12_wkup_feed_data_set(&dev_ctx1,LIS2DW12_HP_FEED);
  
  //设置加速度计检测活动或静止的模式
  lis2dw12_act_mode_set(&dev_ctx1, LIS2DW12_DETECT_ACT_INACT);
  
  //使能活动检测中断
  lis2dw12_pin_int1_route_get(&dev_ctx1, &int_route);
  int_route.int1_wu = PROPERTY_ENABLE;
  lis2dw12_pin_int1_route_set(&dev_ctx1, &int_route);
  
  //设置加速度计的数据输出速率
  lis2dw12_data_rate_set(&dev_ctx1, LIS2DW12_XL_ODR_200Hz);  
}

/*---------------------------------------------------------------*/
//判断是在静止或者运动状态
void Act_State_Judge(void)
{
    lis2dw12_all_sources_t all_source;
    /* Read status register */
    lis2dw12_all_sources_get(&dev_ctx1, &all_source);

    /* Check if Activity/Inactivity events */
    if(all_source.wake_up_src.sleep_state_ia) 
    {        
      dbg_printf("Inactivity Detected\r\n");
    }

    if(all_source.wake_up_src.wu_ia)
    {
      dbg_printf("Activity Detected\r\n");
    }        
}
/*---------------------------------------------------------------*/
//寄存器写
static int32_t platform1_write(uint8_t I2C_addr, uint8_t addr, uint8_t* value, uint16_t len)
{	
  ErrorStatus bret = SUCCESS;

	bret = i2c_1_write(I2C_addr, 1, addr, value, len);
    
  return (int32_t)(!bret);
}

/*---------------------------------------------------------------*/
//寄存器读
static int32_t platform1_read(uint8_t I2C_addr,uint8_t regAddress, uint8_t * data, uint16_t len)
{
	ErrorStatus bret = SUCCESS;
  
	bret = i2c_1_read(I2C_addr, 1, regAddress, data, len);
  
	return (int32_t)(!bret);
}

/*---------------------------------------------------------------*/
//延时
static void platform1_delay(uint16_t ms)
{
  delay_ms(ms);
}
/*---------------------------------------------------------------*/