-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
880 lines (768 loc) · 24.7 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
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
880
// the "usb_hid_rpt_desc.hid" file needs to be viewed with the HID Descriptor Tool from USB.org
// required headers
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include <string.h>
#include <avr/eeprom.h> // text file and calibration data is stored in EEPROM
#include <stdio.h> // allows streaming strings
#include <stdint.h>
// V-USB
#include "usbconfig.h"
#include "usbdrv/usbdrv.h"
#define MOD_CONTROL_LEFT (1<<0)
#define MOD_SHIFT_LEFT (1<<1)
#define MOD_ALT_LEFT (1<<2)
#define MOD_GUI_LEFT (1<<3)
#define MOD_CONTROL_RIGHT (1<<4)
#define MOD_SHIFT_RIGHT (1<<5)
#define MOD_ALT_RIGHT (1<<6)
#define MOD_GUI_RIGHT (1<<7)
#define KEY_1 30
#define KEY_2 31
#define KEY_3 32
#define KEY_4 33
#define KEY_5 34
#define KEY_6 35
#define KEY_7 36
#define KEY_8 37
#define KEY_9 38
#define KEY_0 39
#define KEY_RETURN 40
#define KEY_TAB 43
#define WHITE_LED 1
#define YELLOW_LED 1
#define LED 3
#define UTIL_BIN4(x) (uchar)((0##x & 01000)/64 + (0##x & 0100)/16 + (0##x & 010)/4 + (0##x & 1))
#define UTIL_BIN8(hi, lo) (uchar)(UTIL_BIN4(hi) * 16 + UTIL_BIN4(lo))
#define sbi(var, mask) ((var) |= (uint8_t)(1 << mask))
#define cbi(var, mask) ((var) &= (uint8_t)~(1 << mask))
#ifndef NULL
#define NULL ((void *)0)
#endif
#define clock !(PINB & (1<<PINB4))
#define clockbit !(PINB & (1<<PINB3))
PROGMEM char usbHidReportDescriptor[USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH] = {
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x06, // USAGE (Keyboard)
0xa1, 0x01, // COLLECTION (Application)
0x85, 0x01, // REPORT_ID (1)
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)
0x81, 0x03, // INPUT (Cnst,Var,Abs)
0x95, 0x03, // REPORT_COUNT (3)
0x75, 0x08, // REPORT_SIZE (8)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x65, // LOGICAL_MAXIMUM (101)
0x05, 0x07, // USAGE_PAGE (Keyboard)
0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
0x81, 0x00, // INPUT (Data,Ary,Abs)
0x95, 0x05, // REPORT_COUNT (5)
0x75, 0x01, // REPORT_SIZE (1)
0x05, 0x08, // USAGE_PAGE (LEDs)
0x19, 0x01, // USAGE_MINIMUM (Num Lock)
0x29, 0x05, // USAGE_MAXIMUM (Kana)
0x91, 0x02, // OUTPUT (Data,Var,Abs) ; LED report
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x03, // REPORT_SIZE (3)
0x91, 0x03, // OUTPUT (Cnst,Var,Abs) ; LED report padding
0xc0, // END_COLLECTION
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x02, // USAGE (Mouse)
0xa1, 0x01, // COLLECTION (Application)
0x09, 0x01, // USAGE (Pointer)
0xa1, 0x00, // COLLECTION (Physical)
0x85, 0x02, // REPORT_ID (2)
0x05, 0x09, // USAGE_PAGE (Button)
0x19, 0x01, // USAGE_MINIMUM (Button 1)
0x29, 0x03, // USAGE_MAXIMUM (Button 3)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x95, 0x03, // REPORT_COUNT (3)
0x75, 0x01, // REPORT_SIZE (1)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x05, // REPORT_SIZE (5)
0x81, 0x03, // INPUT (Cnst,Var,Abs)
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x30, // USAGE (X)
0x09, 0x31, // USAGE (Y)
0x09, 0x38, // USAGE (Wheel)
0x15, 0x81, // LOGICAL_MINIMUM (-127)
0x25, 0x7f, // LOGICAL_MAXIMUM (127)
0x75, 0x08, // REPORT_SIZE (8)
0x95, 0x03, // REPORT_COUNT (3)
0x81, 0x06, // INPUT (Data,Var,Rel)
0xc0, // END_COLLECTION
0xc0, // END_COLLECTION
};
typedef struct
{
uint8_t report_id;
uint8_t modifier;
uint8_t reserved;
uint8_t keycode[3];
} keyboard_report_t;
static keyboard_report_t keyboard_report;
#define keyboard_report_reset() keyboard_report.report_id=1;keyboard_report.modifier=0;keyboard_report.reserved=0;keyboard_report.keycode[0]=0;keyboard_report.keycode[1]=0;keyboard_report.keycode[2]=0;
typedef struct
{
uint8_t report_id;
uint8_t buttons;
int8_t x;
int8_t y;
int8_t wheel;
} mouse_report_t;
static mouse_report_t mouse_report;
#define mouse_report_reset() mouse_report.report_id=2;mouse_report.buttons=0;mouse_report.x=0;mouse_report.y=0;mouse_report.wheel=0;
static uint8_t idle_rate = 500 / 4; // see HID1_11.pdf sect 7.2.4
static uint8_t protocol_version = 0; // see HID1_11.pdf sect 7.2.6
/* ------------------------------------------------------------------------- */
/* ------------------------ 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.
*/
//-----------------------USB--------------------------//
//----------------------------------------------------//
usbMsgLen_t usbFunctionSetup(uint8_t data[8])
{
// see HID1_11.pdf sect 7.2 and http://vusb.wikidot.com/driver-api
usbRequest_t *rq = (void *)data;
if ((rq->bmRequestType & USBRQ_TYPE_MASK) != USBRQ_TYPE_CLASS)
return 0; // ignore request if it's not a class specific request
// see HID1_11.pdf sect 7.2
switch (rq->bRequest)
{
case USBRQ_HID_GET_IDLE:
usbMsgPtr = &idle_rate; // send data starting from this byte
return 1; // send 1 byte
case USBRQ_HID_SET_IDLE:
idle_rate = rq->wValue.bytes[1]; // read in idle rate
return 0; // send nothing
case USBRQ_HID_GET_PROTOCOL:
usbMsgPtr = &protocol_version; // send data starting from this byte
return 1; // send 1 byte
case USBRQ_HID_SET_PROTOCOL:
protocol_version = rq->wValue.bytes[1];
return 0; // send nothing
case USBRQ_HID_GET_REPORT:
// check for report ID then send back report
if (rq->wValue.bytes[0] == 1)
{
usbMsgPtr = &keyboard_report;
return sizeof(keyboard_report);
}
else if (rq->wValue.bytes[0] == 2)
{
usbMsgPtr = &mouse_report;
return sizeof(mouse_report);
}
else
{
return 0; // no such report, send nothing
}
case USBRQ_HID_SET_REPORT: // no "output" or "feature" implemented, so ignore
if (rq->wValue.bytes[0] == 1){
return USB_NO_MSG; // send nothing but call usbFunctionWrite
}else{
return 0; // send nothing
}
default: // do not understand data, ignore
return 0; // send nothing
}
}
static uint8_t LED_state = 0; // see HID1_11.pdf appendix B section 1
int blink_count = 0; // keep track of how many times caps lock have toggled
usbMsgLen_t usbFunctionWrite(uint8_t * data, uchar len)
{
if (data[1] != LED_state)
{
// increment count when LED has toggled
LED_state = data[1];
if (bit_is_set(LED_state, 1))
{
sbi(PORTB, YELLOW_LED);
blink_count++;
}
else
{
cbi(PORTB, YELLOW_LED);
}
}
return 1; // return 1 if we have all data
}
usbMsgLen_t usbFunctionRead(uint8_t * data, uchar len)
{
return 1; // return 1 if we have all data
}
void usbEventResetReady(void)
{
calibrateOscillator();
eeprom_write_byte(0, OSCCAL); /* store the calibrated value in EEPROM */
}
// this function is used to guarantee that the data is sent to the computer once
void usbSendHidReport(uchar * data, uchar len)
{
while(1)
{
usbPoll();
if (usbInterruptIsReady())
{
usbSetInterrupt(data, len);
break;
}
}
}
//----------------------------------------------------//
//----------------------------------------------------//
//---------------------ADC----------------------------//
//----------------------------------------------------//
static uchar adcPending;
static uint8_t adcmodepool;
static void adcPoll()
{
if (adcmodepool==1){
if(adcPending && !(ADCSRA & (1 << ADSC))){
adcPending = 0;
printf ("ADC = %d \n",ADC);
}
}
}
//----------------------------------------------------//
//----------------------------------------------------//
//-------------Keyboad---------------------------------//
//----------------------------------------------------//
//----------------------------------------------------//
void send_report_once()
{
usbSendHidReport(&keyboard_report, sizeof(keyboard_report));
}
static void addDigit(uchar key,uchar mod)
{
keyboard_report.report_id=1;
keyboard_report.keycode[0] = key;
keyboard_report.modifier = mod;
send_report_once();
keyboard_report_reset(); // release keys
send_report_once();
}
static void alt_input(int code){
uchar digit;
do{
digit = code % 10;
code /= 10;
if(digit == 0){
keyboard_report.keycode[0] = 98;
keyboard_report.modifier = (1<<6);
}else{
keyboard_report.keycode[0] = 88 + digit;
keyboard_report.modifier = (1<<6);
}
send_report_once();
}while(code != 0);
keyboard_report_reset(); // release keys
send_report_once();
}
// translates ASCII to appropriate keyboard report, taking into consideration the status of caps lock
void ASCII_to_keycode(uint8_t ascii)
{
keyboard_report.keycode[0] = 0x00;
keyboard_report.modifier = 0x00;
// see scancode.doc appendix C
if (ascii >= 'A' && ascii <= 'Z')
{
keyboard_report.keycode[0] = 4 + ascii - 'A'; // set letter
if (bit_is_set(LED_state, 1)) // if caps is on
{
keyboard_report.modifier = 0x00; // no shift
}
else
{
keyboard_report.modifier = _BV(1); // hold shift // hold shift
}
}
else if (ascii >= 'a' && ascii <= 'z')
{
keyboard_report.keycode[0] = 4 + ascii - 'a'; // set letter
if (bit_is_set(LED_state, 1)) // if caps is on
{
keyboard_report.modifier = _BV(1); // hold shift // hold shift
}
else
{
keyboard_report.modifier = 0x00; // no shift
}
}
else if (ascii >= '0' && ascii <= '9')
{
keyboard_report.modifier = 0x00;
if (ascii == '0')
{
keyboard_report.keycode[0] = 0x27;
}
else
{
keyboard_report.keycode[0] = 30 + ascii - '1';
}
}
else
{
switch (ascii) // convert ascii to keycode according to documentation
{
case '!':
keyboard_report.modifier = _BV(1); // hold shift
keyboard_report.keycode[0] = 29 + 1;
break;
case '@':
keyboard_report.modifier = _BV(1); // hold shift
keyboard_report.keycode[0] = 29 + 2;
break;
case '#':
keyboard_report.modifier = _BV(1); // hold shift
keyboard_report.keycode[0] = 29 + 3;
break;
case '$':
keyboard_report.modifier = _BV(1); // hold shift
keyboard_report.keycode[0] = 29 + 4;
break;
case '%':
keyboard_report.modifier = _BV(1); // hold shift
keyboard_report.keycode[0] = 29 + 5;
break;
case '^':
keyboard_report.modifier = _BV(1); // hold shift
keyboard_report.keycode[0] = 29 + 6;
break;
case '&':
keyboard_report.modifier = _BV(1); // hold shift
keyboard_report.keycode[0] = 29 + 7;
break;
case '*':
keyboard_report.modifier = _BV(1); // hold shift
keyboard_report.keycode[0] = 29 + 8;
break;
case '(':
keyboard_report.modifier = _BV(1); // hold shift
keyboard_report.keycode[0] = 29 + 9;
break;
case ')':
keyboard_report.modifier = _BV(1); // hold shift
keyboard_report.keycode[0] = 0x27;
break;
case '~':
keyboard_report.modifier = _BV(1); // hold shift
// fall through
case '`':
keyboard_report.keycode[0] = 0x35;
break;
case '_':
keyboard_report.modifier = _BV(1); // hold shift
// fall through
case '-':
keyboard_report.keycode[0] = 0x2D;
break;
case '+':
keyboard_report.modifier = _BV(1); // hold shift
// fall through
case '=':
keyboard_report.keycode[0] = 0x2E;
break;
case '{':
keyboard_report.modifier = _BV(1); // hold shift
// fall through
case '[':
keyboard_report.keycode[0] = 0x2F;
break;
case '}':
keyboard_report.modifier = _BV(1); // hold shift
// fall through
case ']':
keyboard_report.keycode[0] = 0x30;
break;
case '|':
keyboard_report.modifier = _BV(1); // hold shift
// fall through
case '\\':
keyboard_report.keycode[0] = 0x31;
break;
case ':':
keyboard_report.modifier = _BV(1); // hold shift
// fall through
case ';':
keyboard_report.keycode[0] = 0x33;
break;
case '"':
keyboard_report.modifier = _BV(1); // hold shift
// fall through
case '\'':
keyboard_report.keycode[0] = 0x34;
break;
case '<':
keyboard_report.modifier = _BV(1); // hold shift
// fall through
case ',':
keyboard_report.keycode[0] = 0x36;
break;
case '>':
keyboard_report.modifier = _BV(1); // hold shift
// fall through
case '.':
keyboard_report.keycode[0] = 0x37;
break;
case '?':
keyboard_report.modifier = _BV(1); // hold shift
// fall through
case '/':
keyboard_report.keycode[0] = 0x38;
break;
case ' ':
keyboard_report.keycode[0] = 0x2C;
break;
case '\t':
keyboard_report.keycode[0] = 0x2B;
break;
case '\n':
keyboard_report.keycode[0] = 0x28;
break;
default:
alt_input(ascii);
return;
}
}
send_report_once();
keyboard_report_reset(); // release keys
send_report_once();
}
//----------------------------------------------------//
//----------------------------------------------------//
//simple bitbang input
//time is started at 0 ms and counts to 11 is counting at 60hz
//~700ms per message gives is 2 bitpersec not fast but we are seding key and mouse data...
//+-------------------------------------------+----------+
//| 0-50ms | 51-100ms | 0-133ms | 134-166ms | 183ms |
//+-------------------------------------------+----------|
//| 0 | 1 | start | end | overflow |
//+-------------------------------------------+----------|
//bitbag_mode = ...
// stop_con disables bitbag
// enabled_con enable bitbang
// start_con in a bitbag message
// byte_flag recived a bitbag message
//bitbagPoll(); pool for bitbang needs to be called atleas eaery 1ms
//bitbag_data the byte recived form bitbag message
#define byte_flag 2
#define enabled_con 1
static uchar bitbag_data;
static uchar bitbag_mode;
static void bitbagPoll(void){
static int8_t bitnum;
static uchar nextclock;
static uchar strtbyte;
static uchar inbyte;
if(TIFR & (1 << TOV1)){ //This flag is triggered at 60 hz.
TIFR = (1 << TOV1); /* clear overflow */
if(clock && nextclock == 1) // if the clockbit is pulled down we clock in.
{
nextclock = 0;
strtbyte = clockbit;
if (inbyte == 1)
{
if(!(PINB & (1<<PINB3)))
{
if(++bitnum <= 7)
{ //get ready for bit
cbi(bitbag_data,bitnum); //clear the curent bit
//printf("0");
}else{
bitnum = -1;
inbyte = 0;
bitbag_mode = byte_flag;
//printf("\n");
}
}
else
{
if(++bitnum <= 7){ //get ready for bit
sbi(bitbag_data,bitnum); //set the curent bit
//printf("1");
}else{
bitnum = -1;
inbyte = 0;
bitbag_mode = byte_flag;
//printf("\n");
}
}
}
}
if (clock && nextclock == 0){ //toggle of the clockbit line wile the clock is pulled low.
if (clockbit != strtbyte){
inbyte =1;
}
}
if(!(clock) && nextclock == 0){
nextclock=1; //get ready for next message
}
}
}
static void adc_timer_Poll(void)
{
static uchar timerCnt;
if(TIFR & (1 << TOV1)){ //This flag is triggered at 60 hz.
TIFR = (1 << TOV1); /* clear overflow */
if(++timerCnt >= 31){ /* ~ 0.5 second interval */
timerCnt = 0;
adcPending = 1;
ADCSRA |= (1 << ADSC); /* start next conversion */
}
}
}
//---------------------Mouse--------------------------//
//----------------------------------------------------//
void mouse_report_once()
{
usbPoll();
usbSendHidReport(&mouse_report, sizeof(mouse_report));
}
void mouse_move(int8_t x,int8_t y,uint8_t speed ){
int i;
if (x>0){
for(i=0;i<x;i++){ /* 300 ms disconnect */
mouse_report.x=speed;
mouse_report_once();
}
}
else
{
for(i=x;i<0;i++){ /* 300 ms disconnect */
mouse_report.x=(-1*speed);
mouse_report_once();
}
}
if (y>0){
for(i=0;i<y;i++){ /* 300 ms disconnect */
mouse_report.y=speed;
mouse_report_once();
}
}
else
{
for(i=y;i<0;i++){ /* 300 ms disconnect */
mouse_report.y=(-1*speed);
mouse_report_once();
}
}
mouse_report_reset();
mouse_report_once();
}
//----------------------------------------------------//
//----------------------------------------------------//
int clockstate;
void inputPoll()
{
if (clockstate != clock)
{
clockstate = clock;
if (clock){
sbi(PORTB, YELLOW_LED);
blink_count++;
}
else
{
cbi(PORTB, YELLOW_LED);
}
}
}
//----------------------------------------------------//
//----------------------------------------------------//
int poolcout;
int bootrun = 1;
static void Poll(void)
{
sbi(PORTB, WHITE_LED);
if (bootrun){
switch (poolcout++)
{
case 2:
addDigit(0,0);
//addDigit(21,MOD_GUI_RIGHT);
addDigit(0,0);
break;
case 3:
//puts_P(PSTR("notepad.exe"));
break;
case 4:
puts_P(PSTR("+--------------------+"));
puts_P(PSTR("| USB Testing Device |"));
puts_P(PSTR("|--------------------|"));
puts_P(PSTR("| (1) USB Keyboad |"));
puts_P(PSTR("|--------------------|"));
puts_P(PSTR("| (2) USB Mouse |"));
puts_P(PSTR("+--------------------+"));
puts_P(PSTR("| Now With a Brain!! |"));
puts_P(PSTR("+--------------------+"));
break;
case 5:
puts_P(PSTR("Mouse move x=50 y=0 speed 10"));
mouse_move(50,0,10);
break;
case 6:
puts_P(PSTR("Mouse move x=-50 y=20 speed 5"));
mouse_move(-50,20,5);
break;
case 7:
//addDigit(76,20); //ctrl+alt+delt
puts_P(PSTR("ADC Meater Mode on"));
adcmodepool=1;
break;
case 8:
puts_P(PSTR("ADC Meater Mode off"));
adcmodepool=0;
ADCSRA = 0; //turn off ADC
break;
case 9:
puts_P(PSTR("BitBang Mode On"));
bitbag_mode = enabled_con; //enable bitbang
break;
case 11:
//bitbag_mode = stop_con; //disables bitbag
//puts_P(PSTR("BitBang Mode Off"));
//puts_P(PSTR("Demo over\n ADC Meatering ACTIVE..."));
//poolcout=0;
//bootrun=0;
break;
}
}
else
{
switch (poolcout++)
{
case 0:
//addDigit(76,20); //ctrl+alt+delt
adcmodepool =1;
break;
case 1:
adcmodepool=0;
poolcout=0;
break;
}
}
cbi(PORTB, WHITE_LED);
}
// stdio's stream will use this funct to type out characters in a string
void type_out_char(uint8_t ascii, FILE *stream)
{
ASCII_to_keycode(ascii);
}
static FILE mystdout = FDEV_SETUP_STREAM(type_out_char, NULL, _FDEV_SETUP_WRITE); // setup writing stream
int main()
{
int i;
wdt_disable(); // no watchdog, just because I'm lazy
stdout = &mystdout; // set default stream
DDRB |= (1 << WHITE_LED) | (0 << 3)| (0 << 4); //0 = input, 1 = output,WHITE_LED is output PB3 is input, PB4 is input
PORTB |= 1<<DDB3 | 1<<DDB4;
ADCSRA = UTIL_BIN8(1000, 0111); // enable ADC, not free running, interrupt disable, rate = 1/128
//+-----------------------------------------------+
ADMUX = UTIL_BIN8(1000, 0011); //Internal 1.1V Voltage Reference, Right adjust the result, Single Ended Input ADC3 (PB3)
//+-----------------------------------------------+
TCCR1 = UTIL_BIN8(0000, 1011); // select clock: 16.5M/1k -> overflow rate = 16.5M/256k = 62.94 Hz
sbi(PORTB, WHITE_LED);
for(i=0;i<20;i++){ /* 300 ms disconnect */
_delay_ms(15);
}
cbi(PORTB, WHITE_LED);
sbi(PORTB, YELLOW_LED);
for(i=0;i<20;i++){ /* 300 ms disconnect */
_delay_ms(15);
}
cbi(PORTB, YELLOW_LED);
usbDeviceDisconnect(); // enforce USB re-enumeration, do this while interrupts are disabled!
_delay_ms(250);
usbDeviceConnect();
bitbag_mode = 0; // disable bitbang
usbInit(); // start v-usb
sei(); // enable interrupts
for(;;){
// set the report IDs manually
keyboard_report.report_id = 1;
mouse_report.report_id = 2;
if(blink_count == 1){
blink_count = 0;
Poll();
}
inputPoll();
usbPoll();
if (bitbag_mode == enabled_con){
bitbagPoll();
if (bitbag_mode == byte_flag){
ASCII_to_keycode(bitbag_data);
bitbag_mode=enabled_con;
}
}
else
{
adc_timer_Poll();
}
usbPoll();
adcPoll();
}
return 0;
}