
//******************************************************************************
// Root Menu Routines
//******************************************************************************
//#ROOT
void RootMenu(void)
{
    char cmd;
    short qret = TRUE;

    printf("\n/* MBARI Sensor Controller Root Menu [H] for Help */");
    fflush(stdout);

    while (qret == TRUE) {
        printf("\n");
        qret = QueryChar("MSC>", NULL, "^[?ABCDEFH", &cmd);
        printf("\n");

        if (qret == TRUE) {
            switch (cmd) {
            case '^[':
                break;

            case 'A':
                SetupMenu();
                break;

            case 'B':
                DataMenu();
                break;

            case 'C':
                FileMenu();
                break;

            case 'D':
                TestMenu();
                break;

            case 'E':
                InfoMenu();
                break;

            case 'F':
                if (SysPass() == TRUE) {        //make it bozo proof
                    if (EcoefData != NULL)      //free any allocated memory
                        free_dmatrix(EcoefData, 1L, 256L, 1L, 3L);
                    if (EcoefFile != NULL)
                        free_dmatrix(EcoefFile, 1L, 256L, 1L, 3L);
                    if (DarkData != NULL)
                        free(DarkData);
                    if (SpecData != NULL)
                        free(SpecData);
                    IOPwrDown();
                    SCIRxSetBuffered(false);
                    BIOSResetToPicoDOS();       //jump to PicoDOS Command Shell
                }
                break;

            case '?':
            case 'H':
                printf(" %s\n %s\n %s\n %s\n %s\n %s\n",
                       "[A] Setup Menu",
                       "[B] Data  Menu",
                       "[C] File  Menu",
                       "[D] Test  Menu", "[E] Info  Menu", "[F] PicoDOS");
                break;

            }                   //end switch

        }                       //end qret if

    }                           //end while
}

//******************************************************************************
// Setup Menu Routines
//******************************************************************************
//#SETUP
void SetupMenu(void)
{

    char cmd;
    short qret = TRUE;

    printf("\n/* MBARI Sensor Controller Setup Menu [H] for Help */");

    while (qret == TRUE) {
        printf("\n");
        qret = QueryChar("MSC_SETUP>", NULL, "^[?ABCDH", &cmd);
        printf("\n");

        if (qret == TRUE) {
            switch (cmd) {
            case '^[':
                break;

            case 'A':
                IsusSetup();
                break;

            case 'B':
                DuraSetup();
                break;

            case 'C':
                LoggerSetup();
                break;

            case 'D':
                RtcSetup();
                break;

            case '?':
            case 'H':
                printf(" %s\n %s\n %s\n %s\n",
                       "[A] ISUS   Setup",
                       "[B] DURA   Setup",
                       "[C] Logger Setup", "[D] Clock  Setup");
                break;

            }                   //end switch

        }                       //end qret if

    }                           //end while

}

void IsusSetup(void)
{
    char cmd;
    short qret = TRUE;

    printf("\n/* ISUS Sensor Setup Menu <H> for Help */");

    while (qret == TRUE) {
        printf("\n");
        qret = QueryChar("MSC_SETUP_ISUS>", NULL, "^[ABCDEH", &cmd);
        printf("\n");

        if (qret == TRUE) {
            switch (cmd) {
            case '^[':
                break;
				
	        case 'A':
                printf("\n  ISUS Error Counter =  %lu\n", isus_error_ctr);
                printf("\n  Last ISUS Error Event = %d at %s",
                       isus_last_error, ctime(&isus_error_time));
                if (QueryYesNo("\n  Reset", FALSE)) {
                    if (SysPass() == TRUE) {
                        isus_last_error = 0;
                        DS3234_WrSRAM(ISUS_LASTERROR_PTR, sizeof(int),
                                      (int *) &isus_last_error);
                        isus_error_ctr = isus_error_time = 0UL;
                        DS3234_WrSRAM(ISUS_ERRORCTR_PTR, sizeof(ulong),
                                      (ulong *) &isus_error_ctr);
                        DS3234_WrSRAM(ISUS_LASTERRTIME_PTR, sizeof(ulong),
                                      (ulong *) &isus_error_time);
                    }
                }
                break;

            case 'B':
                printf("\n  ISUS Sample Counter =  %lu\n",
                       isus_sample_ctr);
                printf("\n  Last Sample Time    =  %s",
                       ctime(&isus_sample_time));
                if (QueryYesNo("\n  Reset", FALSE)) {
                    if (SysPass() == TRUE) {
                        isus_sample_ctr = 0UL;
                        DS3234_WrSRAM(ISUS_SAMPLECTR_PTR, sizeof(ulong),
                                      (ulong *) &isus_sample_ctr);
                        isus_sample_time = 0UL;
                        DS3234_WrSRAM(ISUS_LASTSAMPTIME_PTR, sizeof(ulong),
                                      (ulong *) &isus_sample_time);
                    }
                }
                break;

            case 'C':
                IsusSpecSetup();
                break;

            case 'D':
                IsusLampSetup();
                break;

            case 'E':
                IsusFitSetup();
                break;

            case 'H':
                printf(" %s\n %s\n %s\n %s\n %s\n",
				       "[A] ISUS Error  Counter",
                       "[B] ISUS Sample Counter",
                       "[C] Spectrometer Setup",
                       "[D] Fiberlite Lamp Setup",
                       "[E] Curve Fitting Setup");
                break;

            }                   //end switch

        }                       //end qret if

    }                           //end while        

}

void IsusSpecSetup(void)
{
    char ebuf[ERRBUFSIZE];
    char cmd;
    int i;
    short qret = TRUE;
    ulong input_var;

    printf("\n/* ISUS Spectrometer Setup Menu <H> for Help */");

    while (qret == TRUE) {
        printf("\n");
        qret = QueryChar("MSC_SETUP_ISUS_SPEC>", NULL, "^[ABCDEH", &cmd);
        printf("\n");

        if (qret == TRUE) {
            switch (cmd) {
            case '^[':
                break;

            case 'A':
                printf("\n  Spectrometer Integration Period (ms) =  %lu\n",
                       isus_spec_period);
                if (QueryYesNo("\n  Modify", FALSE)) {
                    input_var = isus_spec_period;
                    QueryNum("\n  Enter integration period (ms)", "%lu",
                             "%lu", &input_var);
                    if (input_var != isus_spec_period) {
                        if ((input_var >= 4) && (input_var <= 4000)) {
                            isus_spec_period = input_var;
                            DS3234_WrSRAM(ISUS_PERIOD_PTR, sizeof(ulong),
                                          (ulong *) & isus_spec_period);
                        } else
                            RangeError(4L, 4000L);
                    }
                }               //end modify if
                break;

            case 'B':
                printf("\n  Number of Spectra Scans per sample = %hu\n",
                       isus_scan_num);
                printf("\n  [no effect in continous sampling mode]\n");
                if (QueryYesNo("\n  Modify", FALSE)) {
                    input_var = isus_scan_num;
                    QueryNum("\n  Enter number of spectra scans", "%lu",
                             "%lu", &input_var);
                    if (input_var != isus_scan_num) {
                        if ((input_var >= 1) && (input_var <= 200)) {
                            isus_scan_num = (ushort) input_var;
                            free(SpecData);
                            SpecData =
                                calloc((size_t) (256 * isus_scan_num),
                                       sizeof(ushort));
                            if (SpecData == NULL) {
                                printf
                                    ("CAN'T allocate ScanData Array memory\n");
                                sprintf(ebuf,
                                        "CAN'T allocate ScanData Array memory");
                                LogErrorMsg(ebuf, (int) MALLOC_FAILURE,
                                            sys_message_enable);
                                DS3234_RdSRAM(ISUS_INTSCANS_PTR,
                                              sizeof(ushort),
                                              (ushort *) & isus_scan_num);
                            } else
                                DS3234_WrSRAM(ISUS_INTSCANS_PTR,
                                              sizeof(ushort),
                                              (ushort *) & isus_scan_num);
                        } else
                            RangeError(1L, 200L);
                    }
                }               //end modify if
                break;

            case 'C':
                printf
                    ("\n  Number of Dark Current Scans per sample  = %hu\n",
                     isus_dark_num);
                if (QueryYesNo("\n  Modify", FALSE)) {
                    input_var = isus_dark_num;
                    QueryNum("\n  Enter number of dark current scans",
                             "%lu", "%lu", &input_var);
                    if (input_var != isus_dark_num) {
                        if ((input_var >= 1) && (input_var <= 200)) {
                            isus_dark_num = (ushort) input_var;
                            free(DarkData);
                            DarkData =
                                calloc((size_t) (256 * isus_dark_num),
                                       sizeof(ushort));
                            if (DarkData == NULL) {
                                printf
                                    ("CAN'T allocate DarkScanData Array memory\n");
                                sprintf(ebuf,
                                        "CAN'T allocate DarkScanData Array memory");
                                LogErrorMsg(ebuf, (int) MALLOC_FAILURE,
                                            sys_message_enable);
                                DS3234_RdSRAM(ISUS_DCSCANS_PTR,
                                              sizeof(ushort),
                                              (ushort *) & isus_dark_num);
                            } else
                                DS3234_WrSRAM(ISUS_DCSCANS_PTR,
                                              sizeof(ushort),
                                              (ushort *) & isus_dark_num);
                        } else
                            RangeError(1L, 200L);
                    }
                }               //end modify if
                break;

            case 'D':
                printf
                    ("\n  Dark Current Data Rate = %lu spectra scans per DC reading\n",
                     isus_scans_per_dc);
                printf("\n  [only relative when continously sampling]\n");
                if (QueryYesNo("\n  Modify", FALSE)) {
                    input_var = isus_scans_per_dc;
                    QueryNum
                        ("\n  Enter number of spectra scans per dark current reading",
                         "%lu", "%lu", &input_var);
                    if (input_var != isus_scans_per_dc) {
                        if ((input_var >= 1) && (input_var <= 10000)) {
                            isus_scans_per_dc = input_var;
                            DS3234_WrSRAM(ISUS_SAMPSPERDC_PTR,
                                          sizeof(ulong),
                                          (ulong *) & isus_scans_per_dc);
                        } else
                            RangeError(1L, 10000L);
                    }
                }               //end modify if
                break;

            case 'E':
                printf("\n  Current Coefs:\n");
                for (i = 0; i < 5; i++) {
                    printf("  %e\n", ZeissCoefs[i]);
                }
                if (QueryYesNo("\n  Reload", FALSE)) {
                    Zcoefs = LoadIsusZCoefs(ZeissCoefs);
                    if (Zcoefs > 0) {
                        printf
                            ("\n  %d Coefs found, Wavelength Array Initialized\n",
                             Zcoefs);
                        InitLambdaArray(LambdaData, ZeissCoefs);
                    } else
                        printf
                            ("\n  Coef File (ZCOEFDAT.CAL) could NOT be loaded!\n");
                }
                break;

            case 'H':
                printf(" %s\n %s\n %s\n %s\n %s\n",
                       "[A] Integration Period",
                       "[B] Spectra Scans",
                       "[C] DarkCurrent Scans",
                       "[D] Collection Rate of DC Data",
                       "[E] Reload Zeiss Coef File");
                break;

            }                   //end switch

        }                       //end qret if

    }                           //end while

    printf("\n");
}

void IsusLampSetup(void)
{
    char cmd;
    uchar rtc_cfg, rtc_stat;
    ushort rtc_ctr;
    short qret = TRUE;
    ulong input_var;

    printf("\n/* ISUS Fiberlite Lamp Setup Menu <H> for Help */");

    while (qret == TRUE) {
        printf("\n");
        qret = QueryChar("MSC_SETUP_ISUS_LAMP>", NULL, "^[ABCDEFGH", &cmd);
        printf("\n");

        if (qret == TRUE) {
            switch (cmd) {
            case '^[':
                break;

            case 'A':
                DS1683_RdElapsedTimer(&isus_lamp_elp);
                hdiv = ldiv(isus_lamp_elp, 3600L);
                mdiv = ldiv(hdiv.rem, 60L);
                printf
                    ("\n  Fiberlite Odometer = %02ld:%02ld:%02ld = %lu secs\n",
                     hdiv.quot, mdiv.quot, mdiv.rem, isus_lamp_elp);

                if (QueryYesNo("\n  Reset", FALSE)) {
                    if (SysPass() == TRUE) {
                        input_var = 0;
                        QueryNum("\n  Enter Elapsed Lamp On Time (hours)",
                                 "%lu", "%lu", &input_var);
                        if ((input_var >= 0) && (input_var <= 1200)) {
                            isus_lamp_elp = input_var * 3600L;
                            DS1683_WrElapsedTimer(&isus_lamp_elp);
                        } else
                            RangeError(0L, 1200L);
                    }           //end IsusPass if
                }
                break;

            case 'B':
                DS1683_RdEventCounter(&isus_lamp_evt);
                printf("\n  Fiberlite Lamp Enable Event Counter = %hu\n",
                       isus_lamp_evt);

                if (QueryYesNo("\n  Reset", FALSE)) {
                    if (SysPass() == TRUE) {
                        rtc_ctr = 0;
                        DS1683_WrEventCounter(&rtc_ctr);
                    }
                }
                break;

            case 'C':
                DS1683_ReadReg(DS1683_CONFIG_REG, &rtc_cfg);
                DS1683_ReadReg(DS1683_STATUS_REG, &rtc_stat);
                DisplayByteVal("DS1683 Config Reg Val", rtc_cfg);
                DisplayByteVal("DS1683 Status Reg Val", rtc_stat);
                break;

           case 'D' :
                    printf("\n  Max Pixel Intensity = Lamp ON (A/D cts) %hu\n", isus_ref_limit);
                    if( QueryYesNo("\n  Modify", FALSE) ){
                      input_var = isus_ref_limit;
                      QueryNum("\n  Enter Intensity Threshold (A/D cts)", "%lu", "%lu", &input_var);
                      if(input_var != isus_ref_limit){
                        if( (input_var >= 4500) && (input_var <= 35000) ){
                          isus_ref_limit =  (ushort)input_var;
                          DS3234_WrSRAM(ISUS_REFLIMIT_PTR, sizeof(ushort), (ushort*)&isus_ref_limit);
                        }
                        else
                          RangeError(4500L, 35000L);
                        }
                      } //end modify if

                 break;								
				
            case 'E':
                printf("\n  Fiberlite Warmup Period (sec) =  %hu\n",
                       isus_warm_lamp);
                if (QueryYesNo("\n  Modify", FALSE)) {
                    input_var = isus_warm_lamp;
                    QueryNum("\n  Enter warmup period (sec)", "%lu", "%lu",
                             &input_var);
                    if (input_var != isus_warm_lamp) {
                        if ((input_var >= 0) && (input_var <= 10)) {
                            isus_warm_lamp = (ushort) input_var;
                            DS3234_WrSRAM(ISUS_LAMPWARM_PTR,
                                          sizeof(ushort),
                                          (ushort *) & isus_warm_lamp);
                        } else
                            RangeError(0L, 600L);
                    }
                }               //end modify if
                break;

            case 'F':
                printf("\n  Fiberlite Shutter is ");
                if (isus_warm_shut == TRUE)
                    printf("OPEN");
                else
                    printf("CLOSED");
                printf(" during Warmup\n");
                if (QueryYesNo("\n  Modify", FALSE)) {
                    input_var = isus_warm_shut;
                    QueryNum("\n  Enter [1] Open or [0] Closed", "%lu",
                             "%lu", &input_var);
                    if (input_var != isus_warm_shut) {
                        if ((input_var >= 0) && (input_var <= 1)) {
                            isus_warm_shut = (uchar) input_var;
                            DS3234_WrSRAM(ISUS_WARMSHUT_PTR, sizeof(uchar),
                                          (uchar *) & isus_warm_shut);
                        } else
                            RangeError(0L, 1L);
                    }
                }               //end modify if
                break;
				
				
             case 'G':
                printf
                    ("\n  Number of Samples per UV cleaning cycle   = %lu\n",
                     isus_samps_per_clean);
                printf
                    ("\n  Total Lamp ON time for UV cleaning (secs) = %hu\n",
                     isus_clean_lamp);
                if (QueryYesNo("\n  Modify", FALSE)) {
                    input_var = isus_samps_per_clean;
                    QueryNum
                        ("\n  Enter number of samples per cleaning cycle",
                         "%lu", "%lu", &input_var);
                    if (input_var != isus_samps_per_clean) {
                        if ((input_var >= 1) && (input_var <= 100000)) {
                            isus_samps_per_clean = input_var;
                            DS3234_WrSRAM(ISUS_SAMPSPERUV_PTR,
                                          sizeof(ulong),
                                          (ulong *) &
                                          isus_samps_per_clean);
                        } else
                            RangeError(1L, 100000L);
                    }

                    input_var = isus_clean_lamp;
                    QueryNum("\n  Enter lamp ON time for cleaning period",
                             "%lu", "%lu", &input_var);
                    if (input_var != isus_clean_lamp) {
                        if ((input_var >= 1) && (input_var <= 300)) {
                            isus_clean_lamp = (ushort) input_var;
                            DS3234_WrSRAM(ISUS_CLEANUV_PTR, sizeof(ushort),
                                          (ushort *) & isus_clean_lamp);
                        } else
                            RangeError(1L, 300L);
                    }
                }               //end modify if
                break;


            case 'H':
                printf(" %s\n %s\n %s\n %s\n %s\n %s\n",
                       "[A] Lamp RTC Total On Time Odometer",
                       "[B] Lamp RTC Enable Events Counter",
                       "[C] Lamp RTC Status/Config Register",
                       "[D] Intensity Threshold for Lamp ON",
                       "[E] Power On Warmup Period",
                       "[F] Shutter during Warmup",
                       "[G] UV \"Cleaning\" Cycle");

                break;

            }                   //end switch

        }                       //end qret if

    }                           //end while

    printf("\n");

}

void IsusFitSetup(void)
{
    char cmd;
    int i;
    short qret = TRUE;
    int lambda_beg, lambda_end;
    ushort new_beg, old_beg, new_end, old_end;
    ulong input_var;
    float float_var;

    printf("\n/* ISUS Curve Fitting Setup Menu <H> for Help */");

    while (qret == TRUE) {
        printf("\n");
        qret =
            QueryChar("MSC_SETUP_ISUS_CFIT>", NULL, "^[ABCDEFGHI", &cmd);
        printf("\n");

        if (qret == TRUE) {
            switch (cmd) {
            case '^[':
                break;

            case 'A':
                if (isus_pixel_beg > 255)       //just in case!
                    isus_pixel_beg = (ushort) ISUS_PIXELBEG_VAL;
                if (isus_pixel_end > 255)
                    isus_pixel_end = (ushort) ISUS_PIXELEND_VAL;

                printf
                    ("\n  Pixel Fitting Range (pixel)  = %03hu - %03hu\n",
                     isus_pixel_beg, isus_pixel_end);
                if ((Zcoefs > 0) || (Ecoefs == 256))    //wavelength array SHOULD be loaded
                    printf
                        ("\n  Lambda Fitting Range (nm)    = %.3lf - %.3lf\n",
                         LambdaData[isus_pixel_beg],
                         LambdaData[isus_pixel_end]);
                if (QueryYesNo("\n  Modify", FALSE)) {
                    input_var = 0;
                    QueryNum
                        ("\n  Select Input Range:\n  [1] By Wavelength\n  [2] By Pixel\n  Enter>",
                         "%lu", "%lu", &input_var);
                    if (input_var == 1) {
                        if ((Zcoefs > 0) || (Ecoefs == 256)) {  //wavelength info SHOULD be loaded
                            old_beg = isus_pixel_beg;
                            old_end = isus_pixel_end;
                            input_var =
                                (long) (LambdaData[isus_pixel_beg] + 0.5);
                            QueryNum("\n  Enter starting wavelength",
                                     "%lu", "%lu", &input_var);
                            if (input_var !=
                                (long) (LambdaData[isus_pixel_beg] +
                                        0.5)) {
                                if ((input_var >= 210)
                                    && (input_var <= 300)) {
                                    lambda_beg = (int) input_var;
                                    MatchPixelsToLambda(LambdaData,
                                                        lambda_beg, 300,
                                                        &new_beg,
                                                        &new_end);
                                    isus_pixel_beg = new_beg;
                                } else {
                                    RangeError(210L, 300L);
                                    break;
                                }
                            }
                            input_var =
                                (long) (LambdaData[isus_pixel_end] + 0.5);
                            QueryNum("\n  Enter ending wavelength", "%lu",
                                     "%lu", &input_var);
                            if (input_var !=
                                (long) (LambdaData[isus_pixel_end] +
                                        0.5)) {
                                if ((input_var >= 210)
                                    && (input_var <= 300)) {
                                    lambda_end = (int) input_var;
                                    MatchPixelsToLambda(LambdaData, 210,
                                                        lambda_end,
                                                        &new_beg,
                                                        &new_end);
                                    isus_pixel_end = new_end;
                                } else {
                                    RangeError(210L, 300L);
                                    break;
                                }
                            }
                            if ((isus_pixel_end - isus_pixel_beg) + 1 <=
                                10) {
                                printf
                                    ("\n  Fitting Range must be at least 10 pixels wide!\n");
                                isus_pixel_beg = old_beg;
                                isus_pixel_end = old_end;
                                break;
                            }

                            DS3234_WrSRAM(ISUS_PIXELBEG_PTR,
                                          sizeof(ushort),
                                          (ushort *) & isus_pixel_beg);
                            DS3234_WrSRAM(ISUS_PIXELEND_PTR,
                                          sizeof(ushort),
                                          (ushort *) & isus_pixel_end);
                            printf
                                ("\n\n  New Lambda Fitting Range = %.3lf - %.3lf\n",
                                 LambdaData[isus_pixel_beg],
                                 LambdaData[isus_pixel_end]);

                        }       //end Zcoef Ecoef if

                        else
                            printf
                                ("\nNeither Zcoefs nor Ecoefs are loaded!\n");

                    }           //end input_var == 1 if

                    else if (input_var == 2) {
                        old_beg = isus_pixel_beg;
                        old_end = isus_pixel_end;

                        input_var = isus_pixel_beg;
                        QueryNum("\n  Enter starting pixel", "%lu", "%lu",
                                 &input_var);
                        if (input_var != isus_pixel_beg) {
                            if ((input_var >= 1) && (input_var <= 256)) {
                                isus_pixel_beg = (ushort) input_var;
                            } else {
                                RangeError(1L, 256L);
                                break;
                            }
                        }

                        input_var = isus_pixel_end;
                        QueryNum("\n  Enter ending   pixel", "%lu", "%lu",
                                 &input_var);
                        if (input_var != isus_pixel_end) {
                            if ((input_var >= 1) && (input_var <= 256)) {
                                isus_pixel_end = (ushort) input_var;
                            } else {
                                RangeError(1L, 256L);
                                break;
                            }
                        }

                        if ((isus_pixel_end - isus_pixel_beg) + 1 <= 10) {
                            printf
                                ("\n  Fitting Range must be at least 10 pixels wide!\n");
                            isus_pixel_beg = old_beg;
                            isus_pixel_end = old_end;
                            break;
                        }

                        DS3234_WrSRAM(ISUS_PIXELBEG_PTR, sizeof(ushort),
                                      (ushort *) & isus_pixel_beg);
                        DS3234_WrSRAM(ISUS_PIXELEND_PTR, sizeof(ushort),
                                      (ushort *) & isus_pixel_end);

                        if ((Zcoefs > 0) || (Ecoefs == 256))    //wavelength info is available
                            printf
                                ("\n\n  New Lambda Fitting Range = %.3lf - %.3lf\n",
                                 LambdaData[isus_pixel_beg],
                                 LambdaData[isus_pixel_end]);

                    }           //end input_var == 2 if

                    else
                        RangeError(1L, 2L);

                }               //end modify if
                break;

            case 'B':
                if ((Zcoefs > 0) || (Ecoefs == 256)) {  //wavelength info SHOULD be loaded
                    input_var = 0;
                    QueryNum("\n  Enter pixel number ", "%lu", "%lu",
                             &input_var);
                    if (input_var != 0) {
                        if ((input_var >= 1) && (input_var <= 256))
                            printf("\n  Lambda at pixel #%lu is %.3fnm\n",
                                   input_var, LambdaData[input_var]);
                        else
                            printf("\n  Invalid pixel\n");
                    }
                } else
                    printf("\nNeither Zcoefs nor Ecoefs are loaded!\n");
                break;

            case 'C':
                printf("\n  Concentrations to be fitted = %hu\n",
                       isus_fit_concs);
                if (QueryYesNo("\n  Modify", FALSE)) {
                    input_var = isus_fit_concs;
                    QueryNum("\n  Enter conc number", "%lu", "%lu",
                             &input_var);
                    if (input_var != isus_fit_concs) {
                        if ((input_var >= 2) && (input_var <= 3)) {
                            isus_fit_concs = (ushort) input_var;
                            DS3234_WrSRAM(ISUS_FITCONCS_PTR,
                                          sizeof(ushort),
                                          (ushort *) & isus_fit_concs);
                        } else
                            RangeError(2L, 3L);
                    }
                }               //end modify if
                break;

            case 'D':
                if (isus_bsl_model == 1)
                    printf("\n  Baseline Model = (ax + b)\n");
                else if (isus_bsl_model == 2)
                    printf("\n  Baseline Model = (ax^2 + bx + c)\n");
                if (QueryYesNo("\n  Modify", FALSE)) {
                    input_var = isus_bsl_model;
                    QueryNum
                        ("\n  Select Model:\n  [1] (ax +b)\n  [2] (ax^2+bx+c)\n  Enter>",
                         "%lu", "%lu", &input_var);
                    if (input_var != isus_bsl_model) {
                        if ((input_var >= 1) && (input_var <= 2)) {
                            isus_bsl_model = (ushort) input_var;
                            DS3234_WrSRAM(ISUS_BSLNMODL_PTR,
                                          sizeof(ushort),
                                          (ushort *) & isus_bsl_model);
                        } else
                            RangeError(1L, 2L);
                    }
                }               //end modify if
                break;


            case 'E':
                printf
                    ("\n  Bromide Temperature Compensation is currently ");
                if (isus_tcomp_mode == FALSE)
                    printf("DISABLED\n");
                else
                    printf("ENABLED\n");
                if (QueryYesNo("\n  Modify", FALSE)) {
                    input_var = isus_tcomp_mode;
                    QueryNum
                        ("\n  Selection:\n   [0] Disable\n   [1] Enable\n  Enter>",
                         "%lu", "%lu", &input_var);
                    if (input_var != isus_tcomp_mode) {
                        if ((input_var >= 0) && (input_var <= 1)) {
                            isus_tcomp_mode = (uchar) input_var;
                            DS3234_WrSRAM(ISUS_TCOMPMODE_PTR,
                                          sizeof(uchar),
                                          (uchar *) & isus_tcomp_mode);
                        } else
                            RangeError(0L, 1L);
                    }
                }               //end modify if
                if (QueryYesNo("\n  Display Tcoefs", FALSE)) {
                    printf("\n\nBr Tcomp Coefs\n");
                    for (i = 0; i < TCOEFS; i++)
                        printf("  %d, %+lf\n", i, TcompCoefs[i]);
                }
                break;

            case 'F':
                if (isus_tcomp_mode == FALSE)
                    printf
                        ("\n  WARNING, Bromide Temperature Compensation\n  must be ENABLED for Salinity to be FIXED!\n");
                printf("\n  ISUS Salinity value is currently ");
                if (isus_salfit_mode == FIX)
                    printf("FIXED\n");
                else
                    printf("FITTED\n");
                if (QueryYesNo("\n  Modify", FALSE)) {
                    input_var = isus_salfit_mode;
                    QueryNum
                        ("\n  Selection:\n   [0] Fix Value\n   [1] Fit Value\n  Enter>",
                         "%lu", "%lu", &input_var);
                    if (input_var != isus_salfit_mode) {
                        if ((input_var >= 0) && (input_var <= 1)) {
                            isus_salfit_mode = (uchar) input_var;
                            DS3234_WrSRAM(ISUS_SALFITMODE_PTR,
                                          sizeof(uchar),
                                          (uchar *) & isus_salfit_mode);
                        } else
                            RangeError(0L, 1L);
                    }
                }               //end modify if
                break;

            case 'G':
                float_var = no3_span;
                //DS3234_RdSRAM(ISUS_NO3SPAN_PTR, sizeof(float), (float*)&float_var);
                QueryFloat("\n  Enter Full Scale NO3 Conc", "%.2f", "%f",
                           &float_var);
                if (float_var != no3_span) {
                    if ((float_var >= 10.00) && (float_var <= 50.00)) {
                        no3_span = float_var;
                        DS3234_WrSRAM(ISUS_NO3SPAN_PTR, sizeof(float),
                                      (float *) &no3_span);
                    } else {
                        printf
                            ("\n  Invalid Entry, valid range = %.2f - %.2f\n",
                             10.00, 50.00);
                        break;
                    }
                }

                float_var = no3_offset;
                //DS3234_RdSRAM(ISUS_NO3OFFSET_PTR, sizeof(float), (float*)&float_var);
                QueryFloat("\n  Enter Zero Offset NO3 Conc", "%.2f", "%f",
                           &float_var);
                if (float_var != no3_offset) {
                    if ((float_var >= 0.10) && (float_var <= 5.00)) {
                        no3_offset = float_var;
                        DS3234_WrSRAM(ISUS_NO3OFFSET_PTR, sizeof(float),
                                      (float *) &no3_offset);
                    } else {
                        printf
                            ("\n  Invalid Entry, valid range = %.2f - %.2f\n",
                             0.10, 5.00);
                        break;
                    }
                }
                printf
                    ("\n  CTD Nitrate Conc Uploy (a0 + a1 * Volts) = -%.2f + %.4e * Volts\n",
                     no3_offset, ((no3_span + no3_offset) / 5.0));

                break;

            case 'I':
                printf("\n  Current Coefficient Vals Loaded = %d\n",
                       Ecoefs);
                if (QueryYesNo("\n  Display", FALSE)) {
                    printf("\n\n");
                    for (i = 1; i <= 256; i++) {
                        printf("%.5lf,%e,%e,%e,%lf\n", LambdaData[i],
                               EcoefFile[i][0], EcoefFile[i][1],
                               EcoefFile[i][2], ZeroData[i]);
                    }
                }
                if (QueryYesNo("\n  Reload", FALSE)) {
                    if (Zcoefs > 0) {
                        InitLambdaArray(LambdaData, ZeissCoefs);
                        Ecoefs =
                            LoadIsusECoefs(NULL, EcoefFile, ZeroData,
                                           TcompCoefs, &SwCalTemp,
                                           CalDate);
                        CopyEcoefs(EcoefFile, EcoefData);
                    } else {
                        Ecoefs =
                            LoadIsusECoefs(LambdaData, EcoefFile, ZeroData,
                                           TcompCoefs, &SwCalTemp,
                                           CalDate);
                        CopyEcoefs(EcoefFile, EcoefData);
                    }
                    if (Ecoefs == 256)
                        printf
                            ("\n  Cal Date = %s, Cal Temp = %.2lf, %d Coefs were loaded\n",
                             CalDate, SwCalTemp, Ecoefs);
                    else
                        printf
                            ("\n  Coef File could NOT be loaded!, Ret = %d\n",
                             Ecoefs);
                }
                break;

            case 'H':
                printf(" %s\n %s\n %s\n %s\n %s\n %s\n %s\n %s\n",
                       "[A] Fitting Range",
                       "[B] Pixel to Lambda",
                       "[C] Concentrations",
                       "[D] Baseline Model",
                       "[E] TempComp Mode",
                       "[F] Salinity Mode",
                       "[G] Analog Output", "[I] Reload Ecoefs/Zero File");
                break;

            }                   //end switch

        }                       //end qret if

    }                           //end while

    printf("\n");

}

void DuraSetup(void)
{
    char cmd;
    short qret = TRUE;
    ulong input_var;

    printf("\n/* DuraFET Sensor Setup Menu [H] for Help */");

    while (qret == TRUE) {
        printf("\n");
        qret = QueryChar("MSC_SETUP_DURA>", NULL, "^[?ABCDEFGH", &cmd);
        printf("\n");

        if (qret == TRUE) {
            switch (cmd) {
            case '^[':
                break;

            case 'A':
                printf("\n  DURA Error Counter =  %lu\n", dura_error_ctr);
                printf("\n  Last DURA Error Event = %d at %s",
                       dura_last_error, ctime(&dura_error_time));
                if (QueryYesNo("\n  Reset", FALSE)) {
                    if (SysPass() == TRUE) {
                        dura_last_error = 0;
                        DS3234_WrSRAM(DURA_LASTERROR_PTR, sizeof(int),
                                      (int *) &dura_last_error);
                        dura_error_ctr = dura_error_time = 0UL;
                        DS3234_WrSRAM(DURA_ERRORCTR_PTR, sizeof(ulong),
                                      (ulong *) & dura_error_ctr);
                        DS3234_WrSRAM(DURA_LASTERRTIME_PTR, sizeof(ulong),
                                      (ulong *) & dura_error_time);
                    }
                }
                break;

            case 'B':
                printf("\n  DURA Sample Counter =  %lu\n",
                       dura_sample_ctr);
                printf("\n  Last Sample Time    =  %s",
                       ctime(&dura_sample_time));
                if (QueryYesNo("\n  Reset", FALSE)) {
                    if (SysPass() == TRUE) {
                        dura_sample_ctr = 0UL;
                        DS3234_WrSRAM(DURA_SAMPLECTR_PTR, sizeof(ulong),
                                      (ulong *) & dura_sample_ctr);
                        dura_sample_time = 0UL;
                        DS3234_WrSRAM(DURA_LASTSAMPTIME_PTR, sizeof(ulong),
                                      (ulong *) & dura_sample_time);
                    }
                }
                break;

            case 'C':
                printf("\n  Number of AtoD Readings per Sample = %hu\n",
                       dura_atod_readings);
                if (QueryYesNo("\n  Modify", FALSE)) {
                    input_var = dura_atod_readings;
                    QueryNum("\n  Enter number of readings", "%lu", "%lu",
                             &input_var);
                    if (input_var != dura_atod_readings) {
                        if ((input_var >= 1) && (input_var <= 20)) {
                            dura_atod_readings = (ushort) input_var;
                            DS3234_WrSRAM(DURA_ATODREADINGS_PTR,
                                          sizeof(ushort),
                                          (ushort *) & dura_atod_readings);
                        } else
                            RangeError(1L, 20L);
                    }
                }               //end modify if
                break;

            case 'D':
                printf
                    ("\n  Mux Change to AtoD Reading Delay Period (ms) =  %hu\n",
                     dura_atod_delay);
                if (QueryYesNo("\n  Modify", FALSE)) {
                    input_var = dura_atod_delay;
                    QueryNum("\n  Enter Reading Delay (ms)", "%lu", "%lu",
                             &input_var);
                    if (input_var != dura_atod_delay) {
                        if ((input_var >= 4) && (input_var <= 4000)) {
                            dura_atod_delay = (ushort) input_var;
                            DS3234_WrSRAM(DURA_ATODDELAY_PTR,
                                          sizeof(ushort),
                                          (ushort *) & dura_atod_delay);
                        } else
                            RangeError(4L, 4000L);
                    }
                }               //end modify if
                break;

            case 'E':
                printf
                    ("\n  Number of Samples per AtoD recalibration  = %lu\n",
                     dura_samps_per_cal);
                if (QueryYesNo("\n  Modify", FALSE)) {
                    input_var = dura_samps_per_cal;
                    QueryNum
                        ("\n  Enter number of samples per recalibration",
                         "%lu", "%lu", &input_var);
                    if (input_var != dura_samps_per_cal) {
                        if ((input_var >= 1) && (input_var <= 100000)) {
                            dura_samps_per_cal = input_var;
                            DS3234_WrSRAM(DURA_SAMPSPERCAL_PTR,
                                          sizeof(ulong),
                                          (ulong *) & dura_samps_per_cal);
                        } else
                            RangeError(1L, 100000L);
                    }
                }               //end modify if
                break;

            case 'F':
                printf("\n  AtoD LED indicator is ");
                if (dura_led_enable == TRUE)
                    printf("ENABLED\n");
                else
                    printf("DISABLED\n");
                if (QueryYesNo("\n  Modify", FALSE)) {
                    input_var = dura_led_enable;
                    QueryNum
                        ("\n\n  Selection:\n   [0] Disabled\n   [1] Enabled\n  Enter>",
                         "%lu", "%lu", &input_var);
                    if (input_var != dura_led_enable) {
                        if ((input_var >= 0) && (input_var <= 1)) {
                            dura_led_enable = (uchar) input_var;
                            DS3234_WrSRAM(DURA_LEDENABLE_PTR,
                                          sizeof(uchar),
                                          (uchar *) & dura_led_enable);
                        } else
                            RangeError(0L, 1L);
                    }
                }               //end modify if
                break;

            case 'G':
                printf("Not Yet Implemented!\n");
                break;

            case '?':
            case 'H':
                printf(" %s\n %s\n %s\n %s\n %s\n %s\n %s\n",
                       "[A] DURA Error  Counter",
                       "[B] DURA Sample Counter",
                       "[C] Readings per Sample",
                       "[D] Reading Delay Period",
                       "[E] Samples per AtoD Recal",
                       "[F] LED Enable", "[G] Sensor Cal Values");
                break;

            }                   //end switch

        }                       //end qret if

    }                           //end while

    printf("\n");

}


void LoggerSetup(void)
{
    char cmd;
    short qret = TRUE;
    ulong input_var;

    printf("\n/* DataLogger Setup Menu [H] for Help */");

    while (qret == TRUE) {
        printf("\n");
        qret =
            QueryChar("MSC_SETUP_LOGGER>", NULL, "^[?ABCDEFGHIJKLM", &cmd);
        printf("\n");

        if (qret == TRUE) {
            switch (cmd) {
            case '^[':
                break;

            case 'A':
                printf("\n  MSC Serial Number is %04hu\n", sys_serial_num);
                if (QueryYesNo("\n  Modify", FALSE)) {
                    if (SysPass() == TRUE) {
                        input_var = 0;
                        QueryNum("\n  Enter MSC Serial Number", "%lu",
                                 "%lu", &input_var);
                        if (input_var != sys_serial_num) {
                            if ((input_var > 0) && (input_var <= 10000)) {
                                sys_serial_num = (int) input_var;
                                DS3234_WrSRAM(SYS_SERIALN_PTR,
                                              sizeof(ushort),
                                              (ushort *) & sys_serial_num);
                            } else
                                RangeError(1L, 10000L);
                        }
                    }           //end SysPass if
                }               //end QueryYesNo if
                break;

            case 'B':
                printf("\n  Power On Reset Counter = %lu\n", sys_por_ctr);
                if (QueryYesNo("\n  Reset", FALSE)) {
                    if (SysPass() == TRUE) {
                        sys_por_ctr = 0UL;
                        DS3234_WrSRAM(SYS_PORCTR_PTR, sizeof(ulong),
                                      (ulong *) & sys_por_ctr);
                    }
                }
                break;

            case 'C':
                printf("\n  Total Number of System Resets =  %lu\n",
                       sys_reset_ctr);
                printf("\n  Last System Reset =  %s",
                       ctime(&sys_reset_time));
                if (QueryYesNo("\n  Reset", FALSE)) {
                    if (SysPass() == TRUE) {
                        sys_reset_ctr = sys_reset_time = 0UL;
                        DS3234_WrSRAM(SYS_RESETCTR_PTR, sizeof(ulong),
                                      (ulong *) & sys_reset_ctr);
                        DS3234_WrSRAM(SYS_RESETTIME_PTR, sizeof(ulong),
                                      (ulong *) & sys_reset_time);
                    }
                }
                break;

            case 'D':
                printf("\n  System Error Counter =  %lu\n", sys_error_ctr);
                printf("\n  Last Sys Error Event = %d at %s",
                       sys_last_error, ctime(&sys_error_time));
                if (QueryYesNo("\n  Reset", FALSE)) {
                    if (SysPass() == TRUE) {
                        sys_last_error = 0;
                        DS3234_WrSRAM(SYS_LASTERROR_PTR, sizeof(int),
                                      (int *) &sys_last_error);
                        sys_error_ctr = sys_error_time = 0UL;
                        DS3234_WrSRAM(SYS_ERRORCTR_PTR, sizeof(ulong),
                                      (ulong *) & sys_error_ctr);
                        DS3234_WrSRAM(SYS_LASTERRTIME_PTR, sizeof(ulong),
                                      (ulong *) & sys_error_time);
                    }
                }
                break;

            case 'E':
                current_time = RTCGetTime(NULL, NULL);
                tp = localtime(&current_time);
                printf("\n  Last DOY = %03hu, Current DOY = %03hu\n",
                       sys_last_doy, DayOfYear(tp->tm_year + 1900,
                                               tp->tm_mon + 1,
                                               tp->tm_mday));
                printf("\n  Log File Counter =  %lu\n", sys_file_ctr);
                if (QueryYesNo("\n  Modify", FALSE)) {
                    if (SysPass() == TRUE) {
                        input_var = sys_file_ctr;
                        QueryNum("\n  Enter file new file number", "%lu",
                                 "%lu", &input_var);
                        if ((input_var >= 1) && (input_var <= 900)) {
                            sys_file_ctr = input_var;
                            DS3234_WrSRAM(SYS_FILECTR_PTR, sizeof(ulong),
                                          (ulong *) & sys_file_ctr);
                        } else
                            RangeError(1L, 900L);
                    }
                }               //end modify if
                break;

            case 'F':
                printf("\n  Hardware Watchdog is ");
                if (sys_watchdog_enable == TRUE)
                    printf("ENABLED\n");
                else
                    printf("DISABLED\n");
                if (QueryYesNo("\n  Modify", FALSE)) {
                    input_var = sys_watchdog_enable;
                    QueryNum
                        ("\n  Selection:\n   [0] Disable\n   [1] Enable\n  Enter>",
                         "%lu", "%lu", &input_var);
                    if (input_var != sys_watchdog_enable) {
                        if ((input_var >= 0) && (input_var <= 1)) {
                            sys_watchdog_enable = (uchar) input_var;
                            DS3234_WrSRAM(SYS_WDOGENABLE_PTR,
                                          sizeof(uchar),
                                          (uchar *) & sys_watchdog_enable);
                            if (sys_watchdog_enable == TRUE) {
                                EnableWatchDog();
                            } else {
                                DisableWatchDog();
                            }
                        } else
                            RangeError(0L, 1L);
                    }
                }               //end modify if
                break;

            case 'G':
                printf
                    ("\n  Maximum size (in MBytes) for an Individual DataFile = %hu\n",
                     sys_df_limit);
                if (QueryYesNo("\n  Modify", FALSE)) {
                    input_var = sys_df_limit;
                    QueryNum("\n  Enter max file size (MB)", "%lu", "%lu",
                             &input_var);
                    if (input_var != sys_df_limit) {
                        if ((input_var >= 1) && (input_var <= 65)) {
                            sys_df_limit = (ushort) input_var;
                            DS3234_WrSRAM(SYS_DFLIMIT_PTR, sizeof(ushort),
                                          (ushort *) & sys_df_limit);
                        } else
                            RangeError(1L, 65L);
                    }
                }               //end modify if

                printf
                    ("\n\n  Maximum size (in kBytes) for the Spooled DataFile = %hu\n",
                     sys_sf_limit);
                if (QueryYesNo("\n  Modify", FALSE)) {
                    input_var = sys_sf_limit;
                    QueryNum("\n  Enter max file size (kB)", "%lu", "%lu",
                             &input_var);
                    if (input_var != sys_sf_limit) {
                        if ((input_var >= 0) && (input_var <= 4000)) {
                            sys_sf_limit = (ushort) input_var;
                            DS3234_WrSRAM(SYS_SFLIMIT_PTR, sizeof(ushort),
                                          (ushort *) & sys_sf_limit);
                        } else
                            RangeError(0L, 4000L);
                    }
                }               //end modify if

                printf
                    ("\n\n  Maximum size (in kBytes) for the Error File  = %hu\n",
                     sys_ef_limit);
                if (QueryYesNo("\n  Modify", FALSE)) {
                    input_var = sys_ef_limit;
                    QueryNum("\n  Enter max file size (kB)", "%lu", "%lu",
                             &input_var);
                    if (input_var != sys_ef_limit) {
                        if ((input_var >= 0) && (input_var <= 1000)) {
                            sys_ef_limit = (ushort) input_var;
                            DS3234_WrSRAM(SYS_EFLIMIT_PTR, sizeof(ushort),
                                          (ushort *) & sys_ef_limit);
                        } else
                            RangeError(0L, 1000L);
                    }
                }               //end modify if
                break;

            case 'I':
                if (sys_deploy_mode == 1)
                    printf
                        ("\n  Sensor(s) are in scheduled logging operation\n");
                else if (sys_deploy_mode == 2)
                    printf
                        ("\n  Sensor(s) are in continuous logging operation\n");
                else
                    printf
                        ("\n  WARNING: Var is not currently set to a Valid Mode, val = %d.\n",
                         sys_deploy_mode);
                if (QueryYesNo("\n  Modify", FALSE)) {
                    input_var = sys_deploy_mode;
                    QueryNum
                        ("\n  Select Mode:\n  [1] Scheduled\n  [2] Continuous\n  Enter>",
                         "%lu", "%lu", &input_var);
                    if (input_var != sys_deploy_mode) {
                        if ((input_var >= 1) && (input_var <= 2)) {
                            sys_deploy_mode = (uchar) input_var;
                            DS3234_WrSRAM(SYS_DEPLMODE_PTR, sizeof(uchar),
                                          (uchar *) & sys_deploy_mode);
                        } else
                            RangeError(1L, 2L);
                    }
                }               //end modify if
                break;
			
            case 'J':
				input_var = 0;
                if (sys_sensor_type == 1)
					printf("\n\n  NO Sensors Selected\n");
                else if (sys_sensor_type == 2) 
                    printf("\n\n  ISUS Sensor Only Selected\n");
				else if (sys_sensor_type == 3) 
                    printf("\n\n  DuraFet Sensor Only Selected\n");
                else if (sys_sensor_type == 4) 
                    printf("\n\n  ISUS and DuraFET Sensors Selected\n");	
                else {
					printf("\n  WARNING: Invalid Sensor Selection Value, val = %d.\n", sys_sensor_type);
                    input_var = 4;
                }
                if (QueryYesNo("\n  Modify", FALSE)) {
                    input_var = sys_sensor_type;
                    QueryNum
                        ("\n  Select MSC Sensors:\n  [1] No Sensors\n  [2] ISUS Only Sensor\n  [3] DURA Only Sensor\n  [4] ISUS and DuraFET Sensors\n  Enter>",
                         "%lu", "%lu", &input_var);
                    if (input_var != sys_sensor_type) {
                        if ((input_var >= 1) && (input_var <= 4)) {
                            sys_sensor_type = (uchar) input_var;
                            DS3234_WrSRAM(SYS_SENSORTYPE_PTR, sizeof(uchar),
                                          (uchar *) & sys_sensor_type);
                        } else
                            RangeError(1L, 4L);
                    }
                }               //end modify if
                break;			

            case 'K':
                if (sys_data_mode == 1) {
                    printf("\n\n  Sensors will collect real data\n");
                    input_var = 0;
                } else if (sys_data_mode == 2) {
                    printf
                        ("\n\n  Sensors will collect pseudo data (TEST MODE)\n");
                    input_var = 0;
                } else {
                    input_var = 1;
                }
                if (QueryYesNo("\n  Modify", FALSE)) {
                    input_var = sys_data_mode;
                    QueryNum
                        ("\n  Select Data Collection Mode:\n  [1] Collect Real Data\n  [2] Collect Pseudo Data\n  Enter>",
                         "%lu", "%lu", &input_var);
                    if (input_var != sys_data_mode) {
                        if ((input_var >= 1) && (input_var <= 2)) {
                            sys_data_mode = (uchar) input_var;
                            DS3234_WrSRAM(SYS_DATAMODE_PTR, sizeof(uchar),
                                          (uchar *) & sys_data_mode);
                        } else
                            RangeError(1L, 2L);
                    }
                }               //end modify if
                break;

            case 'L':
                printf("\n  Power Active Timeout Period is %hu hours\n",
                       sys_apf_tmout);
                if (QueryYesNo("\n  Modify", FALSE)) {
                    if (SysPass() == TRUE) {
                        input_var = sys_apf_tmout;
                        QueryNum("\n  Enter Timeout Period (hours)", "%lu",
                                 "%lu", &input_var);
                        if ((ushort) input_var != sys_apf_tmout) {
                            if ((input_var >= 1) && (input_var <= 18)) {
                                sys_apf_tmout = (ushort) input_var;
                                DS3234_WrSRAM(SYS_APFTMOUT_PTR,
                                              sizeof(ushort),
                                              (ushort *) & sys_apf_tmout);
                                SyncNextSample();
                                DisplayAlarmTime();
                            } else
                                RangeError(1L, 18L);
                        }
                    }           //end SysPass if
                }               //end QueryYesNo if
                break;

            case 'M':
                if (SysPass() == TRUE) {        //make it bozo proof
                    SetDefaultsIntoNVRAM();
                    GetVarsFromNVRAM();
                }
                break;

            case '?':
            case 'H':
                printf
                    (" %s\n %s\n %s\n %s\n %s\n %s\n %s\n %s\n %s\n %s\n %s\n %s\n",
                     "[A] Logger Serial #", 
					 "[B] Power Cycle  Counter",
                     "[C] System Reset Counter",
                     "[D] System Error Counter",
					 "[E] LogFile Counter",
                     "[F] WatchDog Enable", 
					 "[G] File Sizes",
                     "[I] Deploy Mode", 
					 "[J] Sensor Mode",
					 "[K] Data   Mode",                     
					 "[L] Power Active Timeout Period",
                     "[M] Copy defaults into RTC SRAM");
                break;

            }                   //end switch

        }                       //end qret if

    }                           //end while

    printf("\n");
}

void RtcSetup(void)
{
    char cmd;
    short qret = TRUE;
    uchar rtc_ctrl;
    ulong input_var;
    float rv;

    printf("\n/* CF2 & DS3234 RTC Setup Menu [H] for Help */");

    while (qret == TRUE) {
        printf("\n");
        qret = QueryChar("MSC_SETUP_RTC>", NULL, "^[?ABCDEFGHI", &cmd);
        printf("\n");

        if (qret == TRUE) {
            switch (cmd) {
            case '^[':
                break;

            case 'A':
                DisplayTimes();
                break;

            case 'B':
                /* get the current times from CF2 and Dallas DS3234 */
                CF2_secs = RTCGetTime(NULL, NULL);
                DS3234_secs = DS3234_GetTime();
                printf("\n  CF2RTC Date/Time is %s", ctime(&CF2_secs));
                printf("  DS3234 Date/Time is %s", ctime(&DS3234_secs));
                if (QueryYesNo("\n  Modify", FALSE)) {
                    tp = localtime(&CF2_secs);
                    if (QueryDateTime("\n  Enter New Date/Time", FALSE, tp)
                        != TRUE) {
                        printf
                            ("\n  Error, could not parse, value NOT saved!\n");
                        break;
                    }
                    current_time = mktime(tp);
                    if ((current_time == -1) || (current_time == CF2_secs)) {
                        printf
                            ("\n  Error, entry failed, value NOT saved!\n");
                        break;
                    } else {
                        RTCSetTime(current_time, 0);
                        CF2_secs = RTCGetTime(NULL, NULL);
                        DS3234_SetTime(CF2_secs);
                        SyncNextSample();
                        printf("\n  Entered Time was %s\n", asctime(tp));
                        DisplayTimes();
                        DisplayAlarmTime();
                        break;
                    }
                }
                break;

            case 'C':
                printf
                    ("\n  Offset of Local Time Zone from GMT ( [+] is W [-] is E ) %+02d",
                     sys_tzoffset);
                if (QueryYesNo("\n  Modify", FALSE)) {
                    input_var = (long) sys_tzoffset;
                    QueryNum("\n  Enter local time zone offset in hours",
                             "%ld", "%ld", &input_var);
                    if ((long) input_var != (long) sys_tzoffset) {
                        if (((long) input_var >= -12)
                            && ((long) input_var <= 12)) {
                            sys_tzoffset = (int) input_var;
                            DS3234_WrSRAM(SYS_TZOFFSET_PTR, sizeof(int),
                                          (int *) &sys_tzoffset);
                        } else
                            RangeError(-12L, 12L);
                    }
                }
                break;

            case 'D':
                DisplayAlarmTime();
                break;

            case 'E':
                printf("\n  Data Timestamp is set to ");
                if (sys_time_type == FRACSECS)
                    printf("include Fractional Seconds.\n");
                else
                    printf("record Whole Seconds Only.\n");
                if (QueryYesNo("\n  Modify", FALSE)) {
                    input_var = sys_time_type;
                    QueryNum
                        ("\n  Selection:\n   [0] Whole\n   [1] Fractional\n  Enter>",
                         "%lu", "%lu", &input_var);
                    if (input_var != sys_time_type) {
                        if (input_var == 0) {
                            sys_time_type = WHOLESECS;
                            DS3234_WrSRAM(SYS_TIMETYPE_PTR, sizeof(uchar),
                                          (uchar *) & sys_time_type);
                        } else if (input_var == 1) {
                            sys_time_type = FRACSECS;
                            DS3234_WrSRAM(SYS_TIMETYPE_PTR, sizeof(uchar),
                                          (uchar *) & sys_time_type);
                        } else
                            RangeError(0L, 1L);
                    }
                }               //end modify if
                break;

            case 'F':
                printf("\n  Transfering DS3234 RTC Values to CF2 RTC\n");
                RTCSetTime(DS3234_GetTime(), 0);
                break;

            case 'G':
                rtc_ctrl = spiReadRTCReg(RTC_CTRL);
                DisplayByteVal("DS3234 Control Reg Val", rtc_ctrl);
                DisplayByteVal("DS3234 Status  Reg Val",
                               spiReadRTCReg(RTC_CTRLSTAT));
                break;

            case 'I':
                DisplayByteVal("DS3234 T CTRL  Reg Val",
                               spiReadRTCReg(RTC_TEMPCTRL));
                rv = DS3234_GetTemp();
                printf("DS3234 Temperature Val = %.2fC = %.2fF\n", rv,
                       9.0 / 5.0 * rv + 32.0);
                break;

            case '?':
            case 'H':
                printf(" %s\n %s\n %s\n %s\n %s\n %s\n %s\n %s\n",
                       "[A] Get Time Values",
                       "[B] Set Time Values",
                       "[C] Time Zone Value",
                       "[D] Wakeup Time Value",
                       "[E] Fractional Secs Timestamp",
                       "[F] Transfer Time Values",
                       "[G] RTC Status/Control Register",
                       "[I] RTC Temp/Control Register");
                break;

            }                   //end switch

        }                       //end qret if

    }                           //end while

    printf("\n");
}

//******************************************************************************
// Data Menu Routines
//******************************************************************************
//#DATA
void DataMenu(void)
{
    char cmd;
    short qret = TRUE;
    float volts, amps, temp, humi, dew_point;

    printf("\n/* MBARI Sensor Controller Data Menu [H] for Help */");

    while (qret == TRUE) {
        printf("\n");
        qret = QueryChar("MSC_DATA>", NULL, "^[?ABCDH", &cmd);
        printf("\n");

        if (qret == TRUE) {
            switch (cmd) {
            case '^[':
                break;

            case 'A':
                IsusDataMenu();
                break;

            case 'B':
                DuraDataMenu();
                break;

            case 'C':
                HIH6130_ReadTempHumidity(&temp, &humi);
                dew_point = calc_dewpoint(humi, temp);
                printf("\n  Housing Temperature = %+06.2fC = %+06.2fF\n",
                       temp, CtoF(temp));
                printf("  Housing    Humidity = %5.1f%%  = %5.1fC (DP)\n",
                       humi, dew_point);
                break;

            case 'D':
                INA219_ReadVoltageCurrent(&volts, &amps);
                printf("\n  System Batt Voltage = %.3f\n", volts);
                printf("  System Load Current = %.4e\n", amps);
                break;

            case '?':
            case 'H':
                printf(" %s\n %s\n %s\n %s\n",
                       "[A] ISUS Data",
                       "[B] DURA Data",
                       "[C] Temp/Humi Data", "[D] Volt/Amps Data");
                break;

            }                   //end switch

        }                       //end qret if

    }                           //end while

}

void IsusDataMenu(void)
{
    char cmd;
    char fnbuf[MAXINBUFF];
    short qret = TRUE;

    printf("\n/* ISUS Data Menu <H> for Help */");

    while (qret == TRUE) {
        printf("\n");
        qret = QueryChar("MSC_DATA_ISUS>", NULL, "^[ABCH", &cmd);
        printf("\n");

        if (qret == TRUE) {
            switch (cmd) {
            case '^[':
                break;

            case 'A':
                IsusAcquireData(stdout);
                break;

            case 'B':
                pdcfinfo("C:", &size, &avail);
                printf("\nCF Card: Size=%ld Avail=%.2f%%\n\n", size,
                       (float) (((float) avail / (float) size) * 100.0));
                printf("\nEnter DataFile Name: ");
                if (safegets(fnbuf, MAXREAD) == 0) {
                    printf("\nERROR: No Filename given!\n");
                    break;
                } else {
                    if ((dat_file = fopen(fnbuf, "a+")) == FNULL) {
                        printf("\nERROR: Could NOT open File!\n");
                        break;
                    }
                }
                IsusAcquireData(dat_file);
                fclose(dat_file);
                break;

            case 'C':
                break;

            case 'H':
                printf(" %s\n %s\n %s\n",
                       "[A] Send Spectra",
                       "[B] Collect and Send Spectra",
                       "[C] Output Spectrometer Scan Data");
                break;

            }                   //end switch

        }                       //end qret if

    }                           //end while

    printf("\n");
}

void IsusAcquireData(FILE * fptr)
{
    uchar UART_char;
    ulong loop_ctr;
    float volts, amps;
    float temp, humi, dew_point;


    if (SpecPwrOn(&SpecPort) != TRUE) {
        printf("ERROR, Spectrometer not responding - exiting!\n");
        return;
    }

    if (LampPwrOn() != TRUE) {
        SpecPwrOff(SpecPort);
        printf("ERROR, Fiberlite won't turn on - exiting!\n");
        return;
    }

    printf("\n\n");
    OutputIsusHeader(fptr);

    INA219_ReadVoltageCurrent(&volts, &amps);
    printf("\n  System Batt Voltage = %.3f\n", volts);
    printf("  System Load Current = %.4e\n", amps);

    HIH6130_ReadTempHumidity(&temp, &humi);
    dew_point = calc_dewpoint(humi, temp);
    printf("\n  Housing Temperature = %+06.2fC = %+06.2fF\n", temp,
           CtoF(temp));
    printf("  Housing    Humidity = %5.1f%%  = %5.1fC (DP)\n", humi,
           dew_point);

    printf("\nEntering Acquisition Loop, press S to break.\n");

    SCIRxFlush();
    UART_char = 0;
    loop_ctr = isus_scans_per_dc;

    while (UART_char != (uchar) CAPS_S) {

        if (loop_ctr == isus_scans_per_dc) {
            LampCloseShutter();
            if (ScanSpectra(SpecPort, fptr, 1, DarkData, DARKCURRENT_SCAN)
                != TRUE)
                break;
            LampOpenShutter();
            loop_ctr = 0;
        } else {
            if (ScanSpectra(SpecPort, fptr, 1, SpecData, INTENSITY_SCAN) !=
                TRUE)
                break;
        }
        ++loop_ctr;

        if (SCIRxQueuedCount() != 0) {
            UART_char = toupper((char) SCIRxGetByte(false));
            SCIRxFlush();
        }

    }                           //end while

    LampPwrOff();
    SpecPwrOff(SpecPort);

    if (UART_char == (uchar) CAPS_S)
        printf("Break: Char S Received!\n");

}

void DuraDataMenu(void)
{
    char fnbuf[MAXINBUFF];
    char cmd;
    short qret = TRUE;

    printf("\n/* DuraFET Data Menu [H] for Help */");

    while (qret == TRUE) {
        printf("\n");
        qret = QueryChar("MSC_DATA_DURA>", NULL, "^[?ABCH", &cmd);
        printf("\n");

        if (qret == TRUE) {
            switch (cmd) {
            case '^[':
                break;

            case 'A':
                DuraAcquireData(stdout);
                break;

            case 'B':
                pdcfinfo("C:", &size, &avail);
                printf("\nCF Card: Size=%ld Avail=%.2f%%\n\n", size,
                       (float) (((float) avail / (float) size) * 100.0));
                printf("\nEnter DataFile Name: ");
                if (safegets(fnbuf, MAXREAD) == 0) {
                    printf("\nERROR: No Filename given!\n");
                    break;
                } else {
                    if ((dat_file = fopen(fnbuf, "a+")) == FNULL) {
                        printf("\nERROR: Could NOT open File!\n");
                        break;
                    }
                }
                DuraAcquireData(dat_file);
                fclose(dat_file);
                break;

            case 'C':
                DuraAcquireAtodValues(stdout);
                break;

            case '?':
            case 'H':
                printf(" %s\n %s\n %s\n",
                       "[A] Output  Data",
                       "[B] Collect Data", "[C] Output  AtoD");
                break;

            }                   //end switch

        }                       //end qret if

    }                           //end while

    printf("\n");
}

void DuraAcquireData(FILE * fptr)
{
    uchar UART_char;
    ulong loop_ctr;
    ulong samp_rate;
    float battv, volts, amps, sd;
    float temp, humi, dew_point;
    float StdDevVrsExt, StdDevVk;
    float VrsExt, Vk, Ik, Ib;
    struct tm *tp;
    RTCTimer TmOut;


    samp_rate = 0;
    QueryNum("\nEnter sample collection rate, secs>", "%lu", "%lu",
             &samp_rate);
    if (samp_rate > 120) {
        RangeError(0L, 120L);
        printf("EXITING, try again!\n");
    }

    if (AtodPowerOn() != TRUE) {
        AtodPowerOff();
        printf("ERROR, AtodPowerOn() - exiting!\n");
        return;
    }

    if (AtodEnable() != TRUE) {
        AtodPowerOff();
        printf("ERROR, AtodEnable() - exiting!\n");
        return;
    }

    printf("\n\n");
    OutputDuraHeader(fptr);

    INA219_ReadVoltageCurrent(&volts, &amps);
    printf("\n  System Batt Voltage = %.3f\n", volts);
    printf("  System Load Current = %.4e\n", amps);

    AtodReadValue(DURAFET_BATT, dura_atod_delay, dura_atod_readings,
                  &battv, &sd);
    printf("  Backup Batt Voltage = %.4f StdDev = %f\n", battv, sd);

    HIH6130_ReadTempHumidity(&temp, &humi);
    dew_point = calc_dewpoint(humi, temp);
    printf("\n  Housing Temperature = %+06.2fC = %+06.2fF\n", temp,
           CtoF(temp));
    printf("  Housing    Humidity = %5.1f%%  = %5.1fC (DP)\n", humi,
           dew_point);

    printf("\nEntering Acquisition Loop, press S to break.\n");

    SCIRxFlush();
    UART_char = 0;
    loop_ctr = 0;

    while (UART_char != (uchar) CAPS_S) {

        RTCCountdownTimerSetup(&TmOut, (samp_rate * 1000000L));

        if (loop_ctr == dura_samps_per_cal) {
            AtodCalibrate();
            printf("A/D was Recalibrated\n");
            loop_ctr = 0;
        } else {

            HIH6130_ReadTempHumidity(&temp, &humi);

            AtodReadValue(DURAFET_IB_4096, dura_atod_delay,
                          dura_atod_readings, &Ib, &sd);
            if ((Ib == IB_ERANGE_POS) || (Ib == IB_ERANGE_NEG))
                AtodReadValue(DURAFET_IB_512, dura_atod_delay,
                              dura_atod_readings, &Ib, &sd);
            if ((Ib == IB_ERANGE_POS) || (Ib == IB_ERANGE_NEG))
                AtodReadValue(DURAFET_IB_32, dura_atod_delay,
                              dura_atod_readings, &Ib, &sd);
			if ((Ib == IB_ERANGE_POS) || (Ib == IB_ERANGE_NEG))
                AtodReadValue(DURAFET_IB_4, dura_atod_delay,
                              dura_atod_readings, &Ib, &sd);

            AtodReadValue(DURAFET_IK_4096, dura_atod_delay,
                          dura_atod_readings, &Ik, &sd);
            if ((Ik == IK_ERANGE_POS) || (Ik == IK_ERANGE_NEG))
                AtodReadValue(DURAFET_IK_512, dura_atod_delay,
                              dura_atod_readings, &Ik, &sd);
            if ((Ik == IK_ERANGE_POS) || (Ik == IK_ERANGE_NEG))
                AtodReadValue(DURAFET_IK_32, dura_atod_delay,
                              dura_atod_readings, &Ik, &sd);
            if ((Ik == IK_ERANGE_POS) || (Ik == IK_ERANGE_NEG))
                AtodReadValue(DURAFET_IK_4, dura_atod_delay,
                              dura_atod_readings, &Ik, &sd);

            AtodReadValue(DURAFET_VK, dura_atod_delay, dura_atod_readings,
                          &Vk, &StdDevVk);
            AtodReadValue(DURAFET_VRS_EXT, dura_atod_delay,
                          dura_atod_readings, &VrsExt, &StdDevVrsExt);

            data_time = RTCGetTime(NULL, NULL);
            tp = localtime(&data_time);
            fprintf(fptr, "D, %02d/%02d/%02d %02d:%02d:%02d, ",
                    tp->tm_mon + 1, tp->tm_mday, tp->tm_year + 1900,
                    tp->tm_hour, tp->tm_min, tp->tm_sec);
            fprintf(fptr, "%.1f, %.1f, ", temp, humi);
            fprintf(fptr, "%+f, %f, %+f, %f, %+.3e, %+.3e\n", VrsExt,
                    StdDevVrsExt, Vk, StdDevVk, Ik, Ib);
        }

        ++loop_ctr;

        while ((SCIRxQueuedCount() == 0)
               && (RTCCountdownTimeout(&TmOut) == false))
            continue;

        if (SCIRxQueuedCount() != 0) {
            UART_char = toupper((char) SCIRxGetByte(false));
            SCIRxFlush();
        }


    }                           //end while

    AtodPowerOff();

    if (UART_char == (uchar) CAPS_S)
        printf("Break: Char S Received!\n");

}

void DuraAcquireAtodValues(FILE * fptr)
{
    uchar UART_char;
    ulong loop_ctr;
    ulong samp_rate;
    ulong C1, C2;
    float V1, V2;
    float battv, volts, amps, sd;
    float temp, humi, dew_point;
    struct tm *tp;
    RTCTimer TmOut;


    samp_rate = 0;
    QueryNum("\nEnter sample collection rate, secs>", "%lu", "%lu",
             &samp_rate);
    if (samp_rate > 120) {
        RangeError(0L, 120L);
        printf("EXITING, try again!\n");
    }

    if (AtodPowerOn() != TRUE) {
        AtodPowerOff();
        printf("ERROR, AtodPowerOn() - exiting!\n");
        return;
    }

    if (AtodEnable() != TRUE) {
        AtodPowerOff();
        printf("ERROR, AtodEnable() - exiting!\n");
        return;
    }

    printf("\n\n");
    OutputDuraHeader(fptr);

    INA219_ReadVoltageCurrent(&volts, &amps);
    printf("\n  System Batt Voltage = %.3f\n", volts);
    printf("  System Load Current = %.4e\n", amps);

    AtodReadValue(DURAFET_BATT, dura_atod_delay, dura_atod_readings,
                  &battv, &sd);
    printf("  Backup Batt Voltage = %.4f StdDev = %f\n", battv, sd);

    HIH6130_ReadTempHumidity(&temp, &humi);
    dew_point = calc_dewpoint(humi, temp);
    printf("\n  Housing Temperature = %+06.2fC = %+06.2fF\n", temp,
           CtoF(temp));
    printf("  Housing    Humidity = %5.1f%%  = %5.1fC (DP)\n", humi,
           dew_point);

    printf
        ("\nEntering Acquisition Loop, press S to break, B for bipolar, U for unipolar, R for recal.\n");

    SCIRxFlush();
    UART_char = 0;
    loop_ctr = 0;

    while (UART_char != (uchar) CAPS_S) {

        RTCCountdownTimerSetup(&TmOut, (samp_rate * 1000000L));

        if (loop_ctr == dura_samps_per_cal) {
            AtodCalibrate();
            printf("A/D was Recalibrated\n");
            loop_ctr = 0;
        } else {

            HIH6130_ReadTempHumidity(&temp, &humi);

            AtodChannel(CS5506_AIN2);
            DelayMilliSecs(dura_atod_delay);
            if (AtodConversion(&C1, &V1) == TIMEOUT) {
                printf("AtoD Timeout!\n");
                continue;
            }

            AtodChannel(CS5506_AIN3);
            DelayMilliSecs(dura_atod_delay);
            if (AtodConversion(&C2, &V2) == TIMEOUT) {
                printf("AtoD Timeout!\n");
                continue;
            }

            data_time = RTCGetTime(NULL, NULL);
            tp = localtime(&data_time);
            fprintf(fptr, "D, %02d/%02d/%02d %02d:%02d:%02d, ",
                    tp->tm_mon + 1, tp->tm_mday, tp->tm_year + 1900,
                    tp->tm_hour, tp->tm_min, tp->tm_sec);
            fprintf(fptr, "%.1f, %.1f, ", temp, humi);
            fprintf(fptr, "0x%05lX, %+f, 0x%05lX, %+f\n", C1, V1, C2, V2);
        }

        ++loop_ctr;

        while ((SCIRxQueuedCount() == 0)
               && (RTCCountdownTimeout(&TmOut) == false))
            continue;

        if (SCIRxQueuedCount() != 0) {

            UART_char = toupper((char) SCIRxGetByte(false));

            if (UART_char == (uchar) CAPS_B) {
                AtodPolarity(CS5506_BIPOLAR);
                printf("A/D now in Bipolar Mode\n");
            }
            if (UART_char == (uchar) CAPS_U) {
                AtodPolarity(CS5506_UNIPOLAR);
                printf("A/D now in Unipolar Mode\n");
            }
            if (UART_char == (uchar) CAPS_R) {
                AtodCalibrate();
                printf("A/D was Recalibrated\n");
                loop_ctr = 0;
            }

            SCIRxFlush();
        }

    }                           //end while

    AtodPowerOff();

    if (UART_char == (uchar) CAPS_S)
        printf("Break: Char S Received!\n");

}

//******************************************************************************
// Test Menu Routines
//******************************************************************************
//#TEST
void TestMenu(void)
{

    char cmd;
    short qret = TRUE;

    printf("\n/* MBARI Sensor Controller Test Menu [H] for Help */");

    while (qret == TRUE) {
        printf("\n");
        qret = QueryChar("MSC_TEST>", NULL, "^[?ABH", &cmd);
        printf("\n");

        if (qret == TRUE) {
            switch (cmd) {
            case '^[':
                break;

            case 'A':
                IsusTestMenu();
                break;

            case 'B':
                DuraTestMenu();
                break;

            case '?':
            case 'H':
                printf(" %s\n %s\n", "[A] ISUS Test", "[B] DURA Test");
                break;

            }                   //end switch

        }                       //end qret if

    }                           //end while

}

void IsusTestMenu(void)
{
    char cmd;
    short qret = TRUE;
    int i, j, scanret;
    ushort *dataptr;
    ulong input_var;


    printf("\n/* ISUS Test Menu <H> for Help */");

    while (qret == TRUE) {
        printf("\n");
        qret = QueryChar("MSC_TEST_ISUS>", NULL, "^[ABCDEFGH", &cmd);
        printf("\n");

        if (qret == TRUE) {
            switch (cmd) {
            case '^[':
                break;

            case 'A':
                if (isus_spec_pwr == TRUE) {
                    if (SpecPwrOff(SpecPort) == TRUE)
                        printf("\n  Spec was powered OFF.\n");
                } else {
                    if (SpecPwrOn(&SpecPort) == TRUE)
                        printf("\n  Spec was powered ON.\n");
                    else {
                        printf("\n  Could not power Spec.\n");
                        //add time to verify/fix problem!
                        if (QueryYesNo("\n  Power Spec OFF? ", TRUE))
                            SpecPwrOff(SpecPort);
                    }
                }
                break;

            case 'B':
                if (isus_lamp_pwr == TRUE) {
                    if (LampPwrOff() == TRUE)
                        printf("\n  Lamp was powered OFF.\n");
                    if (RefPwrOff() == TRUE)
                        printf("\n  RefCh was powered OFF.\n");
                    break;
                } else {
					if ( LampPwrOnUsingSpec( SpecPort, SpecData, TRUE ) == TRUE )
                        printf("\n  Lamp was powered ON.\n");
                    else {
                        printf("\n  Could not power Lamp.\n");
						//add time to verify/fix problem!
                        if (QueryYesNo("\n  Power Lamp OFF? ", TRUE)){
                            LampPwrOff();
                            break;
						}		
                    }
                }
                input_var = 0;
                QueryNum
                    ("\n  Select Lamp:\n  [1] Dueterium\n  [2] Tungsten\n  [3] Both\n  Enter>",
                     "%lu", "%lu", &input_var);
                if (input_var != 0) {
                    if (input_var == 1) {
                        printf("\n  Dueterium Enabled\n");
                    } else if (input_var == 2) {
                        /* DISABLE THE DEUTERIUM LAMP */
                        PCAL6416_WritePortBit(PCAL6416_OUTPUT_PORT0,
                                              LAMP_DU_ENABLE, LOW);
                        DelayMilliSecs(100);
                        printf("\n  Dueterium Disabled\n");

                        /* ENABLE THE TUNGSTEN */
                        PCAL6416_WritePortBit(PCAL6416_OUTPUT_PORT0,
                                              LAMP_W_ENABLE, HIGH);
                        DelayMilliSecs(100);
                        printf("\n  Tungsten Enabled\n");
					} else if (input_var == 3) {
                        /* ENABLE THE DEUTERIUM LAMP */
                        PCAL6416_WritePortBit(PCAL6416_OUTPUT_PORT0,
                                              LAMP_DU_ENABLE, HIGH);
                        DelayMilliSecs(100);

                        /* ENABLE THE TUNGSTEN */
                        PCAL6416_WritePortBit(PCAL6416_OUTPUT_PORT0,
                                              LAMP_W_ENABLE, HIGH);
                        DelayMilliSecs(100);
                        printf("\n  Both Dueterium & Tungsten Enabled\n");
                    } else {
                        RangeError(1L, 3L);
                        LampPwrOff();
                    }
                }
                break;

            case 'C':
                if (isus_ref_pwr == TRUE) {
                    if (RefPwrOff() == TRUE)
                        printf("\n  RefCh was powered OFF.\n");
                } else {
                    if (RefPwrOn() == TRUE)
                        printf
                            ("\n  RefCh was powered ON, PS READY count =\n");
                    else {
                        printf
                            ("\n  Could not power RefCh, PS READY count =\n");
                        RefPwrOff();
                    }
                }
                break;

            case 'D':
                if (isus_lamp_pwr == FALSE) {
                    printf("\n  Lamp is not currently powered!\n");
                    break;
                }
                input_var = 0;
                QueryNum
                    ("\n  Select Lamp Shutter State:\n  [1] Open\n  [2] Closed\n  Enter>",
                     "%lu", "%lu", &input_var);
                if (input_var != 0) {
                    if (input_var == 1) {
                        LampOpenShutter();
                        printf("\n  Lamp Shutter Opened\n");
                    } else if (input_var == 2) {
                        LampCloseShutter();
                        printf("\n  Lamp Shutter Closed\n");
                    } else
                        RangeError(1L, 2L);
                }
                break;

            case 'E':
                input_var = 3;
                QueryNum("  Enter # of readings", "%lu", "%lu",
                         &input_var);
                if ((input_var > 0) && (input_var <= 25)) {
                    printf("\n");
                    if (isus_spec_pwr != (uchar) TRUE) {
                        printf
                            ("\n  Spectrometer is not powered, exiting!\n");
                        break;
                    }
                    // dynamically allocate array */
                    dataptr =
                        calloc((size_t) (256 * input_var),
                               sizeof(unsigned short));
                    if (dataptr == NULL) {
                        printf("CAN'T allocate memory\n");
                        break;
                    }
                    // collect spectra
                    scanret =
                        ScanSpectra(SpecPort, FNULL, (ushort) input_var,
                                    dataptr, INTENSITY_SCAN);
                    if (scanret != TRUE) {
                        printf("Scan ERROR, ret = %d\n", scanret);
                        free(dataptr);
                        break;
                    }
                    // print formatted (row-column) data, start at lower wavelengths and work up
                    for (i = 0; i < 256; i++) {
                        for (j = 0; j < input_var; j++) {
                            if (j == 0)
                                printf("%.5lf,", LambdaData[i + 1]);
                            if (j == (input_var - 1))
                                printf("%hu\n", dataptr[i + (256 * j)]);
                            else
                                printf("%hu,", dataptr[i + (256 * j)]);
                            fflush(stdout);
                        }
                    }
                    // deallocate array
                    free(dataptr);
                }
                break;

            case 'F':
                IsusLampTest();
                break;

            case 'G':
                IsusRefChTest();
                break;

            case 'H':
                printf(" %s\n %s\n %s\n %s\n %s\n %s\n %s\n",
                       "[A] Spectrometer Power Toggle",
                       "[B] Fiberlite Power Toggle",
                       "[C] Reference Power Toggle",
                       "[D] Fiberlite Shutter State",
                       "[E] Output Spectra Scans",
                       "[F] Lamp Test", "[G] Reference Chan Test");

                break;

            }                   //end switch

        }                       //end qret if

    }                           //end while

}

void IsusLampTest(void)
{
    int i;
    ushort LampOffDC[256];
    ushort LampOnDC[256];
    double mean1, mean2, mean3;
    double stdev1, stdev2, stdev3;
    float volts, amps;
    float temp, humi, dew_point;
    struct tm *t;
    FILE *lamptest;


    INA219_ReadVoltageCurrent(&volts, &amps);
    printf("\n  System Batt Voltage = %.3f\n", volts);
    printf("  System Load Current = %.4e\n", amps);

    HIH6130_ReadTempHumidity(&temp, &humi);
    dew_point = calc_dewpoint(humi, temp);
    printf("\n  Housing Temperature = %+06.2fC = %+06.2fF\n", temp,
           CtoF(temp));
    printf("  Housing    Humidity = %5.1f%%  = %5.1fC (DP)\n", humi,
           dew_point);

    if (isus_lamp_pwr == TRUE) {
        LampPwrOff();
        printf("\nLamp was turned off for inital DarkCurrent.\n");
        DelaySecs(1);           //wait a sec...
    }

    if (isus_spec_pwr == FALSE) {
        if (SpecPwrOn(&SpecPort) == TRUE)
            printf("\n  Spec needed to be powered ON.\n");
        else {
            printf("\n  Spec couldn't be powered ON - exiting!\n");
            SpecPwrOff(SpecPort);
            return;
        }
    }

    DelayMilliSecs(500);

    printf("Taking DarkCurrent Scan with DU Lamp OFF\n");
    ScanSpectra(SpecPort, stdout, 1, DarkData, DARKCURRENT_SCAN);
    mean1 = DataMean;
    stdev1 = DataStdDev;

//    if (LampPwrOn() != TRUE) {
//        SpecPwrOff(SpecPort);
//        printf("ERROR, Fiberlite won't turn on - exiting!\n");
//        return;
//    }
	
	LampPwrOnUsingSpec( SpecPort, SpecData, TRUE );

    printf("Taking Spectra Scan\n");
    ScanSpectra(SpecPort, stdout, 1, SpecData, INTENSITY_SCAN);
    mean2 = DataMean;
    stdev2 = DataStdDev;

    LampCloseShutter();

    printf("Taking DarkCurrent Scans with DU Lamp ON\n");
    ScanSpectra(SpecPort, stdout, 1, LampOnDC, DARKCURRENT_SCAN);
    mean3 = DataMean;
    stdev3 = DataStdDev;

    LampPwrOff();

    printf("Taking DarkCurrent Scan with DU Lamp OFF\n");
    ScanSpectra(SpecPort, stdout, 1, LampOffDC, DARKCURRENT_SCAN);

    SpecPwrOff(SpecPort);

    if (Ecoefs == 256)
        CalcAbs(&ZeroData[0], &SpecData[0], &AbsData[0], mean1, 256);

    t = localtime(&data_time);
    lamptest = fopen("c:\\isus\\lamptest.dat", "w");

    fprintf(lamptest, "#%02d/%02d/%02d %02d:%02d:%02d\n", t->tm_mon + 1,
            t->tm_mday, t->tm_year + 1900, t->tm_hour, t->tm_min,
            t->tm_sec);

    fprintf(lamptest, "#WAVELENGTH,INTENSITY,DARK_CURRENT,ABSORBANCE\n");
    for (i = 0; i < 256; i++) {
        if (Ecoefs != 256)
            AbsData[i + 1] = -1.0;
        fprintf(lamptest, "%.5f,%hu,%hu,%.5f\n", LambdaData[i + 1],
                SpecData[i], DarkData[i], AbsData[i + 1]);
    }
    fclose(lamptest);

    if (QueryYesNo("\n  Output C:\\ISUS\\LAMPTEST.DAT", FALSE)) {
        DisplayFile();
    }

}

void IsusRefChTest(void)
{

}

void DuraTestMenu(void)
{
    char cmd;
    uchar Port0, Port1, Port2;
    short qret = TRUE;
    int ret;
    float battv, sd;

    printf("\n/* DuraFET Test Menu [H] for Help */");

    while (qret == TRUE) {
        printf("\n");
        qret = QueryChar("MSC_TEST_DURA>", NULL, "^[?ABCH", &cmd);
        printf("\n");

        if (qret == TRUE) {
            switch (cmd) {
            case '^[':
                break;

            case 'A':
                if (dura_atod_pwr == TRUE) {
                    if (AtodPowerOff() == TRUE)
                        printf("\n  AtoD Interface was powered OFF.\n");
                } else {
                    if (AtodPowerOn() == TRUE) {
                        printf("\n  AtoD Interface was powered ON.\n");
                        if (AtodEnable() == TRUE)
                            printf("\n  AtoD was Enabled.\n");
                        else
                            printf("\n  AtoD could NOT be Enabled.\n");
                    } else {
                        printf
                            ("\n  Could not initialize AtoD Interface.\n");
                        if (QueryYesNo("\n  Force Power ON? ", FALSE))
                            PCAL6416_WritePortBit(PCAL6416_OUTPUT_PORT0, DURA_PWR_ON, HIGH);    // turn on ISO DC/DC
                    }
                }
                break;

            case 'B':
                if (dura_atod_pwr == TRUE) {
                    AtodReadValue(DURAFET_BATT, dura_atod_delay,
                                  dura_atod_readings, &battv, &sd);
                    printf("Backup Batt Voltage = %.4f StdDev = %f\n",
                           battv, sd);
                } else
                    printf("AtoD not powered!\n");
                break;

            case 'C':
                if (dura_atod_pwr == TRUE) {
                    ret = TCA6424_ReadPorts(&Port0, &Port1, &Port2);
                    printf("I2C Read Ret = %d\n", ret);
                    if (ret == TRUE) {
                        DisplayByteVal("Port0 Value", Port0);
                        DisplayByteVal("Port1 Value", Port1);
                        DisplayByteVal("Port2 Value", Port2);
                    }
                } else
                    printf("AtoD not powered!\n");
                break;

            case '?':
            case 'H':
                printf(" %s\n %s\n %s\n",
                       "[A] AtoD Power Toggle",
                       "[B] Backup Battery [V]", "[C] TCA6424 Port Data");

                break;

            }                   //end switch

        }                       //end qret if

    }                           //end while       
}

//******************************************************************************
// Info Menu Routines
//******************************************************************************
//#INFO
void InfoMenu(void)
{

    char cmd;
    short qret = TRUE;
    ulong input_var;

    printf("\n/* MBARI Sensor Controller Info Menu [H] for Help */");

    while (qret == TRUE) {
        printf("\n");
        qret = QueryChar("MSC_INFO>", NULL, "^[?ABCDEFGHI", &cmd);
        printf("\n");

        if (qret == TRUE) {
            switch (cmd) {

            case '^[':
                break;

            case 'A':
                printf("\nBuild: %s Date: %s %s\n", strupr(bfile), bdate, btime);
				
				printf("\nCompile Time Config: Spectrometer = %s\n",
#if SPECTROMETER_TYPE == MMS_UV
				"MMS_UV");		
#elif SPECTROMETER_TYPE == MMS_UV_VIS
				"MMS_UV_VIS");	
#else			
				"UNKNOWN");	
#endif

				printf("\nCompile Time Config: DuraFET = %s\n",
#if DURA_SENSOR_TYPE == DURA_MBARI
				"MBARI");
#elif DURA_SENSOR_TYPE == DURA_III
				"Honeywell");
#else
				"UNKNOWN");
#endif	   
                break;

            case 'B':
                DisplaySystemInfo();
                break;

            case 'C':
                OutputIsusHeader(stdout);
                break;

            case 'D':
                OutputDuraHeader(stdout);
                break;

            case 'E':
                DisplayTimes();
                break;

            case 'F':
                DisplayAlarmTime();
                break;

            case 'G':
                DisplayChipSelectSettings();
                break;

            case 'I':
                printf("\n  Status Messages are ");
                if (sys_message_enable == TRUE)
                    printf("ENABLED\n");
                else
                    printf("DISABLED\n");
                if (QueryYesNo("\n  Modify", FALSE)) {
                    input_var = sys_message_enable;
                    QueryNum
                        ("\n  Selection:\n   [0] Disable\n   [1] Enable\n  Enter>",
                         "%lu", "%lu", &input_var);
                    if (input_var != sys_message_enable) {
                        if ((input_var >= 0) && (input_var <= 1)) {
                            sys_message_enable = (uchar) input_var;
                            DS3234_WrSRAM(SYS_MSGENABLE_PTR, sizeof(uchar),
                                          (uchar *) & sys_message_enable);
                        } else
                            RangeError(0L, 1L);
                    }
                }               //end modify if
                break;

            case '?':
            case 'H':
                printf(" %s\n %s\n %s\n %s\n %s\n %s\n %s\n",
                       "[A] Build Info",
                       "[B] System Banner",
                       "[C] ISUS Datafile Header",
                       "[D] DURA Datafile Header",
                       "[E] RTC Time Values",
                       "[F] Alarm Time Value",
                       "[G] Chip Selects", "[I] Enable Status Messages");
                break;
            }                   //end switch

        }                       //end qret if

    }                           //end while

}


//******************************************************************************
// File Menu Routines
//******************************************************************************
//#FILE
void FileMenu(void)
{
    char cmd;
    short qret = TRUE;
    ulong input_var;

    printf("\n/* MBARI Sensor Controller File Menu [H] for Help */");

    while (qret == TRUE) {
        printf("\n");
        qret = QueryChar("MSC_FILE>", NULL, "^[?ABCDEFGHI", &cmd);
        printf("\n");

        if (qret == TRUE) {
            switch (cmd) {

            case '^[':
                break;

            case 'A':
                input_var = 1;
                QueryNum
                    ("\n  Select Directory:\n  [1] ROOT\n  [2] ISUS\n  [3] DURA\n  Enter>",
                     "%lu", "%lu", &input_var);
                if (input_var != 0) {
                    if (input_var == 1)
                        execstr("DIR C: /P /4\\");
                    else if (input_var == 2)
                        execstr("DIR C:\\ISUS /P /4");
                    else if (input_var == 3)
                        execstr("DIR C:\\DURA /P /4");
                    else
                        RangeError(1L, 3L);
                }
                break;

            case 'B':
                DownloadFile();
                break;

            case 'C':
                UploadFile();
                break;

            case 'D':
                DisplayFile();
                break;

            case 'E':
                RenameFile();
                break;

            case 'F':
                if (SysPass() == TRUE) {        //make it bozo proof
                    EraseFile();
                }
                break;

            case 'G':
                if (SysPass() == TRUE) {        //make it bozo proof
                    EraseFiles();
                }
                break;
				
			case 'I':
                ScanDir();
                break;	

            case '?':
            case 'H':
                printf(" %s\n %s\n %s\n %s\n %s\n %s\n %s\n %s\n",
                       "[A] Directory",
                       "[B] Download",
                       "[C] Upload",
                       "[D] Output",
                       "[E] Rename", "[F] Erase", "[G] Multi Erase", "[I] Scan Dir");
                break;

            }                   //end switch

        }                       //end qret if

    }                           //end while

}

void DownloadFile(void)
{
    char fnbuf[MAXINBUFF];
	char fpbuf[MAXINBUFF];
    char pico_cmd[85];          //PicoDOS command line buff is only 81 chars
    int ret, verbose;
    ulong proto, baud;
    long old_baud, new_baud;
    ushort old_freq, new_freq;
    FILE *flash_file;

    printf("\n/* File Download */");

    proto = 0;
    QueryNum("\nSelect Protocol:\n  [1] Xmodem\n  [2] Ymodem\n  [3] Zmodem\n  Enter>",
             "%lu", "%lu", &proto);
    if ((proto < 1) || (proto > 3)) {
        printf("\nNo Protocol Selected\n");
        return;
    }

    if ((proto == 1) || (proto == 2)) {
        printf("\n\nEnter Filename: ");
        ret = safegets(fnbuf, MAXREAD);
        if (ret != 0) {
            flash_file = fopen(fnbuf, "r");
            if (flash_file == FNULL) {
                printf("Could not locate file: %s\n", fnbuf);
                return;
            }
            fclose(flash_file);
        } else {
            printf("No name was entered\n");
            return;
        }
    }                           //end proto=1or2

    else if ( proto == 3 ) {
		memset(fpbuf, '\0', sizeof(fpbuf));
		printf("\n\nEnter Filepath [\\]: ");
        safegets(fpbuf, MAXREAD);

		memset(fnbuf, '\0', sizeof(fnbuf));
        printf("\nEnter Filename [*.LOG]: ");
		ret = safegets(fnbuf, MAXREAD);
		if (ret == 0) {
			fnbuf[0] = '*';
			fnbuf[1] = '.';
			fnbuf[2] = 'L';
			fnbuf[3] = 'O';			
			fnbuf[4] = 'G';			
		}

		verbose = 0;
		if( QueryYesNo("\nVerbose Mode", FALSE) )
			verbose = 5;
    }                     
	
    baud = 0;
    QueryNum
        ("\n\nSelect BaudRate:\n  [0] CurrentBaud\n  [1] 9600\n  [2] 19200\n  [3] 38400\n  [4] 57600\n  [5] 115200\n  Enter>",
         "%lu", "%lu", &baud);
    if ((baud < 0) || (baud > 5)) {
        printf("\nInvalid BaudRate Selected\n");
        return;
    }

    old_freq = TMGGetSpeed();
    old_baud = SCIGetConfig(NULL, NULL);

    if (baud != 0) {
        printf("\n\nSwitching BaudRate in 30 secs!\n");
        DelaySecs(30);
        if (baud == 1)
            new_baud = SCIConfigure(9600L, 'N', false);
        if (baud == 2)
            new_baud = SCIConfigure(19200L, 'N', false);
        if (baud == 3)
            new_baud = SCIConfigure(38400L, 'N', false);
        if (baud == 4)
            new_baud = SCIConfigure(57600L, 'N', false);
        if (baud == 5) {
            new_freq = TMGSetSpeed(14720);
            new_baud = SCIConfigure(115200L, 'N', false);
        }
        printf("BaudRate switched to %ld\n", new_baud);
    } 

    if (proto == 1) {
        printf("\n\nStarting XMODEM Transfer of: %s\n", fnbuf);
        sprintf(pico_cmd, "XS -Q %s", fnbuf);
        execstr(pico_cmd);
    } else if (proto == 2) {
        printf("\n\nStarting YMODEM Transfer of: %s\n", fnbuf);
        sprintf(pico_cmd, "YS -Q %s", fnbuf);
        execstr(pico_cmd);
    } else if (proto == 3) {
  		printf("\n\nStarting ZMODEM Transfer of: %s\\%s\n", fpbuf, fnbuf);
		ret = zmSendFiles((char *)fpbuf, (char *)fnbuf, verbose, 1024, 0);
		printf("\n\nZModem finished, errcnt = %d\n", ret);
    }

    if (baud != 0) {
        printf("Switching BaudRate back to %ld in 30 secs!\n", old_baud);
        DelaySecs(30);
        if (baud == 5)
            new_freq = TMGSetSpeed(old_freq);
        SCIConfigure(old_baud, 'N', false);
    }

    printf("\nDownload complete\n");

}

void UploadFile(void)
{
    char fnbuf[MAXINBUFF];
    char cmdbuff[MAXINBUFF];
    char legal[] = ":\\.-_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    int lchar, ret, i;
    size_t lentot;
    ulong input_var;

    input_var = 0;
    QueryNum
        ("\n  Select File:\n  [1] ISUS ECOEF File\n  [2] ISUS ZCOEF File\n  [3] OTHER\n  Enter>",
         "%lu", "%lu", &input_var);
    if (input_var != 0) {
        //ECOEFS
        if (input_var == 1) {
            if (pdx_access("C:\\ISUS\\ECOEFZER.CAL", _A_ANY) == OK) {
                printf
                    ("\nECOEF File already exists, either erase or rename it!\n");
                return;
            }
            printf("\nBegin XMODEM Upload of ECOEF File\n");
            sprintf(cmdbuff, "XR -X C:\\ISUS\\ECOEFZER.CAL");
            execstr(cmdbuff);
            if (Zcoefs > 0) {
                Ecoefs =
                    LoadIsusECoefs(NULL, EcoefFile, ZeroData, TcompCoefs,
                                   &SwCalTemp, CalDate);
                CopyEcoefs(EcoefFile, EcoefData);
            } else {
                Ecoefs =
                    LoadIsusECoefs(LambdaData, EcoefFile, ZeroData,
                                   TcompCoefs, &SwCalTemp, CalDate);
                CopyEcoefs(EcoefFile, EcoefData);
            }
            if (Ecoefs == 256)
                printf
                    ("\n  Cal Date = %s, Cal Temp = %.2lf, %d Coefs were loaded\n",
                     CalDate, SwCalTemp, Ecoefs);
            else
                printf("\n  Coef File could NOT be loaded!, Ret = %d\n",
                       Ecoefs);
        }
        //ZCOEFS
        else if (input_var == 2) {
            if (pdx_access("C:\\ISUS\\ZCOEFDAT.CAL", _A_ANY) == OK) {
                printf
                    ("\nZCOEF File already exists, either erase or rename it!\n");
                return;
            }
            printf("\nBegin XMODEM Upload of ZCOEF File\n");
            sprintf(cmdbuff, "XR -X C:\\ISUS\\ZCOEFDAT.CAL");
            execstr(cmdbuff);
            Zcoefs = LoadIsusZCoefs(ZeissCoefs);
            if (Zcoefs > 0) {
                InitLambdaArray(LambdaData, ZeissCoefs);
                printf
                    ("\n  %d Coefs found, Wavelength Array Initialized\n",
                     Zcoefs);
            } else
                printf
                    ("\n  Coef File (ZCOEFDAT.CAL) could NOT be loaded!\n");
        }
        //OTHER
        else if (input_var == 3) {
            printf("\nEnter Filename: ");
            ret = safegets(fnbuf, MAXREAD);
            if (ret != 0) {
                strupr(fnbuf);
                lentot = strlen(fnbuf);
                for (i = 0; i < lentot; i++) {
                    lchar = fnbuf[i];
                    if (strchr(legal, lchar) == NULL) {
                        printf("\nIllegal char %c at %d in filename!\n",
                               lchar, i + 1);
                        return;
                    }
                }
                if (pdx_access(fnbuf, _A_ANY) == OK) {
                    printf
                        ("\n%s already exists, either erase or rename it!\n",
                         fnbuf);
                    return;
                }
                printf("\nStarting XMODEM Upload of %s\n", fnbuf);
                sprintf(cmdbuff, "XR -X %s", fnbuf);
                execstr(cmdbuff);
            }                   // end ret if
            else {
                printf("\nNo name was entered\n");
                return;
            }
        } else
            RangeError(1L, 3L);
    }

    printf("\n");

}

void DisplayFile(void)
{
    char fnbuf[MAXINBUFF];
    char cmdbuff[MAXINBUFF];
    int ret;
    FILE *flash_file;

    printf("\nEnter Filename: ");
    ret = safegets(fnbuf, MAXREAD);
    if (ret != 0) {
        flash_file = fopen(fnbuf, "r");
        if (flash_file == FNULL) {
            printf("\nCould not locate file: %s\n", fnbuf);
            return;
        }
        fclose(flash_file);
    } else {
        printf("\nNo name was entered\n");
        return;
    }

    printf("\nOutputFileStart\n");
    sprintf(cmdbuff, "type %s", fnbuf);
    execstr(cmdbuff);
    printf("OutputFileEnd\n");

}

void RenameFile(void)
{
    char fn1buf[MAXINBUFF];
    char fn2buf[MAXINBUFF];
    char legal[] = ":\\.-_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    int lchar, ret, i;
    size_t lentot;
    FILE *flash_file;

    printf("\nEnter Old Filename: ");
    ret = safegets(fn1buf, MAXREAD);
    if (ret != 0) {
        flash_file = fopen(fn1buf, "r");
        if (flash_file == FNULL) {
            printf("\nCould not locate file: %s\n", fn1buf);
            return;
        }
        fclose(flash_file);
    } else {
        printf("\nNo name was entered\n");
        return;
    }

    printf("\nEnter New Filename: ");
    ret = safegets(fn2buf, MAXREAD);
    if (ret != 0) {
        strupr(fn2buf);
        lentot = strlen(fn2buf);
        for (i = 0; i < lentot; i++) {
            lchar = fn2buf[i];
            if (strchr(legal, lchar) == NULL) {
                printf("\nIllegal char %c at %d in filename!\n", lchar,
                       i + 1);
                return;
            }
        }
    }                           // end ret if
    else {
        printf("\nNo name was entered\n");
        return;
    }

    if (strcmp(strupr(fn1buf), fn2buf) == 0) {
        printf("\nFilenames are the same!\n");
        return;
    }

    rename(fn1buf, fn2buf);

    flash_file = fopen(fn2buf, "r");
    if (flash_file == FNULL) {
        printf("\n%s could not be renamed!\n", fn1buf);
        return;
    }
    fclose(flash_file);
    printf("\n%s was renamed to %s!\n", fn1buf, fn2buf);

}

void EraseFile(void)
{
    char fnbuf[MAXINBUFF];
    int ret;
    ulong input_var;
    FILE *flash_file;

    printf("\nEnter Filename: ");
    ret = safegets(fnbuf, MAXREAD);
    if (ret != 0) {
        flash_file = fopen(fnbuf, "r");
        if (flash_file == FNULL) {
            printf("\nCould not locate file: %s\n", fnbuf);
            return;
        }
        fclose(flash_file);
    } else {
        printf("\nNo name was entered\n");
        return;
    }

    printf("\n");
    input_var = 0;
    input_var = QueryYesNo(fnbuf, TRUE);
    if (input_var == TRUE) {
        printf("\nBe patient it could take awhile.\n");
        ret = remove(fnbuf);
        if (ret == 0)
            printf("\n%s Was Deleted!\n", fnbuf);
        else
            printf("\n%s Could NOT be Deleted!\n", fnbuf);
    }

}

void EraseFiles(void)
{

}

void ScanDir( void )
{
	char name[128];
	char fnbuf[MAXINBUFF];
	char fpbuf[MAXINBUFF];
    struct dirent	de;
	struct stat fs;
	FILE *infp = NULL;
    int	   i, ret;
	
	memset(fpbuf, '\0', sizeof(fpbuf));
	printf("\n\nEnter Filepath: ");
    ret = safegets(fpbuf, MAXREAD);
    if (ret == 0) {
		fpbuf[0] = 'C';
		fpbuf[1] = ':';
    }
		
	memset(fnbuf, '\0', sizeof(fnbuf));
    printf("\n\nEnter Filename: ");
    ret = safegets(fnbuf, MAXREAD);
    if (ret == 0) {
		fnbuf[0] = '*';
		fnbuf[1] = '.';
		fnbuf[2] = '*';
    }

	i=0;
	printf("\n\n");
    if (DIRFindFirst((char *)fpbuf, &de) != dsdEndOfDir)
    {
	do
	{
	    if (DIRMatchName((char *) de.d_name, (char *)fnbuf)){
			if ((de.d_attr & (_A_SUBDIR | _A_VOLID)) == 0){	
				memset(name, '\0', sizeof(name));
				snprintf(name, sizeof(name), "%s\\%s", fpbuf, (char *)de.d_name);
				if ((infp=fopen(name, "r"))!=NULL){
					++i;
					stat(name, &fs);
					printf("%3d  %24s %12ld  %s", i, (char *)fs.st_path, fs.st_size, ctime(&fs.st_mtime) );
					fclose(infp);
				}	
			}		
        }
	} while (DIRFindNext(&de) != dsdEndOfDir);
    }

    DIRFindEnd(&de);
	
    return;
}

//*****************************************************************************
// Misc Menu Routines
//*****************************************************************************

void FNameError(void)
{
    printf("Filename can only have 8 char NAME and 3 char EXTENSION!\n");
}

void RangeError(long val1, long val2)
{
    printf("\n  Invalid Entry, valid range = %ld - %ld\n", val1, val2);
}

int SysPass(void)
{
    char inbuff[MAXINBUFF];
    ulong input_var;

    input_var = -1;
    printf("\n  Enter Password: ");
    if ((safegets(inbuff, MAXREAD)) != 0) {
        input_var = strcmp(strupr(inbuff), "DUNLIN");
    }
    if (input_var == 0)
        return (TRUE);
    else {
        printf("\n  Invalid Password!\n");
        return (FALSE);
    }
}
