//==============================================================================
//    S E N S I R I O N   AG,  Laubisruetistr. 50, CH-8712 Staefa, Switzerland
//==============================================================================
/// \version   1.0
/// \author    UBR, RFU
/// \date      24. Nov 2014
//==============================================================================

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SFC5xxxDevice.h"

int main(void)
{
  // define local variables
  const char* conStr = "COM6, 115200, ECHOOFF";   // <-- define the COM-port and the baudrate here
  const uint8_t deviceAdr = 0;                    // <-- define the device slave address here
  uint32_t portHandle = 0;
  uint32_t error = 0;
  float currentSetpoint = 0.0f;
  float measuredFlow = 0.0f;
  char productName[256];
  char inputStr[256];

  // print out header
  printf("SFC5xxx Demo CLI\n");
  printf("================\n");
  printf("Connection parameters: %s\n", conStr);
  printf("Device address:        %d\n", deviceAdr);
  printf("\n");

  // load DLLs
  if (SFC5xxxDevice_LoadLibrary() != 0)
  {
    printf("Error while loading DLL.\n");
    printf("\nPress [Enter] to exit the program.\n");
    while (getchar() != '\n');
    return -1;
  }

  // open the com port
	error = Shdlc_OpenPort(0, (char*)conStr, &portHandle);

  // if no error has occurred, read device product name
  if(error == 0)
  {
    error = Shdlc_GetDeviceInfo(portHandle, deviceAdr, 500, 1, productName, 256);
  }

  // if no error has occurred, print the device product name and further instructions
  if(error == 0)
  {
    printf("Connected successfully to device '%s'.\n", productName);
    printf("Press [Enter] to read the measured flow. To set a new setpoint is optional.\n");
    printf("Enter any character as setpoint to abort.\n\n");

    // read current setpoint (normalized)
    error = SFC5xxx_GetSetpoint(portHandle, deviceAdr, &currentSetpoint, 0);
  }

  // set setpoint and read measured flow in a loop
  while (error == 0)
  {
    // read current flow (normalized)
    error = SFC5xxx_ReadMeasuredValue(portHandle, deviceAdr, 0, &measuredFlow);
    if (error == 0)
    {
      printf("Measured flow: %6.2f%%\t\t", measuredFlow * 100.0f);
      printf("Set setpoint [%6.2f%%]: ", currentSetpoint * 100.0f);

      // read next setpoint or abort loop
      gets_s(inputStr, 256);
      if (strnlen(inputStr, 256) > 0)
      {
        if (sscanf_s(inputStr, "%f", &currentSetpoint))
        {
          // set new setpoint (normalized)
          currentSetpoint /= 100.0f; // normalize to 0..1
          error = SFC5xxx_SetSetpoint(portHandle, deviceAdr, currentSetpoint, 0);
        }
        else
        {
          // abort loop
          break;
        }
      }
    }
  }

  // print the error message when an error occurred.
  if (error != 0)
  {
    printf("\n");
    printf(SFC5xxx_TranslateErrorCode(error));
    printf("\n");
  }

  // close the com port and release the DLLs
  Shdlc_ClosePort(portHandle);
  SFC5xxxDevice_ReleaseLibrary();

  // wait for the enter key to exit the application
  printf("\nPress [Enter] to exit the program.\n");
  while (getchar() != '\n');

  return 0;
}