|
| 1 | +#include <wixel.h> |
| 2 | +#include <usb.h> |
| 3 | +#include <usb_hid.h> |
| 4 | +#include "data.h" |
| 5 | + |
| 6 | +uint8 lastKeyCodeSent = 0; |
| 7 | +BIT sending = 0; |
| 8 | +char XDATA * nextCharToSend; |
| 9 | +BIT started = 0; |
| 10 | + |
| 11 | +void updateLeds() |
| 12 | +{ |
| 13 | + usbShowStatusWithGreenLed(); |
| 14 | + |
| 15 | + LED_YELLOW(!sending); |
| 16 | + LED_RED(sending); |
| 17 | +} |
| 18 | + |
| 19 | +uint8 usbHidModifiersFromAsciiChar(char c) |
| 20 | +{ |
| 21 | + if ( (c >= 0x21 && c <= 0x26) || |
| 22 | + (c >= 0x28 && c <= 0x2B) || |
| 23 | + (c == ':') || |
| 24 | + (c == '<') || |
| 25 | + (c >= 0x3E && c <= 0x5A) || |
| 26 | + (c >= 0x5E && c <= 0x5F) || |
| 27 | + (c == '|') || |
| 28 | + (c == '~') |
| 29 | + ) |
| 30 | + { |
| 31 | + return (1<<MODIFIER_SHIFT_LEFT); |
| 32 | + } |
| 33 | + else |
| 34 | + { |
| 35 | + return 0; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// See keyboardService for an example of how to use this function correctly. |
| 40 | +// Assumption: usbHidKeyboardInputUpdated == 0. Otherwise, this function |
| 41 | +// could clobber a keycode sitting in the buffer that has not been sent to |
| 42 | +// the computer yet. |
| 43 | +// Assumption: usbHidKeyBoardInput[1 through 5] are all zero. |
| 44 | +// Assumption: usbHidKeyboardInput.modifiers is 0. |
| 45 | +// NOTE: To send two identical characters, you must send a 0 in between. |
| 46 | +void sendKeyCode(uint8 keyCode, uint8 modifiers) |
| 47 | +{ |
| 48 | + lastKeyCodeSent = keyCode; |
| 49 | + usbHidKeyboardInput.keyCodes[0] = keyCode; |
| 50 | + usbHidKeyboardInput.modifiers = modifiers; |
| 51 | + |
| 52 | + // Tell the HID library to send the new keyboard state to the computer. |
| 53 | + usbHidKeyboardInputUpdated = 1; |
| 54 | +} |
| 55 | + |
| 56 | +void keyboardService() |
| 57 | +{ |
| 58 | + if (getMs() >= 5000 && !started && !sending) |
| 59 | + { |
| 60 | + started = 1; |
| 61 | + sending = 1; |
| 62 | + nextCharToSend = (char XDATA *)payload_data; |
| 63 | + } |
| 64 | + |
| 65 | + // Feed data to the HID library, one character at a time. |
| 66 | + if (sending && !usbHidKeyboardInputUpdated) |
| 67 | + { |
| 68 | + uint8 nextChar = *nextCharToSend; |
| 69 | + if (nextChar == 0) |
| 70 | + { |
| 71 | + sending = 0; |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + uint8 keyCode = usbHidKeyCodeFromAsciiChar(*nextCharToSend); |
| 76 | + uint8 nextChar = *nextCharToSend; |
| 77 | + |
| 78 | + if (keyCode != 0 && keyCode == lastKeyCodeSent) |
| 79 | + { |
| 80 | + // If we need to send the same character twice in a row, |
| 81 | + // send a 0 between them so the computer registers it as |
| 82 | + // two different separate key strokes. |
| 83 | + keyCode = 0; |
| 84 | + } |
| 85 | + else |
| 86 | + { |
| 87 | + nextCharToSend++; |
| 88 | + } |
| 89 | + |
| 90 | + sendKeyCode(keyCode, usbHidModifiersFromAsciiChar(nextChar)); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // Send a 0 to signal the release of the last key. |
| 95 | + if (!sending && lastKeyCodeSent != 0 && !usbHidKeyboardInputUpdated) |
| 96 | + { |
| 97 | + sendKeyCode(0, 0); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +void main() |
| 102 | +{ |
| 103 | + systemInit(); |
| 104 | + usbInit(); |
| 105 | + |
| 106 | + while(1) |
| 107 | + { |
| 108 | + updateLeds(); |
| 109 | + boardService(); |
| 110 | + usbHidService(); |
| 111 | + keyboardService(); |
| 112 | + } |
| 113 | +} |
0 commit comments