/* Set Analog Output for I-9024U and I-9028U modules.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

/* File level history (record changes for this file here.)
   v 0.0.0  1 Sep 2018  */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "msw.h"
#include "i8028U.h"
#define iTalkVersion	15

int main(int argc, char **argv)
{
  char GainStr[6][32]={"0~ 5V", "0~ 10V", "+/- 5V", "+/- 10V", "4~ 20mA", "0~ 20mA"};
	int ret=-1, slot, channel;
	//short enStatus = 1;
	//unsigned char c;
	//char temp[128];
	float value, powerVal=5, f;
	short gain=0, RetValue;
	
	if (argc != 5) {
		//printf("ICPDAS iTalk utility v%d\n", iTalkVersion);
		printf("Function: setao_9024U_9028U\n");
		printf("Set analog analog output value to a module\n");
		printf("AO Range:\n");
    printf("\tGain 0: 0~ 5V AO   \tGain 1: 0~ 10V AO\n");
	  printf("\tGain 2: +/- 5V AO  \tGain 3: +/- 10V AO\n");
	  printf("\tGain 4: 4~ 20mA AO \tGain 5: 0~ 20mA AO\n");
		printf("Channel Range: I-9024U Channel index 0~3, I-9028U Channel index 0~7\n\n");
		
		printf("Usage: setao_9024U_9028U slot channel gain data\n");
		printf("Example: setao_9024U_9028U 1 2 0 1.2\n");
		printf("Set the analog value 1.2 to channel 2 of slot 1\n");
		return FAILURE;
	}
	
	slot=atoi(argv[1]);
	channel=atoi(argv[2]);
	gain=atoi(argv[3]);
	value=atof(argv[4]);
	printf("slot=%d channel=%d gain=%d value=%f\n",slot,channel,gain, value);
	
	/* open device file */
	RetValue = Open_Slot(slot);
	if (RetValue >0) {
		printf("open Slot %d failed!\n",slot);
		return FAILURE;
	}
	

	ret=i8028U_Init(slot);

  if (ret==0){	
	   i9028U_WriteAO(slot,channel,gain, value);
	   printf("Write I9024U voltage value= %f \n", value);
  } else
     printf("I9024U_Init error, ret= %d \n",ret);

  //i9028U_WriteAOHex(slot,channel+2,gain, value);
	
	Close_Slot(slot);
	return SUCCESS;
}
