#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <errno.h>

#define BAUDRATE B57600
#define MODEMDEVICE "/dev/cua0"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1

volatile int STOP=FALSE;
void SetPVA(unsigned long p, unsigned long v, unsigned long a);
void StopMotor(unsigned char value);

int fd;

main(int argc, char **argv)
{
  int i,count,ret;
  int loop;
  unsigned char buf[40];
  struct termios oldtio,newtio;
 unsigned long p; unsigned long v; unsigned long a;

 argc--;*argv++;
 if(argc-- > 0) p= atol(*argv++);
 if(argc-- > 0) v= atol(*argv++);
 if(argc-- > 0) a= atol(*argv++);
 printf("Setting values to  %ld %ld %ld\n",p,v,a);

 fd = open(MODEMDEVICE, O_RDWR);
 if (fd <0) {perror(MODEMDEVICE); exit(-1); }

 tcgetattr(fd,&oldtio); /* save current port settings */

 /* bzero(newtio, sizeof(newtio)); */
 newtio.c_cflag = BAUDRATE | CS8 | CREAD;
 newtio.c_iflag = 0;
 newtio.c_oflag = 0;

 /* set input mode (non-canonical, no echo,...) */
 newtio.c_lflag = 0;

 newtio.c_cc[VTIME]    = 0;   /* inter-character timer unused */
 newtio.c_cc[VMIN]     = 1;   /* blocking read until 1 chars received */

 tcflush(fd, TCIFLUSH);
 tcsetattr(fd,TCSANOW,&newtio);

 SetPVA(p,v,a);
 StopMotor(1);

#if(0)
for(loop=0;loop<30;loop++) {
 RequestStatus ();
 count = 0;
 do {
   ret = read(fd,&buf[count],3-count);
   count += ret;
  } while (count < 3);
  printf("0x%02x 0x%02x\n",buf[0],buf[1]);
}
sleep(2);
for(loop=0;loop<30;loop++) {
 RequestStatus ();
 count = 0;
 do {
   ret = read(fd,&buf[count],3-count);
   count += ret;
  } while (count < 3);
  printf("0x%02x 0x%02x\n",buf[0],buf[1]);
}
#endif
 tcsetattr(fd,TCSANOW,&oldtio);
}



void
SetPVA(unsigned long p, unsigned long v, unsigned long ac){
    unsigned char a[40];
    int i = 0;
    int j;
    unsigned char sum = 0;
	int ret;

    a[i++] = 0xAA;
    a[i++] = 0x02;
    a[i++] = 0xd4;
    a[i++] = 0x97;

    a[i++] = p & 0xFF;
    a[i++] = (p >>  8) & 0xff;
    a[i++] = (p >> 16) & 0xff;
    a[i++] = (p >> 24) & 0xff;

    a[i++] = v & 0xFF;
    a[i++] = (v >>  8) & 0xff;
    a[i++] = (v >> 16) & 0xff;
    a[i++] = (v >> 24) & 0xff;

    a[i++] = ac & 0xFF;
    a[i++] = (ac >>  8) & 0xff;
    a[i++] = (ac >> 16) & 0xff;
    a[i++] = (ac >> 24) & 0xff;

	sum = 0;
    for(j=1;j<i;j++) sum += a[j];
    a[i] = sum & 0xFF;

    for(j=0;j<=i;j++) {
		ret = write(fd,&a[j],1);
		if(ret != 1) {
			printf("write returned %d errno = %d\n",ret,errno);
			perror("Write: ");
		}
	}
	ReadStatus();
}

ReadStatus () {
    unsigned char buf[4];
    int ret;
    int count = 0;

 do {
   count += read(fd,&buf[count],2-count);
  } while (count < 2);
	printf("ReadStaus: count = %d 0x%02x %02x\n",count,buf[0],buf[1]);
}
ReadAuxStatus () {
    unsigned char buf[4];
    int ret;
    int count = 0;

 do {
   count += read(fd,&buf[count],3-count);
  } while (count < 3);
	printf("ReadAuxStaus: count = %d 0x%02x 0x%02x 0x%02x\n"
		,count,buf[0],buf[1],buf[2]);
}

RequestStatus () {
    unsigned char a[40];
    int i = 0;
    int j;
    unsigned char sum = 0;
	int ret;

    a[i++] = 0xAA;
    a[i++] = 0x02;
    a[i++] = 0x13;
    a[i++] = 0x08;

	sum = 0;
    for(j=1;j<i;j++) sum += a[j];
    a[i] = sum & 0xFF;

    for(j=0;j<=i;j++) {
		ret = write(fd,&a[j],1);
		if(ret != 1) {
			printf("write returned %d errno = %d\n",ret,errno);
			perror("Write: ");
		}
	}
}

void
StopMotor(unsigned char value){
    unsigned char a[40];
    int i = 0;
    int j;
    unsigned char sum = 0;
	int ret;

    a[i++] = 0xAA;
    a[i++] = 0x01;
    a[i++] = 0x17;
    a[i++] = value;

	sum = 0;
    for(j=1;j<i;j++) sum += a[j];
    a[i] = sum & 0xFF;

    for(j=0;j<=i;j++) {
		ret = write(fd,&a[j],1);
		if(ret != 1) {
			printf("write returned %d errno = %d\n",ret,errno);
			perror("Write: ");
		}
	}
	ReadStatus();
}
