#ifdef MERGELATER
/*
 * ph_base.h
 * base definitions for pH structures and protocols for comms between pH receiver and sender
 * also shared functions
 * lots of protocol copied from ADV
 *
 *  Created on: Feb 18, 2022
 *      Author: Irene Hu
 */


#include "ph_base.h"

unsigned char prnBuf[SIZEOF_PRNBUF+4];      // size is defd in system.h - this copied straight from sample.c

int16_t ph_calcChecksum(struct pH_ptdata *pHDatStruct)
// calculates checksum and returns it (so phf can store it, phb can compare it)
// different size iterator from send/receive, so might as well do it separately
{
    int i;
    int16_t checksum = PH_CHECKSUM_BASE;
    int16_t *iterator = (int16_t *)pHDatStruct; // can i do this now that i'm starting from the beginning

    for (i=0; i<6; i++) {
        // structure is 12 bytes including sync/ID and excluding checksum
        checksum += iterator[i];
    }
    return checksum;
}


int ph_printpHDataHdr(FIL* ofs, int txFlg)
// print the header for the pH+therm data as printed by printVelData
// does NOT print \n at end
// as with adv print user config, txFlg to tell it whether to print to screen
// pass NULL to ofs to not output to file
// returns 0 if success, 1 if fail
{
    if (ofs == NULL & txFlg != 1) // well i don't know what you're doing in this case
        return 1;

    // file output variables
    FRESULT fresult;
    int bw;
    uint32_t indx = 0;

#ifdef PRINT_PH_TIMING
    indx += sprintf(&prnBuf[indx], "Elapsed time when rcvd (s)\t");
#endif
    indx += sprintf(&prnBuf[indx], "#\tVrs (V)\tVtherm (V)\tChecksum");

    if (txFlg) {
        bw = UARTwrite(prnBuf, strlen(prnBuf));
        UARTFlushTx(FALSE);
    }
    if (ofs != NULL) {
        fresult = f_write( ofs, prnBuf, strlen(prnBuf), (UINT*)&bw);
        if(fresult != FR_OK)
        {
            uprintf("f_write error: %s\n", StringFromFresult(fresult));
            // StringFromFresult is in microsd.c
            return 1;
        }
    }

    return 0;

}


int ph_printpHData(struct pH_ptdata *pHDatStruct, FIL* ofs, int txFlg)
// outputs pH+therm data given in pHDatStruct to ofs
// does NOT print \n at end
// as with adv print user config, txFlg to tell it whether to print to screen
// pass NULL to ofs to not output to file
// returns 0 if success, 1 if fail
// count is the pHDatStruct's count; no longer accepts external counter (3/9/22)
{
    if (!pHDatStruct) return 1;

    if (ofs == NULL & txFlg != 1) // well i don't know what you're doing in this case
        return 1;

    // file output variables
    FRESULT fresult;
    int bw;
    uint32_t indx = 0;

    // for .f types, precision field specifies number of digits after decimal point
    // https://docwiki.embarcadero.com/RADStudio/Sydney/en/Printf_Precision_Specifiers
    // copy sample.c with 6 decimal points
    // do the conversion from ADC units here

    indx += sprintf(&prnBuf[indx], "%d\t%.6f\t%.6f\t", pHDatStruct->count, pHDatStruct->Vrs * COUNTSTOVOLTS, pHDatStruct->Vtherm * COUNTSTOVOLTS);
    //indx += sprintf(&prnBuf[indx], "%d\t%" PRId32 "\t%" PRId32 "\t", counter, pHDatStruct->Vrs, pHDatStruct->Vtherm);
    indx += sprintf(&prnBuf[indx], "%d", (int)(pHDatStruct->checksum));

    if (txFlg) {
        bw = UARTwrite(prnBuf, strlen(prnBuf));
        UARTFlushTx(FALSE);
    }
    if (ofs != NULL) {
        fresult = f_write( ofs, prnBuf, strlen(prnBuf), (UINT*)&bw);
        if(fresult != FR_OK)
        {
            uprintf("f_write error: %s\n", StringFromFresult(fresult));
            // StringFromFresult is in microsd.c
            return 1;
        }
    }

    return 0;

}



void ph_printHexData(struct pH_ptdata *pHDatStruct)
// print pHThermData as hex bytes
// for troubleshooting
{
    char *iterator = (char *)pHDatStruct;
    int i;
    for (i = 0; i < sizeof(struct pH_ptdata); i++) {
        uprintf("%#02x ", iterator[i]);
    }
    uprintf("\n");

}
#endif
