#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include "usb2200.h"

int main(int argc, char **argv)
{
	int handle, err;
  WORD pid = USB2255_32;
  BYTE bid = 0x0;
  QWORD val;

  if (argc != 2) {
    printf("Usage: %s <value>\n", argv[0]);
    printf("  value: decimal (123) or hex (0xFF)\n");
    return 0;
  }

  if (argv[1][0] == '-') {
    printf("Error: negative value not allowed.\n");
    return 0;
  }

  QWORD value = (QWORD)strtoull(argv[1], NULL, 0);
  
  handle = USB2200_OpenDevice(pid, bid);
	if(handle < 0) {
		printf("open failed, error code %d\n", handle);
		return 0;
	}

  err = USB2200_DO_WriteValue(handle, value);
  if(err) {
    printf("USB2200_DO_WriteValue Error number %d\n", err);
  }

  err = USB2200_DO_ReadValue(handle, &val);
  if(err) {
    printf("USB2200_DO_ReadValue Error number %d\n", err);
  }

  printf("Read DO value 0x%lx\n",val);

  USB2200_CloseDevice(handle);

	return 0;
}

