-
Notifications
You must be signed in to change notification settings - Fork 22
/
AR1100.cpp
205 lines (178 loc) · 6.22 KB
/
AR1100.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
127
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "AR1100.h"
#include <stdio.h>
static const int MAX_INTERRUPT_IN_TRANSFER_SIZE = 64;
static const int MAX_INTERRUPT_OUT_TRANSFER_SIZE = 64;
static const int INTERFACE_NUMBER = 0;
static const int TIMEOUT_MS = AR1100_TIMEOUT;
static const int INTERRUPT_IN_ENDPOINT = 0x81;
static const int INTERRUPT_OUT_ENDPOINT = 0x01;
typedef struct {
unsigned char sync;
unsigned char size;
unsigned char cmd;
unsigned char data[MAX_INTERRUPT_OUT_TRANSFER_SIZE-3];
}AR1100_cmd_packet;
typedef struct {
unsigned char sync;
unsigned char size;
unsigned char status;
unsigned char cmd;
unsigned char data[MAX_INTERRUPT_IN_TRANSFER_SIZE-4];
}AR1100_response_packet;
AR1100::AR1100(){
device_ready=0;
devh=NULL;
}
AR1100::~AR1100(){
if (devh!=NULL) libusb_close(devh);
}
int AR1100::connect(int vid, int pid){
int result;
result = libusb_init(NULL);
if (result >= 0){
devh = libusb_open_device_with_vid_pid(NULL, vid, pid);
if (devh != NULL){
// The HID has been detected.
// Detach the hidusb driver from the HID to enable using libusb.
libusb_detach_kernel_driver(devh, INTERFACE_NUMBER);
result = libusb_claim_interface(devh, INTERFACE_NUMBER);
if (result >= 0){
device_ready = 1;
return 0;
}else{
#ifdef DEBUG
fprintf(stderr, "libusb_claim_interface error %d\n", result);
#endif
}
}else{
#ifdef DEBUG
fprintf(stderr, "Unable to find the device.\n");
#endif
result=1;
}
}else{
#ifdef DEBUG
fprintf(stderr, "Unable to initialize libusb.\n");
#endif
}
return result;
/*if (device_ready){
// Send and receive data.
exchange_input_and_output_reports_via_interrupt_transfers(devh);
libusb_release_interface(devh, 0);
}
libusb_close(devh);
libusb_exit(NULL);*/
}
int AR1100::write_data(unsigned char * data, int data_count, int * bytes_written, int time_out){
#ifdef DEBUG
printf("USB tx %d:\n", data_count);
for (int i=0; i< data_count; i++){
printf("%02x ", data[i]);
if (((i+1)%16)==0) printf("\n");
}
printf("\n");
#endif
int result = libusb_interrupt_transfer(devh,INTERRUPT_OUT_ENDPOINT,data,data_count,bytes_written,time_out);
#ifdef DEBUG
printf("Result %d\n", result);
#endif
return result;
}
int AR1100::read_data(unsigned char * data, int data_count, int * bytes_read, int time_out){
int result= libusb_interrupt_transfer(devh,INTERRUPT_IN_ENDPOINT,data,data_count,bytes_read,time_out);
#ifdef DEBUG
if (result==0){
printf("USB rx %d:\n", data_count);
for (int i=0; i<data_count;i++){
printf("%02x ", data[i]);
if (((i+1)%16)==0) printf("\n");
}
printf("\n");
}
#endif
return result;
}
//clears all waiting in USB packets
void AR1100::clear_in_buffer(){
int bytes_transferred;
int result = 0;
unsigned char data_in[MAX_INTERRUPT_IN_TRANSFER_SIZE];
if (!device_ready) return;
while (result==0){
result = read_data(data_in, MAX_INTERRUPT_IN_TRANSFER_SIZE, &bytes_transferred,10); //libusb_interrupt_transfer(devh,INTERRUPT_IN_ENDPOINT,data_in,MAX_INTERRUPT_IN_TRANSFER_SIZE,&bytes_transferred,10);
if (bytes_transferred==0) return;
}
}
//sends a command to AR1100 and waits TIMEOUT_MS for a return packet (if wait_response is set to true)
int AR1100::send_command(unsigned char cmd, unsigned char * data, int data_count, bool wait_response){
int bytes_transferred;
int result = 0;
AR1100_cmd_packet command={0};
AR1100_response_packet response={0};
#ifdef DEBUG
printf("Sending cmd %#x\n", cmd);
#endif
if (data_count > sizeof(command.data)) data_count = sizeof(command.data);
command.sync = AR1100_SYNC;
command.size = data_count+1;
command.cmd = cmd;
for (int i=0; i<data_count;i++){
command.data[i]=data[i];
}
if (device_ready){
// Write data to the device.
result = write_data((unsigned char *)&command,sizeof(command),&bytes_transferred);
if (result==0){
if (wait_response){
//read the result of the command
result = read_data((unsigned char *) & response,sizeof(response),&bytes_transferred);
if (result==0){
#ifdef DEBUG
printf("COMMAND response: %d\n", response.status);
#endif
if (response.sync==AR1100_SYNC && response.cmd==cmd){
return response.status; //return status if OK
}
}
}else{
return AR1100_CMD_RESULT_OK;
}
}
}else{
#ifdef DEBUG
printf("Device not connected %d\n", device_ready);
#endif
}
return AR1100_CMD_TRANSFER_ERROR;
}
bool AR1100::touch_disable(){
return send_command(AR1100_CMD_TOUCH_DISABLE, NULL, 0, true)==AR1100_CMD_RESULT_OK;
}
bool AR1100::touch_enable(){
return send_command(AR1100_CMD_TOUCH_ENABLE, NULL, 0, true)==AR1100_CMD_RESULT_OK;
}
bool AR1100::calibrate(unsigned char type){
return send_command(AR1100_CMD_CALIBRATE, &type, 1, true)==AR1100_CMD_RESULT_OK;
}
bool AR1100::calibrate_next_point(){
AR1100_response_packet response;
int bytes_read;
int result = read_data((unsigned char *) & response, sizeof(response), & bytes_read, 50);
if (result==0 && bytes_read>0){
#ifdef DEBUG
printf("Next point: %d\n", response.status);
#endif
return response.sync==AR1100_SYNC && response.cmd==AR1100_CMD_CALIBRATE && response.status==AR1100_CMD_RESULT_OK;
}
return false;
}
bool AR1100::switch_hid_generic(){ //switch to hid generic
return send_command(AR1100_CMD_HID_GENERIC, NULL, 0, false)==AR1100_CMD_RESULT_OK;
}
bool AR1100::switch_usb_mouse(){ //switch to usb mouse
return send_command(AR1100_CMD_USB_MOUSE, NULL, 0, false)==AR1100_CMD_RESULT_OK;
}
bool AR1100::switch_digitizer(){ //switch to digitizer
return send_command(AR1100_CMD_DIGITIZE, NULL, 0, false)==AR1100_CMD_RESULT_OK;
}