-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tablet_IF.cpp
179 lines (149 loc) · 4.44 KB
/
Tablet_IF.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
#include <stdio.h>
#include <windows.h>
#include "Tablet_IF.h"
void PrintStatus(TABLETSTATE state)
{
int tool = state.tool;
printf("Proximity: %d, Tool: %s, Pos_X: %d, Pos_Y: %d, Pressure: %d, Button: %0x\n",
state.proximity,
tool ? (tool == WACOMTOOLTYPE_ERASER ? "Eraser" : "Pen") : "none",
state.posX,
state.posY,
state.pressure,
state.buttons
);
}
TABLETSTATE TabletPC_Parse(const unsigned char* puchData)
{
TABLETSTATE state;
int x=0, y=0, prox=0, tool=WACOMTOOLTYPE_NONE,
button=0, press=0, eraser;
/* Tablet PC Supports: 256 pressure, eraser, 1/2 side-switch */
prox = puchData[0] & 0x20 ? 1 : 0;
if (prox)
{
eraser = (puchData[0] & 0x04) ? 1 : 0;
press = ((puchData[6] & 0x01) << 7) | (puchData[5] & 0x7F);
/* tools are distinguishable */
if (eraser) tool = WACOMTOOLTYPE_ERASER;
else tool = WACOMTOOLTYPE_PEN;
button = (puchData[0] & 0x01) ? BIT(WACOMBUTTON_TOUCH) : 0;
/* pen has side-switch(es), eraser has none */
if (tool == WACOMTOOLTYPE_PEN)
{
button |= (puchData[0] & 0x02) ?
BIT(WACOMBUTTON_STYLUS) : 0;
button |= (puchData[0] & 0x04) ?
BIT(WACOMBUTTON_STYLUS2) : 0;
}
x = (((int)puchData[6] & 0x60) >> 5) |
((int)puchData[2] << 2) |
((int)puchData[1] << 9);
y = (((int)puchData[6] & 0x18) >> 3) |
((int)puchData[4] << 2) |
((int)puchData[3] << 9);
}
/* set valid fields */
state.proximity = prox;
state.tool = tool;
state.posX = x;
state.posY = y;
state.pressure = press;
state.buttons = button;
return state;
}
HANDLE SerialInit(LPCWSTR comport, int baudrate)
{
HANDLE hSerial;
DWORD dwBytesWritten;
hSerial = CreateFile(
comport,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0
);
if(hSerial==INVALID_HANDLE_VALUE){
if(GetLastError()==ERROR_FILE_NOT_FOUND){
fprintf(stderr, "No serial port at %s\n", comport);
}
fprintf(stderr, "Unknown Error\n");
}
DCB dcbSerialParams = {0};
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
if (!GetCommState(hSerial, &dcbSerialParams)) {
fprintf(stderr, "error getting state\n");
}
dcbSerialParams.BaudRate=baudrate;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;
if(!SetCommState(hSerial, &dcbSerialParams)){
fprintf(stderr, "error setting serial port state\n");
}
// Retrieve the timeout parameters for all read and write operations
// on the port.
COMMTIMEOUTS CommTimeouts;
GetCommTimeouts (hSerial, &CommTimeouts);
// Change the COMMTIMEOUTS structure settings.
CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 100;
CommTimeouts.ReadTotalTimeoutConstant = 10000;
CommTimeouts.WriteTotalTimeoutMultiplier = 10;
CommTimeouts.WriteTotalTimeoutConstant = 1000;
// Set the timeout parameters for all read and write operations
// on the port.
if (!SetCommTimeouts (hSerial, &CommTimeouts))
{
// Could not set the timeout parameters.
WCHAR lastError[1024];
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
lastError,
1024,
NULL);
fprintf(stderr, "error occurred. Could not Read. %s\n", lastError);
return (HANDLE)FALSE;
}
// Tablet SPecific Init
if(!WriteFile(hSerial, "1", 1, &dwBytesWritten, NULL)){ //get tablet to start communicating
fprintf(stderr, "error occurred. Could not Write.\n");
} else {
printf("Wrote %d bytes.\n", dwBytesWritten);
}
return hSerial;
}
DWORD SerialReadRaw(HANDLE hSerial, unsigned char* puchData, unsigned int uSize)
{
DWORD dwBytesRead = 0;
unsigned int currentBytes;
unsigned char* head;
currentBytes = 0;
if(!ReadFile(hSerial, puchData, uSize, &dwBytesRead, NULL)){
//error occurred. Report to user.
fprintf(stderr, "error occurred. Could not Read.\n");
}
for (head=puchData, currentBytes=dwBytesRead ; currentBytes ; currentBytes--)
{
if (*head & 0x80) break;
}
if (head!=puchData)
{
memcpy(puchData, head, currentBytes);
}
while (currentBytes < uSize)
{
if(!ReadFile(hSerial, puchData+currentBytes, uSize-currentBytes, &dwBytesRead, NULL)){
//error occurred. Report to user.
fprintf(stderr, "error occurred. Could not Read.\n");
} else {
currentBytes+=dwBytesRead;
}
}
return currentBytes;
}