Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#include <ncurses.h>#include <malloc.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <usb.h>#define DISPLAYS_ATTACHED 1static int usbGetDescriptorString(usb_dev_handle *dev, int index, int langid, char *buf, int buflen);static usb_dev_handle * usbOpenDevice(int vendor, char *vendorName, int product, char *productName);char sendDigit(char dis, char dig, char val, char dp);char sendShort(char dis, char dig, short val);char sendLatch(char dis);char getInput(char dis);char* getBCD(char maxLen);short getHex();void printBCD(char* data);void printB(char *data);void drawScreen();usb_dev_handle *handle = NULL;char usbReplyBuf[64];typedef struct display {char digits[10];char rotary;char buttons;} display_t;typedef struct controller_t {display_t* dis[2];} controller_t;typedef struct cursor_t {char dis;char dig;char val;char dp;} cursor_t;controller_t controller;cursor_t cursor;main() {handle = usbOpenDevice(0x20a0, "newioit.com.au", 0x4236, "NIT Comm/Nav");if(handle == NULL) {fprintf(stderr, "Could not find USB device!\n");exit(1);}initscr();raw();keypad(stdscr, TRUE);noecho();nodelay(stdscr, TRUE);controller.dis[0]->buttons = 0;controller.dis[0]->rotary = 0;cursor.dis = 0x00;cursor.dig = 0x00;cursor.val = 0x0a;cursor.dp = 0x00;char cont = 1;//char display[] = {while (cont) {clear();printw("\nCursor: ");printw("Dis: %x ", cursor.dis);printw("Dig %x ", cursor.dig);printw("Val %x ", cursor.val);printw("Dp %x ", cursor.dp);printw("\n");printw("But: %02X ", controller.dis[0]->buttons & 0xff);printw("Rot: %02X ", controller.dis[0]->rotary & 0xff);printw("\n");//printw("\nCursor: ");char key = 0x00;while ((key == getch() != ERR)) {if (key == 'q') {cont = 0;}}}/*if (key >= 0x30 && key <= 0x39) {val = key - '0';} else if (key == 'w') {if (val != 10)val++;} else if (key == 's') {if (val != 0)val--;} else if (key == 'a') {if (dig!=0)dig--;} else if (key == 'd') {if (dig != 9)dig++;} else if (key == 'q') {if (dis != DISPLAYS_ATTACHED)dis++;} else if (key == 'e') {if (dis != 0)dis--;} else if (key == '.') {dp = 1 - dp;} else if (key == 'g') {getInput(0);but = usbReplyBuf[0];rot = usbReplyBuf[1];} else if (key == 'p') {unsigned short send = 1000;sendShort(dis, dig, send);} else if (key == 'l') {sendLatch(0);} else if (key == 'j') {printw(":");char* data = getBCD(10);char i;for (i=0; i<10; i++) {sendDigit(dis, i, data[i], 0);}free(data);} else if (key == 'k') {printw("k");char i;for (i=0; i<10; i++) {sendDigit(0, i, 0x0a, 0);}sendLatch(0);} else if (key == 0x0a) {sendDigit(dis, dig, val, dp);sendLatch(0);} else if (key == 0x1b) {cont = 0;}*/printw("\nPress key to exit\n");getch();echo();nodelay(stdscr, false);endwin();usb_close(handle);return 0;}void drawScreen() {printw("\nCursor: ");printw("Dis: %x ", cursor.dis);printw("Dig %x ", cursor.dig);printw("Val %x ", cursor.val);printw("Dp %x ", cursor.dp);printw("\n");printw("But: %02X ", controller.dis[0]->buttons & 0xff);printw("Rot: %02X ", controller.dis[0]->rotary & 0xff);printw("\n");}char getInput(char dis) {char nBytes = 0;nBytes = usb_control_msg(handle,USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,30,dis,0,(char*)usbReplyBuf,sizeof(usbReplyBuf),5000);return nBytes;}char sendLatch(char dis) {char nBytes = 0;nBytes = usb_control_msg(handle,USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,20,dis,0,(char*)usbReplyBuf,sizeof(usbReplyBuf),5000);return nBytes;}char sendDigit(char dis, char dig, char val, char dp) {char nBytes = 0;nBytes = usb_control_msg(handle,USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,21,(short)dis<<8 | dig,dp << 8 | val,(char*)usbReplyBuf,sizeof(usbReplyBuf),5000);}char sendShort(char dis, char dig, short val) {char nBytes = 0;nBytes = usb_control_msg(handle,USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,50,(short)dis<<8 | dig,val,(char*)usbReplyBuf,sizeof(usbReplyBuf),5000);return nBytes;}char* getBCD(char maxLen) {if (maxLen > 10) maxLen == 10;char* buffer = malloc(sizeof(char) * maxLen);char len = 0;nodelay(stdscr, false);while (1) {char key = 0x00;key = getch();if ((key >= 0x30 && key <= 0x39) && len <= maxLen) {buffer[len] = key - '0';len++;printw("%c", key);} else if (key == 0x07) {delch();delch();len--;buffer[len] = 0x00;} else if (key == 0x0a) {return buffer;} else if (key == 0x1b) {return malloc(sizeof(char));} else if (key == 0x71) {}}nodelay(stdscr, false);}/* Used to get descriptor strings for device identification */static int usbGetDescriptorString(usb_dev_handle *dev, int index, int langid,char *buf, int buflen) {char buffer[256];int rval, i;// make standard request GET_DESCRIPTOR, type string and given index// (e.g. dev->iProduct)rval = usb_control_msg(dev,USB_TYPE_STANDARD | USB_RECIP_DEVICE | USB_ENDPOINT_IN,USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) + index, langid,buffer, sizeof(buffer), 1000);if(rval < 0) // errorreturn rval;// rval should be bytes read, but buffer[0] contains the actual response sizeif((unsigned char)buffer[0] < rval)rval = (unsigned char)buffer[0]; // string is inter than bytes readif(buffer[1] != USB_DT_STRING) // second byte is the data typereturn 0; // invalid return type// we're dealing with UTF-16LE here so actual chars is half of rval,// and index 0 doesn't countrval /= 2;/* lossy conversion to ISO Latin1 */for(i = 1; i < rval && i < buflen; i++) {if(buffer[2 * i + 1] == 0)buf[i-1] = buffer[2 * i];elsebuf[i-1] = '?'; /* outside of ISO Latin1 range */}buf[i-1] = 0;return i-1;}static usb_dev_handle * usbOpenDevice(int vendor, char *vendorName,int product, char *productName) {struct usb_bus *bus;struct usb_device *dev;char devVendor[256], devProduct[256];usb_dev_handle * handle = NULL;usb_init();usb_find_busses();usb_find_devices();for(bus=usb_get_busses(); bus; bus=bus->next) {for(dev=bus->devices; dev; dev=dev->next) {if(dev->descriptor.idVendor != vendor ||dev->descriptor.idProduct != product)continue;/* we need to open the device in order to query strings */if(!(handle = usb_open(dev))) {fprintf(stderr, "Warning: cannot open USB device: %s\n",usb_strerror());continue;}/* get vendor name */if(usbGetDescriptorString(handle, dev->descriptor.iManufacturer, 0x0409, devVendor, sizeof(devVendor)) < 0) {fprintf(stderr, "Warning: cannot query manufacturer for device: %s\n", usb_strerror());usb_close(handle);continue;}/* get product name */if(usbGetDescriptorString(handle, dev->descriptor.iProduct, 0x0409, devProduct, sizeof(devVendor)) < 0) {fprintf(stderr, "Warning: cannot query product for device: %s\n", usb_strerror()); usb_close(handle);continue;}if(strcmp(devVendor, vendorName) == 0 && strcmp(devProduct, productName) == 0)return handle;elseusb_close(handle);}}return NULL;}