]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/usb_hid/USB_Host_Shield_2.0/examples/HID/USBHIDBootKbdAndMouse/USBHIDBootKbdAndMouse.ino
lufa: usb-usb: Use LUFA startup instead of cusotom
[max/tmk_keyboard.git] / tmk_core / protocol / usb_hid / USB_Host_Shield_2.0 / examples / HID / USBHIDBootKbdAndMouse / USBHIDBootKbdAndMouse.ino
1 #include <hidboot.h>
2 #include <usbhub.h>
3
4 // Satisfy IDE, which only needs to see the include statment in the ino.
5 #ifdef dobogusinclude
6 #include <spi4teensy3.h>
7 #include <SPI.h>
8 #endif
9
10 class MouseRptParser : public MouseReportParser
11 {
12   protected:
13     void OnMouseMove(MOUSEINFO *mi);
14     void OnLeftButtonUp(MOUSEINFO *mi);
15     void OnLeftButtonDown(MOUSEINFO *mi);
16     void OnRightButtonUp(MOUSEINFO *mi);
17     void OnRightButtonDown(MOUSEINFO *mi);
18     void OnMiddleButtonUp(MOUSEINFO *mi);
19     void OnMiddleButtonDown(MOUSEINFO *mi);
20 };
21 void MouseRptParser::OnMouseMove(MOUSEINFO *mi)
22 {
23   Serial.print("dx=");
24   Serial.print(mi->dX, DEC);
25   Serial.print(" dy=");
26   Serial.println(mi->dY, DEC);
27 };
28 void MouseRptParser::OnLeftButtonUp     (MOUSEINFO *mi)
29 {
30   Serial.println("L Butt Up");
31 };
32 void MouseRptParser::OnLeftButtonDown   (MOUSEINFO *mi)
33 {
34   Serial.println("L Butt Dn");
35 };
36 void MouseRptParser::OnRightButtonUp    (MOUSEINFO *mi)
37 {
38   Serial.println("R Butt Up");
39 };
40 void MouseRptParser::OnRightButtonDown  (MOUSEINFO *mi)
41 {
42   Serial.println("R Butt Dn");
43 };
44 void MouseRptParser::OnMiddleButtonUp   (MOUSEINFO *mi)
45 {
46   Serial.println("M Butt Up");
47 };
48 void MouseRptParser::OnMiddleButtonDown (MOUSEINFO *mi)
49 {
50   Serial.println("M Butt Dn");
51 };
52
53 class KbdRptParser : public KeyboardReportParser
54 {
55     void PrintKey(uint8_t mod, uint8_t key);
56
57   protected:
58     void OnControlKeysChanged(uint8_t before, uint8_t after);
59     void OnKeyDown      (uint8_t mod, uint8_t key);
60     void OnKeyUp        (uint8_t mod, uint8_t key);
61     void OnKeyPressed(uint8_t key);
62 };
63
64 void KbdRptParser::PrintKey(uint8_t m, uint8_t key)
65 {
66   MODIFIERKEYS mod;
67   *((uint8_t*)&mod) = m;
68   Serial.print((mod.bmLeftCtrl   == 1) ? "C" : " ");
69   Serial.print((mod.bmLeftShift  == 1) ? "S" : " ");
70   Serial.print((mod.bmLeftAlt    == 1) ? "A" : " ");
71   Serial.print((mod.bmLeftGUI    == 1) ? "G" : " ");
72
73   Serial.print(" >");
74   PrintHex<uint8_t>(key, 0x80);
75   Serial.print("< ");
76
77   Serial.print((mod.bmRightCtrl   == 1) ? "C" : " ");
78   Serial.print((mod.bmRightShift  == 1) ? "S" : " ");
79   Serial.print((mod.bmRightAlt    == 1) ? "A" : " ");
80   Serial.println((mod.bmRightGUI    == 1) ? "G" : " ");
81 };
82
83 void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key)
84 {
85   Serial.print("DN ");
86   PrintKey(mod, key);
87   uint8_t c = OemToAscii(mod, key);
88
89   if (c)
90     OnKeyPressed(c);
91 }
92
93 void KbdRptParser::OnControlKeysChanged(uint8_t before, uint8_t after) {
94
95   MODIFIERKEYS beforeMod;
96   *((uint8_t*)&beforeMod) = before;
97
98   MODIFIERKEYS afterMod;
99   *((uint8_t*)&afterMod) = after;
100
101   if (beforeMod.bmLeftCtrl != afterMod.bmLeftCtrl) {
102     Serial.println("LeftCtrl changed");
103   }
104   if (beforeMod.bmLeftShift != afterMod.bmLeftShift) {
105     Serial.println("LeftShift changed");
106   }
107   if (beforeMod.bmLeftAlt != afterMod.bmLeftAlt) {
108     Serial.println("LeftAlt changed");
109   }
110   if (beforeMod.bmLeftGUI != afterMod.bmLeftGUI) {
111     Serial.println("LeftGUI changed");
112   }
113
114   if (beforeMod.bmRightCtrl != afterMod.bmRightCtrl) {
115     Serial.println("RightCtrl changed");
116   }
117   if (beforeMod.bmRightShift != afterMod.bmRightShift) {
118     Serial.println("RightShift changed");
119   }
120   if (beforeMod.bmRightAlt != afterMod.bmRightAlt) {
121     Serial.println("RightAlt changed");
122   }
123   if (beforeMod.bmRightGUI != afterMod.bmRightGUI) {
124     Serial.println("RightGUI changed");
125   }
126
127 }
128
129 void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key)
130 {
131   Serial.print("UP ");
132   PrintKey(mod, key);
133 }
134
135 void KbdRptParser::OnKeyPressed(uint8_t key)
136 {
137   Serial.print("ASCII: ");
138   Serial.println((char)key);
139 };
140
141 USB     Usb;
142 USBHub     Hub(&Usb);
143
144 HIDBoot < HID_PROTOCOL_KEYBOARD | HID_PROTOCOL_MOUSE > HidComposite(&Usb);
145 HIDBoot<HID_PROTOCOL_KEYBOARD>    HidKeyboard(&Usb);
146 HIDBoot<HID_PROTOCOL_MOUSE>    HidMouse(&Usb);
147
148 //uint32_t next_time;
149
150 KbdRptParser KbdPrs;
151 MouseRptParser MousePrs;
152
153 void setup()
154 {
155   Serial.begin( 115200 );
156 #if !defined(__MIPSEL__)
157   while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
158 #endif
159   Serial.println("Start");
160
161   if (Usb.Init() == -1)
162     Serial.println("OSC did not start.");
163
164   delay( 200 );
165
166   //next_time = millis() + 5000;
167
168   HidComposite.SetReportParser(0, (HIDReportParser*)&KbdPrs);
169   HidComposite.SetReportParser(1, (HIDReportParser*)&MousePrs);
170   HidKeyboard.SetReportParser(0, (HIDReportParser*)&KbdPrs);
171   HidMouse.SetReportParser(0, (HIDReportParser*)&MousePrs);
172 }
173
174 void loop()
175 {
176   Usb.Task();
177 }
178