]> git.piffa.net Git - arduino/blob - books/ArduinoNextSteps-master/ArduinoNextSteps/sketch_11_04_host_scroll_lock/sketch_11_04_host_scroll_lock.ino
first commit
[arduino] / books / ArduinoNextSteps-master / ArduinoNextSteps / sketch_11_04_host_scroll_lock / sketch_11_04_host_scroll_lock.ino
1 // sketch_11_04_host_scroll_lock
2
3 // This sketch is based on the LCDlbd example sketch from the USB_Host_Shield library
4
5 #include <Spi.h>
6 #include <Max3421e.h>
7 #include <Usb.h>
8
9
10 /* keyboard data taken from configuration descriptor */
11 #define KBD_ADDR        1
12 #define KBD_EP          1
13 #define KBD_IF          0
14 #define EP_MAXPKTSIZE   8
15 #define EP_POLL         0x0a
16
17 /**/
18 EP_RECORD ep_record[ 2 ];  //endpoint record structure for the keyboard
19
20 char buf[ 8 ] = { 0 };      //keyboard buffer
21 char old_buf[ 8 ] = { 0 };  //last poll
22
23 char leds = 0;
24
25
26 MAX3421E Max;
27 USB Usb;
28
29 void setup() {
30   // set up the LCD's number of rows and columns: 
31   Serial.begin( 9600 );
32   Serial.println("Start");
33   Max.powerOn();
34   delay( 200 );
35 }
36
37 void loop() {
38     Max.Task();
39     Usb.Task();
40     if( Usb.getUsbTaskState() == USB_STATE_CONFIGURING ) {  //wait for addressing state
41         kbd_init();
42         Usb.setUsbTaskState( USB_STATE_RUNNING );
43     }
44     if( Usb.getUsbTaskState() == USB_STATE_RUNNING ) {  //poll the keyboard  
45         toggleLEDs();
46         delay(500);
47     }
48 }
49 /* Initialize keyboard */
50 void kbd_init( void )
51 {
52   byte rcode = 0;  //return code
53   ep_record[ 0 ] = *( Usb.getDevTableEntry( 0,0 ));  //copy endpoint 0 parameters
54   ep_record[ 1 ].MaxPktSize = EP_MAXPKTSIZE;
55   ep_record[ 1 ].Interval  = EP_POLL;
56   ep_record[ 1 ].sndToggle = bmSNDTOG0;
57   ep_record[ 1 ].rcvToggle = bmRCVTOG0;
58   Usb.setDevTableEntry( 1, ep_record );              //plug kbd.endpoint parameters to devtable
59   /* Configure device */
60   rcode = Usb.setConf( KBD_ADDR, 0, 1 );                    
61   if( rcode ) {
62       Serial.print("Error attempting to configure keyboard. Return code :");
63       Serial.println( rcode, HEX );
64       while(1);  //stop
65   }
66   /* Set boot protocol */
67   rcode = Usb.setProto( KBD_ADDR, 0, 0, 0 );
68   if( rcode ) {
69       Serial.print("Error attempting to configure boot protocol. Return code :");
70       Serial.println( rcode, HEX );
71       while( 1 );  //stop
72   }
73   Serial.println("Keyboard initialized");
74   delay(2000);
75 }
76
77
78 void toggleLEDs( void )
79 {
80   if (leds == 0) {
81     leds = 0b00000111;
82   }
83   else {
84     leds = 0;
85   }
86   Usb.setReport( KBD_ADDR, 0, 1, KBD_IF, 0x02, 0, &leds );
87 }
88
89
90