/*
 * ctd_Aand5860.c
 *
 *  Created on: May 2, 2021
 *      Author: thomm
 */


#include "system.h"
#include "uartstdio.h"
#include "ctd_base.h"
#include "ctd_aand5860.h"

//(open / power on)
//   time = 240 msec
//%
//!do sample
//   time = 1460 msec
//MEASUREMENT     5860    29      Conductivity[mS/cm]     -0.005  Temperature[Deg.C]      24.152  Salinity[PSU]   0.012   Density[kg/m3]  997.270 Soundspeed[m/s] 1494.439
//5860    29      -0.001  26.054  0.012   996.780 1499.510
//#


#ifdef FORSIMULATOR
ctd serial analyzer, bytes and times  Ctrl-X to quit
open
RX: % [0x25]  time = 0 msec
TX: crlf
RX: % [0x25]  time = 240 msec
TX: crlf
RX: ! [0x21]  time = 10 msec
TX: crlf do sample crlf
RX: 5 [0x35]  time = 1460 msec
RX: 8 [0x38]  time = 0 msec
RX: 6 [0x36]  time = 0 msec
RX: 0 [0x30]  time = 0 msec
RX:      [0x09]  time = 10 msec
RX: 2 [0x32]  time = 0 msec
RX: 9 [0x39]  time = 0 msec
RX:      [0x09]  time = 0 msec
RX: - [0x2d]  time = 0 msec
RX: 0 [0x30]  time = 0 msec
RX: . [0x2e]  time = 0 msec
RX: 0 [0x30]  time = 0 msec
RX: 0 [0x30]  time = 0 msec
RX: 2 [0x32]  time = 0 msec
RX:      [0x09]  time = 10 msec
RX: 2 [0x32]  time = 0 msec
RX: 5 [0x35]  time = 0 msec
RX: . [0x2e]  time = 0 msec
RX: 5 [0x35]  time = 0 msec
RX: 8 [0x38]  time = 0 msec
RX: 9 [0x39]  time = 0 msec
RX:      [0x09]  time = 0 msec
RX: 0 [0x30]  time = 0 msec
RX: . [0x2e]  time = 0 msec
RX: 0 [0x30]  time = 10 msec
RX: 1 [0x31]  time = 0 msec
RX: 2 [0x32]  time = 0 msec
RX:      [0x09]  time = 0 msec
RX: 9 [0x39]  time = 0 msec
RX: 9 [0x39]  time = 0 msec
RX: 6 [0x36]  time = 0 msec
RX: . [0x2e]  time = 0 msec
RX: 9 [0x39]  time = 10 msec
RX: 0 [0x30]  time = 0 msec
RX: 3 [0x33]  time = 0 msec
RX:      [0x09]  time = 0 msec
RX: 1 [0x31]  time = 0 msec
RX: 4 [0x34]  time = 0 msec
RX: 9 [0x39]  time = 0 msec
RX: 8 [0x38]  time = 0 msec
RX: . [0x2e]  time = 0 msec
RX: 2 [0x32]  time = 0 msec
RX: 9 [0x39]  time = 10 msec
RX: 5 [0x35]  time = 0 msec
 [0x0d]  time = 0 msec
RX:
 [0x0a]  time = 0 msec
RX: # [0x23]  time = 0 msec
 [0x0d]  time = 0 msec
RX:
 [0x0a]  time = 0 msec
#endif




extern struct ctdDriver ctd;

/*
 * ctd_poll() tells the Optode to "do_sample."  Data is received
 * via the UART3 interrupt handler.
 */
void ctd_Aand5860_poll(void)
{

    ctd_write("do sample\r\n",12 );
    //ROM_SysCtlDelay(1000*MILLISECOND); //add 800 to try to limit "SYNTAX ERROR"

}



/*
 * Parse the data returned by the Aanderaa 5860 Conductivity sensor and put it in the output form we'd like (tab delimited etc.).
 * While parsing, move the data out of poll_Buffers.condoBuffer and into the instrument buffer which
 * will have all of our measurements.
 */

void ctd_Aand5860_parseData(void)
{
    unsigned int model, sernum;
    float condo, tempC, salt, density, speedSound;

    //if( sscanf( ctd.buf, "%u\t%u\t%f\t%f\t%f\t%f\t%f", &model, &sernum, &condo, &tempC, &salt, &density, &speedSound) != 7)
    if( sscanf( ctd.buf, "%u %u %f %f %f %f %f", &model, &sernum, &condo, &tempC, &salt, &density, &speedSound) != 7)
    {
        uprintf("\nError:  sscanf for parsing Aanderaa 5860 does not return 7 args.  ctd.type = %u.  TempC and salt are 0.0\n", ctd.type);
        uprintf("\nCheck setup of Aanderaa 5860, enable derived, raw, etc.   To Fix, use Test Menu, Communicate with Instrument\n");
        condo = tempC = salt = 0.0;
    }

    sys_data.sensor_sal = salt;     // sys_data.sensor_sal is used when CTD is configured for vrse ph calc called in fast_sample in sample.c

    ctd.condo = condo;
    ctd.tempC = tempC;
    ctd.salt = salt;

}


/*
 * ctd_Aand5860_comm_test is called to communicate directly with the instrument thru the users console port
 */

void ctd_Aand5860_comm_test(void)
{
    unsigned char charInput;

#ifdef NOCODE
    Enable Derived Parameters: Controls inclusion of Salinity, Density and Speed of sound in the output string
    Enable Rawdata:  Controls inclusion of Controls inclusion of Conductivity in the output string
    Enable Temperature:
    set enable text(yes) to see data header
#endif

    uprintf("\nNote: smart sensor mode, disable data descriptor text, set flow control to none \n\n");
    uprintf("With enable text(1), output has field descriptors and units:\n");
    uprintf("do sample\n");
    uprintf("MEASUREMENT     5860    29      Conductivity[mS/cm]     -0.000  Temperature[Deg.C]      16.021  Salinity[PSU]   0.008   Density[kg/m3]  998.947 Soundspeed[m/s] 1469.494\n\n");
    uprintf("\n\nCommand procedure to setup Aanderaa Conductivty 5860 for numeric output:\n\n");
    uprintf("set passkey(1)\n");
    uprintf("set mode(smart sensor terminal)\n");
    uprintf("set enable polled mode(yes)\n");
    uprintf("save\n");
    uprintf("\n");
    uprintf("set passkey(1)\n");
    uprintf("set enable text(no)\n");
    uprintf("save\n");
    uprintf("\n");
    uprintf("set passkey(1000)\n");       // response: #
    uprintf("set Flow Control(none)\n");  // response: #
    uprintf("save\n");
    uprintf("\n");

    uprintf("do sample\n");
    uprintf("\nThis should return a sample: \t5860\t29\t-0.003\t16.892\t0.009\t998.802\t1472.435\t\n\n");
    //uprintf("                               \t5860\t29\t-0.003\t16.892\t0.009\t998.802\t1472.435\t\n");
#ifdef GETALLOUTPUT
    get all
    Product Name    5860    29      Conductivity Sensor
    Product Number  5860    29      5860
    Serial Number   5860    29      29
    SW ID   5860    29      1940039
    SW Version      5860    29      5       5       1
    Enable Conductivity     5860    29      Yes
    Node Description        5860    29      Conductivity Sensor #29
    Owner   5860    29
    Mode    5860    29      Smart Sensor Terminal
    Enable Sleep    5860    29      Yes
    Enable Polled Mode      5860    29      Yes
    Enable Text     5860    29      No
    Enable Decimalformat    5860    29      Yes
    Enable Temperature      5860    29      Yes
    Enable Rawdata  5860    29      No
    Enable Derived Parameters       5860    29      Yes
    Interval[s]     5860    29      10.000
    Location        5860    29
    Geographic Position     5860    29      36.8044, -121.7869
    Vertical Position       5860    29
    Reference       5860    29
    Pressure        5860    29      0.000000E+00
#endif



    uprintf("\n\n*Press CTRL-X to stop communicating with the Conductivity instrument.*\n\n");

    //set up the condo
    ctd_open();               // power on optode
    ROM_SysCtlDelay(500);       //500 is a guess

    while(1)
    {
        //if condo sent anything, print it out on the console
        if( ctd_rxBytesAvail() )      // condo
        {
            uprintf( "%c", ctd_getc() );
        }

        //if user types anything on console, send it to the condo -- exit on CTRL-X
        if(UARTRxBytesAvail())
        {
            charInput = UARTgetc();

            if(charInput == 13)     // If user hit carriage return, add a line feed
            {
                ctd_putc(charInput);  // CR
                ctd_putc(10);     // LF
            }
            else if(charInput == 24)
            {
                break;
            }
            else
            {
                ctd_putc(charInput);
            }
        }
    }

    ctd_close();  // Close up the Optode
}
