/* Copyright 2025 by COG9 Enterprises LLC
 * All rights reserved.
 * Title: ina219.h
 * Author: Jed Marti KI7NNP
 * Description: Stuff for a non c++ version with better calibraion and saving code.
 * Created:
 * Revision History:
 */

#ifndef ina219_h
#define ina219_h

// This is the pseudo object that gets passed around.
typedef struct {
  uint8_t  i2caddr;             // Device I2C address.
  uint8_t  range;               // Voltage range (16 or 32).
  uint8_t  pg;                  // PGA bits 0 -> 3
  uint8_t  badc;                // Bus ADC Resolution/Averaging.
  uint8_t  sadc;                // Shunt ADC Resolution/Averaging.
  uint8_t  mode;                // Mode 0->7.
  uint8_t  errno;               // An error.
  uint32_t lastSumTime;         // Last time (in millis) sum was started.
  uint32_t maSum;               // Running Milliamp sum.
  float    voltsSum;            // Voltage sum.
  uint32_t Wms;                 // 10th of second watts.
} INA219;


// INA219 registers.
enum {
      INA219_Configuration = 0x00,
      INA219_ShuntVoltage = 0x01,
      INA219_BusVoltage = 0x02,
      INA219_Power = 0x03,
      INA219_Current = 0x04,
      INA219_Calibration = 0x05 };

// Error indices. These have an equivalent vector of strings for error messages.
enum {
      INA219_OK,
      INA219_BADDEV,
      INA219_BADI2CADDR,
      INA219_NODEVICE,
      INA219_BADRANGE,
      INA219_BADPGA,
      INA219_BADC,
      INA219_SADC,
      INA219_BADMODE
};

#define isOK(x) (x.errno == INA219_OK)

void  ina219Reset(INA219 *dev);
float ina219GetVoltage(INA219 *dev);
float ina219GetCurrentf(INA219 *dev);
uint16_t ina219GetCurrent(INA219 *dev);
float ina219GetShuntVoltage(INA219 *dev);
void  ina219Begin(INA219 *dev);
char  *ina219Emsg(INA219 *dev);
#endif
