//i2c_ads1115.h
#ifndef __BSP_IIC_ADS1115_H
#define __BSP_IIC_ADS1115_H
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"
// #include "i2c.h"
#include "bsp_i2c.h"

#define ADS1115_WRITE_ADDRESS        0x92
#define ADS1115_READ_ADDRESS         0x93
//以下两个预处理作为配置寄存器预处理要更改其配在这里更改
//也可以添加新的预处理对不同通道的采集或者选取不同的采样速率等

/*事件开始 set, 事件完成  clear */
typedef enum
{
   ADS1115_Event_READY,                   /*!< Startup finished. */       
   ADS1115_Event_TEST_SUCCESS,                  
   ADS1115_Event_Get_Data,                  
   ADS1115_Event_Get_Data_Set_Reg01,                                
   ADS1115_Event_DATA_RCV_OK,                 
} ADS1115_Event_TypeDef;

typedef enum {
   ADS1115_WORK_STATE_WAITING,
   ADS1115_WORK_STATE_START,
   ADS1115_WORK_STATE_TEST,
   ADS1115_WORK_STATE_TESTING,
   ADS1115_WORK_STATE_TEST_OK,
   ADS1115_WORK_STATE_GET_DATA_READY,
   ADS1115_WORK_STATE_GET_DATA_START,
   ADS1115_WORK_STATE_GET_DATA_SET_REG01,
   ADS1115_WORK_STATE_GET_DATA_SET_REG01_Wait,
   // ADS1115_WORK_STATE_GET_DATA_Convert_Register,
   ADS1115_WORK_STATE_GET_DATA_RCV,
   ADS1115_WORK_STATE_GET_DATA_RCV_Wait,
   // ADS1115_WORK_STATE_GET_DATA_FLOAT,
   ADS1115_WORK_STATE_GET_DATA_OK,
   ADS1115_WORK_STATE_ERROR,
   ADS1115_WORK_STATE_STOP
}ADS1115_WORK_STATE_TypeDef;

typedef enum {
   ADS1115_OUTPUT_UNIT_v,
   ADS1115_OUTPUT_UNIT_mv
}ADS1115_OUTPUT_UNIT_TypeDef;


//地址指针寄存器
typedef enum 
{
   ADS1115_REG_POINTER_CONVERT      =  0x00,   // 转换寄存器
   ADS1115_REG_POINTER_CONFIG       =  0x01  , // 配置寄存器  16bit
   ADS1115_REG_POINTER_LOWTHRESH    =  0x02 ,  
   ADS1115_REG_POINTER_HITHRESH     =  0x03 
} ADS1115_REG_POINTER_TypeDef;


// 单次转换开始
typedef enum 
{
   ADS1115_REG_CONFIG_OS_START = (0x1U << 7),//设备单词转换开启 高字节的最高位
   ADS1115_REG_CONFIG_OS_NULL = (0x0U << 7)
} ADS1115_REG_CONFIG_OS_TypeDef;


//输入引脚选择和输入方式选择
typedef enum 
{
    ADS1115_REG_CONFIG_MUX_Diff_01 =  (0x0U << 4),  // 差分输入0引脚和1引脚
    ADS1115_REG_CONFIG_MUX_Diff_03 =  (0x1U << 4),  // 差分输入0引脚和3引脚
    ADS1115_REG_CONFIG_MUX_Diff_13 =  (0x2U << 4),  // 差分输入1引脚和3引脚
    ADS1115_REG_CONFIG_MUX_Diff_23 =  (0x3U << 4),  // 差分输入2引脚和3引脚
    ADS1115_REG_CONFIG_MUX_SINGLE_0=  (0x4U << 4),  //单端输入 0
    ADS1115_REG_CONFIG_MUX_SINGLE_1=  (0x5U << 4),  //单端输入 1
    ADS1115_REG_CONFIG_MUX_SINGLE_2=  (0x6U << 4),  //单端输入 2
    ADS1115_REG_CONFIG_MUX_SINGLE_3=  (0x7U << 4)  //单端输入 3
} ADS1115_REG_CONFIG_MUX_Diff_TypeDef;



//量程选择
typedef enum 
{
    ADS1115_REG_CONFIG_PGA_6  = (0x0U << 1),    // +/- 6.1144
    ADS1115_REG_CONFIG_PGA_4  =(0x1U << 1),     // +/- 4.096
    ADS1115_REG_CONFIG_PGA_2  =    (0x2U << 1), // +/- 2.048
    ADS1115_REG_CONFIG_PGA_1  = (0x3U << 1) ,   // +/- 1.024
    ADS1115_REG_CONFIG_PGA_05 = (0x4U << 1) ,   // +/- 0.512
    ADS1115_REG_CONFIG_PGA_02 =(0x5U << 1) ,    // +/- 0.256
} ADS1115_REG_CONFIG_PGA_TypeDef;


//运行方式
typedef enum 
{
    ADS1115_REG_CONFIG_MODE_SINGLE  =  (0x1U << 0) ,  //  单次
    ADS1115_REG_CONFIG_MODE_CONTIN =     (0x0U << 0)  //连续转换
} ADS1115_REG_CONFIG_MODE_TypeDef;


//转换速率
typedef enum 
{
    ADS1115_REG_CONFIG_DR_8  =  (0x0U << 5),
    ADS1115_REG_CONFIG_DR_16=                 (0x1U << 5),
    ADS1115_REG_CONFIG_DR_32 =                   (0x2U << 5),
    ADS1115_REG_CONFIG_DR_64 =                   (0x3U << 5),
    ADS1115_REG_CONFIG_DR_128 =                  (0x4U << 5),
    ADS1115_REG_CONFIG_DR_250 =                  (0x5U << 5),
    ADS1115_REG_CONFIG_DR_475 =                  (0x6U << 5),
    ADS1115_REG_CONFIG_DR_860 =                  (0x7U << 5)
} ADS1115_REG_CONFIG_DR_TypeDef;


 
//比较器模式
typedef enum 
{
    ADS1115_REG_CONFIG_COMP_MODE_TRADITIONAL =  (0x0U << 4),//默认
    ADS1115_REG_CONFIG_COMP_MODE_WINDOW  =   (0x1U << 4)
} ADS1115_REG_CONFIG_COMP_MODE_TypeDef;


typedef enum 
{
    ADS1115_REG_CONFIG_COMP_POL_LOW  =    (0x0U << 3),//默认
    ADS1115_REG_CONFIG_COMP_POL_HIG   =      (0x1U << 3)
} ADS1115_REG_CONFIG_COMP_POL_TypeDef;



typedef enum 
{
    ADS1115_REG_CONFIG_COMP_LAT_NONLATCH = (0x0U << 2),
    ADS1115_REG_CONFIG_COMP_LAT_LATCH   =    (0x1U << 2)
} ADS1115_REG_CONFIG_COMP_LAT_TypeDef;


typedef enum 
{
    ADS1115_REG_CONFIG_COMP_QUE_ONE=   (0x0U << 0),
    ADS1115_REG_CONFIG_COMP_QUE_TWO=       (0x1U << 0),
    ADS1115_REG_CONFIG_COMP_QUE_THR=       (0x2U << 0),
    ADS1115_REG_CONFIG_COMP_QUE_DIS=       (0x3U << 0)
} ADS1115_REG_CONFIG_COMP_QUE_TypeDef;

extern I2C_HELPER_TypeDef *ads1115_i2c_helper;

typedef struct{
   int (*init)(void);
   void (*port)(void);
   int (*test)(void);
   void (*start)(void);
   void (*stop)(void);
   // I2C_HandleTypeDef *hi2c;

   uint8_t config_os;
   uint8_t config_mux;
   uint8_t config_PGA;
   uint8_t config_MODE;
   uint8_t config_DR;
   uint8_t config_COMP_MODE;
   uint8_t config_COMP_POL;
   uint8_t config_COMP_LAT;
   uint8_t config_COMP_QUE;
   uint8_t point_ADDR;        // 要读取的数据指针地址
   volatile uint8_t config_H;
   volatile uint8_t config_L;
   uint8_t reg_data[3];
   ADS1115_OUTPUT_UNIT_TypeDef unit;
   uint16_t result_int16;   // 多组可用于
   double coeff;   // 放大1000倍数
   double voltage_mv;
   volatile ADS1115_WORK_STATE_TypeDef state;
   uint8_t event_flag;
}ADS1115_TypeDef;

extern ADS1115_TypeDef my_ads1115;

static void ads1115_get_reg_data(void)
{
    my_ads1115.config_H = my_ads1115.config_os|my_ads1115.config_mux
                |my_ads1115.config_PGA|my_ads1115.config_MODE;
    my_ads1115.config_L = my_ads1115.config_DR|my_ads1115.config_COMP_MODE|my_ads1115.config_COMP_POL
            |my_ads1115.config_COMP_LAT|my_ads1115.config_COMP_QUE ;

    // my_ads1115.reg_data[0] = 0x01;                 //  point addr  8bit  配置寄存器
    my_ads1115.reg_data[0] = my_ads1115.config_H;  //  config_reg 高位
    my_ads1115.reg_data[1] = my_ads1115.config_L;  //  config_reg 低位
}

int ads1115_init(void);
void ads1115_port(void);
int ads1115_test(void);
void ads1115_start(void);
void ads1115_stop(void);
int ads1115_setup(ADS1115_REG_CONFIG_MUX_Diff_TypeDef mux);
// void ads1115_config_register(I2C_HandleTypeDef *ads1115_I2cHandle,uint8_t pointADD,uint8_t configH,uint8_t configL);
int16_t ads1115_read_data( );
double ads1115_get_coeff( );
double ads1115_get_voltage_val( );
void ads1115_get_data_start(void);

void ads1115_callback(ADS1115_TypeDef *pAds1115, uint8_t *buf, uint16_t size);
#endif

/***************/
//配置寄存器说明



//config register
/*CRH[15:8](R/W)
   BIT      15      14      13      12      11      10      9       8
   NAME     OS      MUX2    MUX1    MUX0    PGA2    PGA1    PGA0    MODE
CRL[7:0] (R/W)
   BIT      7       6       5       4       3       2       1       0
   NAME    DR0     DR1     DR0   COM_MODE COM_POL COM_LAT COM_QUE1 COM_QUE0


   -----------------------------------------------------------------------------------
 * 15    | OS             |  运行状态会单词转换开始
 *       |                | 写时:
 *       |                | 0   : 无效
 *       |                | 1   : 开始单次转换处于掉电状态时
 *       |                | 读时:
 *       |                | 0   : 正在转换
 *       |                | 1   : 未执行转换
 * -----------------------------------------------------------------------------------
 * 14:12 | MUX [2:0]      | 输入复用多路配置  -- 如果多路的话需要每次分开测量
 *       |                | 000 : AINP = AIN0 and AINN = AIN1 (default)
 *       |                | 001 : AINP = AIN0 and AINN = AIN3
 *       |                | 010 : AINP = AIN1 and AINN = AIN3
 *       |                | 011 : AINP = AIN2 and AINN = AIN3
 *       |                | 100 : AINP = AIN0 and AINN = GND
 *       |                | 101 : AINP = AIN1 and AINN = GND
 *       |                | 110 : AINP = AIN2 and AINN = GND
 *       |                | 111 : AINP = AIN3 and AINN = GND
 * -----------------------------------------------------------------------------------
 * 11:9  | PGA [2:0]      | 可编程增益放大器配置(FSR  full scale range)
 *       |                | 000 : FSR = В±6.144 V
 *       |                | 001 : FSR = В±4.096 V
 *       |                | 010 : FSR = В±2.048 V (默认)
 *       |                | 011 : FSR = В±1.024 V
 *       |                | 100 : FSR = В±0.512 V
 *       |                | 101 : FSR = В±0.256 V
 *       |                | 110 : FSR = В±0.256 V
 *       |                | 111 : FSR = В±0.256 V
 * -----------------------------------------------------------------------------------
 * 8     | MODE           | 工作模式
 *       |                | 0   : 连续转换
 *       |                | 1   : 单词转换
 * -----------------------------------------------------------------------------------
 * 7:5   | DR [2:0]       | 采样频率
 *       |                | 000 : 8 SPS
 *       |                | 001 : 16 SPS
 *       |                | 010 : 32 SPS
 *       |                | 011 : 64 SPS
 *       |                | 100 : 128 SPS (默认)
 *       |                | 101 : 250 SPS
 *       |                | 110 : 475 SPS
 *       |                | 111 : 860 SPS
 * -----------------------------------------------------------------------------------
 * 4     | COMP_MODE      | 比较器模式
 *       |                | 0   : 传统比较器 (default)
 *       |                | 1   : 窗口比较器
 * -----------------------------------------------------------------------------------
 * 3     | COMP_POL       | Comparator polarity
 *       |                | 0   : 低电平有效 (default)
 *       |                | 1   : 高电平有效
 * -----------------------------------------------------------------------------------
 * 2     | COMP_LAT       | Latching comparator
 *       |                | 0   : 非锁存比较器. (default)
 *       |                | 1   : 锁存比较器.
 * -----------------------------------------------------------------------------------
 * 1:0   | COMP_QUE [1:0] | Comparator queue and disable
 *       |                | 00  : Assert after one conversion
 *       |                | 01  : Assert after two conversions
 *       |                | 10  : Assert after four conversions
 *       |                | 11  : 禁用比较器并将ALERT/RDY设置为高阻抗 (default)
 * -----------------------------------------------------------------------------------
*/
