diff --git a/Demos/Keyboard/Keyboard.c b/Demos/Keyboard/Keyboard.c index 92f4ae9..71bf588 100644 --- a/Demos/Keyboard/Keyboard.c +++ b/Demos/Keyboard/Keyboard.c @@ -35,6 +35,7 @@ * is responsible for the initial application hardware configuration. */ +#include #include "Keyboard.h" /* Project Tags, for reading out using the ButtLoad project */ @@ -289,6 +290,45 @@ ISR(TIMER0_COMPA_vect, ISR_BLOCK) IdleMSRemaining--; } +/* Translates ASCII characters in input argument 'c' into equivalent key presses in output argument + * 'USB_KeyboardReport_Data_t *r'. + * Keycodes discovered by observing physical USB keyboard with 'usbmon': + * http://www.mjmwired.net/kernel/Documentation/usb/usbmon.txt + * */ +static void AsciiToKeyCode(char c, USB_KeyboardReport_Data_t *r) +{ + enum { + MODIFIER_LEFT_SHIFT = 0x02, + }; + memset(r, 0, sizeof(USB_KeyboardReport_Data_t)); + if (isupper(c)) { + r->Modifier = MODIFIER_LEFT_SHIFT; + r->KeyCode[0] = 0x04 + c - 'A'; + } else if (islower(c)) { + r->KeyCode[0] = 0x04 + c - 'a'; + } else if (c == '0') { + r->KeyCode[0] = 0x27; + } else if ('1' <= c && c <= '9') { + r->KeyCode[0] = 0x1e + c - '1'; + } else if (c == '!') { + r->Modifier = MODIFIER_LEFT_SHIFT; + r->KeyCode[0] = 0x1e; + } else if (c == '?') { + r->Modifier = MODIFIER_LEFT_SHIFT; + r->KeyCode[0] = 0x38; + } else if (c == '\n') { + r->KeyCode[0] = 0x28; + } else if (c == ' ') { + r->KeyCode[0] = 0x2c; + } else if (c == '.') { + r->KeyCode[0] = 0x36; + } else if (c == ',') { + r->KeyCode[0] = 0x37; + } else { // replace unknown characters with 'x' + r->KeyCode[0] = 0x04 + 'x' - 'a'; + } +} + /** Fills the given HID report data structure with the next HID report to send to the host. * * \param ReportData Pointer to a HID report data structure to be filled @@ -297,34 +337,26 @@ ISR(TIMER0_COMPA_vect, ISR_BLOCK) */ bool GetNextReport(USB_KeyboardReport_Data_t* ReportData) { - static uint8_t PrevJoyStatus = 0; - uint8_t JoyStatus_LCL = Joystick_GetStatus(); - bool InputChanged = false; - - /* Clear the report contents */ - memset(ReportData, 0, sizeof(USB_KeyboardReport_Data_t)); - - if (JoyStatus_LCL & JOY_UP) - ReportData->KeyCode[0] = 0x04; // A - else if (JoyStatus_LCL & JOY_DOWN) - ReportData->KeyCode[0] = 0x05; // B - - if (JoyStatus_LCL & JOY_LEFT) - ReportData->KeyCode[0] = 0x06; // C - else if (JoyStatus_LCL & JOY_RIGHT) - ReportData->KeyCode[0] = 0x07; // D - - if (JoyStatus_LCL & JOY_PRESS) - ReportData->KeyCode[0] = 0x08; // E - - /* Check if the new report is different to the previous report */ - InputChanged = (uint8_t)(PrevJoyStatus ^ JoyStatus_LCL); - - /* Save the current joystick status for later comparison */ - PrevJoyStatus = JoyStatus_LCL; - - /* Return whether the new report is different to the previous report or not */ - return InputChanged; + static bool gen_key_up; // generate a key-down and a key-up for each key + static uint16_t call_count; + static uint8_t msg_index; + const char msg[] = "Out of work and no play makes Scott a dull boy...\n"; + + call_count++; + if (call_count % 2048 != 0) + return false; + + if (gen_key_up) { + memset(ReportData, 0, sizeof(USB_KeyboardReport_Data_t)); + gen_key_up = false; + } else { + char c = msg[msg_index++]; + AsciiToKeyCode(c, ReportData); + if (msg[msg_index] == '\0') + msg_index = 0; + gen_key_up = true; + } + return true; } /** Processes a given LED report mask from the host and sets the board LEDs to match. diff --git a/Demos/Keyboard/makefile b/Demos/Keyboard/makefile index b2a3bee..400ba7b 100644 --- a/Demos/Keyboard/makefile +++ b/Demos/Keyboard/makefile @@ -87,7 +87,7 @@ BOARD = USBKEY # F_CPU = 16000000 # F_CPU = 18432000 # F_CPU = 20000000 -F_CPU = 8000000 +F_CPU = 16000000 # Output format. (can be srec, ihex, binary)