Blame view

usb.c 4.47 KB
9c066355   henyxia   USB Initialized
1
2
3
4
  #include <stdio.h>
  #include <stdlib.h>
  #include <stdbool.h>
  #include <libusb-1.0/libusb.h>
8b194ee7   henyxia   Getting touch inf...
5
  #include "printx.h"
9c066355   henyxia   USB Initialized
6
7
  #include "usb.h"
  
8b194ee7   henyxia   Getting touch inf...
8
9
10
11
12
13
14
15
  #define VENDOR_ID			0xfccf
  #define PRODUCT_ID			0xa001
  #define ENDPOINT_OUT		0x01
  #define	ENDPOINT_IN			0x82
  #define	MAX_SIZE_OUT		64
  #define HEIGHT				240
  #define WIDTH				320
  #define	STATUS_QUERY_LENGTH	11
9c066355   henyxia   USB Initialized
16
  
31093b73   henyxia   Home screen final...
17
18
19
20
  libusb_device_handle*	screenHandle;
  unsigned char			data[MAX_SIZE_OUT];
  unsigned short			dataPointer = 0;
  
bc20374b   henyxia   Low IPS
21
22
23
24
25
26
27
  void blackScreen(void)
  {
  	unsigned char data[] = {0x81, 0x00, 0x00};
  	int transfered;
  	libusb_bulk_transfer(screenHandle, ENDPOINT_OUT, data, 3, &transfered, 0);
  }
  
31093b73   henyxia   Home screen final...
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  void fillDataWithHeader(void)
  {
  	data[dataPointer] = 0x82; // HEADER
  	dataPointer++;
  	data[dataPointer] = 0x00; // XPOS
  	dataPointer++;
  	data[dataPointer] = 0x00; // XPOS
  	dataPointer++;
  	data[dataPointer] = 0x00; // YPOS
  	dataPointer++;
  	data[dataPointer] = 0x00; // YPOS
  	dataPointer++;
  	data[dataPointer] = 0x40; // Width 0x40
  	dataPointer++;
  	data[dataPointer] = 0x01; // Width 0x01
  	dataPointer++;
  	data[dataPointer] = 0xF0; // Height 
  	dataPointer++;
  	data[dataPointer] = 0x00; // Height
  	dataPointer++;
  	data[dataPointer] = 0x00; // Copy
  	dataPointer++;
  }
  
  void fillDataWithSubHeader(void)
  {
  	data[dataPointer] = 0x02; // HEADER
  	dataPointer++;
  }
  
  void sendUSBData(int i)
  {
fbc5816b   henyxia   UI should be upda...
60
  	//printf("Sending data %d\n", i);
31093b73   henyxia   Home screen final...
61
62
63
64
65
66
67
68
69
70
  	int transfered;
  	libusb_bulk_transfer(screenHandle, ENDPOINT_OUT, data, dataPointer, &transfered, 0);
  	dataPointer = 0;
  	fillDataWithSubHeader();
  }
  
  void displayPicture(char* filename)
  {
  	FILE* file = NULL;
  	file = fopen(filename, "r");
bc20374b   henyxia   Low IPS
71
  	dataPointer = 0;
31093b73   henyxia   Home screen final...
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  
  	if(file == NULL)
  	{
  		printf("Unable to open the picture\n");
  		return;
  	}
  	else
  		printf("Picture successfully opened\n");
  
  	fillDataWithHeader();
  	for(int i=0; i<HEIGHT*WIDTH*2; i++)
  	{
  		if(feof(file))
  		{
  			printf("File corrupted\n");
  			return;
  		}
  		data[dataPointer] = getc(file);
  		dataPointer++;
  		if(dataPointer == MAX_SIZE_OUT)
  			sendUSBData(i);
  	}
bc20374b   henyxia   Low IPS
94
95
96
97
98
  
  	if(dataPointer != 0)
  		sendUSBData(-1);
  
  	printf("Screen updated\n");
31093b73   henyxia   Home screen final...
99
  }
9c066355   henyxia   USB Initialized
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  
  bool initUSB(void)
  {
  	int				ret;
  
  	ret = libusb_init(NULL);
  	if(ret != 0)
  	{
  		printf("Error while initializing libusb, return : %d\n", ret);
  		return false;
  	}
  
  	struct			libusb_config_descriptor* dConfig = NULL;
  	libusb_device**	list = NULL;
  	ssize_t			cnt = libusb_get_device_list(NULL, &list);
  
  	struct libusb_device_descriptor dDevice;
  
  
  	printf("Starting lsusb things\n");
  
  	if(cnt < 0)
  	{
  		printf("Unable to get USB device list\n");
  		return false;
  	}
  
bf0cbb04   Pierre Letousey   Automatic regulation
127
  	printf("%d devices detected\n", (int)cnt);
9c066355   henyxia   USB Initialized
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
  	printf("List of compatible devices detected\n");
  
  	for (int i = 0; i < cnt; i++)
  	{
  		libusb_device *device = list[i];
  		ret = libusb_get_device_descriptor(device, &dDevice);
  		if(VENDOR_ID == dDevice.idVendor && PRODUCT_ID == dDevice.idProduct)
  		{
  			printf("Bus %03d Device %03d: ID %04x:%04x\n",
  					libusb_get_bus_number(device),
  					libusb_get_device_address(device), dDevice.idVendor,
  					dDevice.idProduct);
  			ret = libusb_open(device, &screenHandle);
  			if(ret != 0)
  			{
  				printf("Unable to open this device, error %d\n", ret);
  				return false;
  			}
  		}
  	}
  
  	libusb_free_device_list(list, 1);
  
  	ret = libusb_get_config_descriptor(libusb_get_device(screenHandle), 0, &dConfig);
  	if(ret!=0)
  	{
  		printf("Descriptor for this device unavailable\n");
  		return false;
  	}
  	else
  	{
  		for(int j=0; j<dConfig->bNumInterfaces; j++)
  		{
  			if(libusb_kernel_driver_active(screenHandle, j) && (libusb_detach_kernel_driver(screenHandle, j) != 0))
  			{
  				printf("Unable to detach this device\n");
  				return false;
  			}
  		}
  		ret = libusb_set_configuration(screenHandle, dConfig->bConfigurationValue);
  		if(ret != 0)
  		{
  			printf("Configuration unavailable, error %d\n", ret);
  			return false;
  		}
  		for(int j=0; j<dConfig->bNumInterfaces; j++)
  			if(libusb_claim_interface(screenHandle, j) != 0)
  			{
  				printf("Device not claimed\n");
  				return false;
  			}
  			else
  				printf("Interface %d ready\n", j);
  	}
  	
  	libusb_free_config_descriptor(dConfig);
  
5a135e77   henyxia   MAP Files generated
185
  	displayPicture("img/home.rgb");
31093b73   henyxia   Home screen final...
186
  
9c066355   henyxia   USB Initialized
187
188
  	return true;
  }
8b194ee7   henyxia   Getting touch inf...
189
190
191
192
193
194
195
196
197
198
199
200
  
  bool screenTouched(int* x, int* y)
  {
  	unsigned char	data[STATUS_QUERY_LENGTH];
  	int				transfered;
  	libusb_interrupt_transfer(screenHandle, ENDPOINT_IN, data, STATUS_QUERY_LENGTH, &transfered, 0);
  	if(transfered != STATUS_QUERY_LENGTH)
  	{
  		printx(ERROR, UI, "Status recieved is %d bytes long, and expected %d\n", transfered, STATUS_QUERY_LENGTH);
  		return false;
  	}
  
8b194ee7   henyxia   Getting touch inf...
201
  	*x = data[3] + (256 * (int)data[4]);
8b194ee7   henyxia   Getting touch inf...
202
203
204
205
206
207
  	*y = data[7];
  
  	printx(DEBUG, UI, "Screen %s at X %d Y %d\n", data[2] == 1 ? "touched" : "untouched", *x, *y);
  
  	return data[2] == 1;
  }