Subversion Repositories group.electronics

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
137 pfowler 1
#include <ncurses.h>
2
#include <malloc.h>
3
 
4
#include <stdio.h>
5
#include <stdlib.h>
6
#include <string.h>
7
#include <usb.h>
8
 
139 pfowler 9
#define DISPLAYS_ATTACHED 1
137 pfowler 10
 
11
static int usbGetDescriptorString(usb_dev_handle *dev, int index, int langid, char *buf, int buflen);
12
static usb_dev_handle * usbOpenDevice(int vendor, char *vendorName, int product, char *productName);
139 pfowler 13
char sendDigit(char dis, char dig, char val, char dp);
14
char sendShort(char dis, char dig, short val);
15
char sendLatch(char dis);
16
char getInput(char dis);
137 pfowler 17
 
18
char* getBCD(char maxLen);
19
short getHex();
20
void printBCD(char* data);
21
void printB(char *data);
22
 
139 pfowler 23
void drawScreen();
24
 
137 pfowler 25
usb_dev_handle *handle = NULL;
139 pfowler 26
char usbReplyBuf[64];
137 pfowler 27
 
139 pfowler 28
typedef struct display {
29
	char digits[10];
30
	char rotary;
31
	char buttons;
32
} display_t;
33
 
34
typedef struct controller_t {
35
	display_t* dis[2];
36
} controller_t;
37
 
38
typedef struct cursor_t {
39
	char dis;
40
	char dig;
41
	char val;
42
	char dp;
43
} cursor_t;
44
 
45
controller_t controller;
46
cursor_t cursor;
47
 
137 pfowler 48
main() {
49
 
50
 
51
	handle = usbOpenDevice(0x20a0, "newioit.com.au", 0x4236, "NIT Comm/Nav");
52
 
53
	if(handle == NULL) {
54
		fprintf(stderr, "Could not find USB device!\n");
55
		exit(1);
56
	}
57
 
58
	initscr();
59
	raw();
60
	keypad(stdscr, TRUE);
61
	noecho();
139 pfowler 62
	nodelay(stdscr, TRUE);
137 pfowler 63
 
139 pfowler 64
	controller.dis[0]->buttons = 0;
65
	controller.dis[0]->rotary = 0;
137 pfowler 66
 
139 pfowler 67
	cursor.dis = 0x00;
68
	cursor.dig = 0x00;
69
	cursor.val = 0x0a;
70
	cursor.dp = 0x00;
137 pfowler 71
 
72
	char cont = 1;
73
 
74
	//char display[] = {
75
 
76
	while (cont) {
77
		clear();
78
 
139 pfowler 79
 
137 pfowler 80
		printw("\nCursor: ");
139 pfowler 81
		printw("Dis: %x ", cursor.dis);
82
		printw("Dig %x ", cursor.dig);
83
		printw("Val %x ", cursor.val);
84
		printw("Dp %x ", cursor.dp);
85
		printw("\n");
137 pfowler 86
 
139 pfowler 87
		printw("But: %02X ", controller.dis[0]->buttons & 0xff);
88
		printw("Rot: %02X ", controller.dis[0]->rotary & 0xff);
89
		printw("\n");
90
		//printw("\nCursor: ");
91
 
137 pfowler 92
		char key = 0x00;
139 pfowler 93
		while ((key == getch() != ERR)) {
94
			if (key == 'q') {
95
				cont = 0;
96
			}
97
		}
137 pfowler 98
 
99
 
139 pfowler 100
 
101
 
102
 
103
	}
104
 
105
		/*
137 pfowler 106
		if (key >= 0x30 && key <= 0x39) {
107
			val = key - '0';
108
		} else if  (key == 'w') {
109
			if (val != 10)
110
				val++;
111
		} else if (key == 's') {
112
			if (val != 0)
113
				val--;
139 pfowler 114
		} else if (key == 'a') {
137 pfowler 115
			if (dig!=0)
116
				dig--;
139 pfowler 117
		} else if (key == 'd') {
118
			if (dig != 9)
137 pfowler 119
				dig++;
120
		} else if (key == 'q') {
139 pfowler 121
			if (dis != DISPLAYS_ATTACHED)
137 pfowler 122
				dis++;
123
		} else if (key == 'e') {
124
			if (dis != 0)
125
				dis--;
139 pfowler 126
		} else if (key == '.') {
127
			dp = 1 - dp;
128
		} else if (key == 'g') {
129
			getInput(0);
130
			but = usbReplyBuf[0];
131
			rot = usbReplyBuf[1];
132
 
133
		} else if (key == 'p') {
134
			unsigned short send = 1000;
135
			sendShort(dis, dig, send);
137 pfowler 136
		} else if (key == 'l') {
139 pfowler 137
			sendLatch(0);
138
		} else if (key == 'j') {
137 pfowler 139
			printw(":");
139 pfowler 140
			char* data = getBCD(10);
137 pfowler 141
			char i;
139 pfowler 142
			for (i=0; i<10; i++) {
143
				sendDigit(dis, i, data[i], 0);
137 pfowler 144
			}
145
			free(data);
146
		} else if (key == 'k') {
147
			printw("k");
148
			char i;
139 pfowler 149
			for (i=0; i<10; i++) {
150
				sendDigit(0, i, 0x0a, 0);
137 pfowler 151
			}
139 pfowler 152
			sendLatch(0);
137 pfowler 153
		} else if (key == 0x0a) {
139 pfowler 154
			sendDigit(dis, dig, val, dp);
155
			sendLatch(0);
137 pfowler 156
		} else if (key == 0x1b) {
157
			cont = 0;
158
		}
139 pfowler 159
		*/
137 pfowler 160
 
161
	printw("\nPress key to exit\n");
162
	getch();
139 pfowler 163
	echo();
164
	nodelay(stdscr, false);
137 pfowler 165
	endwin();
166
 
167
	usb_close(handle);
168
 
169
	return 0;
170
}
171
 
139 pfowler 172
void drawScreen() {
173
	printw("\nCursor: ");
174
	printw("Dis: %x ", cursor.dis);
175
	printw("Dig %x ", cursor.dig);
176
	printw("Val %x ", cursor.val);
177
	printw("Dp %x ", cursor.dp);
178
	printw("\n");
179
 
180
	printw("But: %02X ", controller.dis[0]->buttons & 0xff);
181
	printw("Rot: %02X ", controller.dis[0]->rotary & 0xff);
182
	printw("\n");
183
}
184
 
185
char getInput(char dis) {
137 pfowler 186
	char nBytes = 0;
187
 
188
	nBytes = usb_control_msg(handle,
189
		USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
139 pfowler 190
		30,
191
		dis,
192
		0,
193
		(char*)usbReplyBuf,
194
		sizeof(usbReplyBuf),
195
		5000);
196
 
197
	return nBytes;
198
}
199
 
200
char sendLatch(char dis) {
201
	char nBytes = 0;
202
 
203
	nBytes = usb_control_msg(handle,
204
		USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
137 pfowler 205
		20,
139 pfowler 206
		dis,
207
		0,
208
		(char*)usbReplyBuf,
209
		sizeof(usbReplyBuf),
210
		5000);
211
 
212
	return nBytes;
213
}
214
 
215
char sendDigit(char dis, char dig, char val, char dp) {
216
	char nBytes = 0;
217
 
218
	nBytes = usb_control_msg(handle,
219
		USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
220
		21,
137 pfowler 221
		(short)dis<<8 | dig,
139 pfowler 222
		dp << 8 | val,
223
		(char*)usbReplyBuf,
224
		sizeof(usbReplyBuf),
225
		5000);
226
}
227
 
228
char sendShort(char dis, char dig, short val) {
229
	char nBytes = 0;
230
 
231
	nBytes = usb_control_msg(handle,
232
		USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
233
		50,
234
		(short)dis<<8 | dig,
137 pfowler 235
		val,
139 pfowler 236
		(char*)usbReplyBuf,
137 pfowler 237
		sizeof(usbReplyBuf),
238
		5000);
139 pfowler 239
	return nBytes;
137 pfowler 240
}
241
 
242
char* getBCD(char maxLen) {
139 pfowler 243
	if (maxLen > 10) maxLen == 10;
137 pfowler 244
	char* buffer = malloc(sizeof(char) * maxLen);
245
	char len = 0;
246
 
139 pfowler 247
	nodelay(stdscr, false);
137 pfowler 248
	while (1) {
249
		char key = 0x00;
250
		key = getch();
139 pfowler 251
		if ((key >= 0x30 && key <= 0x39) && len <= maxLen) {
137 pfowler 252
			buffer[len] = key - '0';
253
			len++;
254
			printw("%c", key);
255
		} else if (key == 0x07) {
256
			delch();
257
			delch();
258
			len--;
259
			buffer[len] = 0x00;
260
		} else if  (key == 0x0a) {
261
			return buffer;
262
		} else if (key == 0x1b) {
263
			return malloc(sizeof(char));
264
		} else if (key == 0x71) {
265
 
266
		}
267
 
268
	}
139 pfowler 269
	nodelay(stdscr, false);
137 pfowler 270
}
271
 
272
 
273
 
274
/* Used to get descriptor strings for device identification */
275
static int usbGetDescriptorString(usb_dev_handle *dev, int index, int langid,
276
                                  char *buf, int buflen) {
277
    char buffer[256];
278
    int rval, i;
279
 
280
	// make standard request GET_DESCRIPTOR, type string and given index
281
    // (e.g. dev->iProduct)
282
	rval = usb_control_msg(dev,
283
        USB_TYPE_STANDARD | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
284
        USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) + index, langid,
285
        buffer, sizeof(buffer), 1000);
286
 
287
    if(rval < 0) // error
288
		return rval;
289
 
290
    // rval should be bytes read, but buffer[0] contains the actual response size
291
	if((unsigned char)buffer[0] < rval)
292
		rval = (unsigned char)buffer[0]; // string is inter than bytes read
293
 
294
	if(buffer[1] != USB_DT_STRING) // second byte is the data type
295
		return 0; // invalid return type
296
 
297
	// we're dealing with UTF-16LE here so actual chars is half of rval,
298
	// and index 0 doesn't count
299
	rval /= 2;
300
 
301
	/* lossy conversion to ISO Latin1 */
302
	for(i = 1; i < rval && i < buflen; i++) {
303
		if(buffer[2 * i + 1] == 0)
304
			buf[i-1] = buffer[2 * i];
305
		else
306
			buf[i-1] = '?'; /* outside of ISO Latin1 range */
307
	}
308
	buf[i-1] = 0;
309
 
310
	return i-1;
311
}
312
 
313
static usb_dev_handle * usbOpenDevice(int vendor, char *vendorName,
314
                                      int product, char *productName) {
315
	struct usb_bus *bus;
316
	struct usb_device *dev;
317
	char devVendor[256], devProduct[256];
318
 
319
	usb_dev_handle * handle = NULL;
320
 
321
	usb_init();
322
	usb_find_busses();
323
	usb_find_devices();
324
 
325
	for(bus=usb_get_busses(); bus; bus=bus->next) {
326
		for(dev=bus->devices; dev; dev=dev->next) {
327
 
328
			if(dev->descriptor.idVendor != vendor ||
329
               			dev->descriptor.idProduct != product)
330
                		continue;
331
 
332
		         /* we need to open the device in order to query strings */
333
            		if(!(handle = usb_open(dev))) {
334
                		fprintf(stderr, "Warning: cannot open USB device: %s\n",
335
                    		usb_strerror());
336
                		continue;
337
            		}
338
 
339
			/* get vendor name */
340
            		if(usbGetDescriptorString(handle, dev->descriptor.iManufacturer, 0x0409, devVendor, sizeof(devVendor)) < 0) {
341
                		fprintf(stderr, "Warning: cannot query manufacturer for device: %s\n", usb_strerror());
342
                		usb_close(handle);
343
                		continue;
344
            		}
345
 
346
            		/* get product name */
347
            		if(usbGetDescriptorString(handle, dev->descriptor.iProduct, 0x0409, devProduct, sizeof(devVendor)) < 0) {
348
                		fprintf(stderr, "Warning: cannot query product for device: %s\n", usb_strerror()); usb_close(handle);
349
                		continue;
350
            		}
351
 
352
            		if(strcmp(devVendor, vendorName) == 0 && strcmp(devProduct, productName) == 0)
353
                		return handle;
354
            		else
355
                		usb_close(handle);
356
		}
357
	}
358
 
359
	return NULL;
360
}