Repository navigation
Expand file tree
/
Copy pathPoKeysLibMatrixKB.c
More file actions
222 lines (182 loc) · 6.74 KB
/
Copy pathPoKeysLibMatrixKB.c
File metadata and controls
222 lines (182 loc) · 6.74 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
Copyright (C) 2013 Matev� Bo�nak (matevz@poscope.com)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "PoKeysLibHal.h"
#include "PoKeysLibCore.h"
int32_t PK_MatrixKBConfigurationGet(sPoKeysDevice* device)
{
uint32_t n, key, x;
if (device == NULL) return PK_ERR_NOT_CONNECTED;
if (device->info.iMatrixKeyboard)
{
// Get matrix KB configuration
CreateRequest(device->request, 0xCA, 10, 0, 0, 0);
if (SendRequest(device) == PK_OK)
{
device->matrixKB.matrixKBconfiguration = device->response[8];
device->matrixKB.matrixKBheight = 1 + (device->response[9] & 0x0F);
device->matrixKB.matrixKBwidth = 1 + ((device->response[9] >> 4) & 0x0F);
for (n = 0; n < 8; n++)
{
device->matrixKB.matrixKBrowsPins[n] = device->response[10 + n];
device->matrixKB.matrixKBrowsPins[8 + n] = device->response[42 + n];
device->matrixKB.matrixKBcolumnsPins[n] = device->response[18 + n];
}
for (n = 0; n < 128; n++)
{
device->matrixKB.macroMappingOptions[n] = ((device->response[26 + n / 8] & (1 << (n % 8))) > 0) ? 1 : 0;
}
device->matrixKB.matrixKBScanningDecimation = device->response[51];
} else return PK_ERR_TRANSFER;
}
if (device->info.iKeyMapping)
{
// Key codes and modifiers
for (n = 0; n < 8; n++)
{
CreateRequest(device->request, 0xCA, 12 + n, 0, 0, 0);
if (SendRequest(device) == PK_OK)
{
for (key = 0; key < 16; key++)
{
device->matrixKB.keyMappingKeyCode[n * 16 + key] = device->response[8 + key];
device->matrixKB.keyMappingKeyModifier[n * 16 + key] = device->response[24 + key];
}
if (device->info.iMatrixKeyboardTriggeredMapping)
{
// Get trigger status
for (x = 0; x < 8; x++)
{
device->matrixKB.keyMappingTriggeredKey[n * 16 + x] = ((device->response[40] & (1 << x)) > 0) ? 1 : 0;
device->matrixKB.keyMappingTriggeredKey[n * 16 + 8 + x] = ((device->response[41] & (1 << x)) > 0) ? 1 : 0;
}
// Get up key codes and modifiers
CreateRequest(device->request, 0xCA, 32 + n, 0, 0, 0);
if (SendRequest(device) == PK_OK)
{
for (key = 0; key < 16; key++)
{
device->matrixKB.keyMappingKeyCodeUp[n * 16 + key] = device->response[8 + key];
device->matrixKB.keyMappingKeyModifierUp[n * 16 + key] = device->response[24 + key];
}
}
}
}
}
}
return PK_OK;
}
int32_t PK_MatrixKBConfigurationSet(sPoKeysDevice* device)
{
uint32_t n, key, x;
if (device == NULL) return PK_ERR_NOT_CONNECTED;
if (device->info.iMatrixKeyboard)
{
// Set matrix KB configuration
CreateRequest(device->request, 0xCA, 1, 0, 0, 0);
device->request[8] = 0; // Inactivate the keyboard for now...
device->request[9] = (unsigned char)(((device->matrixKB.matrixKBheight - 1) & 0x0F) + (((device->matrixKB.matrixKBwidth - 1) << 4) & 0xF0));
for (n = 0; n < 8; n++)
{
device->request[10 + n] = device->matrixKB.matrixKBrowsPins[n];
device->request[42 + n] = device->matrixKB.matrixKBrowsPins[8 + n];
device->request[18 + n] = device->matrixKB.matrixKBcolumnsPins[n];
}
memset(&(device->request[26]), 0, 16);
for (n = 0; n < 128; n++)
{
if (device->matrixKB.macroMappingOptions[n]) device->request[26 + n / 8] |= (1 << (n % 8));
}
if (SendRequest(device) != PK_OK) return PK_ERR_TRANSFER;
// Set matrix KB configuration
CreateRequest(device->request, 0xCA, 50, 0, 0, 0);
device->request[8] = device->matrixKB.matrixKBScanningDecimation;
if (SendRequest(device) != PK_OK) return PK_ERR_TRANSFER;
}
if (device->info.iKeyMapping)
{
// Key codes and modifiers
for (n = 0; n < 8; n++)
{
CreateRequest(device->request, 0xCA, 2 + n, 0, 0, 0);
for (key = 0; key < 16; key++)
{
device->request[8 + key] = device->matrixKB.keyMappingKeyCode[n * 16 + key];
device->request[24 + key] = device->matrixKB.keyMappingKeyModifier[n * 16 + key];
}
if (device->info.iMatrixKeyboardTriggeredMapping)
{
device->request[40] = 0;
device->request[41] = 0;
// Trigger status
for (x = 0; x < 8; x++)
{
if (device->matrixKB.keyMappingTriggeredKey[n * 16 + x]) device->request[40] |= (1 << x);
if (device->matrixKB.keyMappingTriggeredKey[n * 16 + 8 + x]) device->request[41] |= (1 << x);
}
}
if (SendRequest(device) != PK_OK) return PK_ERR_TRANSFER;
if (device->info.iMatrixKeyboardTriggeredMapping)
{
// Get up key codes and modifiers
CreateRequest(device->request, 0xCA, 22 + n, 0, 0, 0);
for (key = 0; key < 16; key++)
{
device->request[8 + key] = device->matrixKB.keyMappingKeyCodeUp[n * 16 + key];
device->request[24 + key] = device->matrixKB.keyMappingKeyModifierUp[n * 16 + key];
}
if (SendRequest(device) != PK_OK) return PK_ERR_TRANSFER;
}
}
}
// Resend and activate
if (device->info.iMatrixKeyboard)
{
// Set matrix KB configuration
CreateRequest(device->request, 0xCA, 1, 0, 0, 0);
device->request[8] = device->matrixKB.matrixKBconfiguration;
device->request[9] = (unsigned char)(((device->matrixKB.matrixKBheight - 1) & 0x0F) + (((device->matrixKB.matrixKBwidth - 1) << 4) & 0xF0));
for (n = 0; n < 8; n++)
{
device->request[10 + n] = device->matrixKB.matrixKBrowsPins[n];
device->request[42 + n] = device->matrixKB.matrixKBrowsPins[8 + n];
device->request[18 + n] = device->matrixKB.matrixKBcolumnsPins[n];
}
memset(&(device->request[26]), 0, 16);
for (n = 0; n < 128; n++)
{
if (device->matrixKB.macroMappingOptions[n]) device->request[26 + n / 8] |= (1 << (n % 8));
}
if (SendRequest(device) != PK_OK) return PK_ERR_TRANSFER;
}
return PK_OK;
}
int32_t PK_MatrixKBStatusGet(sPoKeysDevice* device)
{
uint32_t n;
if (device == NULL) return PK_ERR_NOT_CONNECTED;
if (device->info.iMatrixKeyboard)
{
// Get matrix KB status
CreateRequest(device->request, 0xCA, 20, 0, 0, 0);
if (SendRequest(device) == PK_OK)
{
for (n = 0; n < 128; n++)
{
device->matrixKB.matrixKBvalues[n] = (device->response[8 + n / 8] & (1 << (n % 8))) > 0;
}
} else return PK_ERR_TRANSFER;
}
return PK_OK;
}