]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDBootKbd/USBHIDBootKbd.ino
Merge commit 'f6d56675f9f981c5464f0ca7a1fbb0162154e8c5'
[qmk_firmware.git] / tmk_core / protocol / usb_hid / USB_Host_Shield_2.0 / examples / HID / USBHIDBootKbd / USBHIDBootKbd.ino
1 #include <hidboot.h>
2 #include <usbhub.h>
3
4 // Satisfy the IDE, which needs to see the include statment in the ino too.
5 #ifdef dobogusinclude
6 #include <spi4teensy3.h>
7 #include <SPI.h>
8 #endif
9
10 class KbdRptParser : public KeyboardReportParser
11 {
12     void PrintKey(uint8_t mod, uint8_t key);
13
14   protected:
15     void OnControlKeysChanged(uint8_t before, uint8_t after);
16
17     void OnKeyDown      (uint8_t mod, uint8_t key);
18     void OnKeyUp        (uint8_t mod, uint8_t key);
19     void OnKeyPressed(uint8_t key);
20 };
21
22 void KbdRptParser::PrintKey(uint8_t m, uint8_t key)
23 {
24   MODIFIERKEYS mod;
25   *((uint8_t*)&mod) = m;
26   Serial.print((mod.bmLeftCtrl   == 1) ? "C" : " ");
27   Serial.print((mod.bmLeftShift  == 1) ? "S" : " ");
28   Serial.print((mod.bmLeftAlt    == 1) ? "A" : " ");
29   Serial.print((mod.bmLeftGUI    == 1) ? "G" : " ");
30
31   Serial.print(" >");
32   PrintHex<uint8_t>(key, 0x80);
33   Serial.print("< ");
34
35   Serial.print((mod.bmRightCtrl   == 1) ? "C" : " ");
36   Serial.print((mod.bmRightShift  == 1) ? "S" : " ");
37   Serial.print((mod.bmRightAlt    == 1) ? "A" : " ");
38   Serial.println((mod.bmRightGUI    == 1) ? "G" : " ");
39 };
40
41 void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
42 {
43   Serial.print("DN ");
44   PrintKey(mod, key);
45   uint8_t c = OemToAscii(mod, key);
46
47   if (c)
48     OnKeyPressed(c);
49 }
50
51 void KbdRptParser::OnControlKeysChanged(uint8_t before, uint8_t after) {
52
53   MODIFIERKEYS beforeMod;
54   *((uint8_t*)&beforeMod) = before;
55
56   MODIFIERKEYS afterMod;
57   *((uint8_t*)&afterMod) = after;
58
59   if (beforeMod.bmLeftCtrl != afterMod.bmLeftCtrl) {
60     Serial.println("LeftCtrl changed");
61   }
62   if (beforeMod.bmLeftShift != afterMod.bmLeftShift) {
63     Serial.println("LeftShift changed");
64   }
65   if (beforeMod.bmLeftAlt != afterMod.bmLeftAlt) {
66     Serial.println("LeftAlt changed");
67   }
68   if (beforeMod.bmLeftGUI != afterMod.bmLeftGUI) {
69     Serial.println("LeftGUI changed");
70   }
71
72   if (beforeMod.bmRightCtrl != afterMod.bmRightCtrl) {
73     Serial.println("RightCtrl changed");
74   }
75   if (beforeMod.bmRightShift != afterMod.bmRightShift) {
76     Serial.println("RightShift changed");
77   }
78   if (beforeMod.bmRightAlt != afterMod.bmRightAlt) {
79     Serial.println("RightAlt changed");
80   }
81   if (beforeMod.bmRightGUI != afterMod.bmRightGUI) {
82     Serial.println("RightGUI changed");
83   }
84
85 }
86
87 void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key)
88 {
89   Serial.print("UP ");
90   PrintKey(mod, key);
91 }
92
93 void KbdRptParser::OnKeyPressed(uint8_t key)
94 {
95   Serial.print("ASCII: ");
96   Serial.println((char)key);
97 };
98
99 USB     Usb;
100 //USBHub     Hub(&Usb);
101 HIDBoot<HID_PROTOCOL_KEYBOARD>    HidKeyboard(&Usb);
102
103 uint32_t next_time;
104
105 KbdRptParser Prs;
106
107 void setup()
108 {
109   Serial.begin( 115200 );
110 #if !defined(__MIPSEL__)
111   while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
112 #endif
113   Serial.println("Start");
114
115   if (Usb.Init() == -1)
116     Serial.println("OSC did not start.");
117
118   delay( 200 );
119
120   next_time = millis() + 5000;
121
122   HidKeyboard.SetReportParser(0, (HIDReportParser*)&Prs);
123 }
124
125 void loop()
126 {
127   Usb.Task();
128 }
129