#ifndef INA260_COMPONENT
#define INA260_COMPONENT
#include "i2cbrn.h"
#define TAG_INA "INA260"
extern i2c_device_config_t INA260_DEV_CFG;

extern i2c_master_dev_handle_t INA260_DEV_HANDLE;

#define INA260_CONFIG_REGISTER (0x00)
#define INA260_CURRENT_REGISTER (0x01)
#define INA260_VOLTAGE_REGISTER (0x02)
#define INA260_POWER_REGISTER (0x03)
#define INA260_MASK_ENABLE_REGISTER (0x06)
#define INA260_ALERT_LIMIT_REGISTER (0x07)
#define INA260_MANUFACTURER_ID_REGISTER (0xFE)
#define INA260_DIE_ID_REGISTER (0xFF)

// Bit masks
#define CONFIG_RST_BIT (1 << 15)
#define CONFIG_AVG_MASK (0x7 << 9)
#define CONFIG_VBUSCT_MASK (0x7 << 6)
#define CONFIG_ISHCT_MASK (0x7 << 3)
#define CONFIG_MODE_MASK (0x7 << 0)

// Read-only bits
#define CONFIG_RESERVED_BITS (0x7 << 12) // Bits 14–12 = 110b

// Averaging modes (AVG)
#define CONFIG_AVG_1 (0x0 << 9)
#define CONFIG_AVG_4 (0x1 << 9)
#define CONFIG_AVG_16 (0x2 << 9)
#define CONFIG_AVG_64 (0x3 << 9)
#define CONFIG_AVG_128 (0x4 << 9)
#define CONFIG_AVG_256 (0x5 << 9)
#define CONFIG_AVG_512 (0x6 << 9)
#define CONFIG_AVG_1024 (0x7 << 9)

// Bus voltage conversion time (VBUSCT)
#define CONFIG_VBUSCT_140US (0x0 << 6)
#define CONFIG_VBUSCT_204US (0x1 << 6)
#define CONFIG_VBUSCT_332US (0x2 << 6)
#define CONFIG_VBUSCT_588US (0x3 << 6)
#define CONFIG_VBUSCT_1_1MS (0x4 << 6)
#define CONFIG_VBUSCT_2_116MS (0x5 << 6)
#define CONFIG_VBUSCT_4_156MS (0x6 << 6)
#define CONFIG_VBUSCT_8_244MS (0x7 << 6)

// Shunt current conversion time (ISHCT)
#define CONFIG_ISHCT_140US (0x0 << 3)
#define CONFIG_ISHCT_204US (0x1 << 3)
#define CONFIG_ISHCT_332US (0x2 << 3)
#define CONFIG_ISHCT_588US (0x3 << 3)
#define CONFIG_ISHCT_1_1MS (0x4 << 3)
#define CONFIG_ISHCT_2_116MS (0x5 << 3)
#define CONFIG_ISHCT_4_156MS (0x6 << 3)
#define CONFIG_ISHCT_8_244MS (0x7 << 3)

// Operating mode (MODE)
#define CONFIG_MODE_POWERDOWN 0x00
#define CONFIG_MODE_CURRENT_TRIGGER 0x01
#define CONFIG_MODE_VOLTAGE_TRIGGER 0x02
#define CONFIG_MODE_CURRENT_VOLTAGE_TRIGGER 0x03
#define CONFIG_MODE_POWERDOWN2 0x04
#define CONFIG_MODE_CURRENT_CONTINOUS 0x05
#define CONFIG_MODE_VOLTAGE_CONTINOUS 0x06
#define CONFIG_MODE_CURRENT_VOLTAGE_CONTINOUS 0x07

esp_err_t ina260_init();
void ina260_readParams(uint16_t *volt, uint16_t *cur, uint16_t *pow);
void ina260_printParams(uint16_t volt, uint16_t cur, uint16_t pow);

#endif