#ifndef __DRV_BQ27Z561_H__
#define __DRV_BQ27Z561_H__


#include "rtthread.h"
#include "stdint.h"
#include "bf0_hal.h"
#include "stdbool.h"
#include "stdio.h"


#define BQ27Z561_DRIVER_ADDR                (0x55)      /**< 设备I2C地址 */

#define BQ27Z561_TEMPERATURE_REG            (0x06)      /**< 温度寄存器 */
#define BQ27Z561_VOLTAGE_REG                (0x08)      /**< 电压寄存器 */
#define BQ27Z561_STATUS_REG                 (0x0A)      /**< 状态寄存器 */
#define BQ27Z561_CURRENT_REG                (0x0C)      /**< 电流寄存器 */
#define BQ27Z561_REMAINING_CAPACITY_REG     (0x10)      /**< 剩余容量寄存器 */
#define BQ27Z561_FULL_CHARGE_CAPACITY_REG   (0x12)      /**< 满充电容量寄存器 */
#define BQ27Z561_SOC_REG                    (0x2C)      /**< 电量百分比寄存器 */

/**
 * @brief 读取到的原始电池数据
 */
typedef struct {
    uint16_t voltage;
    int16_t current;
    uint16_t temperature;
    uint16_t status;
    uint16_t soc;
    uint16_t remaining_capacity;
    uint16_t full_charge_capacity;
} bq27z561_raw_data_t;

/**
 * @brief I2C读写回调定义
 * @details reg_len 为寄存器地址长度（字节）
 */
typedef rt_err_t (*bq27z561_read_cb_t)(uint8_t dev_addr, uint16_t reg, uint8_t reg_len, uint8_t *buf, uint16_t len, void *user_data);
typedef rt_err_t (*bq27z561_write_cb_t)(uint8_t dev_addr, uint16_t reg, uint8_t reg_len, const uint8_t *buf, uint16_t len, void *user_data);

/**
 * @brief bq27z561驱动结构体
 */
typedef struct {
    bool is_initlized;
    bq27z561_read_cb_t read_cb;
    bq27z561_write_cb_t write_cb;
    void *user_data;
} bq27z561_drv_t;

/**
 * @brief 初始化bq27z561驱动
 * @details 需要传入I2C读写回调与用户上下文
 */
rt_err_t bq27z561_drv_init(bq27z561_drv_t *drv, bq27z561_read_cb_t read_cb, bq27z561_write_cb_t write_cb, void *user_data);

/**
 * @brief 反初始化bq27z561驱动
 */
rt_err_t bq27z561_drv_deinit(bq27z561_drv_t *drv);

/**
 * @brief 读取bq27z561电池数据
 */
rt_err_t bq27z561_drv_read(bq27z561_drv_t *drv, bq27z561_raw_data_t *raw_data);

#endif  /* __DRV_BQ27Z561_H__ */

