Subversion Repositories group.electronics

Rev

Go to most recent revision | 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
 
9
 
10
static int usbGetDescriptorString(usb_dev_handle *dev, int index, int langid, char *buf, int buflen);
11
static usb_dev_handle * usbOpenDevice(int vendor, char *vendorName, int product, char *productName);
12
char sendDigit(char dis, char dig, char val);
13
 
14
char* getBCD(char maxLen);
15
short getHex();
16
void printBCD(char* data);
17
void printB(char *data);
18
 
19
usb_dev_handle *handle = NULL;
20
 
21
main() {
22
 
23
 
24
	handle = usbOpenDevice(0x20a0, "newioit.com.au", 0x4236, "NIT Comm/Nav");
25
 
26
	if(handle == NULL) {
27
		fprintf(stderr, "Could not find USB device!\n");
28
		exit(1);
29
	}
30
 
31
	initscr();
32
	raw();
33
	keypad(stdscr, TRUE);
34
	noecho();
35
 
36
 
37
 
38
	char dis = 0x00;
39
	char dig = 0x03;
40
	short val = 0x02;
41
 
42
	char cont = 1;
43
 
44
	//char display[] = {
45
 
46
	while (cont) {
47
		clear();
48
 
49
		printw("\nCursor: ");
50
		printw("Dis: %x ", dis);
51
		printw("Dig %x ", dig);
52
		printw("Val %x \n", val);
53
 
54
		char key = 0x00;
55
		key = getch();
56
 
57
 
58
		if (key >= 0x30 && key <= 0x39) {
59
			val = key - '0';
60
		} else if  (key == 'w') {
61
			if (val != 10)
62
				val++;
63
		} else if (key == 's') {
64
			if (val != 0)
65
				val--;
66
		} else if (key == 'd') {
67
			if (dig!=0)
68
				dig--;
69
		} else if (key == 'a') {
70
			if (dig != 4)
71
				dig++;
72
		} else if (key == 'q') {
73
			if (dis != 1)
74
				dis++;
75
		} else if (key == 'e') {
76
			if (dis != 0)
77
				dis--;
78
		} else if (key == 'l') {
79
			printw(":");
80
			char* data = getBCD(5);
81
			char i;
82
			char n = 4;
83
			for (i=0; i<5; i++) {
84
				sendDigit(dis, i, data[n]);
85
				n--;
86
			}
87
			free(data);
88
		} else if (key == 'k') {
89
			printw("k");
90
			char data1[5] = {0x0a, 0x0a, 0x0a, 0x0a, 0x0a};
91
			char data2[5] = {0x0a, 0x0a, 0x0a, 0x0a, 0x0a};
92
			char i;
93
			for (i=0; i<5; i++) {
94
				sendDigit(0, i, data1[i]);
95
			}
96
			for (i=0; i<5; i++) {
97
				sendDigit(1, i, data2[i]);
98
			}
99
		} else if (key == 0x0a) {
100
			sendDigit(dis, dig, val);
101
		} else if (key == 0x1b) {
102
			cont = 0;
103
		}
104
	}
105
 
106
	printw("\nPress key to exit\n");
107
	getch();
108
	endwin();
109
 
110
	usb_close(handle);
111
 
112
	return 0;
113
}
114
 
115
char sendDigit(char dis, char dig, char val) {
116
	char nBytes = 0;
117
	char* usbReplyBuf[64];
118
 
119
	nBytes = usb_control_msg(handle,
120
		USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
121
		20,
122
		(short)dis<<8 | dig,
123
		val,
124
		*usbReplyBuf,
125
		sizeof(usbReplyBuf),
126
		5000);
127
}
128
 
129
char* getBCD(char maxLen) {
130
	if (maxLen > 8) maxLen == 8;
131
	char* buffer = malloc(sizeof(char) * maxLen);
132
	char len = 0;
133
 
134
 
135
	while (1) {
136
		char key = 0x00;
137
		key = getch();
138
		if ((key >= 0x30 && key <= 0x39) && len < maxLen) {
139
			buffer[len] = key - '0';
140
			len++;
141
			printw("%c", key);
142
		} else if (key == 0x07) {
143
			delch();
144
			delch();
145
			len--;
146
			buffer[len] = 0x00;
147
		} else if  (key == 0x0a) {
148
			return buffer;
149
		} else if (key == 0x1b) {
150
			return malloc(sizeof(char));
151
		} else if (key == 0x71) {
152
 
153
		}
154
 
155
	}
156
}
157
 
158
 
159
 
160
/* Used to get descriptor strings for device identification */
161
static int usbGetDescriptorString(usb_dev_handle *dev, int index, int langid,
162
                                  char *buf, int buflen) {
163
    char buffer[256];
164
    int rval, i;
165
 
166
	// make standard request GET_DESCRIPTOR, type string and given index
167
    // (e.g. dev->iProduct)
168
	rval = usb_control_msg(dev,
169
        USB_TYPE_STANDARD | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
170
        USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) + index, langid,
171
        buffer, sizeof(buffer), 1000);
172
 
173
    if(rval < 0) // error
174
		return rval;
175
 
176
    // rval should be bytes read, but buffer[0] contains the actual response size
177
	if((unsigned char)buffer[0] < rval)
178
		rval = (unsigned char)buffer[0]; // string is inter than bytes read
179
 
180
	if(buffer[1] != USB_DT_STRING) // second byte is the data type
181
		return 0; // invalid return type
182
 
183
	// we're dealing with UTF-16LE here so actual chars is half of rval,
184
	// and index 0 doesn't count
185
	rval /= 2;
186
 
187
	/* lossy conversion to ISO Latin1 */
188
	for(i = 1; i < rval && i < buflen; i++) {
189
		if(buffer[2 * i + 1] == 0)
190
			buf[i-1] = buffer[2 * i];
191
		else
192
			buf[i-1] = '?'; /* outside of ISO Latin1 range */
193
	}
194
	buf[i-1] = 0;
195
 
196
	return i-1;
197
}
198
 
199
static usb_dev_handle * usbOpenDevice(int vendor, char *vendorName,
200
                                      int product, char *productName) {
201
	struct usb_bus *bus;
202
	struct usb_device *dev;
203
	char devVendor[256], devProduct[256];
204
 
205
	usb_dev_handle * handle = NULL;
206
 
207
	usb_init();
208
	usb_find_busses();
209
	usb_find_devices();
210
 
211
	for(bus=usb_get_busses(); bus; bus=bus->next) {
212
		for(dev=bus->devices; dev; dev=dev->next) {
213
 
214
			if(dev->descriptor.idVendor != vendor ||
215
               			dev->descriptor.idProduct != product)
216
                		continue;
217
 
218
		         /* we need to open the device in order to query strings */
219
            		if(!(handle = usb_open(dev))) {
220
                		fprintf(stderr, "Warning: cannot open USB device: %s\n",
221
                    		usb_strerror());
222
                		continue;
223
            		}
224
 
225
			/* get vendor name */
226
            		if(usbGetDescriptorString(handle, dev->descriptor.iManufacturer, 0x0409, devVendor, sizeof(devVendor)) < 0) {
227
                		fprintf(stderr, "Warning: cannot query manufacturer for device: %s\n", usb_strerror());
228
                		usb_close(handle);
229
                		continue;
230
            		}
231
 
232
            		/* get product name */
233
            		if(usbGetDescriptorString(handle, dev->descriptor.iProduct, 0x0409, devProduct, sizeof(devVendor)) < 0) {
234
                		fprintf(stderr, "Warning: cannot query product for device: %s\n", usb_strerror()); usb_close(handle);
235
                		continue;
236
            		}
237
 
238
            		if(strcmp(devVendor, vendorName) == 0 && strcmp(devProduct, productName) == 0)
239
                		return handle;
240
            		else
241
                		usb_close(handle);
242
		}
243
	}
244
 
245
	return NULL;
246
}
247
 
248
 
249
/*******************************************************/
250
/*
251
char* getBCD(char maxLen) {
252
	if (maxLen > 8) maxLen == 8;
253
	char* buffer = malloc(sizeof(char) * maxLen);
254
	char len = 0;
255
 
256
 
257
	while (1) {
258
		char key = 0x00;
259
		key = getch();
260
		if ((key >= 0x30 && key <= 0x38) && len < maxLen) {
261
			buffer[len] = key - '0';
262
			len++;
263
			printw("%c", key);
264
		} else if (key == 0x08) {
265
			delch();
266
			len--;
267
			buffer[len] = 0x00;
268
		} else if  (key == 0x0a) {
269
			return buffer;
270
		} else if (key == 0x1b) {
271
			return malloc(sizeof(char));
272
		} else if (key == 0x71) {
273
 
274
		}
275
 
276
	}
277
}
278
*/