/**


simple iobox test program.
just reads the adc and buttons.

(c) ch. klippel

based on :

A USB interface to the Microchip PICkit 1 FLASH Starter Kit
device programmer and breadboard.

Orion Sky Lawlor, olawlor@acm.org, 2003/8/3
Updated 2004/1/19: select alternate configuration.
*/
#include <usb.h> /* libusb header */
#include <unistd.h> /* for geteuid */

/* PICkit USB values */
const static int multio_vendorID=0xdead; // Microchip, Inc
const static int multio_productID=0xbeef; // PICkit 1 FLASH starter kit
const static int multio_configuration=2; /* 1: HID; 2: vendor specific */
const static int multio_interface=0;

const static int reqLen=8;
typedef unsigned char byte;

void bad(const char *why) {
	printf("Fatal error> %s\n",why);
	exit(17);
}

int main(void)
{
  struct usb_device *device;
  struct usb_bus* bus;
	      unsigned char buffer[8];
	      int bytesread;

  if (geteuid()!=0)
    bad("This program must be run as root, or made setuid root");

#ifdef USB_DEBUG
  usb_debug=4;
#endif
  printf("Locating mamalala multI/O (vendor 0x%04x/product 0x%04x)\n",
  	multio_vendorID,multio_productID);
  /* (libusb setup code stolen from John Fremlin's cool "usb-robot") */
  usb_init();
  usb_find_busses();
  usb_find_devices();

  for (bus=usb_busses;bus!=NULL;bus=bus->next)
  {
    struct usb_device* usb_devices = bus->devices;
    for(device=usb_devices;device!=NULL;device=device->next)
    {
      if (device->descriptor.idVendor == multio_vendorID
        &&device->descriptor.idProduct == multio_productID)
      {
	  usb_dev_handle *d;
	  printf( "Found mamalala multI/O as device '%s' on USB bus %s\n",
		   device->filename,
		   device->bus->dirname);
          d=usb_open(device);
	  if (d)
	  { /* This is our device-- claim it */
	      byte retData[reqLen];
	      if (usb_set_configuration(d,multio_configuration)) {
	          bad("Error setting USB configuration.\n");
	      }
	      else printf("Selecting non-HID config\n");
	      if (usb_claim_interface(d,multio_interface)) {
	          bad("Claim failed-- the mamalala multI/O is in use by another driver.\n"
		  	"Do a `dmesg` to see which kernel driver has claimed it--\n"
			"You may need to `rmmod hid` or patch your kernel's hid driver.\n");
	      }
	      else printf("Claimed interface\n");
		while(1)
		{
	      		bytesread = usb_interrupt_read(d, 1, buffer, 8, 100);
	      		if(bytesread > 0)
			{
				if(bytesread == 5)
				{
	        			printf("read 5 bytes : %i %i %i %i\n",buffer[0], buffer[1], buffer[2], buffer[3]+(buffer[4]<<8));
				}
				else
				if(bytesread == 3)
				{
	        			printf("read 3 bytes : %i %i\n",buffer[0], buffer[1]+(buffer[2]<<8));
				}
				else
				if(bytesread == 2)
				{
					printf("read 2 bytes : %i %i\n", buffer[0], buffer[1]);
				}
				else
				{
	        			printf("read %i bytes : %i %i\n",bytesread, buffer[0], buffer[1]);
				}
			}
		}
		return 0;
	  }
          else
	      bad("Open failed for mamalala multI/O device");
      }
      /* else some other vendor's device-- keep looking... */
    }
  }
  bad("Could not find mamalala multI/O device--\n"
      "you might try lsusb to see if it's actually there.");
  return -1;
}
