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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
|
/*
* This software is based on the LUFA library. Modifications of the
* software are released under GPL but LUFA library itself is copyrigthed
* by its creator Dean Camera. Refer to the license below on the usage of
* LUFA library.
*
* Chris Boudacoff @ Olimex Ltd chris <at> protonic <dot> co <dot> uk
*/
/*
* LUFA Library Copyright (C) Dean Camera, 2015.
*
* dean [at] fourwalledcubicle [dot] com www.lufa-lib.org
*/
/*
* Copyright 2015 Dean Camera (dean [at] fourwalledcubicle [dot] com)
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and that
* both that the copyright notice and this permission notice and warranty
* disclaimer appear in supporting documentation, and that the name of the
* author not be used in advertising or publicity pertaining to
* distribution of the software without specific, written prior
* permission.
*
* The author disclaims all warranties with regard to this software,
* including all implied warranties of merchantability and fitness. In no
* event shall the author be liable for any special, indirect or
* consequential damages or any damages whatsoever resulting from loss of
* use, data or profits, whether in an action of contract, negligence or
* other tortious action, arising out of or in connection with the use or
* performance of this software.
*/
/** \file
*
* Main source file for the KeyboardMouse demo. This file contains the main tasks of
* the demo and is responsible for the initial application hardware configuration.
*/
#include "KeyboardMouse.h"
/** Magic lock for forced application start. If the HWBE fuse is programmed and BOOTRST is unprogrammed, the bootloader
* will start if the /HWB line of the AVR is held low and the system is reset. However, if the /HWB line is still held
* low when the application attempts to start via a watchdog reset, the bootloader will re-start. If set to the value
* \ref MAGIC_BOOT_KEY the special init function \ref Application_Jump_Check() will force the application to start.
*/
uint16_t MagicBootKey ATTR_NO_INIT;
/** Buffer to hold the previously generated Keyboard HID report, for comparison purposes inside the HID class driver. */
static uint8_t
PrevKeyboardHIDReportBuffer[sizeof(USB_KeyboardReport_Data_t)];
/** Buffer to hold the previously generated Mouse HID report, for comparison purposes inside the HID class driver. */
static uint8_t
PrevMouseHIDReportBuffer[sizeof(USB_WheelMouseReport_Data_t)];
int
limited(int value)
{
if (value < 0) {
if (abs(value) > speedlimit)
return -speedlimit;
} else {
if (value > speedlimit)
return speedlimit;
}
return value;
}
/** LUFA CDC Class driver interface configuration and state information. This structure is
* passed to all CDC Class driver functions, so that multiple instances of the same class
* within a device can be differentiated from one another.
*/
USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface = {
.Config = {
.ControlInterfaceNumber = INTERFACE_ID_CDC_CCI,
.DataINEndpoint = {
.Address = CDC_TX_EPADDR,
.Size = CDC_TXRX_EPSIZE,
.Banks = 1,
}
,
.DataOUTEndpoint = {
.Address = CDC_RX_EPADDR,
.Size = CDC_TXRX_EPSIZE,
.Banks = 1,
}
,
.NotificationEndpoint = {
.Address = CDC_NOTIFICATION_EPADDR,
.Size = CDC_NOTIFICATION_EPSIZE,
.Banks = 1,
}
,
}
,
};
/** LUFA HID Class driver interface configuration and state information. This structure is
* passed to all HID Class driver functions, so that multiple instances of the same class
* within a device can be differentiated from one another. This is for the keyboard HID
* interface within the device.
*/
USB_ClassInfo_HID_Device_t Keyboard_HID_Interface = {
.Config = {
.InterfaceNumber = INTERFACE_ID_Keyboard,
.ReportINEndpoint = {
.Address = KEYBOARD_IN_EPADDR,
.Size = HID_EPSIZE,
.Banks = 1,
}
,
.PrevReportINBuffer = PrevKeyboardHIDReportBuffer,
.PrevReportINBufferSize =
sizeof(PrevKeyboardHIDReportBuffer),
}
,
};
/** LUFA HID Class driver interface configuration and state information. This structure is
* passed to all HID Class driver functions, so that multiple instances of the same class
* within a device can be differentiated from one another. This is for the mouse HID
* interface within the device.
*/
USB_ClassInfo_HID_Device_t Mouse_HID_Interface = {
.Config = {
.InterfaceNumber = INTERFACE_ID_Mouse,
.ReportINEndpoint = {
.Address = MOUSE_IN_EPADDR,
.Size = HID_EPSIZE,
.Banks = 1,
}
,
.PrevReportINBuffer = PrevMouseHIDReportBuffer,
.PrevReportINBufferSize = sizeof(PrevMouseHIDReportBuffer),
}
,
};
void
Jump_To_Bootloader(void)
{
// If USB is used, detach from the bus and reset it
USB_Disable();
// Disable all interrupts
cli();
// Wait one seconds for the USB detachment to register on the host
Delay_MS(1000);
// Set the bootloader key to the magic value and force a reset
wdt_enable(WDTO_250MS);
for (;;);
}
void
tp_guarder(void)
{
tp_guard = true;
tpguard = TP_LOCK;
}
/** Main program entry point. This routine contains the overall program flow, including initial
* setup of all components and the main program loop.
*/
int
main(void)
{
SetupHardware();
GlobalInterruptEnable();
for (;;) {
HID_Device_USBTask(&Keyboard_HID_Interface);
DDRE = 0x00;
PORTE = 0xff;
PORTB = 0xfd;
DDRB = 0xff;
HID_Device_USBTask(&Mouse_HID_Interface);
/*
* Must throw away unused bytes from the host, or it will lock up
* while waiting for the device
*/
CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
CDC_Device_USBTask(&VirtualSerial_CDC_Interface);
USB_USBTask();
if ((PINE & (1 << 2)) == 0) {
DDRB = 0xff;
PORTB = 0xfe;
Delay_MS(10);
if ((PINC & (1 << 7)) == 0) {
PORTB = 0xbf;
Delay_MS(10);
if ((PIND & (1 << 4)) == 0)
Jump_To_Bootloader();
}
}
}
}
/** Configures the board hardware and chip peripherals for the demo's functionality. */
void
SetupHardware()
{
#if (ARCH == ARCH_AVR8)
/*
* Disable watchdog if enabled by bootloader/fuses
*/
MCUSR &= ~(1 << WDRF);
wdt_disable();
/*
* Disable clock division
*/
clock_prescale_set(clock_div_1);
#elif (ARCH == ARCH_XMEGA)
/*
* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and
* switch the CPU core to run from it
*/
XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);
/*
* Start the 32MHz internal RC oscillator and start the DFLL to
* increase it to 48MHz using the USB SOF as a reference
*/
XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);
PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
#endif
/*
* Hardware Initialization
*/
// Keyboard init
DDRB = 0xff;
PORTB = 0xff;
DDRD = 0;
PORTD = 0xfc;
DDRC = 0;
PORTC = 0xc0;
DDRE = 0;
PORTE = 0x44;
DDRF = 0;
PORTF = 0xf2;
TWI_Init(TWI_BIT_PRESCALE_4,
TWI_BITLENGTH_FROM_FREQ(TWI_BIT_PRESCALE_4, 100000));
ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_128);
ADCSRA |= 1 << ADIE;
MCUCR = 0;
EIMSK |= 1 << INT2;
ADC_SetupChannel(0);
ADC_StartReading(ADC_REFERENCE_AVCC | ADC_LEFT_ADJUSTED |
ADC_CHANNEL0);
// set timer0 counter initial value to 0
TCNT0 = 0x00;
// start timer0 with /1024 prescaler
TCCR0B = (1 << CS02) | (1 << CS00);
TIMSK0 = 1 << TOIE0;
USB_Init();
button = 0xff;
}
/** Event handler for the library USB Connection event. */
void
EVENT_USB_Device_Connect(void)
{
}
/** Event handler for the library USB Disconnection event. */
void
EVENT_USB_Device_Disconnect(void)
{
}
/** Event handler for the library USB Configuration Changed event. */
void
EVENT_USB_Device_ConfigurationChanged(void)
{
bool ConfigSuccess = true;
ConfigSuccess &=
HID_Device_ConfigureEndpoints(&Keyboard_HID_Interface);
ConfigSuccess &= HID_Device_ConfigureEndpoints(&Mouse_HID_Interface);
ConfigSuccess &=
CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface);
USB_Device_EnableSOFEvents();
}
/** Event handler for the library USB Control Request reception event. */
void
EVENT_USB_Device_ControlRequest(void)
{
HID_Device_ProcessControlRequest(&Keyboard_HID_Interface);
HID_Device_ProcessControlRequest(&Mouse_HID_Interface);
HID_Device_ProcessControlRequest(&Mouse_HID_Interface);
}
/** Event handler for the USB device Start Of Frame event. */
void
EVENT_USB_Device_StartOfFrame(void)
{
HID_Device_MillisecondElapsed(&Keyboard_HID_Interface);
HID_Device_MillisecondElapsed(&Mouse_HID_Interface);
}
/** CDC class driver callback function the processing of changes to the virtual
* control lines sent from the host..
*
* \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced
*/
void
EVENT_CDC_Device_ControLineStateChanged(USB_ClassInfo_CDC_Device_t *
const CDCInterfaceInfo)
{
/*
* You can get changes to the virtual CDC lines in this callback; a
* common use-case is to use the Data Terminal Ready (DTR) flag to
* enable and disable CDC communications in your application when set
* to avoid the application blocking while waiting for a host to
* become ready and read in the pending data from the USB endpoints.
*/
bool HostReady =
(CDCInterfaceInfo->State.ControlLineStates.
HostToDevice & CDC_CONTROL_LINE_OUT_DTR) != 0;
}
void
reset_key(USB_KeyboardReport_Data_t * report)
{
keyc = 0;
report->Modifier = 0;
for (int x = 0; x < 6; x++)
report->KeyCode[x] = 0;
}
void
add_key(USB_KeyboardReport_Data_t * report, uint8_t key)
{
if (key == 0)
return;
report->KeyCode[keyc] = key;
if (keyc < 5)
keyc++;
}
/** HID class driver callback function for the creation of HID reports to the host.
*
* \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
* \param[in,out] ReportID Report ID requested by the host if non-zero, otherwise callback should set to the generated report ID
* \param[in] ReportType Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature
* \param[out] ReportData Pointer to a buffer where the created report should be stored
* \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent)
*
* \return Boolean \c true to force the sending of the report, \c false to let the library determine if it needs to be sent
*/
bool
CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t *
const HIDInterfaceInfo,
uint8_t * const ReportID,
const uint8_t ReportType,
void *ReportData,
uint16_t * const ReportSize)
{
/*
* Determine which interface must have its report generated
*/
if (HIDInterfaceInfo == &Keyboard_HID_Interface) {
USB_KeyboardReport_Data_t *KeyboardReport =
(USB_KeyboardReport_Data_t *) ReportData;
uint8_t x = 0;
uint8_t fn = 0;
reset_key(KeyboardReport);
if (itsDone) {
itsDone = false;
}
DDRE = 0;
DDRF = 0;
DDRC = 0;
DDRD = 0;
PORTD = 0xfc;
PORTE = 0xFF;
PORTF = 0xFF;
PORTC = 0xFF;
DDRB = 0xff;
for (colmn = 0; colmn < 8; colmn++) { // y ?
uint8_t keynow = 0;
// that's crazy... here we set d4=1 (x14-15) and then read
// from port B (y0..y7)
// even if we are supposed to set Y0 and then read along X
if (colmn == 0) {
cli();
DDRB = 0x00; // all input
PORTB = 0x00; // pull up off
DDRD = (1 << 4); // d4 output
PORTD = (1 << 4); // d4 = 1 -> x14-15
Delay_MS(2);
if (PINB & 0x40) { // b6(y6) == 1 -> Fn Key
fn = 112;
// tp_guarder();
if (PINB & 0x01) {
add_key(KeyboardReport, HID_KEYBOARD_SC_PAGE_UP);
}
if (PINB & 0x02) {
KeyboardReport->Modifier |=
HID_KEYBOARD_MODIFIER_LEFTSHIFT;
add_key(KeyboardReport,
HID_KEYBOARD_SC_LEFT_SHIFT);
}
if (PINB & 0x20) {
add_key(KeyboardReport, HID_KEYBOARD_SC_HOME);
}
if (PINB & 0x80) {
KeyboardReport->Modifier |=
HID_KEYBOARD_MODIFIER_RIGHTSHIFT;
add_key(KeyboardReport,
HID_KEYBOARD_SC_RIGHT_SHIFT);
}
} else {
if (PINB & 0x01) {
add_key(KeyboardReport, HID_KEYBOARD_SC_UP_ARROW);
}
if (PINB & 0x02) {
KeyboardReport->Modifier |=
HID_KEYBOARD_MODIFIER_LEFTSHIFT;
add_key(KeyboardReport,
HID_KEYBOARD_SC_LEFT_SHIFT);
}
if (PINB & 0x20) {
add_key(KeyboardReport,
HID_KEYBOARD_SC_LEFT_ARROW);
}
if (PINB & 0x80) {
KeyboardReport->Modifier |=
HID_KEYBOARD_MODIFIER_RIGHTSHIFT;
add_key(KeyboardReport,
HID_KEYBOARD_SC_RIGHT_SHIFT);
}
}
#if 0
// x0
DDRD = (1 << 6); // d6 output (x0)
PORTD = (1 << 6); // d6 =1
DDRB = 0x00; // input
PORTB = 0x00; // no pullup
Delay_MS(2);
if (PINB & 0x10) {
KeyboardReport->Modifier |=
HID_KEYBOARD_MODIFIER_RIGHTCTRL;
add_key(KeyboardReport, HID_KEYBOARD_SC_RIGHT_CONTROL);
}
if (PINB & 0x40) {
KeyboardReport->Modifier |=
HID_KEYBOARD_MODIFIER_LEFTCTRL;
add_key(KeyboardReport, HID_KEYBOARD_SC_LEFT_CONTROL);
}
if (PINB & 0x08) {
if (fn == 112)
add_key(KeyboardReport, HID_KEYBOARD_SC_VOLUME_UP);
else
add_key(KeyboardReport, HID_KEYBOARD_SC_F5);
}
#endif
PORTB = rowY[colmn];
DDRB = 0xff;
DDRD = 0;
PORTD = 0xfc;
Delay_MS(3);
sei();
} // if colmn==0
PORTB = rowY[colmn];
while (PINB != rowY[colmn]);
x = 0;
if ((PIND & (1 << 6)) == 0) {
// tp_guarder();
keynow = keys[colmn * 14 + x + fn];
KeyboardReport->Modifier |= keym[colmn * 14 + x];
add_key(KeyboardReport, keynow);
}
x = 1; // 1
if ((PINC & (1 << 7)) == 0) {
// tp_guarder();
keynow = keys[colmn * 14 + x + fn];
KeyboardReport->Modifier |= keym[colmn * 14 + x];
add_key(KeyboardReport, keynow);
}
x++; // 2
if ((PINC & (1 << 6)) == 0) {
// tp_guarder();
keynow = keys[colmn * 14 + x + fn];
KeyboardReport->Modifier |= keym[colmn * 14 + x];
add_key(KeyboardReport, keynow);
}
x++; // 3
if ((PIND & (1 << 7)) == 0) {
// tp_guarder();
keynow = keys[colmn * 14 + x + fn];
KeyboardReport->Modifier |= keym[colmn * 14 + x];
if (keynow == HID_KEY_LOCK_TOUCHPAD) {
TouchPadLocked = !TouchPadLocked;
while ((PIND & (1 << 7)) == 0);
}
add_key(KeyboardReport, keynow);
}
x++; // 4
// Delay_MS(5);
if ((PIND & (1 << 3)) == 0) {
// tp_guarder();
keynow = keys[colmn * 14 + x + fn];
KeyboardReport->Modifier |= keym[colmn * 14 + x];
add_key(KeyboardReport, keynow);
}
x++; // 5
if ((PINF & (1 << 1)) == 0) {
// tp_guarder();
keynow = keys[colmn * 14 + x + fn];
KeyboardReport->Modifier |= keym[colmn * 14 + x];
add_key(KeyboardReport, keynow);
}
x++; // 6
if ((PINF & (1 << 6)) == 0) {
// tp_guarder();
keynow = keys[colmn * 14 + x + fn];
KeyboardReport->Modifier |= keym[colmn * 14 + x];
add_key(KeyboardReport, keynow);
}
x++; // 7
if ((PINE & (1 << 6)) == 0) {
// tp_guarder();
keynow = keys[colmn * 14 + x + fn];
KeyboardReport->Modifier |= keym[colmn * 14 + x];
add_key(KeyboardReport, keynow);
}
x++; // 8
if ((PINF & (1 << 4)) == 0) {
// tp_guarder();
keynow = keys[colmn * 14 + x + fn];
KeyboardReport->Modifier |= keym[colmn * 14 + x];
add_key(KeyboardReport, keynow);
}
x++; // 9
if ((PIND & (1 << 5)) == 0) {
// tp_guarder();
keynow = keys[colmn * 14 + x + fn];
KeyboardReport->Modifier |= keym[colmn * 14 + x];
add_key(KeyboardReport, keynow);
}
x++; // 10
if ((PINF & (1 << 5)) == 0) {
// tp_guarder();
keynow = keys[colmn * 14 + x + fn];
KeyboardReport->Modifier |= keym[colmn * 14 + x];
add_key(KeyboardReport, keynow);
}
x++; // 11
if ((PINF & (1 << 7)) == 0) {
// tp_guarder();
keynow = keys[colmn * 14 + x + fn];
KeyboardReport->Modifier |= keym[colmn * 14 + x];
add_key(KeyboardReport, keynow);
}
x++; // 12
if ((PINE & (1 << 2)) == 0) {
// tp_guarder();
keynow = keys[colmn * 14 + x + fn];
KeyboardReport->Modifier |= keym[colmn * 14 + x];
add_key(KeyboardReport, keynow);
}
DDRD = 0;
PORTD = 0xfc;
}
if ((KeyboardReport->Modifier == 0) && (keyc != 0))
tp_guarder();
*ReportSize = sizeof(USB_KeyboardReport_Data_t);
return true; // return false;
}
if (HIDInterfaceInfo == &Mouse_HID_Interface) {
USB_WheelMouseReport_Data_t *MouseReport =
(USB_WheelMouseReport_Data_t *) ReportData;
MR_Y = 0;
MR_X = 0;
MR_W = 0;
MR_B |= mouse; // (1 << 0);
// cli();
if (ActionSend) {
uint16_t posx1 = tpdata[3] << 8 | tpdata[4];
uint16_t posy1 = tpdata[5] << 8 | tpdata[6];
switch (tpdata[2]) {
case 0:
if (lastfingers != 0) {
if (time_zero > PRETAP) {
if (time_pressed < CLICK_MS) {
if (abs(startposX + startposY - posx1 - posy1)
< 10) {
if (time_two != 0)
MR_B_REQ = MOUSE_RIGTH;
else
MR_B_REQ = MOUSE_LEFT;
}
}
}
time_zero = 0;
}
speedlimit = 0;
lastposX = 0;
lastposY = 0;
break;
case 1:
touch_to = TOUCH_TO;
switch (lastfingers) {
case 0:
presstime = CLICK_MS;
speedlimit = 0;
break;
case 1:
if (lastposX != 0) {
MR_X = limited(posx1 - lastposX);
MR_Y = limited(posy1 - lastposY);
}
break;
case 2:
speedlimit = 0;
break;
default:
break;
}
if (lastfingers != 1) {
time_one = 0;
startposX = posx1;
startposY = posy1;
lastfingers = 1;
}
lastposX = posx1;
lastposY = posy1;
if (speedlimit != 0x80)
speedlimit += 0x04;
break;
case 2:
if (lastfingers == 0)
touch_to = TOUCH_TO;
if (lastfingers == 2) {
if (lastposY > posy1 + DRAG_HYST)
MR_W = WHEEL;
else if (lastposY + DRAG_HYST < posy1)
MR_W = -WHEEL;
if (MR_W != 0)
time_zero = 0;
presstime = 0;
} else {
time_two = 0;
startposX = posx1;
startposY = posy1;
lastfingers = 2;
}
lastposX = posx1;
lastposY = posy1;
break;
default:
lastposX = 0;
lastposY = 0;
break;
}
lastfingers = tpdata[2];
ActionSend = false;
itsDone = true;
}
MouseReport->Y = MR_Y;
MouseReport->X = MR_X;
MouseReport->Button = MR_B;
MouseReport->Wheel = MR_W;
MR_B = 0;
*ReportSize = sizeof(USB_WheelMouseReport_Data_t);
return true;
}
return false;
}
/** HID class driver callback function for the processing of HID reports from the host.
*
* \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
* \param[in] ReportID Report ID of the received report from the host
* \param[in] ReportType The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature
* \param[in] ReportData Pointer to a buffer where the received report has been stored
* \param[in] ReportSize Size in bytes of the received HID report
*/
void
CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t *
const HIDInterfaceInfo,
const uint8_t ReportID,
const uint8_t ReportType,
const void *ReportData,
const uint16_t ReportSize)
{
if (HIDInterfaceInfo == &Keyboard_HID_Interface) {
}
}
ISR(ADC_vect)
{
ADCSRA |= (1 << ADIF);
button = ADCH;
if (button == oldbutton) {
if (button < 0xe8) {
if (button > 0xa0)
mouse = MOUSE_RIGTH;
else if (button > 0x80)
mouse = MOUSE_LEFT;
else
mouse = MOUSE_MIDDLE;
} else
mouse = 0;
} else
oldbutton = button;
mouse = 0; // temporary fix: disable button
}
ISR(INT2_vect)
{
#if 1
if (TWI_StartTransmission(0x48 | TWI_ADDRESS_WRITE, 10) ==
TWI_ERROR_NoError) {
TWI_SendByte(0x00);
TWI_StopTransmission();
if (TWI_StartTransmission(0x48 | TWI_ADDRESS_READ, 10) ==
TWI_ERROR_NoError) {
// Read some bytes, acknowledge after the last byte is
// received????
for (int s = 0; s < 0x06; s++)
TWI_ReceiveByte(&tpdata[s], false);
TWI_ReceiveByte(&tpdata[0x06], true);
}
}
// Must stop transmission afterwards to release the bus
TWI_StopTransmission();
if ((!TouchPadLocked) && (!tp_guard))
ActionSend = true;
#endif
}
// timer0 overflow ~60hz
ISR(TIMER0_OVF_vect)
{
#if 1
if (TWI_StartTransmission(0x48 | TWI_ADDRESS_WRITE, 10) ==
TWI_ERROR_NoError) {
TWI_SendByte(0x00);
TWI_StopTransmission();
if (TWI_StartTransmission(0x48 | TWI_ADDRESS_READ, 10) ==
TWI_ERROR_NoError) {
// Read some bytes, acknowledge after the last byte is
// received????
for (int s = 0; s < 0x06; s++)
TWI_ReceiveByte(&tpdata[s], false);
TWI_ReceiveByte(&tpdata[0x06], true);
}
}
// Must stop transmission afterwards to release the bus
TWI_StopTransmission();
if ((!TouchPadLocked) && (!tp_guard) && (tpdata[0] != 0))
ActionSend = true;
#endif
if ((time_zero != 0xff) && (tpdata[2] == 0))
time_zero++;
if (time_zero == TOUCH_TO) {
#if TAP_ENABLED
MR_B = MR_B_REQ;
MR_B_REQ = 0;
#endif
time_one = 0;
time_two = 0;
time_pressed = 0;
}
if ((time_one != 0xff) && (tpdata[2] == 1))
time_one++;
if ((time_two != 0xff) && (tpdata[2] == 2))
time_two++;
if (ticks != 0)
ticks--;
if (dragtime != 0)
dragtime--;
if (presstime != 0)
presstime--;
if (tpguard != 0)
tpguard--;
if (tpguard == 1)
tp_guard = false;
if (touch_to != 0)
touch_to--;
if (touch_to == 1) {
tap_enabled = true;
lastposX = 0;
lastposY = 0;
drag = false;
if (presstime != 0)
ActionSend = true;
else
lastfingers = 0;
}
}
|