/* Copyright 2025 by COG9 Enterprises LLC
 * All rights reserved.
 * Title: csv.ino
 * Author: Jed Marti KI7NNP
 * Description: Dump current data in CSV format.
 * Created: Mon Oct 13 13:27:18 2025
 * Revision History:
 */


#include <I2C_24LC1025.h>

#include "powerard.h"

// The EEPROM control.
extern I2C_24LC1025 seeprom;

// Use the buffer from record.
extern recordAmperage            rA;
extern recordAmperageVoltage     rAV;
extern recordAmperageTemperature rAT;
extern recordAll                 rAVT;
extern uint32_t recordByte;
#define frac(x)     (int(100*(x - int(x))))


void tocsv(char *obuf)
{
  uint8_t  mo, day;
  uint8_t  hrs, mins, secs;
  uint16_t yr;

  /*
  switch (whichRecord)
  mo = ((dateTime[5] >> 4) & 0x01) * 10 + (dateTime[5] & 0x0F);
  day = ((dateTime[4] >> 4) & 0x03) * 10 + (dateTime[4] & 0x0F);
  yr = (dateTime[6] >> 4) + (dateTime[6] & 0x0F);
  yr += 2000;
  hrs = ((dateTime[2] >> 4) & 0x03) * 10 + (dateTime[2] & 0x0F);
  mins = ((dateTime[1] >> 4) & 0x07) * 10 + (dateTime[1] & 0x0F);
  secs = ((dateTime[0] >> 4) & 0x07) * 10 + (dateTime[0] & 0x0F);

  
  snprintf(obuf, 50, "%2d,%2d,%2d,%d,%d,%d,%d,%d,%d,%d.%02d,%d,%d,%d\n",
           mo, day, yr, hrs, mins, secs,
           mamps5, mamps12, mamps18, (int)battery / 10.0f,
           frac(battery / 10.0f),
           temp5, temp12,temp18);
  */
}



void csvDump(void)
/*     Dump the records in CSV format willy nilly. Useful for Terraterm and putty that store
 *   their whole interaction. Called from the serial command line.
 * Parameters: none
 * External References:
 *   E.whichRecord: indicates what data values were written.
 *   rA: record stores only amperages * 3.
 *   rAV: record stores amperages and voltages.
 *   rAT: record amperage and temperature.
 *   rAVT: record everything.
 * Returned values: none
 * Errors detected: none
 */
{
  
  char obuf[21];
  uint8_t resp;
  uint32_t readthem;

  // csv header line. 
  // Header.
  switch (E.whichRecord) {
  case rAmps:
    Serial.println(F("\nmonth,day,year,hour,minute,second,maA,maB,maC"));
    break;
    
  case rAmpsVolts:
    Serial.println(F("\nmonth,day,year,hour,minute,second,maA,maB,maC,bat,vA,vB,vC"));
    break;

  case rAmpsTemps:
    Serial.println(F("\nmonth,day,year,hour,minute,second,maA,maB,maC,bat,tA,tB,tC,tX"));
    break;

  case rAll:
    Serial.println(F("\nmonth,day,year,hour,minute,second,maA,maB,maC,bat,vA,vB,vC,tA,tB,tC,tX"));
    break;

  default:
    break;
  }
  
 loop:
  switch (E.whichRecord){
  case rAmps:
    readthem = seeprom.readBlock(recordByte,
                                 (uint8_t*) &rA,
                                 sizeof(recordAmperage) - sizeof(uint16_t));
    if (readthem != sizeof(recordAmperage) - sizeof(uint16_t)) goto done;

    // Some 0's indicate we're done.
    if (rA.dateTime[0] == 0 &&
        rA.dateTime[1] == 0 &&
        (rA.dateTime[2] == 0 || rA.dateTime[2] == 0xFF) &&
        (rA.dateTime[3] == 0 || rA.dateTime[3] == 0xFF)) goto done;

    //  The date.
    ctimec(obuf, rA.dateTime);
    Serial.print(obuf);
    Serial.print(rA.mampsA);
    Serial.print(F(","));
    Serial.print(rA.mampsB);
    Serial.print(F(","));
    Serial.println(rA.mampsC);
    recordByte += sizeof(recordAmperage) - sizeof(uint16_t);
    break;

  case rAmpsVolts:
    readthem = seeprom.readBlock(recordByte,
                                 (uint8_t*) &rAV,
                                 sizeof(recordAmperageVoltage) - sizeof(uint16_t));
    if (readthem != sizeof(recordAmperageVoltage) - sizeof(uint16_t)) goto done;

    // Some 0's indicate we're done.
    if (rAV.dateTime[0] == 0 &&
        rA.dateTime[1] == 0 &&
        (rAV.dateTime[2] == 0 || rAV.dateTime[2] == 0xFF) &&
        (rAV.dateTime[3] == 0 || rAV.dateTime[3] == 0xFF)) goto done;

    //  The date.
    ctimec(obuf, rAV.dateTime);
    Serial.print(obuf);
    Serial.print(rAV.mampsA);
    Serial.print(F(","));
    Serial.print(rAV.mampsB);
    Serial.print(F(","));
    Serial.print(rAV.mampsC);
    Serial.print(F(","));
    Serial.print(rAV.battery / 10.0f, 2);
    Serial.print(F(","));
    Serial.print(rAV.voltA, 2);
    Serial.print(F(","));
    Serial.print(rAV.voltB, 2);
    Serial.print(F(","));
    Serial.println(rAV.voltC, 2);
    recordByte += sizeof(recordAmperageVoltage) - sizeof(uint16_t);
    break;
    
  case rAmpsTemps:
    readthem = seeprom.readBlock(recordByte,
                                 (uint8_t*) &rAT,
                                 sizeof(recordAmperageTemperature) - sizeof(uint16_t));
    if (readthem != sizeof(recordAmperageTemperature) - sizeof(uint16_t)) goto done;

    // Some 0's indicate we're done.
    if (rAT.dateTime[0] == 0 &&
        rA.dateTime[1] == 0 &&
        (rAT.dateTime[2] == 0 || rAT.dateTime[2] == 0xFF) &&
        (rAV.dateTime[3] == 0 || rAT.dateTime[3] == 0xFF)) goto done;

    //  The date.
    ctimec(obuf, rAT.dateTime);
    Serial.print(obuf);
    Serial.print(rAT.mampsA);
    Serial.print(F(","));
    Serial.print(rAT.mampsB);
    Serial.print(F(","));
    Serial.print(rAT.mampsC);
    Serial.print(F(","));
    Serial.print(rAT.tempA);
    Serial.print(F(","));
    Serial.print(rAT.tempB);
    Serial.print(F(","));
    Serial.print(rAT.tempC);
    Serial.print(F(","));
    Serial.println(rAT.tempX);
    recordByte += sizeof(recordAmperageTemperature) - sizeof(uint16_t);
    break;

  case rAll:
    readthem = seeprom.readBlock(recordByte,
                                 (uint8_t*) &rAVT,
                                 sizeof(recordAll) - sizeof(uint16_t));
    if (readthem != sizeof(recordAll) - sizeof(uint16_t)) goto done;

    // Some 0's indicate we're done.
    if (rAVT.dateTime[0] == 0 &&
        rAVT.dateTime[1] == 0 &&
        (rAVT.dateTime[2] == 0 || rAVT.dateTime[2] == 0xFF) &&
        (rAV.dateTime[3] == 0 || rAVT.dateTime[3] == 0xFF)) goto done;

    //  The date.
    ctimec(obuf, rAVT.dateTime);
    Serial.print(obuf);
    Serial.print(rAVT.mampsA);
    Serial.print(F(","));
    Serial.print(rAVT.mampsB);
    Serial.print(F(","));
    Serial.print(rAVT.mampsC);
    Serial.print(F(","));
    Serial.print(rAVT.battery, 2);
    Serial.print(F(","));
    Serial.print(rAVT.voltA, 2);
    Serial.print(F(","));
    Serial.print(rAVT.voltB, 2);
    Serial.print(F(","));
    Serial.print(rAVT.voltC, 2);
    Serial.print(F(","));
    Serial.print(rAVT.tempA);
    Serial.print(F(","));
    Serial.print(rAVT.tempB);
    Serial.print(F(","));
    Serial.print(rAVT.tempC);
    Serial.print(F(","));
    Serial.println(rAVT.tempX);
    recordByte += sizeof(recordAll) - sizeof(uint16_t);
    break;

  default:
    return;
  }
  goto loop;

 done:
  Serial.println("EOF");
}


// EOF
