/*
 * config.c
 *
 *  Created on: Apr 23, 2021
 *      Author: thomm
 */

// Config - data field selection, modes of operation, driver config, set/get and print functions (moved from system.c)


#include "system.h"
#include "config.h"

#include "user_io.h"
#include "ads1248_iso.h"
#include "ads1248_noniso.h"

#include "board_util.h"

#include "ctd_base.h"
#include "optode.h"

#include "sleep.h"
#include "microsd.h"
#include "uartstdio.h"  // User local version with larger RX buffer

extern int microSD_flg;
extern int dbg_flag;
extern struct systemSamplingData sys_samp;
extern struct ctdDriver ctd;

/********************************************************************************************
 *  Configuration Menu.
 ********************************************************************************************/
void configuration_menu(void)
{
    char input, buff[100];
    int response;
    time_t usersTime, timer;
    unsigned long temp;
    float ftemp;


    while(1)
    {
#ifdef OLDCODE
        //print out config menu
        //uprintf("\n\n\nConfiguration Menu -- %s", VERSION);
        uprintf("\n\n\nConfiguration Menu:\n");
        uprintf("1 -- Set Clock\n");
        uprintf("2 -- Change File Name\n");
        uprintf("3 -- Set Deployment Parameters\n");
        uprintf("4 -- Enter pH Sensor Calibration Coefficients\n");
        uprintf("5 -- Calculate pH Sensor Calibration Coefficients\n");
        uprintf("6 -- Change Baud Rate\n");
        uprintf("7 -- Config Data Fields\n");
        uprintf("8 -- Metadata Settings\n");        // was a hidden menu requiring password
        uprintf("D -- Data Header, Sample Data, Sample Timing\n");
        if(sys_data.diag == 1) uprintf("S -- Print EEPROM Sys_data structure\n");  // turn this on by 'Q' in Help menu (main.c)
        uprintf("9 -- Exit to Main Menu\n");
#endif
        if(sys_data.app_cfg[APPCFG_MENU_LEVEL] == 1)        // Brief menu
        {
            //print out config menu
            //uprintf("\n\n\nConfiguration Menu -- %s", VERSION);
            uprintf("\n\n\nConfiguration Menu:\n");
            uprintf("1 -- Clock\n");
            uprintf("2 -- FileName\n");
            uprintf("3 -- Deployment Parameters\n");
            uprintf("4 -- Enter pH Sensor Calibration Coefficients\n");
            uprintf("5 -- Calculate pH Sensor Calibration Coefficients\n");
            uprintf("6 -- Baud Rate\n");
            uprintf("7 -- Data Fields\n");
            uprintf("8 -- Metadata\n");        // was a hidden menu requiring password
            uprintf("D -- Data Header, Live Data, Sample Timing\n");
            uprintf("E -- Edit/Copy EEPROM sys_data\n");
            uprintf("S -- Print EEPROM Sys_data structure\n");  // turn this on by 'Q' in Help menu (main.c)
        }
        else        // Regular menu
        {
            //print out config menu
            //uprintf("\n\n\nConfiguration Menu -- %s", VERSION);
            uprintf("\n\n\nConfiguration Menu:\n");
            uprintf("1 -- Set Clock\n");
            uprintf("2 -- Change File Name\n");
            uprintf("3 -- Set Deployment Parameters\n");
            uprintf("4 -- Enter pH Sensor Calibration Coefficients\n");
            uprintf("5 -- Calculate pH Sensor Calibration Coefficients\n");
            uprintf("6 -- Change Baud Rate\n");
            uprintf("7 -- Config Data Fields\n");
            uprintf("8 -- Metadata Settings\n");        // was a hidden menu requiring password
            uprintf("D -- Data Header, Sample Data, Sample Timing\n");
            uprintf("E -- Edit/Copy EEPROM sys_data structure\n");        // Copy and edit sys_data EEPROM structure after firmware upgrade\n");
            //if(sys_data.diag == 1)
            uprintf("S -- Print EEPROM Sys_data structure\n");  // turn this on by 'Q' in Help menu (main.c)
        }
        uprintf("9 -- Exit to Main Menu\n");
        uprintf("\nEnter Selection [9]: ");

        // Get user's selection with a 5 min timeout
        UARTFlushRx();
        timer = 300 + ROM_HibernateRTCGet();

        while(1)
        {
            if( UARTRxBytesAvail() )
            {
                input = get_key();
                break;
            }

            if( timer <= ROM_HibernateRTCGet() ) sleep(IDLE, 0);
        }

        switch(input)
        {
            case 'S':
            case 's':
                print_sys_data_structure();
                break;

            case 'c':
            case 'C':
                exercise_iso();
                break;

            case 'd':
            case 'D':
            case ' ':
                //print_config_data_fields();
                uprintf("\n Data Fields:\n\n");
                print_write_config_data_fields(2,0);
                uprintf("\n\n");
                print_data_header();     // defd in system.c
                sys_data.app_cfg[APPCFG_SAMPLING_DIAG] = 1;
                fast_sample(1);
                sys_data.app_cfg[APPCFG_SAMPLING_DIAG] = 0;
                break;
            case 'e':
            case 'E':
                copy_edit_sys_data_structure();
                break;

            case '1':  // Set Clock
                response = yesOrNoMenuChoice("\n\nIs time GMT? (Y/N) [N]? ", NO);
                if(response == YES) sys_data.GMT = true;
                else sys_data.GMT = false;

                usersTime = getDateAndTime();

                if(usersTime)
                {
                    ROM_HibernateRTCSet(usersTime); // Set time if user entered valid time
                    uprintf("\nTime now: ");
                    printCurrentTime();
                    if(sys_data.GMT) uprintf(" GMT");
                    else uprintf(" Local");
                    uprintf("\n\nNote: Daylight Savings not observed. Use GMT when this will be an issue.\n");
                }
                break;

            case '2':  //Set / Change file name
                uprintf("\nEnter data file name (no spaces, 8 characters before .txt or .xls) [%s]: ", sys_data.fileName);
                if( getUserInput(buff, MAX_FILENAME) )
                {
                    strcpy(sys_data.fileName, buff);
                    uprintf("\nFile name now: %s\n", sys_data.fileName);
                }

                uprintf("\n\nEnter user initials [%s]: ", sys_data.user);

                if( getUserInput(buff, MAX_USER_INITIALS) )
                {
                    strcpy(sys_data.user, buff);
                    uprintf("\nUser initials now: %s\n", sys_data.user);
                }
                break;

            case '3':  //Set timing (query user for values, save them in EEPROM)

                // Sample period
                if(sys_data.sample_aligned == true)  // THOM new 23Apr2019
                {
                    response = yesOrNoMenuChoice("\n\nSample aligned to hour? (Y/N) [Y]? ", YES);
                }
                else
                {
                    response = yesOrNoMenuChoice("\n\nSample aligned to hour? (Y/N) [N]? ", NO);
                }

                if(response == YES) sys_data.sample_aligned = true;
                else sys_data.sample_aligned = false;

                while(1)
                {
                    uprintf("\nEnter sample period [%u] sec: ", sys_data.sampling_period);
                    if(getUserInput(buff, 20))
                    {
                        if(sscanf(buff, "%u", &temp) == 1)
                        {

                            // sample_period = 0 means POLLED MODE
                            // use COMMAND mode to keep from going into DEPLOY   THOMHERE

                            if(temp == 0)
                            {
                                uprintf("\nSample period is zero - polled mode\n");
                            }


                            if(sys_data.sample_aligned)     // Sample aligned to hour?
                            {
                                //if(3600 % temp)
                                if((3600 % temp) && (temp > 0))     // BUG Fix 6 May 2019, per request to allow sample aligned to hour in polled mode (sample period = 0) - Not sure the sense of it though since in polled mode, the poller is determining the time
                                {
                                    uprintf("\nSample period must evenly divide hour (e.g. 5, 10, 15, 20, 30 minutes)");
                                    continue;
                                }
                            }

                            if(temp <= 3600)        // Check if less than hour
                            {
                                sys_data.sampling_period = temp;
                                uprintf("Sample period now %u sec.\n\n", sys_data.sampling_period);
                                break;
                            }
                            else
                            {
                            uprintf("\nEnter time less than or equal to 3600 sec");
                            }

                            continue;
                        } // scanf
                        else break;

                    }
                    else break;
                }
#if BOARD_MPHOX >= 1
                // Pump on time
                while(1)
                {
                    uprintf("\nEnter pump on time [%u] sec: ", sys_data.pumpon_time);
                    if(getUserInput(buff, 20))
                    {
                        if(sscanf(buff, "%u", &temp) == 1)
                        {
                            if(temp <= 3600)
                            {
                                sys_data.pumpon_time = temp;
                                uprintf("Pump on time now %u sec.\n\n", sys_data.pumpon_time);
                                break;
                            }
                            else uprintf("\nEnter time less than 3600 sec");
                            continue;
                        }
                        else break;
                    }
                    else break;
                }
#endif
                // Low battery voltage
                while(1)
                    {
#if BOARD_MFET >= 1
                        sprintf(buff, "\nEnter low battery_voltage (ex. 5.5 V) [%.1f V]: ", sys_data.low_batt_volt);
#endif
#if BOARD_MPHOX >= 1
                        sprintf(buff, "\nEnter low battery_voltage (ex. 10.5 V with Pump) [%.1f V]: ", sys_data.low_batt_volt);
#endif
#if BOARD_NANOFET >= 1
                        sprintf(buff, "\nEnter low battery_voltage (ex. 5.5 V) [%.1f V]: ", sys_data.low_batt_volt);
#endif

                        uprintf("%s", buff);
                        if(getUserInput(buff, strlen(buff) ))
                        {
                            if(sscanf(buff, "%f", &ftemp) == 1)
                            {
                                if(ftemp >= 0 && ftemp <= 30)
                                {
                                    sys_data.low_batt_volt = ftemp;
                                    sprintf(buff, "\nLow battery_voltage now [%.1f V]\n\n", sys_data.low_batt_volt);
                                    uprintf("%s", buff);
                                break;
                                }
                                else uprintf("\nEnter voltage less than 30 V");
                                continue;
                            }
                            else break;
                        }
                            else break;
                    }


                // Output mode
                uprintf("\nOutput mode menu\n");
                uprintf("1 -- Normal  (Data only)\n");
                uprintf("2 -- Verbose (Data with prompts)\n");
                uprintf("\nEnter Selection [%d]: ", (int)sys_data.output);

                input = get_key();

                if(input)
                {
                    switch(input)
                    {
                        case '1': sys_data.output = NORMAL;
                        uprintf("\n\nOutput mode now: NORMAL");
                            break;

                        case '2': sys_data.output = VERBOSE;
                        uprintf("\n\nOutput mode now: VERBOSE");
                            break;
                    }

                }

                // Error check the setup

                uint32_t sampTimeEst;
                sampTimeEst = sys_data.pumpon_time * 1000;


                if((sys_data.data_cfg[CFG_OPTODE] > 0) ||  (sys_data.data_cfg[CFG_CTD] > 0))
                    sampTimeEst += 2500;        // TBD

                if(sys_data.data_cfg[CFG_BIAS_BAT_NEG] > 0)
                    sampTimeEst += ((VBIAS_TRIALS * 1000) / VBIAS_SPS);
                if(sys_data.data_cfg[CFG_BIAS_BAT_POS] > 0)
                    sampTimeEst += ((VBIAS_TRIALS * 1000) / VBIAS_SPS);
                if(sys_data.data_cfg[CFG_I_SUBSTRATE] > 0)
                    sampTimeEst += ((IB_TRIALS * 1000) / IB_SPS);
                if(sys_data.data_cfg[CFG_I_COUNTER] > 0)
                    sampTimeEst += ((IK_TRIALS * 1000) / IK_SPS);
                if(sys_data.data_cfg[CFG_V_COUNTER_ELECT] > 0)
                    sampTimeEst += ((VK_TRIALS * 1000) / VK_SPS);
                if(sys_data.data_cfg[CFG_V_THERMISTOR] > 0)
                    sampTimeEst += ((VTHERM_TRIALS * 1000) / VTHERM_SPS);
                if(sys_data.data_cfg[CFG_VRSI_BIASED] > 0)
                    sampTimeEst += ((VRSI_TRIALS * 1000) / VRSI_SPS);
                if(sys_data.data_cfg[CFG_VRSI] > 0)
                    sampTimeEst += ((VRSI_TRIALS * 1000) / VRSI_SPS);
                if(sys_data.data_cfg[CFG_VRSE_BIASED] > 0)
                    sampTimeEst += ((VRSE_TRIALS * 1000) / VRSE_SPS);
                if(sys_data.data_cfg[CFG_VRSE] > 0)
                    sampTimeEst += ((VRSE_TRIALS * 1000) / VRSE_SPS);

                sampTimeEst += 150;


                if(sampTimeEst > (250 + sys_data.sampling_period*1000)) // 250msec is margin (somewhat arbitrarily set)
                {
                    if(sys_data.sampling_period > 0)
                        uprintf("\nError: Estimated sample time = %.2f sec, is greater than sample period = %u sec.  Increase sample period\n", (float)sampTimeEst/1000.0, sys_data.sampling_period);
                    else
                        uprintf("\nEstimated sample time = %.2f sec in polled mode (sample period is 0)\n", (float)sampTimeEst/1000.0, sys_data.sampling_period);  // buf fix 22 Jul 2021
                }
                else
                {
                    uprintf("\nEstimated sample time = %.2f sec, sample period = %u sec\n", (float)sampTimeEst/1000.0, sys_data.sampling_period);

                }

                break;

            //float ftemp;
            case '4': //Enter pH Sensor Calibration Coefficients
                //Eo_int_25
                enter_pH_sensor_cal_coeff();
                break;

            case '5': //Calculate pH Sensor Calibration Coeffcients
                ph_calib();
                break;

            case '6':  // Change baud rate
                change_baud_rate();
                break;

            case '7':  // Config data fields (select which fields are logged and displayed)
                config_data_fields();
                break;

            case '8': // Was a Hidden menu option, now metadata menu 2 Apr 2021
                uprintf("\nMetadata Settings:\n");



#ifdef REMOVEPASSWORD       // per Yui 2 Apr 2021
                uprintf("\n\nFactory settings. Enter password: ");
                // Compare user input with password
                if( getUserInput(buff, 14) )
                {
                    uprintf("Pass: %s\n\n", buff);
                    buff[12] = 0x00;
                    if( strcmp("takeshitalab", buff)  == 0  ) // if strings are equal, strcmp returns 0
                        //      012345678901
                        matchFlg = 1;  //break;
                    buff[8] = 0x00;
                    if( strcmp("martzlab", buff) == 0 )
                        matchFlg = 1;  //break;

                }
                else
                    break;

                // if bad password, no access
                if(matchFlg == 0)
                    break;
#endif

                enter_metadata();  // user entry of metadata settings

                break;



#ifdef FUTURECODE_ENTERTRIALSSPS
            case 'Q':

                // pH Vint sample rate setting
                while(1)
                {
                    uprintf("\nEnter pH Vint sample rate setting [%d]: ", sys_data.Vrsi_sps);
                    if(getUserInput(buff, 20))
                    {
                        if(sscanf(buff, "%d", &temp) == 1)
                        {
                            if(temp == 5 || temp == 10 || temp == 20 || temp == 40 || temp == 80 || temp == 160
                                    || temp == 320 || temp == 640 || temp == 1000 || temp == 2000)
                            {
                                sys_data.Vrsi_sps = temp;
                                uprintf("\npH Vint data rate now %d sps\n\n", sys_data.Vrsi_sps);
                                break;
                            }
                            else uprintf("\nEnter 5, 10, 20, 40, 80, 160, 320, 640, 1000, or 2000");
                            continue;
                        }
                        else break;
                    }
                    else break;
                }

                break;


                // pH Vint gain setting
                while(1)
                {
                    uprintf("\nEnter pH Vint gain setting [%d]: ", sys_data.Vint_gain);
                    if(getUserInput(buff, 20))
                    {
                        if(sscanf(buff, "%d", &temp) == 1)
                        {
                            if(temp == 1 || temp == 2 || temp == 4 || temp == 8 || temp == 16 || temp == 32)
                            {
                                sys_data.Vint_gain = temp;
                                uprintf("\npH Vint gain now %d\n\n", sys_data.Vint_gain);
                                break;
                            }
                            else uprintf("\nEnter 1, 2, 4, 8, 16, or 32");
                            continue;
                        }
                        else break;
                    }
                    else break;
                }

#endif
            default: //Exit
                // Write the sys_data struct into EEPROM.  Last argument ensures # of bytes is multiple of 4
                if(storeSysDataVariables())                     //if(EEPROMProgram((uint32_t*) &sys_data, 0x400, (sizeof(sys_data) + 3) & ~3))
                {
                    uprintf("\n\nError storing system data.\n");
                }
                else uprintf("\n\nSystem data stored.  -- Press enter to bring up main menu\n");
                return;
        }
    }
}


void enter_pH_sensor_cal_coeff(void)
{
    char input, buff[100];
    int response;
    time_t usersTime, timer;
    unsigned long temp;
    float ftemp;
    double dtemp;

    while(1)
    {
        // a good default is -0.4
        //sprintf(buff, "\nEnter E0_int @25 C (ex. -0.4) [%f]: ", sys_data.Eo_int_25C);
        sprintf(buff, "\nEnter k0int (ex. -0.4) [%f]: ", sys_data.k0int);

        uprintf("%s", buff);
        if(getUserInput(buff, 20))
        {
            if(sscanf(buff, "%f", &ftemp) == 1)
            {
                if(ftemp >= (-2) && ftemp <= 2)
                {
                    sys_data.k0int = ftemp;
                    break;
                }
                else uprintf("Enter number between -2 and 2 (ex. -0.4)\n\n");
                continue;
            }
        else break;
        }
        else break;
    }


    //Eo_ext_25
    while(1)
    {
            // a good default is -1.3
        //sprintf(buff, "\nEnter E0_ext @25 C (ex. -1.3) [%f]: ", sys_data.Eo_ext_25C);
        sprintf(buff, "\nEnter k0ext (ex. -1.3) [%f]: ", sys_data.k0ext);

        uprintf("%s", buff);
        if(getUserInput(buff, 20))
        {
            if(sscanf(buff, "%f", &ftemp) == 1)
            {
                if(ftemp >= (-2) && ftemp <= 2)
                {
                    sys_data.k0ext = ftemp;
                    break;
                }
                else uprintf("Enter number between -2 and 2 (ex. -1.3)\n\n");
                continue;
            }
            else break;
            }
        else break;
    }

    while(1)
    {
        sprintf(buff, "\nEnter k2int (ex. -0.001455) [%f]: ", sys_data.k2int);

        uprintf("%s", buff);
        if(getUserInput(buff, 20))
        {
            if(sscanf(buff, "%f", &ftemp) == 1)
            {
                if(ftemp >= (-2) && ftemp <= 2)
                {
                    sys_data.k2int = ftemp;
                    break;
                }
                else uprintf("Enter number between -2 and 2 (ex. -0.001)\n\n");
                continue;
            }
            else break;
            }
        else break;
    }

    while(1)
    {
        sprintf(buff, "\nEnter k2ext (ex. -0.001081) [%f]: ", sys_data.k2ext);

        uprintf("%s", buff);
        if(getUserInput(buff, 20))
        {
            if(sscanf(buff, "%f", &ftemp) == 1)
            {
                if(ftemp >= (-2) && ftemp <= 2)
                {
                    sys_data.k2ext = ftemp;
                    break;
                }
                else uprintf("Enter number between -2 and 2 (ex. -0.001)\n\n");
                continue;
            }
            else break;
            }
        else break;
    }


    // new THOM 29 Apr 2019, remove TCOffset per Yui, TCOffset feature restored 3 Feb 2021
    if(sys_data.TCOffset > 400.0 || sys_data.TCOffset < -400.0)  // if offset is whacked, zero it.
        sys_data.TCOffset = 0.0;
    // TCOffset added back 3 Feb 2021
    //TCOffset, standard = 0
    while(1)
    {
        // default of zero is ok
    sprintf(buff, "\nEnter TCOffset [%f]: ", sys_data.TCOffset);
    uprintf("%s", buff);
    if(getUserInput(buff, 20))
    {
        if(sscanf(buff, "%f", &ftemp) == 1)
        {
            if(ftemp >= -400.0 && ftemp <= 400.00)
            {
                sys_data.TCOffset = ftemp;
                break;
            }
            else uprintf("Enter a reasonable TCOffset such as 0.0\n\n");
            continue;
        }
        else break;

        }
        else break;
    }



    // new THOM add entry of default salinity
    if(sys_data.default_sal > 100.0 || sys_data.default_sal < 0.0)  // if salinity is whacked, zero it.
    {
        uprintf("Salinity out of range, recommend running the test menu, qa board menu to initialize vars in eeprom\n");
        sys_data.TCOffset = 35.0;
    }

    while(1)
    {
        // default of 35.0
    sprintf(buff, "\nEnter Salinity for Ext Ref pH calc (typical is 35) [%f]: ", sys_data.default_sal);
    uprintf("%s", buff);
    if(getUserInput(buff, 20))
    {
        if(sscanf(buff, "%f", &ftemp) == 1)
        {
            if(ftemp >= (0.0) && ftemp <= 100.0)
            {
                sys_data.default_sal = ftemp;
                break;
            }
            else uprintf("Enter 35.0\n\n");
            continue;
        }
        else break;

        }
        else break;
    }
    if(sys_data.app_cfg[APPCFG_TEMPC_SH_ALG] == 0)
    {
        uprintf("\n4 Var S-H coefficients for Thermistor to Temperature Celsius:\n");
        uprintf("     c0 = %.11lf\n", sys_data.quadTherm_c0);
        uprintf("     c1 = %.11lf\n", sys_data.quadTherm_c1);
        uprintf("     c2 = %.11lf\n", sys_data.quadTherm_c2);
        uprintf("     c3 = %f.11l\n", sys_data.quadTherm_c3);
        uprintf("\n");
    }
    if(sys_data.app_cfg[APPCFG_TEMPC_SH_ALG] == 1)
    {
        uprintf("\n3 Var S-H coefficients for Thermistor to Temperature Celsius:\n");
        uprintf("      A = %.11lff\n", sys_data.shTherm_A);
        uprintf("      B = %.11lf\n", sys_data.shTherm_B);
        uprintf("      C = %.11lf\n", sys_data.shTherm_C);
        uprintf("\n");
    }


        if(sys_data.app_cfg[APPCFG_TEMPC_SH_ALG] == 0)
        {

        while(1)
        {
            //sprintf(buff, "\nEnter Steinhart-Hart c0 (ex. 340.9819863) [%f]: ", sys_data.quadTherm_c0);
            sprintf(buff, "\nEnter 4 Var S-H c0 (ex. 340.9819863) [%.11lf]: ", sys_data.quadTherm_c0);

            uprintf("%s", buff);
            if(getUserInput(buff, 20))
            {
                //if(sscanf(buff, "%f", &ftemp) == 1)
                if(sscanf(buff, "%lf", &dtemp) == 1)
                {
                    if(dtemp >= (0.0) && dtemp <= 500.0)
                    {
                        sys_data.quadTherm_c0 = dtemp;
                        uprintf("c0 = %.11lf", sys_data.quadTherm_c0);
                        break;
                    }
                    else uprintf("Enter number between 0 and 500 (ex. 340.9819863)\n\n");
                    continue;
                }
                else break;
                }
            else break;
        }

        while(1)                 // -0.0000910257
        {
            sprintf(buff, "\nEnter 4 Var S-H c1 (ex. -0.0000910257, -9.10257E-05) [%.11lf]: ", sys_data.quadTherm_c1);

            uprintf("%s", buff);
            if(getUserInput(buff, 20))
            {
                if(sscanf(buff, "%lf", &dtemp) == 1)
                {
                    if(dtemp >= (-0.1) && dtemp <= 0.1)
                    {
                        sys_data.quadTherm_c1 = dtemp;
                        uprintf("c1 = %.11lf", sys_data.quadTherm_c1);
                        break;
                    }
                    else uprintf("Enter number between 0.1 and -0.1 (ex. 0.0000910257)\n\n");
                    continue;
                }
                else break;
                }
            else break;
        }

        while(1)                 // -0.0000910257
        {
            sprintf(buff, "\nEnter 4 Var S-H  c2 (ex. -95.08806667) [%.11lf]: ", sys_data.quadTherm_c2);

            uprintf("%s", buff);
            if(getUserInput(buff, 20))
            {
                if(sscanf(buff, "%lf", &dtemp) == 1)
                {
                    if(dtemp >= (-200.0) && dtemp <= 200.0)
                    {
                        sys_data.quadTherm_c2 = dtemp;
                        uprintf("c2 = %.11lf", sys_data.quadTherm_c2);
                        break;
                    }
                    else uprintf("Enter number between -200.0 and 200.0 (ex. -95.08806667)\n\n");
                    continue;
                }
                else break;
                }
            else break;
        }


        while(1)                 // -0.0000910257
        {
            sprintf(buff, "\nEnter 4 Var S-H  c3 (ex. 0.965370274) [%.11lf]: ", sys_data.quadTherm_c3);

            uprintf("%s", buff);
            if(getUserInput(buff, 20))
            {
                if(sscanf(buff, "%lf", &dtemp) == 1)
                {
                    if(dtemp >= (-2.0) && dtemp <= 2.0)
                    {
                        sys_data.quadTherm_c3 = dtemp;
                        uprintf("c3 = %.11lf", sys_data.quadTherm_c3);
                        break;
                    }
                    else uprintf("Enter number between -2.0 and 2.0 (ex. 0.965370274)\n\n");
                    continue;
                }
                else break;
                }
            else break;
        }
    } // if 4 var

    if(sys_data.app_cfg[APPCFG_TEMPC_SH_ALG] == 1)
    {
        while(1)
        {
            sprintf(buff, "\nEnter 3 Var Steinhart-Hart A  [%.11lf]: ", sys_data.shTherm_A);
            uprintf("%s", buff);
            if(getUserInput(buff, 20))
            {
                if(sscanf(buff, "%lf", &dtemp) == 1)
                {
                    if(dtemp >= (-500.00) && dtemp <= 500.0)
                    {
                        sys_data.shTherm_A = dtemp;
                        uprintf("A = %.11lf", sys_data.shTherm_A);
                        break;
                    }
                    else uprintf("Enter number between -500 and 500, not sure what to recommend \n\n");
                    continue;
                }
                else break;
                }
            else break;
        }


        while(1)
        {
            sprintf(buff, "\nEnter 3 Var Steinhart-Hart B  [%.11lf]: ", sys_data.shTherm_B);
            uprintf("%s", buff);
            if(getUserInput(buff, 20))
            {
                if(sscanf(buff, "%lf", &dtemp) == 1)
                {
                    if(dtemp >= (-500.00) && dtemp <= 500.0)
                    {
                        sys_data.shTherm_B = dtemp;
                        uprintf("B = %.11lf", sys_data.shTherm_B);
                        break;
                    }
                    else uprintf("Enter number between -500 and 500, not sure what to recommend \n\n");
                    continue;
                }
                else break;
                }
            else break;
        }

        while(1)
        {
            sprintf(buff, "\nEnter 3 Var Steinhart-Hart C  [%.11lf]: ", sys_data.shTherm_C);
            uprintf("%s", buff);
            if(getUserInput(buff, 20))
            {
                if(sscanf(buff, "%lf", &dtemp) == 1)
                {
                    if(dtemp >= (-500.00) && dtemp <= 500.0)
                    {
                        sys_data.shTherm_C = dtemp;
                        uprintf("C = %.11lf", sys_data.shTherm_C);
                        break;
                    }
                    else uprintf("Enter number between -500 and 500, not sure what to recommend \n\n");
                    continue;
                }
                else break;
                }
            else break;
        }
    } // if 3 var
    //uprintf("\n*****Calibration Coefficients Saved*****\n\n");
}

void change_baud_rate(void)
{
    char input;
    time_t timer;

    //print out config menu
    uprintf("\n\n\nChange Baud Rate - select baud menu item, change terminal baud rate, then hit 'V' to verify, then sleep and wakeup");
    //uprintf("\nV -- Verify");
    uprintf("\n1 -- 115200");
    uprintf("\n2 -- 9600");
    uprintf("\n9 -- Exit to Main Menu");
    uprintf("\nEnter Selection [9]: ");

    // Get user's selection with a 5 min timeout
    UARTFlushRx();
    timer = 300 + ROM_HibernateRTCGet();

    while(1)
    {
        if( UARTRxBytesAvail() )
        {
            input = get_key();
            break;
        }

        if( timer <= ROM_HibernateRTCGet() ) sleep(IDLE, 0);
    }

    switch(input)
    {
        case 'V':
        case 'v':
            uprintf("\nBaud rate verify\n");
            break;
        case '1':
            // 115200
            sys_data.baud_rate = 115200L;
            UARTStdioConfig(0, 115200, 16000000);       // 115200 bits per second
            break;
        case '2':
            sys_data.baud_rate = 9600L;
            UARTStdioConfig(0, 9600, 16000000);       // 9600 bits per second
            break;
        default:
            // Write the sys_data struct into EEPROM.  Last argument ensures # of bytes is multiple of 4
            if(storeSysDataVariables())                 //if(EEPROMProgram((uint32_t*) &sys_data, 0x400, (sizeof(sys_data) + 3) & ~3))
            {
                uprintf("\n\nError storing system data.\n");
            }
            else
                uprintf("\n\nSystem data stored.  -- Press enter to bring up main menu\n");
            break;

    }

}


void print_write_config_data_fields(uint32_t printFlg, uint32_t writeFlg)
{
    FIL fileObject;     // File object
    FRESULT fresult;
    UINT bw;
    int retVal;
    char padString[10];

    retVal = 0;     // assume a good return (1 is error due to SD card

    if(microSD_flg == 0)
    {
        retVal = 0;  // pretend it's good
        writeFlg = 0;
    }

    //printFlg = 2;       // DEBUG
    padString[0] = 0;
    if(printFlg > 1 || writeFlg > 1)
    {
        strcpy(padString, "// ");
    }
    //printFlg = 1;       // DEBUG

    if(writeFlg == 1)
    {

        // Open a file
        fresult = f_open(&fileObject, sys_data.fileName, FA_READ |FA_WRITE |FA_OPEN_ALWAYS);
        if(fresult != FR_OK)
        {
            uprintf("f_open error: %s\n", StringFromFresult(fresult));
            retVal = 1;
            writeFlg = 0;
            //return 1;
        }

        // Seek to the end, to append our file
        fresult = f_lseek(&fileObject, fileObject.fsize);
        if(fresult != FR_OK)
        {
            uprintf("f_lseek error: %s\n", StringFromFresult(fresult));
            retVal = 1;
            writeFlg = 0;
        }

    }

    if(printFlg>=1)                 uprintf("%sA -- Sample Num               = %u\n", padString, sys_data.data_cfg[CFG_SAMPLE_NUM]);                 //A + Sample_Number\t
    if(writeFlg==1) sd_fprintf(&fileObject, "%sA -- Sample Num               = %u\n", padString, sys_data.data_cfg[CFG_SAMPLE_NUM]);                 //A + Sample_Number\t

    if(printFlg>=1)                  uprintf("%sB -- MM/DD/YYYY HH:MM:SS      = %u\n", padString, sys_data.data_cfg[CFG_TIMEDATE]);                   //B + MM/DD/YYYY HH:MM:SS\t
    if(writeFlg==1) sd_fprintf(&fileObject,  "%sB -- MM/DD/YYYY HH:MM:SS      = %u\n", padString, sys_data.data_cfg[CFG_TIMEDATE]);                   //B + MM/DD/YYYY HH:MM:SS\t

    if(printFlg==1)                  uprintf("\n");

    if(printFlg>=1)                 uprintf("%sC -- Battery Volt             = %u\n", padString, sys_data.data_cfg[CFG_VIN_BAT_VOLT]);
    if(writeFlg==1) sd_fprintf(&fileObject, "%sC -- Battery Volt             = %u\n", padString, sys_data.data_cfg[CFG_VIN_BAT_VOLT]);

    if(printFlg>=1)                 uprintf("%sD -- Bias Battery Pos         = %u\n", padString, sys_data.data_cfg[CFG_BIAS_BAT_POS]);
    if(writeFlg==1) sd_fprintf(&fileObject, "%sD -- Bias Battery Pos         = %u\n", padString, sys_data.data_cfg[CFG_BIAS_BAT_POS]);

    if(printFlg>=1)                 uprintf("%sE -- Bias Battery Neg         = %u\n", padString, sys_data.data_cfg[CFG_BIAS_BAT_NEG]);
    if(writeFlg==1) sd_fprintf(&fileObject, "%sE -- Bias Battery Neg         = %u\n", padString, sys_data.data_cfg[CFG_BIAS_BAT_NEG]);

    if(printFlg>=1)                 uprintf("%sF -- Board Temperature        = %u\n", padString, sys_data.data_cfg[CFG_BRD_TEMP]);
    if(writeFlg==1) sd_fprintf(&fileObject, "%sF -- Board Temperature        = %u\n", padString, sys_data.data_cfg[CFG_BRD_TEMP]);

    if(printFlg>=1)                 uprintf("%sG -- Board Humidity           = %u\n", padString, sys_data.data_cfg[CFG_BRD_HUMIDITY]);
    if(writeFlg==1) sd_fprintf(&fileObject, "%sG -- Board Humidity           = %u\n", padString, sys_data.data_cfg[CFG_BRD_HUMIDITY]);

    if(printFlg==1) uprintf("\n");

    if(printFlg>=1)                 uprintf("%sH -- Vthermistor              = %u\n", padString, sys_data.data_cfg[CFG_V_THERMISTOR]);
    if(writeFlg==1) sd_fprintf(&fileObject, "%sH -- Vthermistor              = %u\n", padString, sys_data.data_cfg[CFG_V_THERMISTOR]);

    if(printFlg>=1)                 uprintf("%s     Vtermistor Std           = %u\n", padString, sys_data.data_cfg[CFG_V_THERMISTOR_STD]);
    if(writeFlg==1) sd_fprintf(&fileObject, "%s     Vtermistor Std           = %u\n", padString, sys_data.data_cfg[CFG_V_THERMISTOR_STD]);

    if(printFlg>=1)                 uprintf("%sI -- Temperature Calc         = %u\n", padString, sys_data.data_cfg[CFG_CALC_TEMP]);
    if(writeFlg==1) sd_fprintf(&fileObject, "%sI -- Temperature Calc         = %u\n", padString, sys_data.data_cfg[CFG_CALC_TEMP]);


    if(printFlg>=1)                 uprintf("%sJ -- Vrsi (Int Ref pH volt)   = %u\n", padString, sys_data.data_cfg[CFG_VRSI]);
    if(writeFlg==1) sd_fprintf(&fileObject, "%sJ -- Vrsi (Int Ref pH volt)   = %u\n", padString, sys_data.data_cfg[CFG_VRSI]);

    if(printFlg>=1)                 uprintf("%s     Vrsi Std                 = %u\n", padString, sys_data.data_cfg[CFG_VRSI_STD]);
    if(writeFlg==1) sd_fprintf(&fileObject, "%s     Vrsi Std                 = %u\n", padString, sys_data.data_cfg[CFG_VRSI_STD]);

    if(printFlg>=1)                 uprintf("%sK -- pH Vrsi Calc             = %u\n", padString, sys_data.data_cfg[CFG_CALC_PH_VRSI]);
    if(writeFlg==1) sd_fprintf(&fileObject, "%sK -- pH Vrsi Calc             = %u\n", padString, sys_data.data_cfg[CFG_CALC_PH_VRSI]);

    if(printFlg>=1)                 uprintf("%sL -- Vrse (Ext Ref pH volt)   = %u\n", padString, sys_data.data_cfg[CFG_VRSE]);
    if(writeFlg==1) sd_fprintf(&fileObject, "%sL -- Vrse (Ext Ref pH volt)   = %u\n", padString, sys_data.data_cfg[CFG_VRSE]);

    if(printFlg>=1)                 uprintf("%s     Vrse Std                 = %u\n", padString, sys_data.data_cfg[CFG_VRSE_STD]);
    if(writeFlg==1) sd_fprintf(&fileObject, "%s     Vrse Std                 = %u\n", padString, sys_data.data_cfg[CFG_VRSE_STD]);

    if(printFlg>=1)                 uprintf("%sM -- pH Vrse Calc             = %u\n", padString, sys_data.data_cfg[CFG_CALC_PH_VRSE]);
    if(writeFlg==1) sd_fprintf(&fileObject, "%sM -- pH Vrse Calc             = %u\n", padString, sys_data.data_cfg[CFG_CALC_PH_VRSE]);

    if(printFlg>=1)                 uprintf("%sN -- Vrsi Biased (7pH=0v)     = %u\n", padString, sys_data.data_cfg[CFG_VRSI_BIASED]);
    if(writeFlg==1) sd_fprintf(&fileObject, "%sN -- Vrsi Biased (7pH=0v)     = %u\n", padString, sys_data.data_cfg[CFG_VRSI_BIASED]);

    if(printFlg>=1)                 uprintf("%sO -- Vrsi Biased Std          = %u\n", padString, sys_data.data_cfg[CFG_VRSI_BIASED_STD]);
    if(writeFlg==1) sd_fprintf(&fileObject, "%sO -- Vrsi Biased Std          = %u\n", padString, sys_data.data_cfg[CFG_VRSI_BIASED_STD]);

    if(printFlg>=1)                 uprintf("%sP -- Vrse Biased (7pH=0v)     = %u\n", padString, sys_data.data_cfg[CFG_VRSE_BIASED]);
    if(writeFlg==1) sd_fprintf(&fileObject, "%sP -- Vrse Biased (7pH=0v)     = %u\n", padString, sys_data.data_cfg[CFG_VRSE_BIASED]);

    if(printFlg>=1)                 uprintf("%sQ -- Vrse Biased Std          = %u\n", padString, sys_data.data_cfg[CFG_VRSE_BIASED_STD]);
    if(writeFlg==1) sd_fprintf(&fileObject, "%sQ -- Vrse Biased Std          = %u\n", padString, sys_data.data_cfg[CFG_VRSE_BIASED_STD]);

    if(printFlg==1) uprintf("\n");

    if(printFlg>=1)                 uprintf("%sR -- Counter Electrode Vk     = %u\n", padString, sys_data.data_cfg[CFG_V_COUNTER_ELECT]);
    if(writeFlg==1) sd_fprintf(&fileObject, "%sR -- Counter Electrode Vk     = %u\n", padString, sys_data.data_cfg[CFG_V_COUNTER_ELECT]);

    if(printFlg>=1)                 uprintf("%s     Counter Electrode Vk Std = %u\n", padString, sys_data.data_cfg[CFG_V_COUNTER_ELECT_STD]);
    if(writeFlg==1) sd_fprintf(&fileObject, "%s     Counter Electrode Vk Std = %u\n", padString, sys_data.data_cfg[CFG_V_COUNTER_ELECT_STD]);

    if(printFlg>=1)                 uprintf("%sT -- Counter Electrode Cur Ik = %u\n", padString, sys_data.data_cfg[CFG_I_COUNTER]);
    if(writeFlg==1) sd_fprintf(&fileObject, "%sT -- Counter Electrode Cur Ik = %u\n", padString, sys_data.data_cfg[CFG_I_COUNTER]);

    if(printFlg>=1)                 uprintf("%sU -- Substrate Current        = %u\n", padString, sys_data.data_cfg[CFG_I_SUBSTRATE]);
    if(writeFlg==1) sd_fprintf(&fileObject, "%sU -- Substrate Current        = %u\n", padString, sys_data.data_cfg[CFG_I_SUBSTRATE]);

    if(printFlg==1) uprintf("\n");

#if BOARD_MPHOX >= 1 || BOARD_NANOFET >= 1
    if(printFlg>=1)                 uprintf("%sV -- Optode                   = %u\n", padString, sys_data.data_cfg[CFG_OPTODE]);
    if(writeFlg==1) sd_fprintf(&fileObject, "%sV -- Optode                   = %u\n", padString, sys_data.data_cfg[CFG_OPTODE]);
#endif
#if BOARD_MPHOX >= 1
    if( sys_data.data_cfg[CFG_CTD] == 0)
    {
        if(printFlg>=1)                 uprintf("%sW -- Conductivity             = %u\n", padString, sys_data.data_cfg[CFG_CTD]);
        if(writeFlg==1) sd_fprintf(&fileObject, "%sW -- Conductivity             = %u\n", padString, sys_data.data_cfg[CFG_CTD]);
    }
    if( sys_data.data_cfg[CFG_CTD] == TYPE_SBE37_MICROCAT)
    {
        if(printFlg>=1)                 uprintf("%sW -- Conductivity             = %u,  SBE37 MicroCat\n", padString, sys_data.data_cfg[CFG_CTD]);
        if(writeFlg==1) sd_fprintf(&fileObject, "%sW -- Conductivity             = %u,  SBE37 MicroCat\n", padString, sys_data.data_cfg[CFG_CTD]);
    }
    if( sys_data.data_cfg[CFG_CTD] == TYPE_AANDERAA_5860)
    {
        if(printFlg>=1)                 uprintf("%sW -- Conductivity             = %u,  Aanderaa 5860\n", padString, sys_data.data_cfg[CFG_CTD]);
        if(writeFlg==1) sd_fprintf(&fileObject, "%sW -- Conductivity             = %u,  Aanderaa 5860\n", padString, sys_data.data_cfg[CFG_CTD]);
    }
#endif

    // TODO: what if the calling function also has the file open...  27 Jul 2021
    if(writeFlg == 1)
    {
        sys_samp.next_gdata_fptr = fileObject.fptr;     // Store the file pointer for the first sample (for gdata)

        // Close the file
        fresult = f_close( &fileObject);
        if(fresult != FR_OK)
        {
            uprintf("f_close error: %s\n", StringFromFresult(fresult));
            //return 1;
            retVal = 1;
            writeFlg = 0;
        }
    }

}


void print_config_data_fields(void)
{
    print_write_config_data_fields(2,0);    // 2 prints //, 1 has no // pad

}

void user_select_ctd(void)
{
    char input;

    uprintf("Select CTD type[%u]:  1 -- SBE37 MicroCat, 2 -- Aanderaa 5860  ");
    while(1)
    {
        if( UARTRxBytesAvail() )
        {
            input = get_key();
            if(input == '1')
            {
                sys_data.data_cfg[CFG_CTD] = 1;
                break;
            }
            if(input == '2')
            {
                sys_data.data_cfg[CFG_CTD] = 2;
                break;
            }
            if(input == 24)     // Ctrl-x
            {
                sys_data.data_cfg[CFG_CTD] = 0;
                break;
            }
            uprintf("Enter 1 or 2:  1 for SBE37 MicroCat, 2 for Aanderaa 5860, Ctrl-X <enter> to exit  ");
        }

    }
}


/********************************************************************************************
 *  Config Data Fields
 ********************************************************************************************/
void config_data_fields(void)
{
    char input;
    time_t timer;
    //unsigned char cfg;
    //unsigned char index;

    while(1)
    {
        //print out config menu
        uprintf("\n\n\nChange Data Field - lower case turns data field off (=0), upper case turns field on (=1)\n\n\n");

        //print_config_data_fields();
        print_write_config_data_fields(1,0);
        wait_consoleTx();

        uprintf("\n0 -- Zero (Clear) all config fields");
        uprintf("\n1 -- Set all config fields");
        ROM_SysCtlDelay(MILLISECOND * 200);   // 128 byte fifo, use case 5 causes max q so give some time for xmit
        uprintf("\n2 -- Use Case: GLIDER,          (A,B,C,D,E,L,M,R,T,U)"); //(A,B,G,M,N,O,P,Q)");  //(A,B,C,O,P,Q,R,S,T)");  //ABIJPQRS
        uprintf("\n3 -- Use Case: mFET Default,    (A,B,C,D,E,F,G,H,I,J,K,R,T,U)"); //  FIELDS ACTIVE for MiniFET: A= Samp #, B= Time, C= Batt Volt, D= Board Temp, E= Humidity, G= Vtherm, H= Vtherm_STD, I= Vint_off, J= Vint_off_STD, M= Vint, N= Vint_STD, Q= Vk (counter electrode Voltage), R= Vk_STD, S= Counter electrode current, T= Substrate current, Z= Fast mode ON
        uprintf("\n4 -- Use Case: MpHOx Default,   (A,B,C,D,E,F,G,H,I,J,K,L,M,R,T,U,V,W)");   //A,C,D,E,F,G,H,I,J,Q,R,S,T,U,V
        uprintf("\n5 -- Use Case: MpHOx m2m,       (A,C,D,E,F,G,H,I,J,K,R,T,U)");   //A,C,D,E,F,G,H,I,J,Q,R,S,T,U,V
        uprintf("\n6 -- Use Case: nanoFET Default, (A,B,C,D,E,F,G,H,I,L,R,T,U)");    //(A,B,C,D,E,F,G,H,O,P,Q,R,S,T)");
        uprintf("\n");
        //if(sys_data.diag == 1) uprintf("\n8 -- fast_sample() Flex Sampling = %u\t\t\t", sys_data.app_cfg[APPCFG_SAMP_FLEX]);

        //uprintf("\n");
        uprintf("\n9 -- Exit to Main Menu");
        uprintf("\nEnter Selection [9]: ");

        // Get user's selection with a 5 min timeout
        UARTFlushRx();
        timer = 300 + ROM_HibernateRTCGet();

        while(1)
        {
            if( UARTRxBytesAvail() )
            {
                input = get_key();
                break;
            }

            if( timer <= ROM_HibernateRTCGet() ) sleep(IDLE, 0);
        }



        switch(input)
        {
            case 'A':
                sys_data.data_cfg[CFG_SAMPLE_NUM] = 1;   //
                break;
            case 'a':
                sys_data.data_cfg[CFG_SAMPLE_NUM] = 0;
                break;
            case 'B':
                sys_data.data_cfg[CFG_TIMEDATE] = 1;
                break;
            case 'b':
                sys_data.data_cfg[CFG_TIMEDATE] = 0;
                break;


            case 'C':
                sys_data.data_cfg[CFG_VIN_BAT_VOLT] = 1;   //        2   //+ Battery_Volt
                break;
            case 'c':
                sys_data.data_cfg[CFG_VIN_BAT_VOLT] = 0;
                break;
            case 'D':
                sys_data.data_cfg[CFG_BIAS_BAT_POS] = 1;   //        5   //+ Internal_pressure, Now bias bat volt
                break;
            case 'd':
                sys_data.data_cfg[CFG_BIAS_BAT_POS] = 0;
                break;
            case 'E':
                sys_data.data_cfg[CFG_BIAS_BAT_NEG] = 1;   //        5   //new
                break;
            case 'e':
                sys_data.data_cfg[CFG_BIAS_BAT_NEG] = 0;
                break;

            case 'F':
                sys_data.data_cfg[CFG_BRD_TEMP] = 1;   //        3   //+ Board_Temperature\t
                break;
            case 'f':
                sys_data.data_cfg[CFG_BRD_TEMP] = 0;
                break;
            case 'G':
                sys_data.data_cfg[CFG_BRD_HUMIDITY] = 1;   //        4   // + Humidity_sensor
                break;
            case 'g':
                sys_data.data_cfg[CFG_BRD_HUMIDITY] = 0;
                break;


            case 'H':
                sys_data.data_cfg[CFG_V_THERMISTOR] = 1;   //        6       //+ Vthermistor\t
                sys_data.data_cfg[CFG_V_THERMISTOR_STD] = 1;            // New 13 May 2021
                break;
            case 'h':
                sys_data.data_cfg[CFG_V_THERMISTOR] = 0;
                sys_data.data_cfg[CFG_V_THERMISTOR_STD] = 0;
                sys_data.data_cfg[CFG_CALC_PH_VRSI] = 0;
                sys_data.data_cfg[CFG_CALC_PH_VRSE] = 0;
                break;
            case 'I':
                sys_data.data_cfg[CFG_CALC_TEMP] = 1;   //           20   //+ calculated_temperature_Vthermistor\t
                break;
            case 'i':
                sys_data.data_cfg[CFG_CALC_TEMP] = 0;
                sys_data.data_cfg[CFG_CALC_PH_VRSI] = 0;          // cant calc vrsi pH without temp   19 Jul 2021
                sys_data.data_cfg[CFG_CALC_PH_VRSE] = 0;          // cant calc vrsi pH without temp   19 Jul 2021
                break;

            case 'J':
                sys_data.data_cfg[CFG_VRSI] = 1;   //               12   //+ Vint\t
                sys_data.data_cfg[CFG_VRSI_STD] = 1;
                break;
            case 'j':
                sys_data.data_cfg[CFG_VRSI] = 0;
                sys_data.data_cfg[CFG_VRSI_STD] = 0;
                break;
            case 'K':
                sys_data.data_cfg[CFG_CALC_PH_VRSI] = 1;   //          21   //+ estimated_pH_int\r\n
                sys_data.data_cfg[CFG_V_THERMISTOR] = 1; // new 29 Apr 2021
                sys_data.data_cfg[CFG_V_THERMISTOR_STD] = 1; // new 17 Jul 2021
                sys_data.data_cfg[CFG_CALC_TEMP] = 1;   // new 29 Apr 2021
                sys_data.data_cfg[CFG_VRSI_BIASED] = 1; // new 29 Apr 2021
                break;
            case 'k':
                sys_data.data_cfg[CFG_CALC_PH_VRSI] = 0;
                break;

            case 'L':
                sys_data.data_cfg[CFG_VRSE] = 1;   //               14   //+ Vext
                sys_data.data_cfg[CFG_VRSE_STD] = 1;
                break;
            case 'l':
                sys_data.data_cfg[CFG_VRSE] = 0;
                sys_data.data_cfg[CFG_VRSE_STD] = 0;
                break;
            case 'M':
                sys_data.data_cfg[CFG_CALC_PH_VRSE] = 1;   //          22   //+ estimated_pH_ext\r\n (new 6May2019 TM)
                sys_data.data_cfg[CFG_V_THERMISTOR] = 1; // new 29 Apr 2021
                sys_data.data_cfg[CFG_V_THERMISTOR_STD] = 1; // new 17 Jul 2021
                sys_data.data_cfg[CFG_CALC_TEMP] = 1;   // new 29 Apr 2021
                sys_data.data_cfg[CFG_VRSE_BIASED] = 1; // new 29 Apr 2021
                //sys_data.data_cfg[CFG_CTD] = 1;
                break;
            case 'm':
                sys_data.data_cfg[CFG_CALC_PH_VRSE] = 0;
                break;

            case 'N':
                sys_data.data_cfg[CFG_VRSI_BIASED] = 1;   //        8   //+ Vint_offset\t
                break;
            case 'n':
                sys_data.data_cfg[CFG_VRSI_BIASED] = 0;
                sys_data.data_cfg[CFG_VRSI_BIASED_STD] = 0;
                break;
            case 'O':
                sys_data.data_cfg[CFG_VRSI_BIASED_STD] = 1;   //    9   //+ Vint_offset_std\t
                sys_data.data_cfg[CFG_VRSI_BIASED] = 1;
                break;
            case 'o':
                sys_data.data_cfg[CFG_VRSI_BIASED_STD] = 0;
                break;
            case 'P':
                sys_data.data_cfg[CFG_VRSE_BIASED] = 1;   //        10   //+ Vext_offset\t
                break;
            case 'p':
                sys_data.data_cfg[CFG_VRSE_BIASED] = 0;
                sys_data.data_cfg[CFG_VRSE_BIASED_STD] = 0;
                break;
            case 'Q':
                sys_data.data_cfg[CFG_VRSE_BIASED_STD] = 1;   //    11   //+ Vext_offset_std\t
                sys_data.data_cfg[CFG_VRSE_BIASED] = 1;
                break;
            case 'q':
                sys_data.data_cfg[CFG_VRSE_BIASED_STD] = 0;
                break;

            case 'R':
                sys_data.data_cfg[CFG_V_COUNTER_ELECT] = 1;   //     16   //+ Vcounter_electrode
                sys_data.data_cfg[CFG_V_COUNTER_ELECT_STD] = 1;
                break;
            case 'r':
                sys_data.data_cfg[CFG_V_COUNTER_ELECT] = 0;
                sys_data.data_cfg[CFG_V_COUNTER_ELECT_STD] = 0;
                break;
                //S is up for grabs! 22 Jul 2021 (bug fix)
            case 'S':
                //sys_data.data_cfg[CFG_V_COUNTER_ELECT_STD] = 1;   //   //+ Vcounter_electrode_std
                //sys_data.data_cfg[CFG_V_COUNTER_ELECT] = 1;
                break;
            case 's':
                //sys_data.data_cfg[CFG_V_COUNTER_ELECT_STD] = 0;
                break;

            case 'T':
                sys_data.data_cfg[CFG_I_COUNTER] = 1;   //           18   // + Counter_current
                break;
            case 't':
                sys_data.data_cfg[CFG_I_COUNTER] = 0;
                break;
            case 'U':
                sys_data.data_cfg[CFG_I_SUBSTRATE] = 1;   //         19   //+ Substrate_current
                break;
            case 'u':
                sys_data.data_cfg[CFG_I_SUBSTRATE] = 0;
                break;

            case 'V':
                sys_data.data_cfg[CFG_OPTODE] = 1;
                break;
            case 'v':
                sys_data.data_cfg[CFG_OPTODE] = 0;
                break;

            case 'W':
#if BOARD_MPHOX >= 1
                // Ask the user which CTD?
                user_select_ctd();
#ifdef OLDCODE
                uprintf("Select CTD type[%u]:  1 -- SBE37 MicroCat, 2 -- Aanderaa 5860  ");
                while(1)
                {
                    if( UARTRxBytesAvail() )
                    {
                        input = get_key();
                        if(input == '1')
                        {
                            sys_data.data_cfg[CFG_CTD] = 1;
                            break;
                        }
                        if(input == '2')
                        {
                            sys_data.data_cfg[CFG_CTD] = 2;
                            break;
                        }
                        if(input == 24)     // Ctrl-x
                        {
                            sys_data.data_cfg[CFG_CTD] = 0;
                            break;
                        }
                        uprintf("Enter 1 or 2:  1 for SBE37 MicroCat, 2 for Aanderaa 5860, Ctrl-X to exit  ");
                    }

                }
#endif
                //sys_data.data_cfg[CFG_CTD] = 1;
#endif
                break;
            case 'w':
#if BOARD_MPHOX >= 1
                sys_data.data_cfg[CFG_CTD] = 0;
#endif
                break;


            case 'X':
                break;
            case 'x':
                break;
            case 'Y':
                break;
            case 'y':
                break;
            case 'Z':
                break;
            case 'z':
                break;

            case '0':
                // all clear
                sys_data.data_cfg[CFG_SAMPLE_NUM] = 0;          // a
                sys_data.data_cfg[CFG_TIMEDATE] = 0;            // b

                sys_data.data_cfg[CFG_VIN_BAT_VOLT] = 0;        // t
                sys_data.data_cfg[CFG_BIAS_BAT_POS] = 0;        // u
                sys_data.data_cfg[CFG_BIAS_BAT_NEG] = 0;        // v
                sys_data.data_cfg[CFG_BRD_TEMP] = 0;        // w
                sys_data.data_cfg[CFG_BRD_HUMIDITY] = 0;        // x

                sys_data.data_cfg[CFG_V_THERMISTOR] = 0;        // c
                sys_data.data_cfg[CFG_V_THERMISTOR_STD] = 0;    // d
                sys_data.data_cfg[CFG_CALC_TEMP] = 0;           // e

                sys_data.data_cfg[CFG_VRSI] = 0;               // f
                sys_data.data_cfg[CFG_VRSI_STD] = 0;           // g
                sys_data.data_cfg[CFG_CALC_PH_VRSI] = 0;          // h

                sys_data.data_cfg[CFG_VRSE] = 0;               // i
                sys_data.data_cfg[CFG_VRSE_STD] = 0;           // j
                sys_data.data_cfg[CFG_CALC_PH_VRSE] = 0;          // k

                sys_data.data_cfg[CFG_VRSI_BIASED] = 0;        // l
                sys_data.data_cfg[CFG_VRSI_BIASED_STD] = 0;    // m
                sys_data.data_cfg[CFG_VRSE_BIASED] = 0;        // n
                sys_data.data_cfg[CFG_VRSE_BIASED_STD] = 0;    // o

                sys_data.data_cfg[CFG_V_COUNTER_ELECT] = 0;     // p
                sys_data.data_cfg[CFG_V_COUNTER_ELECT_STD] = 0; // q
                sys_data.data_cfg[CFG_I_COUNTER] = 0;           // r
                sys_data.data_cfg[CFG_I_SUBSTRATE] = 0;         // s

                sys_data.data_cfg[CFG_OPTODE] = 0;              // y
                sys_data.data_cfg[CFG_CTD] = 0;                 // z
                break;

            case '1':
                // all set
                sys_data.data_cfg[CFG_SAMPLE_NUM] = 1;          // a
                sys_data.data_cfg[CFG_TIMEDATE] = 1;            // b

                sys_data.data_cfg[CFG_VIN_BAT_VOLT] = 1;        // t
                sys_data.data_cfg[CFG_BIAS_BAT_POS] = 1;        // u
                sys_data.data_cfg[CFG_BIAS_BAT_NEG] = 1;        // v
                sys_data.data_cfg[CFG_BRD_TEMP] = 1;        // w
                sys_data.data_cfg[CFG_BRD_HUMIDITY] = 1;        // x

                sys_data.data_cfg[CFG_V_THERMISTOR] = 1;        // c
                sys_data.data_cfg[CFG_V_THERMISTOR_STD] = 1;    // d
                sys_data.data_cfg[CFG_CALC_TEMP] = 1;           // e

                sys_data.data_cfg[CFG_VRSI] = 1;               // f
                sys_data.data_cfg[CFG_VRSI_STD] = 1;           // g
                sys_data.data_cfg[CFG_CALC_PH_VRSI] = 1;          // h

                sys_data.data_cfg[CFG_VRSE] = 1;               // i
                sys_data.data_cfg[CFG_VRSE_STD] = 1;           // j
                sys_data.data_cfg[CFG_CALC_PH_VRSE] = 1;          // k

                sys_data.data_cfg[CFG_VRSI_BIASED] = 1;        // l
                sys_data.data_cfg[CFG_VRSI_BIASED_STD] = 1;    // m
                sys_data.data_cfg[CFG_VRSE_BIASED] = 1;        // n
                sys_data.data_cfg[CFG_VRSE_BIASED_STD] = 1;    // o

                sys_data.data_cfg[CFG_V_COUNTER_ELECT] = 1;     // p
                sys_data.data_cfg[CFG_V_COUNTER_ELECT_STD] = 1; // q
                sys_data.data_cfg[CFG_I_COUNTER] = 1;           // r
                sys_data.data_cfg[CFG_I_SUBSTRATE] = 1;         // s

                sys_data.data_cfg[CFG_OPTODE] = 1;              // V
#if BOARD_MPHOX >= 1
                sys_data.data_cfg[CFG_CTD] = 1;                 // W
                user_select_ctd();

#endif

                break;

            case '2':
                // -- Glider Data Config Set
                // A Sample Num
                // B MM/DD/YYYY HH:MM:SS
                // C Battery Volt
                // O Vext Ref
                // P Vext Ref Std
                // Q Vcounter Electrode
                // R Vcounter Electrode Std
                // S Counter Electrode Current
                // T Substrate Current
                // Z FAST

                //sys_data.fast_mode = 1;                         // Z
                //ABCDELMRSTU
                sys_data.data_cfg[CFG_SAMPLE_NUM] = 1;          // A
                sys_data.data_cfg[CFG_TIMEDATE] = 1;            // B

                sys_data.data_cfg[CFG_VIN_BAT_VOLT] = 1;        // C
                sys_data.data_cfg[CFG_BIAS_BAT_POS] = 1;        // D  // NEW
                sys_data.data_cfg[CFG_BIAS_BAT_NEG] = 1;        // E
                sys_data.data_cfg[CFG_BRD_TEMP] = 0;        // f
                sys_data.data_cfg[CFG_BRD_HUMIDITY] = 0;        // g

                sys_data.data_cfg[CFG_V_THERMISTOR] = 0;        // h
                sys_data.data_cfg[CFG_V_THERMISTOR_STD] = 0;    //
                sys_data.data_cfg[CFG_CALC_TEMP] = 0;           // i

                sys_data.data_cfg[CFG_VRSI] = 0;               // j
                sys_data.data_cfg[CFG_VRSI_STD] = 0;           //
                sys_data.data_cfg[CFG_CALC_PH_VRSI] = 0;          // k

                sys_data.data_cfg[CFG_VRSE] = 1;               // L
                sys_data.data_cfg[CFG_VRSE_STD] = 1;           //
                sys_data.data_cfg[CFG_CALC_PH_VRSE] = 0;          // M

                sys_data.data_cfg[CFG_VRSI_BIASED] = 0;        // n
                sys_data.data_cfg[CFG_VRSI_BIASED_STD] = 0;    // o
                sys_data.data_cfg[CFG_VRSE_BIASED] = 0;        // p
                sys_data.data_cfg[CFG_VRSE_BIASED_STD] = 0;    // q

                sys_data.data_cfg[CFG_V_COUNTER_ELECT] = 1;     // R
                sys_data.data_cfg[CFG_V_COUNTER_ELECT_STD] = 1; // S
                sys_data.data_cfg[CFG_I_COUNTER] = 1;           // T
                sys_data.data_cfg[CFG_I_SUBSTRATE] = 1;         // U

                sys_data.data_cfg[CFG_OPTODE] = 0;              // v
//#if BOARD_MPHOX >= 1
                sys_data.data_cfg[CFG_CTD] = 0;                 // w
//#endif
                break;

            case '3':           // Updated to add Calc Temp 23 Mar 2021   MFET Default
                    //Default for MFET fields
                // A,B,C,D,E,F,G,H,I,J,R,S,T,U
                sys_data.data_cfg[CFG_SAMPLE_NUM] = 1;          // A
                sys_data.data_cfg[CFG_TIMEDATE] = 1;            // B

                sys_data.data_cfg[CFG_VIN_BAT_VOLT] = 1;        // C
                sys_data.data_cfg[CFG_BIAS_BAT_POS] = 1;        // D
                sys_data.data_cfg[CFG_BIAS_BAT_NEG] = 1;        // E
                sys_data.data_cfg[CFG_BRD_TEMP] = 1;        // F
                sys_data.data_cfg[CFG_BRD_HUMIDITY] = 1;        // G

                sys_data.data_cfg[CFG_V_THERMISTOR] = 1;        // H
                sys_data.data_cfg[CFG_V_THERMISTOR_STD] = 1;    //
                sys_data.data_cfg[CFG_CALC_TEMP] = 1;           // I

                sys_data.data_cfg[CFG_VRSI] = 1;               // J
                sys_data.data_cfg[CFG_VRSI_STD] = 1;           //
                sys_data.data_cfg[CFG_CALC_PH_VRSI] = 1;          // K

                sys_data.data_cfg[CFG_VRSE] = 0;               // l
                sys_data.data_cfg[CFG_VRSE_STD] = 0;           //
                sys_data.data_cfg[CFG_CALC_PH_VRSE] = 0;          // m

                sys_data.data_cfg[CFG_VRSI_BIASED] = 0;        // n
                sys_data.data_cfg[CFG_VRSI_BIASED_STD] = 0;    // o
                sys_data.data_cfg[CFG_VRSE_BIASED] = 0;        // p
                sys_data.data_cfg[CFG_VRSE_BIASED_STD] = 0;    // q

                sys_data.data_cfg[CFG_V_COUNTER_ELECT] = 1;     // R
                sys_data.data_cfg[CFG_V_COUNTER_ELECT_STD] = 1; // S
                sys_data.data_cfg[CFG_I_COUNTER] = 1;           // T
                sys_data.data_cfg[CFG_I_SUBSTRATE] = 1;         // U

                sys_data.data_cfg[CFG_OPTODE] = 0;              // v
                sys_data.data_cfg[CFG_CTD] = 0;                 // w
                //A,B,C,D,E,F,G,L,M,P,Q,R,S,T,U,V,W,X

                break;


            case '4':
                //sys_data.fast_mode = 2;
                //Use Case: MPHOX/ nsFOCE, fields for use with another master controller (time not needed).  Defaults for Durafet, not deepsee durafet
                //A,C,D,E,F,G,H,I,J,K,R,S,T,U
                sys_data.data_cfg[CFG_SAMPLE_NUM] = 1;          // A
                sys_data.data_cfg[CFG_TIMEDATE] = 1;            // B

                sys_data.data_cfg[CFG_VIN_BAT_VOLT] = 1;        // C
                sys_data.data_cfg[CFG_BIAS_BAT_POS] = 1;        // D
                sys_data.data_cfg[CFG_BIAS_BAT_NEG] = 1;        // E
                sys_data.data_cfg[CFG_BRD_TEMP] = 1;        // F
                sys_data.data_cfg[CFG_BRD_HUMIDITY] = 1;        // G

                sys_data.data_cfg[CFG_V_THERMISTOR] = 1;        // H
                sys_data.data_cfg[CFG_V_THERMISTOR_STD] = 1;    //
                sys_data.data_cfg[CFG_CALC_TEMP] = 1;           // I

                sys_data.data_cfg[CFG_VRSI] = 1;               // J
                sys_data.data_cfg[CFG_VRSI_STD] = 1;           //
                sys_data.data_cfg[CFG_CALC_PH_VRSI] = 1;          // K

                sys_data.data_cfg[CFG_VRSE] = 1;               // L
                sys_data.data_cfg[CFG_VRSE_STD] = 1;           //
                sys_data.data_cfg[CFG_CALC_PH_VRSE] = 1;          // M

                sys_data.data_cfg[CFG_VRSI_BIASED] = 0;        // n
                sys_data.data_cfg[CFG_VRSI_BIASED_STD] = 0;    // o
                sys_data.data_cfg[CFG_VRSE_BIASED] = 0;        // p
                sys_data.data_cfg[CFG_VRSE_BIASED_STD] = 0;    // q

                sys_data.data_cfg[CFG_V_COUNTER_ELECT] = 1;     // R
                sys_data.data_cfg[CFG_V_COUNTER_ELECT_STD] = 1; // S
                sys_data.data_cfg[CFG_I_COUNTER] = 1;           // T
                sys_data.data_cfg[CFG_I_SUBSTRATE] = 1;         // U

                sys_data.data_cfg[CFG_OPTODE] = 1;              // V
                sys_data.data_cfg[CFG_CTD] = 1;                 // W   MicroCAT is default
                user_select_ctd();
                break;

            case '5':
                //sys_data.fast_mode = 2;
                //Use Case: MPHOX m2m/ nsFOCE, fields for use with another master controller (time not needed).  Defaults for Durafet, not deepsee durafet
                //A,C,D,E,F,G,H,I,J,K,R,S,T,U
                sys_data.data_cfg[CFG_SAMPLE_NUM] = 1;          // A
                sys_data.data_cfg[CFG_TIMEDATE] = 0;            // b

                sys_data.data_cfg[CFG_VIN_BAT_VOLT] = 1;        // C
                sys_data.data_cfg[CFG_BIAS_BAT_POS] = 1;        // D
                sys_data.data_cfg[CFG_BIAS_BAT_NEG] = 1;        // E
                sys_data.data_cfg[CFG_BRD_TEMP] = 1;        // F
                sys_data.data_cfg[CFG_BRD_HUMIDITY] = 1;        // G

                sys_data.data_cfg[CFG_V_THERMISTOR] = 1;        // H
                sys_data.data_cfg[CFG_V_THERMISTOR_STD] = 1;    //
                sys_data.data_cfg[CFG_CALC_TEMP] = 1;           // I

                sys_data.data_cfg[CFG_VRSI] = 1;               // J
                sys_data.data_cfg[CFG_VRSI_STD] = 1;           //
                sys_data.data_cfg[CFG_CALC_PH_VRSI] = 1;          // K

                sys_data.data_cfg[CFG_VRSE] = 0;               // l
                sys_data.data_cfg[CFG_VRSE_STD] = 0;           //
                sys_data.data_cfg[CFG_CALC_PH_VRSE] = 0;          // m

                sys_data.data_cfg[CFG_VRSI_BIASED] = 0;        // n
                sys_data.data_cfg[CFG_VRSI_BIASED_STD] = 0;    // o
                sys_data.data_cfg[CFG_VRSE_BIASED] = 0;        // p
                sys_data.data_cfg[CFG_VRSE_BIASED_STD] = 0;    // q

                sys_data.data_cfg[CFG_V_COUNTER_ELECT] = 1;     // R
                sys_data.data_cfg[CFG_V_COUNTER_ELECT_STD] = 1; // S
                sys_data.data_cfg[CFG_I_COUNTER] = 1;           // T
                sys_data.data_cfg[CFG_I_SUBSTRATE] = 1;         // U

                sys_data.data_cfg[CFG_OPTODE] = 0;              // v
                sys_data.data_cfg[CFG_CTD] = 0;                 // w
                break;


            case '6':
                //Use Case: nanoFET default, fields
                // A,B,C,D,E,F,G,H,I,L,R,S,T,U
                sys_data.data_cfg[CFG_SAMPLE_NUM] = 1;          // A
                sys_data.data_cfg[CFG_TIMEDATE] = 1;            // B

                sys_data.data_cfg[CFG_VIN_BAT_VOLT] = 1;        // C
                sys_data.data_cfg[CFG_BIAS_BAT_POS] = 1;        // D
                sys_data.data_cfg[CFG_BIAS_BAT_NEG] = 1;        // E
                sys_data.data_cfg[CFG_BRD_TEMP] = 1;        // F
                sys_data.data_cfg[CFG_BRD_HUMIDITY] = 1;        // G

                sys_data.data_cfg[CFG_V_THERMISTOR] = 1;        // H
                sys_data.data_cfg[CFG_V_THERMISTOR_STD] = 1;    //
                sys_data.data_cfg[CFG_CALC_TEMP] = 1;           // I

                sys_data.data_cfg[CFG_VRSI] = 0;               // j
                sys_data.data_cfg[CFG_VRSI_STD] = 0;           //
                sys_data.data_cfg[CFG_CALC_PH_VRSI] = 0;          // k

                sys_data.data_cfg[CFG_VRSE] = 1;               // L
                sys_data.data_cfg[CFG_VRSE_STD] = 1;           //
                sys_data.data_cfg[CFG_CALC_PH_VRSE] = 0;          // m

                sys_data.data_cfg[CFG_VRSI_BIASED] = 0;        // n
                sys_data.data_cfg[CFG_VRSI_BIASED_STD] = 0;    // o
                sys_data.data_cfg[CFG_VRSE_BIASED] = 0;        // p
                sys_data.data_cfg[CFG_VRSE_BIASED_STD] = 0;    // q

                sys_data.data_cfg[CFG_V_COUNTER_ELECT] = 1;     // R
                sys_data.data_cfg[CFG_V_COUNTER_ELECT_STD] = 1; // S
                sys_data.data_cfg[CFG_I_COUNTER] = 1;           // T
                sys_data.data_cfg[CFG_I_SUBSTRATE] = 1;         // U

                sys_data.data_cfg[CFG_OPTODE] = 0;              // v
                sys_data.data_cfg[CFG_CTD] = 0;                 // w

                break;


            case '7':
                break;

            case '8':
                break;


            case '9':
            default:
                sys_data.fast_mode = 1;  // removed from config 21 Apr 2021
                // Write the sys_data struct into EEPROM.  Last argument ensures # of bytes is multiple of 4
                if(storeSysDataVariables())                         //if(EEPROMProgram((uint32_t*) &sys_data, 0x400, (sizeof(sys_data) + 3) & ~3))
                {
                    uprintf("\n\nError storing system data.\n");
                }
                else
                    uprintf("\n\nSystem data stored. (910) -- Press enter to bring up main menu\n");
                return;

        }  // end of switch statement

    }
}







void print_all_metadata(void)
{
    time_t  current_time;

    current_time = ROM_HibernateRTCGet();

    print_write_metadata_header(current_time, 1, 0);

#ifdef NOCODE

#if BOARD_MFET >= 1
    uprintf("MFET Serial #: %s\n", sys_data.board_SN);
#endif
#if BOARD_MPHOX >= 1
    uprintf("MpHOx Serial #: %s\n", sys_data.board_SN);
#endif
#if BOARD_NANOFET >= 1
    uprintf("nanoFET Serial #: %s\n", sys_data.board_SN);
#endif

#if BOARD_MFET >= 1 || BOARD_MPHOX >= 1
    uprintf("MCAP Serial #: %s\n", sys_data.mcap_SN);
#endif

    uprintf("Board Fabrication notes: %s\n", sys_data.board_fab_note);

    uprintf("Durafet Serial #: %s\n", sys_data.durafet_SN);
//    uprintf("CAP adapter Serial #: %s\n", sys_data.capadap_SN);
//    uprintf("ISE Serial #: %s\n", sys_data.ISE_SN);
    uprintf("Package name: %s\n", sys_data.package_name);

    //uprintf("Conductivity Serial #: %s\n", sys_data.condo_SN);

#if BOARD_MPHOX >= 1
    uprintf("Optode Serial #: %s\n", sys_data.optode_SN);
    uprintf("Conductivity Serial #: %s\n", sys_data.condo_SN);
#endif

    uprintf("Pump Serial #: %s\n", sys_data.pump_SN);

    //THOM todo, what about the rest of the metadata?


    //uprintf("\npH Vint gain now %d\n\n", sys_data.Vint_gain);
    //uprintf("\npH Vint data rate now %d sps\n\n", sys_data.Vrsi_sps);

    uprintf("\n\n");

#endif
}

void enter_metadata(void)
{
    char buff[128];

    uprintf("\nEnter data file name (no spaces, 8.3 format) [%s]: ", sys_data.fileName);  // 8 + 1 + 3 + 1 = 13  (null terminate)
    if( getUserInput(buff, 13) ) // was MAX_FILENAME-1) )  // storage size is 80, user input size was 100 - fixed a BUG!
    {
        strcpy(sys_data.fileName, buff);
        uprintf("\nFile name now: %s\n", sys_data.fileName);
    }

    uprintf("\nEnter user initials [%s]: ", sys_data.user);
    if( getUserInput(buff, MAX_USER_INITIALS-1) )       // was 10 user initials
    {
        strcpy(sys_data.user, buff);
        uprintf("\nUser initials now: %s\n", sys_data.user);
    }

    //char board_SN[MAX_BOARD_SN];   MAX_BOARD_SN 30
#if BOARD_MFET >= 1
    uprintf("\nEnter MFET Serial # [%s]: ", sys_data.board_SN);
#endif
#if BOARD_MPHOX >= 1
    uprintf("\nEnter MPHOX Serial # [%s]: ", sys_data.board_SN);
#endif
#if BOARD_NANOFET >= 1
    uprintf("\nEnter nanoFET Serial # [%s]: ", sys_data.board_SN);
#endif
    if( getUserInput(buff, MAX_BOARD_SN-1) )       // board serial number
    {
        strcpy(sys_data.board_SN, buff);
        uprintf("\nBoard Serial #: %s\n", sys_data.board_SN);
    }


    // char mcap_SN[MAX_MCAP_SN];  MAX_MCAP_SN  30
#if BOARD_MFET >= 1 || BOARD_MPHOX >= 1
    uprintf("\nEnter MCAP Serial # [%s]: ", sys_data.mcap_SN);
    if( getUserInput(buff, MAX_MCAP_SN-1) )       // board serial number
    {
        strcpy(sys_data.mcap_SN, buff);
        uprintf("\nMCAP Serial #: %s\n", sys_data.mcap_SN);
    }
#endif


    // char board_fab_note[MAX_board_fab_note];   MAX_board_fab_note 80
    uprintf("\nEnter board Fabrication notes [%s]: ", sys_data.board_fab_note);
    if( getUserInput(buff, MAX_BOARD_FABNOTE-1) )       // board fabricaton notes
    {
        strcpy(sys_data.board_fab_note, buff);
        uprintf("\nBoard Fab notes: %s\n", sys_data.board_fab_note);
    }

    // char sensor_name[MAX_SENSOR_NAME];  30   used for temperature sensor (Needed?)
    uprintf("\nEnter Package name [%s]: ", sys_data.package_name);
    if( getUserInput(buff, MAX_PACKAGE_NAME-1) )
    {
        strcpy(sys_data.package_name, buff);
        uprintf("\nPackage name: %s\n", sys_data.package_name);
    }

    //char durafet_SN[MAX_DURAFET_SN];  30
    uprintf("\nEnter Durafet Serial # [%s]: ", sys_data.durafet_SN);
    if( getUserInput(buff, MAX_DURAFET_SN-1) )       // board serial number
    {
        strcpy(sys_data.durafet_SN, buff);
        uprintf("\nDurafet Serial #: %s\n", sys_data.durafet_SN);
    }


    //char optode_SN[MAX_OPTODE_SN];
    uprintf("\nEnter Optode Serial # [%s]: ", sys_data.optode_SN);
    if( getUserInput(buff, MAX_OPTODE_SN-1) )       //
    {
        strcpy(sys_data.optode_SN, buff);
        uprintf("\nOptode Serial #: %s\n", sys_data.optode_SN);
    }

#if BOARD_MPHOX >= 1
    uprintf("\nEnter Conductivity Serial # [%s]: ", sys_data.ctd_SN);
    if( getUserInput(buff, MAX_CTD_SN-1) )       //
    {
        strcpy(sys_data.ctd_SN, buff);
        uprintf("\nConductivity Serial #: %s\n", sys_data.ctd_SN);
    }

    //char pump_SN[MAX_PUMP_SN];
    uprintf("\nEnter Pump Serial #  [%s]: ", sys_data.pump_SN);
    if( getUserInput(buff, MAX_PUMP_SN-1) )       //
    {
        strcpy(sys_data.pump_SN, buff);
        uprintf("\nPump Serial #: %s\n", sys_data.pump_SN);
    }
#endif


}



// write deployment header to SD card, NOTE: the format should match print_deployment_header except for tabs in the print to format for human readable vs machine readable ascii
int write_metadata_header(const time_t currentTime)   // was deployment header
{
    return(print_write_metadata_header(currentTime, 0, 1));
    //return 0;
}

int print_write_data_header(uint32_t printFlg, uint32_t writeFlg)
{
    struct tm *time_struct;
    char buff[TIMESTAMPLENGTH];
    FIL fileObject;     // File object
    FRESULT fresult;
    UINT bw;
    int retVal;

    retVal = 0;     // assume a good return (1 is error due to SD card

    if(microSD_flg == 0)
    {
        retVal = 0;  // pretend it's good
        writeFlg = 0;
    }

    if(writeFlg == 1)
    {

        // Open a file
        fresult = f_open(&fileObject, sys_data.fileName, FA_READ |FA_WRITE |FA_OPEN_ALWAYS);
        if(fresult != FR_OK)
        {
            uprintf("f_open error: %s\n", StringFromFresult(fresult));
            retVal = 1;
            writeFlg = 0;
            //return 1;
        }

        // Seek to the end, to append our file
        fresult = f_lseek(&fileObject, fileObject.fsize);
        if(fresult != FR_OK)
        {
            uprintf("f_lseek error: %s\n", StringFromFresult(fresult));
            retVal = 1;
            writeFlg = 0;
            //return 1;
        }

    }


    uprintf("\n\n");



    if(sys_data.data_cfg[CFG_SAMPLE_NUM] > 0)
    {                                            //01234567
        if(printFlg == 1)                 uprintf(" SampNum\t");
        if(writeFlg == 1) sd_fprintf(&fileObject, " SampNum\t");
    } //0
    if(sys_data.data_cfg[CFG_TIMEDATE] > 0)
    {                                            //012345678901234567890123
        if(printFlg == 1)                 uprintf("     MM/DD/YYYY HH:MM:SS\t");
        if(writeFlg == 1) sd_fprintf(&fileObject, "     MM/DD/YYYY HH:MM:SS\t");
    } //1
//--- board sensors

    if(sys_data.data_cfg[CFG_VIN_BAT_VOLT] > 0)
    {                                            //01234567
        if(printFlg == 1)                 uprintf("VbatMain\t");
        if(writeFlg == 1) sd_fprintf(&fileObject, "VbatMain\t");
    } //2
    if(sys_data.data_cfg[CFG_BIAS_BAT_POS] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("VbiasPos\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "VbiasPos\t");

    } //5  Bias Battery Volts
    if(sys_data.data_cfg[CFG_BIAS_BAT_NEG] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("VbiasNeg\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "VbiasNeg\t");

    } //5  Bias Battery Volts
    if(sys_data.data_cfg[CFG_BRD_TEMP] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf(" TC_cont\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, " TC_cont\t");
    } //3
    if(sys_data.data_cfg[CFG_BRD_HUMIDITY] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("Humidity\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "Humidity\t");

    } //4

//--- pH voltages
    if(sys_data.data_cfg[CFG_V_THERMISTOR] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("  Vtherm\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "  Vtherm\t");
    } //6
    if(sys_data.data_cfg[CFG_V_THERMISTOR_STD] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("VthrmStd\t");  //Vtherm_std
        if(writeFlg == 1)  sd_fprintf(&fileObject, "VthrmStd\t");
    } //7
    if(sys_data.data_cfg[CFG_CALC_TEMP] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf(" TC_Dfet\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, " TC_Dfet\t");
    } //20
//---
    if(sys_data.data_cfg[CFG_VRSI] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("    Vrsi\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "    Vrsi\t");
    } //12
    if(sys_data.data_cfg[CFG_VRSI_STD] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("Vrsi_std\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "Vrsi_std\t");
    } //13
    if(sys_data.data_cfg[CFG_CALC_PH_VRSI] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("pHintEst\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "pHintEst\t");
    } //21

    if(sys_data.data_cfg[CFG_VRSE] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("    Vrse\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "    Vrse\t");
    } //14
    if(sys_data.data_cfg[CFG_VRSE_STD] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("Vrse_std\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "Vrse_std\t");
    } //15
    if(sys_data.data_cfg[CFG_CALC_PH_VRSE] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("pHextEst\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "pHextEst\t");
    } //22
//---

    if(sys_data.data_cfg[CFG_VRSI_BIASED] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("  Vrsi_B\t");  //Vrsi_B
        if(writeFlg == 1)  sd_fprintf(&fileObject, "  Vrsi_B\t");
    } //8
    if(sys_data.data_cfg[CFG_VRSI_BIASED_STD] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("VrsiBstd\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "VrsiBstd\t");
    } //9
    if(sys_data.data_cfg[CFG_VRSE_BIASED] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("  Vrse_B\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "  Vrse_B\t");
    } //10
    if(sys_data.data_cfg[CFG_VRSE_BIASED_STD] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("VrseBstd\t");  //Vrse_B_std
        if(writeFlg == 1)  sd_fprintf(&fileObject, "VrseBstd\t");
    } //11

//--- ISFET diagnostics
    if(sys_data.data_cfg[CFG_V_COUNTER_ELECT] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("      Vk\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "      Vk\t");
    } //16
    if(sys_data.data_cfg[CFG_V_COUNTER_ELECT_STD] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("  Vk_std\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "  Vk_std\t");
    } //17
    if(sys_data.data_cfg[CFG_I_COUNTER] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("      Ik\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "      Ik\t");
    } //18
    if(sys_data.data_cfg[CFG_I_SUBSTRATE] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("      Ib\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "      Ib\t");
    } //19


//---- External Instruments

    if(sys_data.data_cfg[CFG_OPTODE] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("\tOpt_PN\tOpt_SN\tMoxy\tO2satper\tTC_opt\tDphase\tBphase\tRphase\tBamp\tBpot\tRamp\tOpt_rawtemp");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "\tOpt_PN\tOpt_SN\tMoxy\tO2satper\tTC_opt\tDphase\tBphase\tRphase\tBamp\tBpot\tRamp\tOpt_rawtemp");
		//MEASUREMENT     4831    856     O2Concentration[uM]     273.654 AirSaturation[%]        91.832  Temperature[Deg.C]      17.614  CalPhase[Deg]   28.336  TCPhase[Deg]    30.627  C1RPh[Deg]      38.676  C2RPh[Deg]      8.049   C1Amp[mV]        613.5   C2Amp[mV]       713.9   RawTemp[mV]     270.7
		

    } //22 -
    if(sys_data.data_cfg[CFG_CTD] > 0)
    {                                             //01234567
        if((sys_data.data_cfg[CFG_CTD]) == 1) //if(ctd.type == 1)        // microCAT SBE37
        {
            //TC_mcat, cond, psal, pres, date_mcat, time_mcat
                                                    // "%f %f %f %s %s", &tempC, &dummy, &salt, buff, buff) != 5)
                                                    //21.4218,  0.00006,   -0.147,   0.0105, 02 May 2021, 12:37:24
            if(printFlg == 1)                  uprintf("\tTC_MCat\tCond\tpSal\tPres\tDate_MC\tTime_MC\t");
            if(writeFlg == 1)  sd_fprintf(&fileObject, "\tTC_MCat\tCond\tpSal\tPres\tDate_MC\tTime_MC\t");

        }
        if((sys_data.data_cfg[CFG_CTD]) == 2)   //if(ctd.type == 2)        // aanderaa 5860
        {
                                                //5860    29      -0.001  26.054  0.012   996.780 1499.510
            //if(printFlg == 1)                  uprintf("\tCond_PN\tCond_SN\tCond\tTemp\tUnk1\tUnk2\tUnk3\t");
            //if(writeFlg == 1)  sd_fprintf(&fileObject, "\tCond_PN\tCond_SN\tCond\tTemp\tUnk1\tUnk2\tUnk3\t");
			
            if(printFlg == 1)                  uprintf("\tCond_PN\tCond_SN\tCond\tTemp\tSal\tDensity\tSSpeed\t");
            if(writeFlg == 1)  sd_fprintf(&fileObject, "\tCond_PN\tCond_SN\tCond\tTemp\tSal\tDensity\tSSpeed\t");			

        }

    }


#ifdef OLDCODE
    if(sys_data.data_cfg[CFG_SAMPLE_NUM] > 0)
    {                                            //01234567
        if(printFlg == 1)                 uprintf(" SampNum\t");
        if(writeFlg == 1) sd_fprintf(&fileObject, " SampNum\t");
    } //0
    if(sys_data.data_cfg[CFG_TIMEDATE] > 0)
    {                                            //012345678901234567890123
        if(printFlg == 1)                 uprintf("     MM/DD/YYYY HH:MM:SS\t");
        if(writeFlg == 1) sd_fprintf(&fileObject, "     MM/DD/YYYY HH:MM:SS\t");
    } //1
    if(sys_data.data_cfg[CFG_VIN_BAT_VOLT] > 0)
    {                                            //01234567
        if(printFlg == 1)                 uprintf("VbatMain\t");
        if(writeFlg == 1) sd_fprintf(&fileObject, "VbatMain\t");
    } //2
    if(sys_data.data_cfg[CFG_BRD_TEMP] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf(" TC_cont\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, " TC_cont\t");
    } //3
    if(sys_data.data_cfg[CFG_BRD_HUMIDITY] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("Humidity\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "Humidity\t");

    } //4
    if(sys_data.data_cfg[CFG_BIAS_BAT_POS] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("VbiasPos\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "VbiasPos\t");

    } //5  Bias Battery Volts
    if(sys_data.data_cfg[CFG_BIAS_BAT_NEG] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("VbiasNeg\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "VbiasNeg\t");

    } //5  Bias Battery Volts
    if(sys_data.data_cfg[CFG_V_THERMISTOR] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("  Vtherm\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "  Vtherm\t");
    } //6
    if(sys_data.data_cfg[CFG_V_THERMISTOR_STD] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("VthrmStd\t");  //Vtherm_std
        if(writeFlg == 1)  sd_fprintf(&fileObject, "VthrmStd\t");
    } //7
    if(sys_data.data_cfg[CFG_VRSI_BIASED] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("  Vrsi_B\t");  //Vrsi_B
        if(writeFlg == 1)  sd_fprintf(&fileObject, "  Vrsi_B\t");
    } //8
    if(sys_data.data_cfg[CFG_VRSI_BIASED_STD] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("VrsiBstd\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "VrsiBstd\t");
    } //9
    if(sys_data.data_cfg[CFG_VRSE_BIASED] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("  Vrse_B\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "  Vrse_B\t");
    } //10
    if(sys_data.data_cfg[CFG_VRSE_BIASED_STD] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("VrseBstd\t");  //Vrse_B_std
        if(writeFlg == 1)  sd_fprintf(&fileObject, "VrseBstd\t");
    } //11

    if(sys_data.data_cfg[CFG_VRSI] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("    Vrsi\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "    Vrsi\t");
    } //12
    if(sys_data.data_cfg[CFG_VRSI_STD] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("Vrsi_std\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "Vrsi_std\t");
    } //13
    if(sys_data.data_cfg[CFG_VRSE] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("    Vrse\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "    Vrse\t");
    } //14
    if(sys_data.data_cfg[CFG_VRSE_STD] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("Vrse_std\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "Vrse_std\t");
    } //15
    if(sys_data.data_cfg[CFG_V_COUNTER_ELECT] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("      Vk\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "      Vk\t");
    } //16
    if(sys_data.data_cfg[CFG_V_COUNTER_ELECT_STD] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("  Vk_std\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "  Vk_std\t");
    } //17
    if(sys_data.data_cfg[CFG_I_COUNTER] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("      Ik\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "      Ik\t");
    } //18
    if(sys_data.data_cfg[CFG_I_SUBSTRATE] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("      Ib\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "      Ib\t");
    } //19
    if(sys_data.data_cfg[CFG_CALC_TEMP] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf(" TC_Dfet\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, " TC_Dfet\t");
    } //20
    if(sys_data.data_cfg[CFG_CALC_PH_VRSI] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("pHintEst\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "pHintEst\t");
    } //21
    if(sys_data.data_cfg[CFG_CALC_PH_VRSE] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("pHextEst\t");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "pHextEst\t");
    } //22
    if(sys_data.data_cfg[CFG_OPTODE] > 0)
    {                                             //01234567
        if(printFlg == 1)                  uprintf("\tOpt_PN\tOpt_SN\tMoxy\tO2satper\tTC_opt\tDphase\tBphase\tRphase\tBamp\tBpot\tRamp\tOpt_rawtemp");
        if(writeFlg == 1)  sd_fprintf(&fileObject, "\tOpt_PN\tOpt_SN\tMoxy\tO2satper\tTC_opt\tDphase\tBphase\tRphase\tBamp\tBpot\tRamp\tOpt_rawtemp");

    } //22 -
    if(sys_data.data_cfg[CFG_CTD] > 0)
    {                                             //01234567
        if(ctd.type = 1)        // microCAT
        {
                                                    // "%f %f %f %s %s", &tempC, &dummy, &salt, buff, buff) != 5)
                                                    //21.4218,  0.00006,   -0.147,   0.0105, 02 May 2021, 12:37:24
            if(printFlg == 1)                  uprintf("\tTempC\tCond\tSalt\tUnk1\tDate\tTime\t");
            if(writeFlg == 1)  sd_fprintf(&fileObject, "\tTempC\tCond\tSalt\tUnk1\tDate\tTime\t");
        }
        if(ctd.type = 2)        // aanderaa 5860
        {
                                                //5860    29      -0.001  26.054  0.012   996.780 1499.510
            if(printFlg == 1)                  uprintf("\tCond_PN\tCond_SN\tCond\tTemp\tUnk1\tUnk2\tUnk3\t");
            if(writeFlg == 1)  sd_fprintf(&fileObject, "\tCond_PN\tCond_SN\tCond\tTemp\tUnk1\tUnk2\tUnk3\t");

        }

    }
#endif

    if(printFlg == 1)                  uprintf("\n");      // was uprintf("\r\n);  changed to match write
    if(writeFlg == 1)  sd_fprintf(&fileObject, "\n");      // was uprintf("\r\n);  changed to match write

    if(printFlg == 1)    wait_consoleTx();

    return(retVal);

}

void print_data_header(void)
{
    print_write_data_header(1,0);

}


int write_data_header(void)
{
    return(print_write_data_header(0,1));
}


/*
 * stub for older get_header (used in COMMAND mode gh
 */
void get_header(void)
{
    print_write_data_header(1,0);
}

/*
 * enter_deployment_note with timeout
 */
int enter_deployment_note(uint32_t timeout)
{
    int byteCnt = 0;
    char noteBuf[84];

    noteBuf[0] = ' ';
    noteBuf[1] = 0x00;
    byteCnt = 1;

    sys_data.deploy_note[0] = ' ';
    sys_data.deploy_note[1] = 0;
    uprintf("\nEnter Deployment Note [ ]: ");
    if( getUserInputTimeLimit(noteBuf, (MAX_DEPLOY_NOTE-4), timeout) )
    {
        byteCnt = strcpy(sys_data.deploy_note, noteBuf);
        uprintf("\n%s\n", sys_data.deploy_note);
    }

    return(byteCnt);
}








/*
 * print_write_metadata_header() was written to improve maintainability and consistency of formatted output
 *
 * replaces print_metadata_header and write_metadata_header
 */

int print_write_metadata_header(const time_t currentTime, uint32_t printFlg, uint32_t writeFlg)
{

    struct tm *time_struct;
    char buff[TIMESTAMPLENGTH];
    FIL fileObject;     // File object
    FRESULT fresult;
    UINT bw;
    int retVal;

    retVal = 0;     // assume a good return (1 is error due to SD card

    if(microSD_flg == 0)
    {
        retVal = 0;  // pretend it's good
        writeFlg = 0;
    }

    if(writeFlg == 1)
    {

        // Open a file
        fresult = f_open(&fileObject, sys_data.fileName, FA_READ |FA_WRITE |FA_OPEN_ALWAYS);
        if(fresult != FR_OK)
        {
            uprintf("f_open error: %s\n", StringFromFresult(fresult));
            //return 1;
            retVal = 1;
            writeFlg = 0;
        }

        // Seek to the end, to append our file
        fresult = f_lseek(&fileObject, fileObject.fsize);
        if(fresult != FR_OK)
        {
            uprintf("f_lseek error: %s\n", StringFromFresult(fresult));
            //return 1;
            retVal = 1;
            writeFlg = 0;
        }

    }

    if(writeFlg == 1)   sd_fprintf(&fileObject, "\n\n");
    if(printFlg == 1)                   uprintf("\n\n");
    if(writeFlg == 1)   sd_fprintf(&fileObject, "//++************** Deployment Settings ***************\n");
    if(printFlg == 1)                   uprintf("//++************** Deployment Settings ***************\n");

    if(writeFlg == 1) sd_fprintf(&fileObject, "// Firmware version: \t%s\n", sys_data.qa.firmware_version);
    if(printFlg == 1)                 uprintf("// Firmware version: \t%s\n", sys_data.qa.firmware_version);


    if(writeFlg == 1) sd_fprintf(&fileObject, "// Sampling period (s)\t%u\n", sys_data.sampling_period);
    if(printFlg == 1)                 uprintf("// Sampling period (s)\t%u\n", sys_data.sampling_period);

    if(sys_data.sample_aligned)
    {
        if(writeFlg == 1) sd_fprintf(&fileObject, "// Samples hour align:\tYes\n");
        if(printFlg == 1)                 uprintf("// Samples hour align:\tYes\n");
    }
    else
    {
        if(writeFlg == 1) sd_fprintf(&fileObject, "// Samples hour align:\tNo\n");
        if(printFlg == 1)                 uprintf("// Samples hour align:\tNo\n");
    }

#ifdef NOCODE
    if(writeFlg == 1) sd_fprintf(&fileObject, "// pH Sample average:\t%u\n", sys_data.sample_average);
    if(printFlg == 1)                 uprintf("// pH Sample average:\t%u\n", sys_data.sample_average);
#endif
    if(sys_data.test_mode)
    {
        if(writeFlg == 1) sd_fprintf(&fileObject, "// Pump on time (s):\t%u  (TEST MODE, pump disabled)\n", sys_data.pumpon_time);
        if(printFlg == 1)                 uprintf("// Pump on time (s):\t%u  (TEST MODE, pump disabled)\n", sys_data.pumpon_time);
    }
    else
    {
        if(writeFlg == 1) sd_fprintf(&fileObject, "// Pump on time (s):\t%u\n", sys_data.pumpon_time);
        if(printFlg == 1)                 uprintf("// Pump on time (s):\t%u\n", sys_data.pumpon_time);
    }

    if(writeFlg == 1) sd_fprintf(&fileObject, "// Low battery voltage:\t%.1f\n", sys_data.low_batt_volt);
    if(printFlg == 1)                 uprintf("// Low battery voltage:\t%.1f\n", sys_data.low_batt_volt);

    if(sys_data.output == 1)
    {                                              // Low battery voltage:\t%.1f\n"
        if(writeFlg == 1) sd_fprintf(&fileObject, "// Output mode:        \tNormal\n");
        if(printFlg == 1)                 uprintf("// Output mode:        \tNormal\n");

    }
    if(sys_data.output == 2)
    {
        if(writeFlg == 1) sd_fprintf(&fileObject, "// Output mode:        \tVerbose\n");
        if(printFlg == 1)                 uprintf("// Output mode:        \tVerbose\n");
    }

    time_struct = localtime(&sys_data.deploy_time);
    strftime(buff, sizeof(buff),"// Deploy time:   \t%Y/%m/%d %H:%M:%S\n", time_struct);
#ifdef OLDCODE
    time_struct = localtime(&currentTime);
    strftime(buff, sizeof(buff),"// Current time:\t%Y/%m/%d %H:%M:%S\n", time_struct);
#endif
    if(writeFlg == 1)
    {
        fresult = f_write(&fileObject, buff, strlen(buff), &bw);
        if(fresult != FR_OK)
        {
            uprintf("Current time f_write error: %s\n", StringFromFresult(fresult));
            //return 1;
            retVal = 1;
            writeFlg = 0;
        }
    }
    if(printFlg == 1)  uprintf("%s", buff);

    // Store timezone
    if(sys_data.GMT)
    {
        if(writeFlg == 1)   sd_fprintf(&fileObject, "// Time zone:       \tGMT\n");
        if(printFlg == 1)                   uprintf("// Time zone:       \tGMT\n");
    }
    else
    {
        if(writeFlg == 1)   sd_fprintf(&fileObject, "// Time zone:       \tLocal\n");
        if(printFlg == 1)                   uprintf("// Time zone:       \tLocal\n");
    }


    if(printFlg == 1)    wait_consoleTx();       // defd in uartstdio.c  TODO add timeout


    if(writeFlg == 1) sd_fprintf(&fileObject, "// TCOffset:     \t%f\n", sys_data.TCOffset);
    if(printFlg == 1)                 uprintf("// TCOffset:     \t%f\n", sys_data.TCOffset);
    if(writeFlg == 1) sd_fprintf(&fileObject, "// k0int:        \t%f\n", sys_data.k0int);
    if(printFlg == 1)                 uprintf("// k0int:        \t%f\n", sys_data.k0int);
    if(writeFlg == 1) sd_fprintf(&fileObject, "// k0ext:        \t%f\n", sys_data.k0ext);
    if(printFlg == 1)                 uprintf("// k0ext:        \t%f\n", sys_data.k0ext);
    if(writeFlg == 1) sd_fprintf(&fileObject, "// k2int:        \t%f\n", sys_data.k2int);
    if(printFlg == 1)                 uprintf("// k2int:        \t%f\n", sys_data.k2int);
    if(writeFlg == 1) sd_fprintf(&fileObject, "// k2ext:        \t%f\n", sys_data.k2ext);
    if(printFlg == 1)                 uprintf("// k2ext:        \t%f\n", sys_data.k2ext);
    if(writeFlg == 1) sd_fprintf(&fileObject, "// Def Sal (ppt):\t%f\n", sys_data.default_sal);
    if(printFlg == 1)                 uprintf("// Def Sal (ppt):\t%f\n", sys_data.default_sal);
    if(writeFlg == 1) sd_fprintf(&fileObject, "// Offset Ik:    \t%f\n", sys_data.offset_Ik);
    if(printFlg == 1)                 uprintf("// Offset Ik:    \t%f\n", sys_data.offset_Ik);
    if(sys_data.app_cfg[APPCFG_TEMPC_SH_ALG] == 0)
    {
        if(writeFlg == 1) sd_fprintf(&fileObject, "// S-H c0        \t%f\n", sys_data.quadTherm_c0);
        if(printFlg == 1)                 uprintf("// s-h c0        \t%f\n", sys_data.quadTherm_c0);
        if(writeFlg == 1) sd_fprintf(&fileObject, "// S-H c1        \t%f\n", sys_data.quadTherm_c1);
        if(printFlg == 1)                 uprintf("// s-h c1        \t%f\n", sys_data.quadTherm_c1);
        if(writeFlg == 1) sd_fprintf(&fileObject, "// S-H c2        \t%f\n", sys_data.quadTherm_c2);
        if(printFlg == 1)                 uprintf("// s-h c2        \t%f\n", sys_data.quadTherm_c2);
        if(writeFlg == 1) sd_fprintf(&fileObject, "// S-H c3        \t%f\n", sys_data.quadTherm_c3);
        if(printFlg == 1)                 uprintf("// s-h c3        \t%f\n", sys_data.quadTherm_c3);
    }
    if(sys_data.app_cfg[APPCFG_TEMPC_SH_ALG] == 1)
    {
        if(writeFlg == 1) sd_fprintf(&fileObject, "// S-H A         \t%f\n", sys_data.shTherm_A);
        if(printFlg == 1)                 uprintf("// s-h A         \t%f\n", sys_data.shTherm_A);
        if(writeFlg == 1) sd_fprintf(&fileObject, "// S-H B         \t%f\n", sys_data.shTherm_B);
        if(printFlg == 1)                 uprintf("// s-h B         \t%f\n", sys_data.shTherm_B);
        if(writeFlg == 1) sd_fprintf(&fileObject, "// S-H C         \t%f\n", sys_data.shTherm_C);
        if(printFlg == 1)                 uprintf("// s-h C         \t%f\n", sys_data.shTherm_C);
    }
    if(writeFlg == 1)   sd_fprintf(&fileObject, "// Version Info: \t%s\n", VERSION);
    if(printFlg == 1)                   uprintf("// Version Info: \t%s\n", VERSION);


    // Store the tab delimited deployment header. (Excel treats quoted string as text)
    if(writeFlg == 1) sd_fprintf(&fileObject, "// File name:     \t%s\n", sys_data.fileName);
    if(printFlg == 1)                 uprintf("// File name:     \t%s\n", sys_data.fileName);
    if(writeFlg == 1) sd_fprintf(&fileObject, "// User initials: \t%s\n", sys_data.user);
    if(printFlg == 1)                 uprintf("// User initials: \t%s\n", sys_data.user);

    if(printFlg == 1)    wait_consoleTx();       // defd in uartstdio.c  TODO add timeout

    if(writeFlg == 1) sd_fprintf(&fileObject, "// Board SN:      \t%s\n", sys_data.board_SN);
    if(printFlg == 1)                 uprintf("// Board SN:      \t%s\n", sys_data.board_SN);
//#if BOARD_MPHOX == 1 || BOARD_MFET >= 1
    if(writeFlg == 1) sd_fprintf(&fileObject, "// MCAP SN:       \t%s\n", sys_data.mcap_SN);
    if(printFlg == 1)                 uprintf("// MCAP SN:       \t%s\n", sys_data.mcap_SN);
//#endif
    if(writeFlg == 1) sd_fprintf(&fileObject, "// DuraFET SN:    \t%s\n", sys_data.durafet_SN);
    if(printFlg == 1)                 uprintf("// DuraFET SN:    \t%s\n", sys_data.durafet_SN);

    if(writeFlg == 1) sd_fprintf(&fileObject, "// Optode SN:     \t%s\n", sys_data.optode_SN);
    if(printFlg == 1)                 uprintf("// Optode SN:     \t%s\n", sys_data.optode_SN);

    if(writeFlg == 1) sd_fprintf(&fileObject, "// CTD SN:        \t%s\n", sys_data.ctd_SN);
    if(printFlg == 1)                 uprintf("// CTD SN:        \t%s\n", sys_data.ctd_SN);

    if(writeFlg == 1) sd_fprintf(&fileObject, "// Pump SN:       \t%s\n", sys_data.pump_SN);
    if(printFlg == 1)                 uprintf("// Pump SN:       \t%s\n", sys_data.pump_SN);
    if(writeFlg == 1) sd_fprintf(&fileObject, "// Package name:  \t%s\n", sys_data.package_name);
    if(printFlg == 1)                 uprintf("// Package name:  \t%s\n", sys_data.package_name);
    if(writeFlg == 1) sd_fprintf(&fileObject, "// Fab Note:      \t%s\n", sys_data.board_fab_note);
    if(printFlg == 1)                 uprintf("// Fab Note:      \t%s\n", sys_data.board_fab_note);
    if(writeFlg == 1) sd_fprintf(&fileObject, "// Deploy note:   \t%s\n", sys_data.deploy_note);
    if(printFlg == 1)                 uprintf("// Deploy note:   \t%s\n", sys_data.deploy_note);
#ifdef NOCODE
    time_struct = localtime(&sys_data.deploy_time);
    strftime(buff, sizeof(buff),"// Deploy time:   \t%Y/%m/%d %H:%M:%S\n", time_struct);

    if(writeFlg == 1) fresult = f_write(&fileObject, buff, strlen(buff), &bw);
    if(printFlg == 1)  uprintf("%s", buff);
#endif
    //if(writeFlg == 1) sd_fprintf(&fileObject, "// Vint gain\t%d\n", sys_data.Vint_gain);
    //if(writeFlg == 1) sd_fprintf(&fileObject, "// Vint sample rate (sps)\t%d\n", sys_data.Vrsi_sps);

    int prnFlg, wrtFlg;
    if(printFlg > 0)
        prnFlg = 2;
    if(writeFlg > 0)
        wrtFlg = 2;
    print_write_config_data_fields(prnFlg, wrtFlg);  // new 27 Jul 2021  2 prints // pad

    if(writeFlg == 1) sd_fprintf(&fileObject, "//--**************************************************\n");
    if(printFlg == 1)                 uprintf("//--**************************************************\n");

    if(writeFlg == 1) sd_fprintf(&fileObject, "\n\n");
    if(printFlg == 1)                 uprintf("\n\n");


    if(writeFlg == 1)
    {
        sys_samp.next_gdata_fptr = fileObject.fptr;     // Store the file pointer for the first sample (for gdata)

        // Close the file
        fresult = f_close( &fileObject);
        if(fresult != FR_OK)
        {
            uprintf("f_close error: %s\n", StringFromFresult(fresult));
            //return 1;
            retVal = 1;
            writeFlg = 0;
        }
    }

    if(printFlg == 1)    wait_consoleTx();       // defd in uartstdio.c  TODO add timeout

    //return 0;  // all went well, return 0 for OK
    return(retVal);

}


void print_metadata_header(const time_t currentTime)
{
    print_write_metadata_header(currentTime, 1, 0);

}



void print_sys_data_structure(void)
{
    uprintf("\n");
    uprintf("// ------ sys_data EEPROM structure, size = %u ---- \n", sizeof(struct systemData));
    uprintf("// sampling_period: %u\n", sys_data.sampling_period);
    uprintf("// sample_average: %u\n", sys_data.sample_average);
    uprintf("// averaging_interval: %u\n", sys_data.averaging_interval);
    uprintf("// pumpon_time: %u\n", sys_data.pumpon_time);
    uprintf("// low_batt_volt: %.2f\n", sys_data.low_batt_volt);
    uprintf("\n");
    uprintf("// TCOffset: %.6f\n", sys_data.TCOffset);
    uprintf("// k0ext: %.6f\n", sys_data.k0ext);
    uprintf("// k0int: %.6f\n", sys_data.k0int);
    uprintf("// k2ext: %.6f\n", sys_data.k2ext);
    uprintf("// k2int: %.6f\n", sys_data.k2int);
    uprintf("// quadTherm_c0: %.11lf\n", sys_data.quadTherm_c0);
    uprintf("// quadTherm_c2: %.11lf\n", sys_data.quadTherm_c1);
    uprintf("// quadTherm_c3: %.11lf\n", sys_data.quadTherm_c2);
    uprintf("// quadTherm_c4: %.11lf\n", sys_data.quadTherm_c3);
    uprintf("// shTherm_A: %.11lf\n", sys_data.shTherm_A);                // steinhart-hart coefficient A
    uprintf("// shTherm_B: %.11lf\n", sys_data.shTherm_B);                // steinhart-hart coefficient C
    uprintf("// shTherm_C: %.11lf\n", sys_data.shTherm_C);                // steinhart-hart coefficient B
    uprintf("// default_sal: %.6f\n", sys_data.default_sal);
    uprintf("// sensor_sal: %.6f\n", sys_data.sensor_sal);

    uprintf("//\n");
    wait_consoleTx();

    uprintf("// Vrsi_sps: %u\n", sys_data.Vrsi_sps);
    uprintf("// Vrsi_trials: %u\n", sys_data.Vrsi_trials);
    uprintf("// Vrse_sps: %u\n", sys_data.Vrse_sps);
    uprintf("// Vrse_trials: %u\n", sys_data.Vrse_trials);
    uprintf("// Vtherm_sps: %u\n", sys_data.Vtherm_sps);
    uprintf("// Vtherm_trials: %u\n", sys_data.Vtherm_trials);
    uprintf("// Ik_sps: %u\n", sys_data.Ik_sps);
    uprintf("// Ik_trials: %u\n", sys_data.Ik_trials);
    uprintf("// Vk_sps: %u\n", sys_data.Vk_sps);
    uprintf("// Vk_trials: %u\n", sys_data.Vk_trials);
    uprintf("// Ib_sps: %u\n", sys_data.Ib_sps);
    uprintf("// Ib_trials: %u\n", sys_data.Ib_trials);
    uprintf("// Vb_sps: %u\n", sys_data.Vb_sps);
    uprintf("// Vb_trials: %u\n", sys_data.Vb_trials);
    uprintf("// Vbias_sps: %u\n", sys_data.Vbias_sps);
    uprintf("// Vbias_trials: %u\n", sys_data.Vbias_trials);
    uprintf("// offset_Ik: %u\n", sys_data.offset_Ik);
    uprintf("// offset_Ib: %u\n", sys_data.offset_Ib);

    uprintf("//\n");
    wait_consoleTx();

    uprintf("// output: %u\n", sys_data.output);
    uprintf("// test_mode: %u\n", sys_data.test_mode);
    uprintf("// fast_mode: %u\n", sys_data.fast_mode);

    uprintf("// solenoid_on2hold_msec: %u msec\n", sys_data.solenoid_on2hold_msec);
    uprintf("// solenoid_hold_pwm: %u\n", sys_data.solenoid_hold_pwm);
    uprintf("// solenoid_holdtime: %u sec\n", sys_data.solenoid_holdtime);

    uprintf("// baud_rate: %u\n", sys_data.baud_rate);
    uprintf("//\n");
    wait_consoleTx();

    print_write_config_data_fields(2,0);
    wait_consoleTx();

    uprintf("//\n");
    uprintf("// app_cfg[APPCFG_SAMPLING_DIAG]: %u\n", sys_data.app_cfg[APPCFG_SAMPLING_DIAG]);
    uprintf("// app_cfg[APPCFG_TEMPC_SH_ALG]: %u\n", sys_data.app_cfg[APPCFG_TEMPC_SH_ALG]);
    uprintf("// app_cfg[APPCFG_MICROSD_ENABLE]: %u\n", sys_data.app_cfg[APPCFG_MICROSD_ENABLE]);
    uprintf("// app_cfg[APPCFG_MENU_LEVEL]: %u\n", sys_data.app_cfg[APPCFG_MENU_LEVEL]);
    uprintf("// app_cfg[APPCFG_SPARE4]: %u\n", sys_data.app_cfg[APPCFG_SPARE4]);
    uprintf("// app_cfg[APPCFG_SPARE5]: %u\n", sys_data.app_cfg[APPCFG_SPARE5]);
    uprintf("// app_cfg[APPCFG_SPARE6]: %u\n", sys_data.app_cfg[APPCFG_SPARE6]);
    uprintf("// app_cfg[APPCFG_SPARE7]: %u\n", sys_data.app_cfg[APPCFG_SPARE7]);
    uprintf("//\n");
    wait_consoleTx();

    uprintf("// GMT: %u\n", sys_data.GMT);
    uprintf("// sample_aligned: %u\n", sys_data.sample_aligned);
    uprintf("// deploy_time: %u\n", sys_data.deploy_time);

    uprintf("// pHOffset: %.6f\n", sys_data.pHOffset);
    uprintf("// pHSlope:  %.6f\n", sys_data.pHSlope);
    uprintf("// TCSlope: %.6f\n", sys_data.TCSlope);
    uprintf("// sensor_ctd_TC:  %.6f\n", sys_data.sensor_ctd_TC);
    uprintf("// sensor_optode_TC:  %.6f\n", sys_data.sensor_optode_TC);

    uint32_t indx;
    for(indx=0; indx<4; indx++)   uprintf("// spare[%u] : %.6f\n", indx, sys_data.spare[indx]);

    wait_consoleTx();

    uprintf("// fileName: %s\n", sys_data.fileName);
    uprintf("// user: %s\n", sys_data.user);
    uprintf("// board_SN: %s\n", sys_data.board_SN);
    uprintf("// mcap_SN: %s\n", sys_data.mcap_SN);
    uprintf("// durafet_SN: %s\n", sys_data.durafet_SN);
    uprintf("// package_name: %s\n", sys_data.package_name);
    uprintf("// optode_SN: %s\n", sys_data.optode_SN);
    uprintf("// ctd_SN: %s\n", sys_data.ctd_SN);
    uprintf("// pump_SN: %s\n", sys_data.pump_SN);
    uprintf("// deploy_note: %s\n", sys_data.deploy_note);
    uprintf("// board_fab_note: %s\n", sys_data.board_fab_note);
    uprintf("//\n");

    wait_consoleTx();
    //uprintf("// state: %u\n", sys_samp.state);
    //uprintf("// nextWakeUp: %u\n", sys_samp.nextWakeUp);
    //uprintf("// next_gdata_fptr: %u\n", sys_samp.next_gdata_fptr);
    //uprintf("// current_sample: %u\n", sys_samp.current_sample);
    //uprintf("// AD24_std: %.6f\n", sys_samp.AD24_std);


}





void config_sys_data_default_init(void)
{
    sys_data.deploy_time = ROM_HibernateRTCGet();

    sys_data.low_batt_volt = 9.2;
#if BOARD_MFET >= 1
    sys_data.low_batt_volt = 5.5;
#endif
#if BOARD_MPHOX >= 1
    sys_data.low_batt_volt = 10.0;
#endif
#if BOARD_NANOFET >= 1
    sys_data.low_batt_volt = 5.5;
#endif
    sys_data.sampling_period = 0;
    sys_data.sample_average = 10;           // Deprecated
    sys_data.averaging_interval = 60;
    sys_data.sample_aligned = false;
    sys_data.GMT = false;                       //456 GMT or Local time
    sys_data.sample_aligned = true;            //460 Sample aligned to hour
    sys_data.pumpon_time = 0;
    sys_data.TCOffset = 0.0;
    sys_data.k0ext = -1.3;
    sys_data.k0int = -0.4;
    sys_data.k2ext = -0.001081;
    sys_data.k2int = -0.001455;


    // steinhart-hart coefficients for thermistor (why are the 4 coeff, thought there should only be 3)
    sys_data.quadTherm_c0 = 340.9819863;
    sys_data.quadTherm_c1 = -9.10257E-05;  // -0.0000910257
    sys_data.quadTherm_c2 = -95.08806667;
    sys_data.quadTherm_c3 = 0.965370274;

    sys_data.shTherm_A = 0.002108508173;
    sys_data.shTherm_B = 0.00007979204727;
    sys_data.shTherm_C = 0.0000006535076315;

    sys_data.default_sal = 35.0;
    sys_data.sensor_sal = 0.0;

    sys_data.Vtherm_trials = VTHERM_TRIALS;
    sys_data.Vtherm_sps = VTHERM_SPS;

    sys_data.Vrsi_trials = VRSI_TRIALS;
    sys_data.Vrsi_sps = VRSI_SPS;

    sys_data.Vrse_trials = VRSE_TRIALS;
    sys_data.Vrse_sps = VRSE_SPS;

    sys_data.Ik_trials = IK_TRIALS;
    sys_data.Ik_sps = IK_SPS;

    sys_data.Vk_trials = VK_TRIALS;
    sys_data.Vk_sps = VK_SPS;

    sys_data.Ib_trials = IB_TRIALS;
    sys_data.Ib_sps = IB_SPS;

    sys_data.Vb_trials = VB_TRIALS;
    sys_data.Vb_sps = VB_SPS;

    sys_data.Vbias_trials = VBIAS_TRIALS;
    sys_data.Vbias_sps = VBIAS_SPS;       // used for vbias bat

    sys_data.offset_Ik = 0.000800;             // 800uV or so, tbd measure a QA step.
    sys_data.offset_Ib = 0.000800;

    sys_data.output = NORMAL;
    sys_data.test_mode = 0;
    sys_data.fast_mode = 1;
    sys_data.solenoid_on2hold_msec = 100;   // hit
    sys_data.solenoid_hold_pwm = 40;        // hold
    sys_data.solenoid_holdtime = 10;


    //state;                      //430 IDLE, DEPLOYED, COMMAND,  NOT USER MODIFIABLE                        <<<<<------
    //uint32_t nextWakeUp;            //434 Next intended RTC match (wake-up time) NOT USER MODIFIABLE           <<<<<------
    //unsigned long next_gdata_fptr;  //438 The file position of the next gdata sample. NOT USER MODIFIABLE      <<<<<------
    //unsigned int current_sample;    //442 The current data record to be stored. NOT USER MODIFIABLE            <<<<<------
    //float AD24_std;                 //464 standard deviation for AD24                                          <<<<--------
    sys_samp.state = IDLE;
    //sys_data.nextWakeUp = current_time + 30;
    sys_samp.nextWakeUp = ROM_HibernateRTCGet()+sys_data.sampling_period;
    sys_samp.next_gdata_fptr = 0;
    sys_samp.AD24_std = 0.0;
    sys_samp.current_sample = 0;

    sys_data.deploy_time = 0;


    sys_data.baud_rate = 115200L;
    int indx;
    for(indx=0; indx<MAX_NUMOF_CONFIG_FIELDS; indx++)
        sys_data.data_cfg[indx] = 0;


    sys_data.pHOffset = 0.0;
    sys_data.pHSlope = 1.0;
    sys_data.TCSlope = 1.0;
    sys_data.sensor_ctd_TC = 0.0;
    sys_data.sensor_optode_TC = 0.0;

    sys_data.spare[0] = 0.0;
    sys_data.spare[1] = 0.0;
    sys_data.spare[2] = 0.0;
    sys_data.spare[3] = 0.0;


    sys_data.diag = 0;                      // diagnostics, enables sample time measurement
    sys_data.debug_flag = 0;                // diagnostic printf (dbg_printf() enabled)
    sys_data.GMT = false;                       //456 GMT or Local time
    sys_data.sample_aligned = false;            //460 Sample aligned to hour

    sys_data.pHOffset = 0.0;
    sys_data.pHSlope = 1.0;
    sys_data.fast_mode = 1;

    strcpy(sys_data.fileName, "noname.txt");
    strcpy(sys_data.user, "abc");
    strcpy(sys_data.board_SN, " ");
    strcpy(sys_data.board_fab_note, " ");
    strcpy(sys_data.mcap_SN, " ");
    strcpy(sys_data.durafet_SN, " ");
    strcpy(sys_data.optode_SN, " ");
    strcpy(sys_data.ctd_SN, " ");
    strcpy(sys_data.pump_SN, " ");
    strcpy(sys_data.package_name, " ");   // instrument package name, ex. underway system, glider pH
    strcpy(sys_data.deploy_note, " ");

    sys_data.data_cfg[CFG_SAMPLE_NUM] = 1;          // A
    sys_data.data_cfg[CFG_TIMEDATE] = 1;            // B

    sys_data.data_cfg[CFG_VIN_BAT_VOLT] = 1;        // C
    sys_data.data_cfg[CFG_BIAS_BAT_POS] = 1;        // D
    sys_data.data_cfg[CFG_BIAS_BAT_NEG] = 1;        // E

    sys_data.data_cfg[CFG_BRD_TEMP] = 1;            // F
    sys_data.data_cfg[CFG_BRD_HUMIDITY] = 1;        // G

    sys_data.data_cfg[CFG_V_THERMISTOR] = 1;        // H
    sys_data.data_cfg[CFG_V_THERMISTOR_STD] = 1;    //
    sys_data.data_cfg[CFG_CALC_TEMP] = 1;           // I

    sys_data.data_cfg[CFG_VRSI] = 1;               // J
    sys_data.data_cfg[CFG_VRSI_STD] = 1;           //
    sys_data.data_cfg[CFG_CALC_PH_VRSI] = 1;          // K

    sys_data.data_cfg[CFG_VRSE] = 1;               // L
    sys_data.data_cfg[CFG_VRSE_STD] = 1;           //
    sys_data.data_cfg[CFG_CALC_PH_VRSE] = 0;          // m


    sys_data.data_cfg[CFG_VRSI_BIASED] = 1;        // N
    sys_data.data_cfg[CFG_VRSI_BIASED_STD] = 0;    // o
    sys_data.data_cfg[CFG_VRSE_BIASED] = 0;        // p
    sys_data.data_cfg[CFG_VRSE_BIASED_STD] = 0;    // q

    sys_data.data_cfg[CFG_V_COUNTER_ELECT] = 1;     // R
    sys_data.data_cfg[CFG_V_COUNTER_ELECT_STD] = 1; //
    sys_data.data_cfg[CFG_I_COUNTER] = 1;           // T
    sys_data.data_cfg[CFG_I_SUBSTRATE] = 1;         // U

    sys_data.data_cfg[CFG_OPTODE] = 0;              // v
    sys_data.data_cfg[CFG_CTD] = 0;                 // w

    sys_data.data_cfg[CFG_SPARE1] = 0;
    sys_data.data_cfg[CFG_SPARE2] = 0;
    sys_data.data_cfg[CFG_SPARE3] = 0;
    sys_data.data_cfg[CFG_SPARE4] = 0;
    sys_data.data_cfg[CFG_SPARE5] = 0;
    sys_data.data_cfg[CFG_SPARE6] = 0;

    sys_data.app_cfg[APPCFG_SAMPLING_DIAG] = 0;
    sys_data.app_cfg[APPCFG_TEMPC_SH_ALG] = 0;
    sys_data.app_cfg[APPCFG_MICROSD_ENABLE] = 1;        // enable micro sd card
    sys_data.app_cfg[APPCFG_MENU_LEVEL] = 0;
    sys_data.app_cfg[APPCFG_SPARE4] = 0;
    sys_data.app_cfg[APPCFG_SPARE5] = 0;
    sys_data.app_cfg[APPCFG_SPARE6] = 0;
    sys_data.app_cfg[APPCFG_SPARE7] = 0;


    config_qa_data_init();

}


void config_qa_data_init(void)
{

    sys_data.qa.microSD = 0;            // microSD
    sys_data.qa.time = 0;               // time of QA
    sys_data.qa.test_time = 0;          // time it took to run QA
    sys_data.qa.eeprom = 0;
    sys_data.qa.sys_data_init = 0;      // was the data initialized
    sys_data.qa.fram = 0;
    sys_data.qa.adc12onchip = 0;
    sys_data.qa.adc24 = 0;              // how many of the ADC fields are out of expected Jensenian range


    sys_data.qa.Vrsi = 0.0;
    sys_data.qa.Vrsi_std = 0.0;
    sys_data.qa.Vrsi_err = 0.0;
    sys_data.qa.Vrse = 0.0;
    sys_data.qa.Vrse_std = 0.0;
    sys_data.qa.Vrse_err = 0.0;
    sys_data.qa.Vtherm = 0.0;
    sys_data.qa.Vtherm_std = 0.0;
    sys_data.qa.Vtherm_err = 0.0;

    sys_data.qa.Ik = 0.0;
    sys_data.qa.Ik_std = 0.0;
    sys_data.qa.Ik_err = 0.0;

    sys_data.qa.Vk = 0.0;
    sys_data.qa.Vk_std = 0.0;
    sys_data.qa.Vk_err = 0.0;

    sys_data.qa.Ib = 0.0;
    sys_data.qa.Ib_std = 0.0;
    sys_data.qa.Ib_err = 0.0;

    sys_data.qa.Vb = 0.0;
    sys_data.qa.Vb_std = 0.0;
    sys_data.qa.Vb_err = 0.0;

    sys_data.qa.Vbias_pos = 0.0;
    sys_data.qa.Vbias_neg = 0.0;

    sys_data.qa.aux_pwr1 = 0;
    sys_data.qa.aux_pwr2 = 0;
    sys_data.qa.aux_pwr3 = 0;

    sys_data.qa.ctd = 0;
    sys_data.qa.optode = 0;
    sys_data.qa.comm_pwr = 0;

    strcpy(sys_data.qa.tester, "Count Zero");
    strcpy(sys_data.qa.firmware_version, "Version Not Defined");


}

uint32_t edit_uint(uint32_t *uint_data, uint32_t negLim, uint32_t posLim)
{
    uint32_t temp;
    char buff[24];

    temp = 0;
    if(getUserInput(buff, 20))
    {
        if(sscanf(buff, "%u", &temp) == 1)
        {
             *uint_data = temp;

        }
    }
    return(temp);
}


float edit_float(float *float_data, float negLim, float posLim)
{
    float temp;
    char buff[24];

    temp = 0.0;

    if(getUserInput(buff, 20))
    {
        if(sscanf(buff, "%f", &temp) == 1)
        {
             *float_data = temp;
             //return(temp);
        }
    }
    return(temp);
}

uint32_t edit_string(char *char_data, uint32_t max_chars)
{
    char buff[128];

    if( getUserInput(buff, max_chars) )
    {
        strcpy(char_data, buff);        // oops: strcpy(*char_data, buff);
        //uprintf("\nFile name now: %s\n", sys_data.fileName);
    }

    return(strlen(buff));
}


double edit_double(double *double_data, double negLim, double posLim)
{
    double temp;
    char buff[24];

    temp = 0.0;

    if(getUserInput(buff, 20))
    {
        if(sscanf(buff, "%lf", &temp) == 1)     // BUG fix %f to %lf
        {
             *double_data = temp;
             //return(temp);
        }
    }
    return(temp);
}

void copy_edit_sys_data_structure(void)
{
    char buff[68];
    uint32_t temp;
    int response;



    uprintf("// ------ sys_data structure, size = %u ---- \n", sizeof(struct systemData));

    while(1)
    {
        uprintf("\nFirst 5 sys_data fields:\n\n");

        uprintf("sampling_period [%u] sec: ", sys_data.sampling_period);
        edit_uint(&sys_data.sampling_period, 0, 6000);

        uprintf("sampling_average [%u] sec: ", sys_data.sample_average);
        edit_uint(&sys_data.sampling_period, 0, 300);

        uprintf("averaging_interval: [%u] sec: ", sys_data.averaging_interval);
        edit_uint(&sys_data.averaging_interval, 0, 360);

        uprintf("pumpon_time [%u] sec: ", sys_data.pumpon_time);
        edit_uint(&sys_data.pumpon_time, 0, 3600);

        uprintf("low_batt_volt [%.1f]: ", sys_data.low_batt_volt);
        edit_float(&sys_data.k0ext, 5.5, 18.0);

        //uprintf("\n");
        response = yesOrNoMenuChoice("\n\nRe-visit? (Y/N) [N]? ", NO);
        if(response == NO)
            {
            uprintf("\n");
            break;
            }
    }
    //----
    while(1)
    {
        uprintf("\nSecond batch of 13 sys_data fields:\n\n");

        uprintf("TCOffset [%.6f]: ", sys_data.TCOffset);
        edit_float(&sys_data.TCOffset, -10.0, 10.0);

        uprintf("k0ext [%.6f]: ", sys_data.k0ext);
        edit_float(&sys_data.k0ext, -10.0, 10.0);

        uprintf("k0int [%.6f]: ", sys_data.k0int);
        edit_float(&sys_data.k0int, -10.0, 10.0);

        uprintf("k2ext [%.6f]: ", sys_data.k2ext);
        edit_float(&sys_data.k2ext, -10.0, 10.0);

        uprintf("k2int [%.6f]: ", sys_data.k2int);
        edit_float(&sys_data.k2int, -10.0, 10.0);

        uprintf("quadTherm_c0 [%.11f]: ", sys_data.quadTherm_c0);
        edit_double(&sys_data.quadTherm_c0, -10.0, 10.0);

        uprintf("quadTherm_c1 [%.11f]: ", sys_data.quadTherm_c1);
        edit_double(&sys_data.quadTherm_c1, -10.0, 10.0);

        uprintf("quadTherm_c2 [%.11f]: ", sys_data.quadTherm_c2);
        edit_double(&sys_data.quadTherm_c2, -10.0, 10.0);

        uprintf("quadTherm_c3 [%.11f]: ", sys_data.quadTherm_c3);
        edit_double(&sys_data.quadTherm_c3, -10.0, 10.0);

        uprintf("shTherm_A [%.11f]: ", sys_data.shTherm_A);
        edit_double(&sys_data.shTherm_A, -10.0, 10.0);

        uprintf("shTherm_B [%.11f]: ", sys_data.shTherm_B);
        edit_double(&sys_data.shTherm_B, -10.0, 10.0);

        uprintf("default_sal [%.3f]: ", sys_data.default_sal);
        edit_float(&sys_data.default_sal, 0, 50.0);

        uprintf("sensor_sal [%.3f]: ", sys_data.sensor_sal);
        edit_float(&sys_data.sensor_sal, 0, 50.0);

        response = yesOrNoMenuChoice("\n\nRe-visit? (Y/N) [N]? ", NO);
        if(response == NO)
            break;
    }
    //----
    while(1)
    {
        uprintf("\nThird batch of 20 sys_data fields:\n\n");

        uprintf("Vrsi_sps [%u]: ", sys_data.Vrsi_sps);
        edit_uint(&sys_data.Vrsi_sps, 0, 80);

        uprintf("Vrsi_trials [%u]: ", sys_data.Vrsi_trials);
        edit_uint(&sys_data.Vrsi_trials, 0, 80);

        uprintf("Vrse_sps [%u]: ", sys_data.Vrse_sps);
        edit_uint(&sys_data.Vrse_sps, 0, 80);

        uprintf("Vrse_trials [%u]: ", sys_data.Vrse_trials);
        edit_uint(&sys_data.Vrse_trials, 0, 80);

        uprintf("Vtherm_sps [%u]: ", sys_data.Vtherm_sps);
        edit_uint(&sys_data.Vtherm_sps, 0, 80);

        uprintf("Vtherm_trials [%u]: ", sys_data.Vtherm_trials);
        edit_uint(&sys_data.Vtherm_trials, 0, 80);

        uprintf("Vtherm_trials [%u]: ", sys_data.Vtherm_trials);
        edit_uint(&sys_data.Vtherm_trials, 0, 80);

        uprintf("Ik_sps [%u]: ", sys_data.Ik_sps);
        edit_uint(&sys_data.Ik_sps, 0, 80);

        uprintf("Ik_trials [%u]: ", sys_data.Ik_trials);
        edit_uint(&sys_data.Ik_trials, 0, 80);

        uprintf("Vk_sps [%u]: ", sys_data.Vk_sps);
        edit_uint(&sys_data.Vk_sps, 0, 80);

        uprintf("Vk_sps [%u]: ", sys_data.Vk_sps);
        edit_uint(&sys_data.Vk_sps, 0, 80);

        uprintf("Vk_trials [%u]: ", sys_data.Vk_trials);
        edit_uint(&sys_data.Vk_trials, 0, 80);

        uprintf("Ib_sps [%u]: ", sys_data.Ib_sps);
        edit_uint(&sys_data.Ib_sps, 0, 80);

        uprintf("Ib_trials [%u]: ", sys_data.Ib_trials);
        edit_uint(&sys_data.Ib_trials, 0, 80);

        uprintf("Vb_sps [%u]: ", sys_data.Vb_sps);
        edit_uint(&sys_data.Vb_sps, 0, 80);

        uprintf("Vb_trials [%u]: ", sys_data.Vb_trials);
        edit_uint(&sys_data.Vb_trials, 0, 80);

        uprintf("Vbias_sps [%u]: ", sys_data.Vbias_sps);
        edit_uint(&sys_data.Vbias_sps, 0, 80);

        uprintf("Vbias_trials [%u]: ", sys_data.Vbias_trials);
        edit_uint(&sys_data.Vbias_trials, 0, 80);

        uprintf("offset_Ik [%u]: ", sys_data.offset_Ik);
        edit_uint(&sys_data.offset_Ik, 0, 80);

        uprintf("offset_Ib [%u]: ", sys_data.offset_Ib);
        edit_uint(&sys_data.offset_Ib, 0, 80);

        response = yesOrNoMenuChoice("\n\nRe-visit? (Y/N) [N]? ", NO);
        if(response == NO)
            break;
    }
    //----
    while(1)
    {
        uprintf("\nFourth batch of 7 sys_data fields:\n\n");

        uprintf("output [%u]: ", sys_data.output);
        edit_uint(&sys_data.output, 0, 5);

        uprintf("test_mode [%u]: ", sys_data.test_mode);
        edit_uint(&sys_data.test_mode, 0, 80);

        uprintf("fast_mode [%u]: ", sys_data.fast_mode);
        edit_uint(&sys_data.fast_mode, 0, 80);

        uprintf("solenoid_on2hold_msec [%u]: ", sys_data.solenoid_on2hold_msec);
        edit_uint(&sys_data.solenoid_on2hold_msec, 0, 80);

        uprintf("solenoid_hold_pwm [%u]: ", sys_data.solenoid_hold_pwm);
        edit_uint(&sys_data.solenoid_hold_pwm, 0, 80);

        uprintf("solenoid_holdtime [%u]: ", sys_data.solenoid_holdtime);
        edit_uint(&sys_data.solenoid_holdtime, 0, 80);

        uprintf("baud_rate [%u]: ", sys_data.baud_rate);
        edit_uint(&sys_data.baud_rate, 0, 500000);

        response = yesOrNoMenuChoice("\n\nRe-visit? (Y/N) [N]? ", NO);
        if(response == NO)
            break;
    }
    //----

    while(1)
    {
        uprintf("\nFifth batch of 32 sys_data fields:\n\n");

        // data cfg  MAX_NUMOF_CONFIG_FIELDS
        //print_write_config_data_fields(1,0);
        uprintf("sys_data.data_cfg[CFG_SAMPLE_NUM] [%u]: ", sys_data.data_cfg[CFG_SAMPLE_NUM]);
        edit_uint(&sys_data.data_cfg[CFG_SAMPLE_NUM], 0, 5);
        uprintf("sys_data.data_cfg[CFG_TIMEDATE] [%u]: ", sys_data.data_cfg[CFG_TIMEDATE]);
        edit_uint(&sys_data.data_cfg[CFG_TIMEDATE], 0, 5);
        uprintf("sys_data.data_cfg[CFG_V_THERMISTOR] [%u]: ", sys_data.data_cfg[CFG_V_THERMISTOR]);
        edit_uint(&sys_data.data_cfg[CFG_V_THERMISTOR], 0, 5);
        sys_data.data_cfg[CFG_V_THERMISTOR_STD] = sys_data.data_cfg[CFG_V_THERMISTOR];
        uprintf("sys_data.data_cfg[CFG_V_THERMISTOR_STD] [%u]: ", sys_data.data_cfg[CFG_V_THERMISTOR_STD]);
        edit_uint(&sys_data.data_cfg[CFG_V_THERMISTOR_STD], 0, 5);
        uprintf("sys_data.data_cfg[CFG_CALC_TEMP] [%u]: ", sys_data.data_cfg[CFG_CALC_TEMP]);
        edit_uint(&sys_data.data_cfg[CFG_CALC_TEMP], 0, 5);
        uprintf("sys_data.data_cfg[CFG_VRSI] [%u]: ", sys_data.data_cfg[CFG_VRSI]);
        edit_uint(&sys_data.data_cfg[CFG_VRSI], 0, 5);
        uprintf("sys_data.data_cfg[CFG_VRSI_STD] [%u]: ", sys_data.data_cfg[CFG_VRSI_STD]);
        edit_uint(&sys_data.data_cfg[CFG_VRSI_STD], 0, 5);
        uprintf("sys_data.data_cfg[CFG_CALC_PH_VRSI] [%u]: ", sys_data.data_cfg[CFG_CALC_PH_VRSI]);
        edit_uint(&sys_data.data_cfg[CFG_CALC_PH_VRSI], 0, 5);
        uprintf("sys_data.data_cfg[CFG_VRSE] [%u]: ", sys_data.data_cfg[CFG_VRSE]);
        edit_uint(&sys_data.data_cfg[CFG_VRSE], 0, 5);
        uprintf("sys_data.data_cfg[CFG_VRSE_STD] [%u]: ", sys_data.data_cfg[CFG_VRSE_STD]);
        edit_uint(&sys_data.data_cfg[CFG_VRSE_STD], 0, 5);
        uprintf("sys_data.data_cfg[CFG_CALC_PH_VRSE] [%u]: ", sys_data.data_cfg[CFG_CALC_PH_VRSE]);
        edit_uint(&sys_data.data_cfg[CFG_CALC_PH_VRSE], 0, 5);
        uprintf("sys_data.data_cfg[CFG_VRSI_BIASED] [%u]: ", sys_data.data_cfg[CFG_VRSI_BIASED]);
        edit_uint(&sys_data.data_cfg[CFG_VRSI_BIASED], 0, 5);
        uprintf("sys_data.data_cfg[CFG_VRSI_BIASED_STD] [%u]: ", sys_data.data_cfg[CFG_VRSI_BIASED_STD]);
        edit_uint(&sys_data.data_cfg[CFG_VRSI_BIASED_STD], 0, 5);
        uprintf("sys_data.data_cfg[CFG_VRSE_BIASED] [%u]: ", sys_data.data_cfg[CFG_VRSE_BIASED]);
        edit_uint(&sys_data.data_cfg[CFG_VRSE_BIASED], 0, 5);
        uprintf("sys_data.data_cfg[CFG_VRSE_BIASED_STD] [%u]: ", sys_data.data_cfg[CFG_VRSE_BIASED_STD]);
        edit_uint(&sys_data.data_cfg[CFG_VRSE_BIASED_STD], 0, 5);
        uprintf("sys_data.data_cfg[CFG_V_COUNTER_ELECT] [%u]: ", sys_data.data_cfg[CFG_V_COUNTER_ELECT]);
        edit_uint(&sys_data.data_cfg[CFG_V_COUNTER_ELECT], 0, 5);
        uprintf("sys_data.data_cfg[CFG_V_COUNTER_ELECT_STD] [%u]: ", sys_data.data_cfg[CFG_V_COUNTER_ELECT_STD]);
        edit_uint(&sys_data.data_cfg[CFG_V_COUNTER_ELECT_STD], 0, 5);
        uprintf("sys_data.data_cfg[CFG_I_COUNTER] [%u]: ", sys_data.data_cfg[CFG_I_COUNTER]);
        edit_uint(&sys_data.data_cfg[CFG_I_COUNTER], 0, 5);
        uprintf("sys_data.data_cfg[CFG_I_SUBSTRATE] [%u]: ", sys_data.data_cfg[CFG_I_SUBSTRATE]);
        edit_uint(&sys_data.data_cfg[CFG_I_SUBSTRATE], 0, 5);
        uprintf("sys_data.data_cfg[CFG_VIN_BAT_VOLT] [%u]: ", sys_data.data_cfg[CFG_VIN_BAT_VOLT]);
        edit_uint(&sys_data.data_cfg[CFG_VIN_BAT_VOLT], 0, 5);
        uprintf("sys_data.data_cfg[CFG_BIAS_BAT_POS] [%u]: ", sys_data.data_cfg[CFG_BIAS_BAT_POS]);
        edit_uint(&sys_data.data_cfg[CFG_BIAS_BAT_POS], 0, 5);
        uprintf("sys_data.data_cfg[CFG_BIAS_BAT_NEG] [%u]: ", sys_data.data_cfg[CFG_BIAS_BAT_NEG]);
        edit_uint(&sys_data.data_cfg[CFG_BIAS_BAT_NEG], 0, 5);
        uprintf("sys_data.data_cfg[CFG_BRD_TEMP] [%u]: ", sys_data.data_cfg[CFG_BRD_TEMP]);
        edit_uint(&sys_data.data_cfg[CFG_BRD_TEMP], 0, 5);
        uprintf("sys_data.data_cfg[CFG_BRD_HUMIDITY] [%u]: ", sys_data.data_cfg[CFG_BRD_HUMIDITY]);
        edit_uint(&sys_data.data_cfg[CFG_BRD_HUMIDITY], 0, 5);
        uprintf("sys_data.data_cfg[CFG_OPTODE] [%u]: ", sys_data.data_cfg[CFG_OPTODE]);
        edit_uint(&sys_data.data_cfg[CFG_OPTODE], 0, 5);
        uprintf("sys_data.data_cfg[CFG_CTD] [%u]: ", sys_data.data_cfg[CFG_CTD]);
        edit_uint(&sys_data.data_cfg[CFG_CTD], 0, 5);
        uprintf("sys_data.data_cfg[CFG_SPARE1] [%u]: ", sys_data.data_cfg[CFG_SPARE1]);
        edit_uint(&sys_data.data_cfg[CFG_SPARE1], 0, 5);
        uprintf("sys_data.data_cfg[CFG_SPARE2] [%u]: ", sys_data.data_cfg[CFG_SPARE2]);
        edit_uint(&sys_data.data_cfg[CFG_SPARE2], 0, 5);
        uprintf("sys_data.data_cfg[CFG_SPARE3] [%u]: ", sys_data.data_cfg[CFG_SPARE3]);
        edit_uint(&sys_data.data_cfg[CFG_SPARE3], 0, 5);
        uprintf("sys_data.data_cfg[CFG_SPARE4] [%u]: ", sys_data.data_cfg[CFG_SPARE4]);
        edit_uint(&sys_data.data_cfg[CFG_SPARE4], 0, 5);
        uprintf("sys_data.data_cfg[CFG_SPARE5] [%u]: ", sys_data.data_cfg[CFG_SPARE5]);
        edit_uint(&sys_data.data_cfg[CFG_SPARE5], 0, 5);
        uprintf("sys_data.data_cfg[CFG_SPARE6] [%u]: ", sys_data.data_cfg[CFG_SPARE6]);
        edit_uint(&sys_data.data_cfg[CFG_SPARE6], 0, 5);
        //uprintf("sys_data.data_cfg[] [%u]: ", sys_data.data_cfg[]);
        //edit_uint(&sys_data.data_cfg[], 0, 5);

        response = yesOrNoMenuChoice("\n\nRe-visit? (Y/N) [N]? ", NO);
        if(response == NO)
            break;
    }
    //----
    while(1)
    {
        uprintf("\nSixth batch of 8 sys_data fields:\n\n");

        // app cfg   MAX_NUMOF_APP_CONFIG_FIELDS
        uprintf("app_cfg[APPCFG_SAMPLING_DIAG] [%u]: ", sys_data.app_cfg[APPCFG_SAMPLING_DIAG]);
        edit_uint(&sys_data.app_cfg[APPCFG_SAMPLING_DIAG], 0, 500000);

        uprintf("app_cfg[APPCFG_TEMPC_SH_ALG] [%u]: ", sys_data.app_cfg[APPCFG_TEMPC_SH_ALG]);
        edit_uint(&sys_data.app_cfg[APPCFG_TEMPC_SH_ALG], 0, 500);

        uprintf("app_cfg[APPCFG_MICROSD_ENABLE] [%u]: ", sys_data.app_cfg[APPCFG_MICROSD_ENABLE]);
        edit_uint(&sys_data.app_cfg[APPCFG_MICROSD_ENABLE], 0, 5);
        microSD_flg = sys_data.app_cfg[APPCFG_MICROSD_ENABLE];

        uprintf("app_cfg[APPCFG_MENU_LEVEL] [%u]: ", sys_data.app_cfg[APPCFG_MENU_LEVEL]);
        edit_uint(&sys_data.app_cfg[APPCFG_MENU_LEVEL], 0, 500);

        uprintf("app_cfg[APPCFG_SPARE4] [%u]: ", sys_data.app_cfg[APPCFG_SPARE4]);
        edit_uint(&sys_data.app_cfg[APPCFG_SPARE4], 0, 500);

        uprintf("app_cfg[APPCFG_SPARE5] [%u]: ", sys_data.app_cfg[APPCFG_SPARE5]);
        edit_uint(&sys_data.app_cfg[APPCFG_SPARE5], 0, 500);

        uprintf("app_cfg[APPCFG_SPARE6] [%u]: ", sys_data.app_cfg[APPCFG_SPARE6]);
        edit_uint(&sys_data.app_cfg[APPCFG_SPARE6], 0, 500);

        uprintf("app_cfg[APPCFG_SPARE7] [%u]: ", sys_data.app_cfg[APPCFG_SPARE7]);
        edit_uint(&sys_data.app_cfg[APPCFG_SPARE7], 0, 500);

        response = yesOrNoMenuChoice("\n\nRe-visit? (Y/N) [N]? ", NO);
        if(response == NO)
            break;
    }
    //----

    while(1)
    {
        uprintf("\nSeventh batch of 14 sys_data fields:\n\n");

        uprintf("GMT [%u]: ", sys_data.GMT);
        edit_uint(&sys_data.GMT, 0, 80);

        uprintf("sample_aligned [%u]: ", sys_data.sample_aligned);
        edit_uint(&sys_data.sample_aligned, 0, 80);

        sys_data.deploy_time = 0;
        uprintf("deploy_time [%u]: ", sys_data.deploy_time);
        edit_uint(&sys_data.deploy_time, 0, 80);

        uprintf("diag [%u]: ", sys_data.diag);
        edit_uint(&sys_data.diag, 0, 80);

        uprintf("debug_flag [%u]: ", sys_data.debug_flag);
        edit_uint(&sys_data.debug_flag, 0, 80);
        dbg_flag = sys_data.debug_flag;


        uprintf("pHOffset [%.6f]: ", sys_data.pHOffset);
        edit_float(&sys_data.pHOffset, -1.0, 1.0);

        uprintf("pHSlope [%.6f]: ", sys_data.pHSlope);
        edit_float(&sys_data.pHSlope, -1.0, 1.0);

        uprintf("TCSlope [%.6f]: ", sys_data.TCSlope);
        edit_float(&sys_data.TCSlope, -1.0, 1.0);

        uprintf("sensor_ctd_TC [%.6f]: ", sys_data.sensor_ctd_TC);
        edit_float(&sys_data.sensor_ctd_TC, -1.0, 1.0);

        uprintf("sensor_optode_TC [%.6f]: ", sys_data.sensor_optode_TC);
        edit_float(&sys_data.sensor_optode_TC, -1.0, 1.0);

        uprintf("spare[0] [%.6f]: ", sys_data.spare[0]);
        edit_float(&sys_data.spare[0], -1.0, 1.0);

        uprintf("spare[1] [%.6f]: ", sys_data.spare[1]);
        edit_float(&sys_data.spare[1], -1.0, 1.0);

        uprintf("spare[2] [%.6f]: ", sys_data.spare[2]);
        edit_float(&sys_data.spare[2], -1.0, 1.0);

        uprintf("spare[3] [%.6f]: ", sys_data.spare[3]);
        edit_float(&sys_data.spare[3], -1.0, 1.0);


        response = yesOrNoMenuChoice("\n\nRe-visit? (Y/N) [N]? ", NO);
        if(response == NO)
            break;
    }
    //----

    while(1)
    {
        uprintf("\nEighth batch of 11 sys_data fields:\n\n");

        uprintf("Filename [%s]: ", sys_data.fileName);
        edit_string(&sys_data.fileName, MAX_FILENAME);
        //uprintf("  %s\n\n", sys_data.fileName);

        uprintf("User Initials [%s]: ", sys_data.user);
        edit_string(&sys_data.user, MAX_USER_INITIALS);

        uprintf("board_SN [%s]: ", sys_data.board_SN);
        edit_string(&sys_data.board_SN, MAX_BOARD_SN);

        uprintf("mcap_SN [%s]: ", sys_data.mcap_SN);
        edit_string(&sys_data.mcap_SN, MAX_MCAP_SN);

        uprintf("durafet_SN [%s]: ", sys_data.durafet_SN);
        edit_string(&sys_data.durafet_SN, MAX_DURAFET_SN);

        uprintf("package_name [%s]: ", sys_data.package_name);
        edit_string(&sys_data.package_name, MAX_PACKAGE_NAME);

        uprintf("optode_SN [%s]: ", sys_data.optode_SN);
        edit_string(&sys_data.optode_SN, MAX_OPTODE_SN);

        uprintf("ctd_SN [%s]: ", sys_data.ctd_SN);
        edit_string(&sys_data.ctd_SN, MAX_CTD_SN);

        uprintf("pump_SN [%s]: ", sys_data.pump_SN);
        edit_string(&sys_data.pump_SN, MAX_PUMP_SN);

        uprintf("board_fab_note [%s]: ", sys_data.board_fab_note);
        edit_string(&sys_data.board_fab_note, MAX_BOARD_FABNOTE);

        uprintf("deploy_note [%s]: ", sys_data.deploy_note);
        edit_string(&sys_data.deploy_note, MAX_DEPLOY_NOTE);

        uprintf("\nOld firmware version: %s\n", sys_data.qa.firmware_version);
        uprintf("New firmware version: %s\n", VERSION);
        strcpy(sys_data.qa.firmware_version, VERSION);

        uprintf("\nIf the old firmware version is corrupt or shifted, please re-run QA routine\n\n");

        response = yesOrNoMenuChoice("\n\nRe-visit? (Y/N) [N]? ", NO);
        if(response == NO)
            break;
    }

    uprintf("\nEEPROM sys_data copy/edit is complete\n\n");

    //uprintf(" [%s]: ", sys_data.);
    //edit_string(&sys_data., MAX);

    //uprintf(" [%.6f]: ", sys_data.);
    //edit_float(&sys_data., -1.0, 1.0);

    //uprintf(" [%u]: ", sys_data.);
    //edit_uint(&sys_data., 0, 80);
    //print_sys_data_structure();

    storeSysDataVariables();

}
