/*
 * 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 systemSamplingData sys_samp;


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("\r\nError:  sscanf for parsing Aanderaa 5860 does not return 7 args.  ctd.type = %u.  TempC and salt are 0.0\r\n", ctd.type);
        uprintf("\r\nCheck setup of Aanderaa 5860, enable derived, raw, etc.   To Fix, use Test Menu, Communicate with Instrument\r\n");
        condo = tempC = salt = 0.0;
    }

    sys_samp.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("\r\nNote: smart sensor mode, disable data descriptor text, set flow control to none \r\n\r\n");
    uprintf("With enable text(1), output has field descriptors and units:\r\n");
    uprintf("do sample\r\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\r\n\r\n");
    uprintf("\r\n\r\nCommand procedure to setup Aanderaa Conductivty 5860 for numeric output:\r\n\r\n");
    uprintf("set passkey(1)\r\n");
    uprintf("set mode(smart sensor terminal)\r\n");
    uprintf("set enable polled mode(yes)\r\n");
    uprintf("save\r\n");
    uprintf("\r\n");
    uprintf("set passkey(1)\r\n");
    uprintf("set enable text(no)\r\n");
    uprintf("save\r\n");
    uprintf("\r\n");
    uprintf("set passkey(1000)\r\n");       // response: #
    uprintf("set Flow Control(none)\r\n");  // response: #
    uprintf("save\r\n");
    uprintf("\r\n");

    uprintf("do sample\r\n");
    uprintf("\r\nThis should return a sample: \t5860\t29\t-0.003\t16.892\t0.009\t998.802\t1472.435\t\r\n\r\n");
    //uprintf("                               \t5860\t29\t-0.003\t16.892\t0.009\t998.802\t1472.435\t\r\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("\r\n\r\n*Press CTRL-X to stop communicating with the Conductivity instrument.*\r\n\r\n");

    //set up the condo
    ctd_init();                 // fixes crash in 4.0.2 30 Dec 2021
    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
}
