Subversion Repositories group.electronics

Rev

Rev 115 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 115 Rev 116
Line 27... Line 27...
27
char* I_OK = "+";		// ignored, ok
27
char* I_OK = "+";		// ignored, ok
28
char* I_NEX = "?";		// ignored, unexpected
28
char* I_NEX = "?";		// ignored, unexpected
29
char* I_EXE = "e";		// exception
29
char* I_EXE = "e";		// exception
30
char* X_SEP = "|";		// separator
30
char* X_SEP = "|";		// separator
31
 
31
 
32
 
-
 
33
 
-
 
34
#define		BUTTONS_COM1	0
32
#define		BUTTONS_COM1	0
35
#define		BUTTONS_COM2	1
33
#define		BUTTONS_COM2	1
36
#define		BUTTONS_COMB	2
34
#define		BUTTONS_COMB	2
37
#define		BUTTONS_NAV1	3
35
#define		BUTTONS_NAV1	3
38
#define		BUTTONS_NAV2	4
36
#define		BUTTONS_NAV2	4
39
#define		BUTTONS_MKR 	5
37
#define		BUTTONS_MKR 	5
40
#define		BUTTONS_DME 	6
38
#define		BUTTONS_DME 	6
41
#define		BUTTONS_ADF 	7
39
#define		BUTTONS_ADF 	7
42
 
40
 
43
 
-
 
44
 
-
 
45
 
-
 
46
static usb_dev_handle * usbOpenDevice(int vendor, char *vendorName, int product, char *productName);
41
static usb_dev_handle * usbOpenDevice(int vendor, char *vendorName, int product, char *productName);
47
static int usbGetDescriptorString(usb_dev_handle *dev, int index, int langid, char *buf, int buflen);
42
static int usbGetDescriptorString(usb_dev_handle *dev, int index, int langid, char *buf, int buflen);
48
void testCode(usb_dev_handle *handle);
43
void testCode(usb_dev_handle *handle);
49
 
44
 
50
void setLed(double value, int bit);
45
void setLed(double value, int bit);
-
 
46
void usbPushLeds(void);
-
 
47
void usbGetState(void);
51
 
48
 
52
void CALLBACK dispatchProc(SIMCONNECT_RECV *pData, DWORD cbData, void *pContext);
49
void CALLBACK dispatchProc(SIMCONNECT_RECV *pData, DWORD cbData, void *pContext);
53
 
50
 
54
void Process_Exception(SIMCONNECT_RECV* pData);
51
void Process_Exception(SIMCONNECT_RECV* pData);
55
void Process_Connected();
52
void Process_Connected();
Line 69... Line 66...
69
bool	Paused = true;
66
bool	Paused = true;
70
char 	string[80];
67
char 	string[80];
71
bool	updateLeds = true;
68
bool	updateLeds = true;
72
char 	ledStatus = 0x00;
69
char 	ledStatus = 0x00;
73
int		nBytes = 0;
70
int		nBytes = 0;
74
char	buffer[16];
71
unsigned char	buffer[16];
75
 
72
 
76
struct CommSelectData {
73
struct CommSelectData {
77
	double	com1;
74
	double	com1;
78
	double	com2;
75
	double	com2;
79
	double	comb;
76
	double	comb;
Line 123... Line 120...
123
    if(handle == NULL) {
120
    if(handle == NULL) {
124
            fprintf(stderr, "Could not find USB device\n");
121
            fprintf(stderr, "Could not find USB device\n");
125
            exit(1);
122
            exit(1);
126
    }
123
    }
127
 
124
 
-
 
125
 
-
 
126
 
128
	//testCode(handle);
127
	//testCode(handle);
-
 
128
	//exit(0);
129
	
129
 
130
	int i;
130
	int i;
131
	for (i = 0; i < 15; i++) {
131
	for (i = 0; i < 15; i++) {
132
		buffer[i] = 0x00;
132
		buffer[i] = 0x00;
133
	}
133
	}
134
	
134
	
135
	nBytes = usb_control_msg(handle,
135
	usbPushLeds();	
136
					USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
-
 
137
					USB_LEDS_SET, ledStatus, 0, (char *)buffer, sizeof(buffer), 5000);	
-
 
138
 
136
 
139
	Connect();
137
	Connect();
140
 
138
 
141
	if (hSimConnect != NULL) {
139
	if (hSimConnect != NULL) {
142
		keep_going = true;
140
		keep_going = true;
143
 
141
 
144
		unsigned int lastButtons = 0;
142
		unsigned int lastButtons = 0;
145
 
143
 
146
		while(keep_going)
144
		while(keep_going) {
147
        {
-
 
148
			HRESULT hr;
145
			HRESULT hr;
149
			
146
				
150
			// Put this in an 'On Event' thing later...
-
 
151
			//hr = SimConnect_RequestDataOnSimObjectType(hSimConnect, REQUEST_1, DEFINITION_1, 0, SIMCONNECT_SIMOBJECT_TYPE_USER);
-
 
152
			
-
 
153
			nBytes = usb_control_msg(handle,
147
			usbGetState();		
154
						USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
-
 
155
						USB_STATE_READ, 0, 0, (char *)buffer, sizeof(buffer), 5000);
-
 
156
			
-
 
157
			unsigned int buttons = (unsigned int)buffer[1];
148
			unsigned char buttons = (unsigned char)buffer[1];
158
			
-
 
159
			
149
 
160
			
-
 
161
			for (i=0; i<7; i++) {
150
			for (i=0; i<=7; i++) {
162
					if (rbi(buttons, i) && rbi(lastButtons, i) == 0) {
151
					if (rbi(buttons, i) && rbi(lastButtons, i) == 0) {
163
						sbi(lastButtons, i);
152
						sbi(lastButtons, i);
164
						hr = SimConnect_TransmitClientEvent(hSimConnect,0, i, 1 ,
153
						hr = SimConnect_TransmitClientEvent(hSimConnect,0, i, 1 ,
165
								SIMCONNECT_GROUP_PRIORITY_STANDARD,SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY);
154
								SIMCONNECT_GROUP_PRIORITY_STANDARD,SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY);
166
					}
155
					}
Line 211... Line 200...
211
		}		
200
		}		
212
	}
201
	}
213
 
202
 
214
	printf("\n%s Resetting internal status to disconnected.", Format_SOL(M_NO));
203
	printf("\n%s Resetting internal status to disconnected.", Format_SOL(M_NO));
215
	hSimConnect = NULL;
204
	hSimConnect = NULL;
-
 
205
	
-
 
206
	ledStatus = 0x00;
-
 
207
	usbPushLeds();
-
 
208
 
216
	keep_going = false;
209
	keep_going = false;
217
}
210
}
218
 
211
 
219
void CALLBACK dispatchProc(SIMCONNECT_RECV* pData, DWORD cbData, void *pContext)
212
void CALLBACK dispatchProc(SIMCONNECT_RECV* pData, DWORD cbData, void *pContext)
220
{
213
{
221
    switch(pData->dwID)
214
    switch(pData->dwID)
222
    {
215
    {
223
        
216
        
224
 
217
 
225
		case SIMCONNECT_RECV_ID_QUIT:
218
		case SIMCONNECT_RECV_ID_QUIT:
226
			// FSX is stopping
-
 
227
			Process_FSX_Quit();
219
			Process_FSX_Quit();
228
            break;
220
            break;
229
 
221
 
230
		case SIMCONNECT_RECV_ID_OPEN:
222
		case SIMCONNECT_RECV_ID_OPEN:
231
			// the connection process is complete
-
 
232
			Process_Connected();
223
			Process_Connected();
233
			break;
224
			break;
234
 
225
 
235
		case SIMCONNECT_RECV_ID_EVENT: {
226
		case SIMCONNECT_RECV_ID_EVENT: {
236
			SIMCONNECT_RECV_EVENT *evt = (SIMCONNECT_RECV_EVENT*)pData;
227
			SIMCONNECT_RECV_EVENT *evt = (SIMCONNECT_RECV_EVENT*)pData;
Line 256... Line 247...
256
					setLed(ps->comb, BUTTONS_COMB);
247
					setLed(ps->comb, BUTTONS_COMB);
257
					setLed(ps->vor1, BUTTONS_NAV1);
248
					setLed(ps->vor1, BUTTONS_NAV1);
258
					setLed(ps->vor2, BUTTONS_NAV2);
249
					setLed(ps->vor2, BUTTONS_NAV2);
259
					setLed(ps->mkr, BUTTONS_MKR);
250
					setLed(ps->mkr, BUTTONS_MKR);
260
					setLed(ps->dme, BUTTONS_DME);
251
					setLed(ps->dme, BUTTONS_DME);
261
					setLed(ps->adf, BUTTONS_ADF);
252
					setLed(ps->adf, BUTTONS_ADF);					
262
					
-
 
263
					nBytes = usb_control_msg(handle,
253
					usbPushLeds();
264
						USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
-
 
265
						USB_LEDS_SET, ledStatus, 0, (char *)buffer, sizeof(buffer), 5000);
-
 
266
 
254
 
267
					printf("\nSIMOBJECT_DATA %d LED Update" );
-
 
268
					break;
255
					break;
269
				}
256
				}
270
			}
257
			}
271
			break;	
258
			break;	
272
		}
259
		}
Line 428... Line 415...
428
 
415
 
429
/*
416
/*
430
 *	*** USB Functions ***
417
 *	*** USB Functions ***
431
 */
418
 */
432
 
419
 
-
 
420
void usbPushLeds() {
-
 
421
	nBytes = usb_control_msg(handle,
-
 
422
		USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
-
 
423
		USB_LEDS_SET, ledStatus, 0, (char *)buffer, sizeof(buffer), 5000);
-
 
424
}
-
 
425
 
-
 
426
void usbGetState() {
-
 
427
	nBytes = usb_control_msg(handle,
-
 
428
		USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
-
 
429
		USB_STATE_READ, 0, 0, (char *)buffer, sizeof(buffer), 5000);
-
 
430
}
-
 
431
 
433
static int usbGetDescriptorString(usb_dev_handle *dev, int index, int langid,
432
static int usbGetDescriptorString(usb_dev_handle *dev, int index, int langid,
434
                                  char *buf, int buflen) {
433
                                  char *buf, int buflen) {
435
    char buffer[256];
434
    char buffer[256];
436
    int rval, i;
435
    int rval, i;
437
 
436
 
Line 518... Line 517...
518
}
517
}
519
 
518
 
520
void testCode(usb_dev_handle *handle) {
519
void testCode(usb_dev_handle *handle) {
521
 
520
 
522
	int nBytes = 0;
521
	int nBytes = 0;
523
    char buffer[16];
522
    unsigned char buffer[16];
524
	int i;
523
	int i;
525
	for (i = 0; i < 15; i++) {
524
	for (i = 0; i < 15; i++) {
526
		buffer[i] = 0x00;
525
		buffer[i] = 0x00;
527
	}
526
	}
528
 
527
 
Line 548... Line 547...
548
						USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
547
						USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
549
						USB_STATE_READ, 0, 0, (char *)buffer, sizeof(buffer), 5000);
548
						USB_STATE_READ, 0, 0, (char *)buffer, sizeof(buffer), 5000);
550
			printf("Got %d bytes: %s\n", nBytes, buffer);
549
			printf("Got %d bytes: %s\n", nBytes, buffer);
551
			for (i = 0; i < 15; i++) {
550
			for (i = 0; i < 15; i++) {
552
				if (i>0) printf(":");
551
				if (i>0) printf(":");
553
				printf("%02X",  buffer[i]);
552
				printf("%02X",  (unsigned char)buffer[i]);
554
			}
553
			}
555
			printf("\n");
554
			printf("\n");
556
		}
555
		}
557
		Sleep(1);
556
		Sleep(1);
558
	}
557
	}