#ifndef FFG3105_H
#define FFG3105_H

#include "Arduino.h"
#include "Wire.h"

// FFG3105
// #define I2C_ADDR 0x37
#define I2C_ADDR_WRITE 0x6E
#define I2C_ADDR_READ 0x6F

// REGISTERS
#define PART_ID 0x00          // RO Part Identification
#define CONTROL 0x01          // R/W Control
#define STATUS 0x02           // RO Status
#define CELL_VOLTAGE 0x03     // RO Cell Voltage Measurement
#define PACK_TEMPERATURE 0x04 // RO Pack Temperature
#define CELL_CURRENT 0x05     // N/A Reserved for future use
#define KEYWORD_LSW 0x06      // R/W Keyword Least Significant Word
#define KEYWORD_MSW 0x07      // R/W Keyword Most Significant Word
#define TEST_CONTROL 0x08     // R/W Test Control
#define TEST_STATUS 0x09      // RO Test Status
#define SPARE 0x0A            // R/W Spare

#define USER_00 0x10 // R/W User Configurable
//-//-//  USER_00 – USER_11 0x10-0x1B // R/W User Configurable
#define USER_11 0x1B     // R/W User Configurable
#define BATTERY_ID0 0x20 // ... 0x23  R/W Unique Identification Code 0
#define BATTERY_ID1 0x24 // ... 0x27  R/W Unique Identification Code 1
#define RESERVED 0x30    // ... 0x57 Reserved


// Battery Voltage Measurement Unsigned Short
// MSB = a[15] = 22 = 4 V
// LSB = a[0] = 2-13 = 122.07 μV
// +FS = 16‟b1111111111111111 = 7.99987793 V
// - FS = 16‟b0000000000000000 = 0 V

// Temperature Measurement Signed Short
// MSB = a[15] = -28 = -256 mV
// LSB = a[4] = 2-3 = 0.125 mV
// +FS = 255.9921875 mV
// -FS = -255.9921875 mV

// COMMAND REGISTER
struct ControlRegister
{
    uint16_t command : 4;         // Command (4 bits)
    uint16_t fuses : 2;           // Fuses (2 bits)
    uint16_t reserved : 2;        // Reserved (2 bits)
    uint16_t measurementType : 3; // Type of measurements (3 bits)
    uint16_t zeroes : 5;          // Zeroes (5 bits)
};

class FFG3105
{
public:
    FFG3105();
    void wake();
    void sleep();
    void reset();
    uint16_t test();
    void measuring();
    uint16_t readTemperature();
    uint16_t readVoltage();
    uint16_t readStatus();

};
#endif