// https://github.com/Tinyu-Zhao/INA3221/tree/main


// TODO: it is only 1 channel here, due to only 3 values in struct (v1,i1,p1) 
//   - think how to get into 3 channels without flooding all sensosr on HA with unnecessary entities



#pragma once

#if (USE_INA3221 == 1)

    // V, A, W
    void measure_ina3221(float *v3, float *i3, float *p3, bool compensated)

//   void measure_ina3221(float *v1, float *i1, float *p1, float *v2, float *i2, float *p2, float *v3, float *i3, float *p3, bool compensated)
  {
    #ifdef DEBUG
        long function_start = micros(); 
    #endif // DEBUG
  
    if (!ina3221_ok) 
    {
      Serial.printf("[%s]: INA3221 NOT WORKING\n",__func__);
    //   *v1 = 0; *i1 = 0; *p1 = 0;
    //   *v2 = 0; *i2 = 0; *p2 = 0;
      *v3 = 0; *i3 = 0; *p3 = 0;
    } else 
    {
        #if (INA3221_CH3_EN == 1)
            *v3 = ina3221.getVoltage(INA3221_CH3);
            if (compensated)
                *i3 = ina3221.getCurrentCompensated(INA3221_CH3); 
            else 
                *i3 = ina3221.getCurrent(INA3221_CH3); 
            *p3 = *v3 * *i3;      // Watt
            Serial.printf("[%s]: v3=%0.3fV, i3=%0.4fA,p3=%0.4fW \n",__func__,*v3,*i3,*p3);
        #endif

    }

     #ifdef DEBUG
      Serial.printf("[%s]: took %uus\n",__func__,(micros()-function_start));
    #endif // DEBUG
  }

#endif
