diff --git a/examples/HID/USBHIDBootKbd/USBHIDBootKbd.ino b/examples/HID/USBHIDBootKbd/USBHIDBootKbd.ino new file mode 100644 index 00000000..71e7430a --- /dev/null +++ b/examples/HID/USBHIDBootKbd/USBHIDBootKbd.ino @@ -0,0 +1,96 @@ +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +class KbdRptParser : public KeyboardReportParser +{ + void PrintKey(uint8_t mod, uint8_t key); + +protected: + virtual void OnKeyDown (uint8_t mod, uint8_t key); + virtual void OnKeyUp (uint8_t mod, uint8_t key); + virtual void OnKeyPressed(uint8_t key); +}; + +void KbdRptParser::PrintKey(uint8_t m, uint8_t key) +{ + MODIFIERKEYS mod; + *((uint8_t*)&mod) = m; + Serial.print((mod.bmLeftCtrl == 1) ? "C" : " "); + Serial.print((mod.bmLeftShift == 1) ? "S" : " "); + Serial.print((mod.bmLeftAlt == 1) ? "A" : " "); + Serial.print((mod.bmLeftGUI == 1) ? "G" : " "); + + Serial.print(" >"); + PrintHex(key, 0x80); + Serial.print("< "); + + Serial.print((mod.bmRightCtrl == 1) ? "C" : " "); + Serial.print((mod.bmRightShift == 1) ? "S" : " "); + Serial.print((mod.bmRightAlt == 1) ? "A" : " "); + Serial.println((mod.bmRightGUI == 1) ? "G" : " "); +}; + +void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key) +{ + Serial.print("DN "); + PrintKey(mod, key); + uint8_t c = OemToAscii(mod, key); + + if (c) + OnKeyPressed(c); +} + +void KbdRptParser::OnKeyUp(uint8_t mod, uint8_t key) +{ + Serial.print("UP "); + PrintKey(mod, key); +} + +void KbdRptParser::OnKeyPressed(uint8_t key) +{ + Serial.print("ASCII: "); + Serial.println((char)key); +}; + +USB Usb; +//USBHub Hub(&Usb); +HIDBoot Keyboard(&Usb); + +uint32_t next_time; + +KbdRptParser Prs; + +void setup() +{ + Serial.begin( 115200 ); + Serial.println("Start"); + + if (Usb.Init() == -1) + Serial.println("OSC did not start."); + + delay( 200 ); + + next_time = millis() + 5000; + + Keyboard.SetReportParser(0, (HIDReportParser*)&Prs); +} + +void loop() +{ + Usb.Task(); +} +