#pragma once

//                                   C3 C2 C1 C0
#define LTC_WRITE 0b00000000       // 0  0  0  0  Write to Input Register
#define LTC_UPDATE 0b00010000      // 0  0  0  1  Update (Power Up) DAC Register
#define LTC_WRTIEUPDATE 0b00110000 // 0  0  1  1  Write to and Update (Power Up) DAC Register
#define LTC_SLEEP 0b01000000       // 0  1  0  0  Power Down
#define LTC_INTERNALREF 0b01100000 // 0  1  1  0  Select Internal Reference
#define LTC_EXTERNALREF 0b01110000 // 0  1  1  1  Select External Reference

#define LTC_REF_2V5 2.5
#define LTC_REF_4V096 4.096

typedef enum
{
    LTC2631_8BIT = 8,
    LTC2631_10BIT = 10,
    LTC2631_12BIT = 12
} ltc2631_res_t;

typedef struct
{
    I2C_HandleTypeDef *hi2c;
    uint8_t addr;
    ltc2631_res_t resolution;
    float ref_voltage_f;
    uint32_t ref_voltage_u;
} ltc2631_handle_t;

void ltc2631_init(ltc2631_handle_t *hdac, I2C_HandleTypeDef *hi2c, uint8_t addr, ltc2631_res_t res, float ref);
void ltc2631_setOutputVoltage_f(ltc2631_handle_t *hdac, float volt);
void ltc2631_setOutputVoltage_u(ltc2631_handle_t *hdac, uint32_t volt_x1000);
void ltc2631_setOutputValue(ltc2631_handle_t *hdac, uint16_t value);
void ltc2631_sleep(ltc2631_handle_t *hdac);