-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
412 lines (367 loc) · 12 KB
/
main.c
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* Name: main.c
* Project: Keyboard test
* Author: Ronaldo Oliveira, baseado no exemplo de Christian Starkjohann and others
* Creation Date: 2013-06-23
*/
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <stdlib.h>
#include "keycode.h"
#include "usbdrv.h"
#define BUTTON_PORT_B PORTB /* PORTx - register for BUTTON 1,2 e 3 output */
#define BUTTON_PIN_B PINB /* PINx - register for BUTTON 1,2 e 3 */
#define BUTTON_BIT_B1 PB1 /* bit for BUTTON 1 input/output */
#define BUTTON_BIT_B2 PB3 /* bit for BUTTON 3 input/output */
#define BUTTON_BIT_B3 PB4 /* bit for BUTTON 4 input/output */
#define SIZE 59
#define ALL_SIZE SIZE*4
#define WAIT_DOT 10
#define WAIT_END 45
/* ------------------------------------------------------------------------- */
const PROGMEM char usbHidReportDescriptor[USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH] = { /* USB report descriptor */
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x06, // USAGE (Keyboard)
0xa1, 0x01, // COLLECTION (Application)
0x05, 0x07, // USAGE_PAGE (Keyboard)
0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x08, // REPORT_COUNT (8)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x08, // REPORT_SIZE (8)
0x25, 0x65, // LOGICAL_MAXIMUM (101)
0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
0x81, 0x00, // INPUT (Data,Ary,Abs)
0xc0 // END_COLLECTION
};
/* We use a simplifed keyboard report descriptor which does not support the
* boot protocol. We don't allow setting status LEDs and we only allow one
* simultaneous key press (except modifiers). We can therefore use short
* 2 byte input reports.
* The report descriptor has been created with usb.org's "HID Descriptor Tool"
* which can be downloaded from http://www.usb.org/developers/hidpage/.
* Redundant entries (such as LOGICAL_MINIMUM and USAGE_PAGE) have been omitted
* for the second INPUT item.
*/
PROGMEM const uchar keyinfo[ALL_SIZE] =
{0,1,KEY_E,0,
0,2,KEY_I,0,
0,3,KEY_S,0,
0,4,KEY_H,0,
0,5,KEY_5,0,
0,6,KEY_ENTER,0,
1,1,KEY_T,0,
1,2,KEY_A,0,
1,3,KEY_U,0,
1,4,KEY_V,0,
1,5,KEY_4,0,
1,6,KEY_DELETE,KEY_CTRL + KEY_ALT,
2,2,KEY_N,0,
2,3,KEY_R,0,
2,4,KEY_F,0,
3,2,KEY_M,0,
3,3,KEY_W,0,
3,4,KEY_SPACE,0,
3,5,KEY_3,0,
3,6,KEY_7,KEY_SHIFT,
4,3,KEY_D,0,
4,4,KEY_L,0,
5,3,KEY_K,0,
5,4,KEY_C,0,
6,3,KEY_G,0,
6,4,KEY_P,0,
7,3,KEY_O,0,
7,4,KEY_J,0,
7,5,KEY_2,0,
8,4,KEY_B,0,
8,5,KEY_6,KEY_SHIFT,
9,4,KEY_X,0,
9,7,KEY_4,KEY_SHIFT,
10,4,KEY_C,0,
11,4,KEY_Y,0,
12,4,KEY_Z,0,
12,6,KEY_MINUS,KEY_SHIFT,
13,4,KEY_Q,0,
13,6,KEY_SLASH,KEY_SHIFT,
14,4,KEY_J,0,
15,5,KEY_1,0,
16,5,KEY_6,0,
17,5,KEY_0,KEY_SHIFT,
18,5,KEY_7,KEY_SHIFT,
18,6,KEY_2,KEY_SHIFT,
20,5,KEY_SEMICOLON,0,
21,6,KEY_PERIOD,0,
22,5,KEY_8,KEY_SHIFT,
24,5,KEY_7,0,
26,6,KEY_2,KEY_RIGHT_ALT,
28,5,KEY_8,0,
30,5,KEY_9,0,
30,6,KEY_MINUS,0,
33,6,KEY_SLASH,0,
42,6,KEY_COMMA,KEY_SHIFT,
43,6,KEY_1,KEY_SHIFT,
45,6,KEY_9,KEY_SHIFT,
51,6,KEY_COMMA,0,
120,7,KEY_PERIOD,0
};
/* ------------------------------------------------------------------------- */
static uchar reportBuffer[2]; /* buffer for HID reports */
static uchar idleRate; /* in 4 ms units */
static uchar keyToSend = 0; /* keys to send */
unsigned int timerDelay = 0; /* counter for delay period */
static uchar command = 0;
static uchar lastcommand = 0;
static uchar nextkey = 0;
static uchar keymod = 0; /* no modifiers */
static uchar bits_mask = 0;
static uchar mask_size = 0;
static uchar keybuffer[5];
static uchar keybuffersize = 0;
static uchar ntokey(uchar n) {
if (n == 0) {
return KEY_0;
}
return KEY_Z + n;
}
static void checkButtonChange(void) {
command = 0;
if (bit_is_set(BUTTON_PIN_B, BUTTON_BIT_B1) == 0) {
command = 1;
} else if (bit_is_set(BUTTON_PIN_B, BUTTON_BIT_B2) == 0) {
command = 2;
}
switch (command) {
case 0: if (lastcommand == 1) {
// verifica se é um ponto ou um traco
mask_size ++; // tamanho
bits_mask = bits_mask * 2 ; // onde existe tracos
if (timerDelay > WAIT_DOT) {
bits_mask = bits_mask + 1;
}
// reinicia contador
timerDelay = 0;
} else if (lastcommand == 2) {
/*
// verifica se é um ponto ou um traco
if (timerDelay > WAIT_DOT) {
nextkey = KEY_SLASH;
} else {
nextkey = KEY_PERIOD;
}
keyToSend = 2;
*/
keybuffersize = 0;
keybuffer[keybuffersize++] = KEY_ENTER;
unsigned int t0 = timerDelay / 10;
uchar t1 = timerDelay % 10;
keybuffer[keybuffersize++] = ntokey(t1);
t1 = t0 % 10;
keybuffer[keybuffersize++] = ntokey(t1);
t0 = t0 / 10;
t1 = t0 % 10;
keybuffer[keybuffersize++] = ntokey(t1);
/*
t0 = t0 / 10;
t1 = t0 % 10;
keybuffer[keybuffersize++] = ntokey(t1);
*/
// reinicia contador
timerDelay = 0;
} else if (lastcommand == 0) {
// verifica o timeout
if (mask_size == 0) {
timerDelay = 0;
}
if (timerDelay > WAIT_END) { //1 segundo
// se timeout, define a tecla a enviar
nextkey = 0;
keymod = 0;
uchar i = 0;
timerDelay = 0;
for (; i < SIZE; i++) {
// multiplica por 4
uchar k = i * 4;
if (pgm_read_byte(&keyinfo[k++]) == bits_mask) { // bits
if (pgm_read_byte(&keyinfo[k++]) == mask_size) { // size
nextkey = pgm_read_byte(&keyinfo[k++]);
keymod = pgm_read_byte(&keyinfo[k]);
keyToSend = 2;
}
}
}
bits_mask = 0;
mask_size = 0;
} else {
if (keybuffersize > 0) {
keybuffersize--;
nextkey = keybuffer[keybuffersize];
keymod = 0;
keyToSend = 2;
}
}
}
break;
case 1: if (lastcommand == 0) {
// mudou para pressionado, inicia contador
timerDelay = 0;
}
break;
case 2: if (lastcommand == 0) {
timerDelay = 0;
}
break;
}
timerDelay++;
lastcommand = command;
}
static void buildReport(void)
{
reportBuffer[0] = keymod;
reportBuffer[1] = nextkey;
}
static void verifyCommand(void)
{
if(TIFR & (1 << TOV1)){
TIFR = (1 << TOV1); /* clear overflow */
checkButtonChange();
}
}
/* ------------------------------------------------------------------------- */
static void timerInit(void)
{
TCCR1 = 0x0b; /* select clock: 16.5M/1k -> overflow rate = 16.5M/256k = 62.94 Hz */
}
/* ------------------------------------------------------------------------- */
/* ------------------------ interface to USB driver ------------------------ */
/* ------------------------------------------------------------------------- */
uchar usbFunctionSetup(uchar data[8])
{
usbRequest_t *rq = (void *)data;
usbMsgPtr = reportBuffer;
if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */
if(rq->bRequest == USBRQ_HID_GET_REPORT){ /* wValue: ReportType (highbyte), ReportID (lowbyte) */
/* we only have one report type, so don't look at wValue */
buildReport();
return sizeof(reportBuffer);
}else if(rq->bRequest == USBRQ_HID_GET_IDLE){
usbMsgPtr = &idleRate;
return 1;
}else if(rq->bRequest == USBRQ_HID_SET_IDLE){
idleRate = rq->wValue.bytes[1];
}
}else{
/* no vendor specific requests implemented */
}
return 0;
}
/* ------------------------------------------------------------------------- */
/* ------------------------ Oscillator Calibration ------------------------- */
/* ------------------------------------------------------------------------- */
/* Calibrate the RC oscillator to 8.25 MHz. The core clock of 16.5 MHz is
* derived from the 66 MHz peripheral clock by dividing. Our timing reference
* is the Start Of Frame signal (a single SE0 bit) available immediately after
* a USB RESET. We first do a binary search for the OSCCAL value and then
* optimize this value with a neighboorhod search.
* This algorithm may also be used to calibrate the RC oscillator directly to
* 12 MHz (no PLL involved, can therefore be used on almost ALL AVRs), but this
* is wide outside the spec for the OSCCAL value and the required precision for
* the 12 MHz clock! Use the RC oscillator calibrated to 12 MHz for
* experimental purposes only!
*/
static void calibrateOscillator(void)
{
uchar step = 128;
uchar trialValue = 0, optimumValue;
int x, optimumDev, targetValue = (unsigned)(1499 * (double)F_CPU / 10.5e6 + 0.5);
/* do a binary search: */
do{
OSCCAL = trialValue + step;
x = usbMeasureFrameLength(); /* proportional to current real frequency */
if(x < targetValue) /* frequency still too low */
trialValue += step;
step >>= 1;
}while(step > 0);
/* We have a precision of +/- 1 for optimum OSCCAL here */
/* now do a neighborhood search for optimum value */
optimumValue = trialValue;
optimumDev = x; /* this is certainly far away from optimum */
for(OSCCAL = trialValue - 1; OSCCAL <= trialValue + 1; OSCCAL++){
x = usbMeasureFrameLength() - targetValue;
if(x < 0)
x = -x;
if(x < optimumDev){
optimumDev = x;
optimumValue = OSCCAL;
}
}
OSCCAL = optimumValue;
}
/*
Note: This calibration algorithm may try OSCCAL values of up to 192 even if
the optimum value is far below 192. It may therefore exceed the allowed clock
frequency of the CPU in low voltage designs!
You may replace this search algorithm with any other algorithm you like if
you have additional constraints such as a maximum CPU clock.
For version 5.x RC oscillators (those with a split range of 2x128 steps, e.g.
ATTiny25, ATTiny45, ATTiny85), it may be useful to search for the optimum in
both regions.
*/
void usbEventResetReady(void)
{
calibrateOscillator();
eeprom_write_byte(0, OSCCAL); /* store the calibrated value in EEPROM */
}
/* ------------------------------------------------------------------------- */
/* --------------------------------- main ---------------------------------- */
/* ------------------------------------------------------------------------- */
int main(void)
{
uchar i;
uchar calibrationValue;
calibrationValue = eeprom_read_byte(0); /* calibration value from last time */
if(calibrationValue != 0xff){
OSCCAL = calibrationValue;
}
//odDebugInit();
usbDeviceDisconnect();
for(i=0;i<20;i++){ /* 300 ms disconnect */
_delay_ms(15);
}
usbDeviceConnect();
wdt_enable(WDTO_1S);
/* turn on internal pull-up resistor for the switches */
BUTTON_PORT_B |= _BV(BUTTON_BIT_B1);
BUTTON_PORT_B |= _BV(BUTTON_BIT_B2);
BUTTON_PORT_B |= _BV(BUTTON_BIT_B3);
timerInit();
usbInit();
sei();
for(;;){ /* main event loop */
wdt_reset();
usbPoll();
/* A USB keypress cycle is defined as a scancode being present in a report, and
then absent from a later report. Example, to press and release the Caps Lock key, instead of
holding it down, we need to send the report with the Caps Lock scancode and
then an empty report. */
if (keyToSend > 0) {
if(usbInterruptIsReady()){ /* we can send another key */
buildReport();
usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
keyToSend--;
nextkey = 0;
keymod = 0; /* no modifiers */
}
} else {
verifyCommand();
}
}
return 0;
}