Commit | Line | Data |
---|---|---|
cd011f58 AW |
1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
2 | * | |
3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software | |
4 | * and associated documentation files (the "Software"), to deal in the Software without | |
5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, | |
6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the | |
7 | * Software is furnished to do so, subject to the following conditions: | |
8 | * | |
9 | * The above copyright notice and this permission notice shall be included in all copies or | |
10 | * substantial portions of the Software. | |
11 | * | |
12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING | |
13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | |
15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
17 | */ | |
18 | ||
19 | #ifndef USBKEYBOARD_H | |
20 | #define USBKEYBOARD_H | |
21 | ||
22 | #include "USBHID.h" | |
23 | #include "Stream.h" | |
24 | ||
25 | /* Modifiers */ | |
26 | enum MODIFIER_KEY | |
27 | { | |
28 | KEY_CTRL = 1, | |
29 | KEY_SHIFT = 2, | |
30 | KEY_ALT = 4, | |
31 | }; | |
32 | ||
33 | ||
34 | enum MEDIA_KEY | |
35 | { | |
36 | KEY_NEXT_TRACK, /*!< next Track Button */ | |
37 | KEY_PREVIOUS_TRACK, /*!< Previous track Button */ | |
38 | KEY_STOP, /*!< Stop Button */ | |
39 | KEY_PLAY_PAUSE, /*!< Play/Pause Button */ | |
40 | KEY_MUTE, /*!< Mute Button */ | |
41 | KEY_VOLUME_UP, /*!< Volume Up Button */ | |
42 | KEY_VOLUME_DOWN, /*!< Volume Down Button */ | |
43 | }; | |
44 | ||
45 | enum FUNCTION_KEY | |
46 | { | |
47 | KEY_F1 = 128, /* F1 key */ | |
48 | KEY_F2, /* F2 key */ | |
49 | KEY_F3, /* F3 key */ | |
50 | KEY_F4, /* F4 key */ | |
51 | KEY_F5, /* F5 key */ | |
52 | KEY_F6, /* F6 key */ | |
53 | KEY_F7, /* F7 key */ | |
54 | KEY_F8, /* F8 key */ | |
55 | KEY_F9, /* F9 key */ | |
56 | KEY_F10, /* F10 key */ | |
57 | KEY_F11, /* F11 key */ | |
58 | KEY_F12, /* F12 key */ | |
59 | KEY_PRINT_SCREEN, /* Print Screen key */ | |
60 | KEY_SCROLL_LOCK, /* Scroll lock */ | |
61 | KEY_CAPS_LOCK, /* caps lock */ | |
62 | KEY_NUM_LOCK, /* num lock */ | |
63 | KEY_INSERT, /* Insert key */ | |
64 | KEY_HOME, /* Home key */ | |
65 | KEY_PAGE_UP, /* Page Up key */ | |
66 | KEY_PAGE_DOWN, /* Page Down key */ | |
67 | }; | |
68 | ||
69 | /** | |
70 | * USBKeyboard example | |
71 | * @code | |
72 | * | |
73 | * #include "mbed.h" | |
74 | * #include "USBKeyboard.h" | |
75 | * | |
76 | * USBKeyboard key; | |
77 | * | |
78 | * int main(void) | |
79 | * { | |
80 | * while (1) | |
81 | * { | |
82 | * key.printf("Hello World\r\n"); | |
83 | * wait(1); | |
84 | * } | |
85 | * } | |
86 | * | |
87 | * @endcode | |
88 | */ | |
89 | class USBKeyboard: public USBHID, public Stream | |
90 | { | |
91 | public: | |
92 | ||
93 | /** | |
94 | * Constructor | |
95 | * | |
96 | * | |
97 | * @param leds Leds bus: first: NUM_LOCK, second: CAPS_LOCK, third: SCROLL_LOCK | |
98 | * @param vendor_id Your vendor_id (default: 0x1235) | |
99 | * @param product_id Your product_id (default: 0x0050) | |
100 | * @param product_release Your preoduct_release (default: 0x0001) | |
101 | * | |
102 | */ | |
103 | USBKeyboard(uint16_t vendor_id = 0x1235, uint16_t product_id = 0x0050, uint16_t product_release = 0x0001): | |
104 | USBHID(0, 0, vendor_id, product_id, product_release, false){ | |
105 | lock_status = 0; | |
106 | connect(); | |
107 | }; | |
108 | ||
109 | /** | |
110 | * To send a character defined by a modifier(CTRL, SHIFT, ALT) and the key | |
111 | * | |
112 | * @code | |
113 | * //To send CTRL + s (save) | |
114 | * keyboard.keyCode('s', KEY_CTRL); | |
115 | * @endcode | |
116 | * | |
117 | * @param modifier bit 0: KEY_CTRL, bit 1: KEY_SHIFT, bit 2: KEY_ALT (default: 0) | |
118 | * @param key character to send | |
119 | * @returns true if there is no error, false otherwise | |
120 | */ | |
121 | bool keyCode(uint8_t key, uint8_t modifier = 0); | |
122 | ||
123 | /** | |
124 | * Send a character | |
125 | * | |
126 | * @param c character to be sent | |
127 | * @returns true if there is no error, false otherwise | |
128 | */ | |
129 | virtual int _putc(int c); | |
130 | ||
131 | /** | |
132 | * Control media keys | |
133 | * | |
134 | * @param key media key pressed (KEY_NEXT_TRACK, KEY_PREVIOUS_TRACK, KEY_STOP, KEY_PLAY_PAUSE, KEY_MUTE, KEY_VOLUME_UP, KEY_VOLUME_DOWN) | |
135 | * @returns true if there is no error, false otherwise | |
136 | */ | |
137 | bool mediaControl(MEDIA_KEY key); | |
138 | ||
139 | /* | |
140 | * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength. | |
141 | * | |
142 | * @returns pointer to the report descriptor | |
143 | */ | |
144 | virtual uint8_t * reportDesc(); | |
145 | ||
146 | /* | |
147 | * Called when a data is received on the OUT endpoint. Useful to switch on LED of LOCK keys | |
148 | * | |
149 | * @returns if handle by subclass, return true | |
150 | */ | |
151 | virtual bool EP1_OUT_callback(); | |
152 | ||
153 | /** | |
154 | * Read status of lock keys. Useful to switch-on/off leds according to key pressed. Only the first three bits of the result is important: | |
155 | * - First bit: NUM_LOCK | |
156 | * - Second bit: CAPS_LOCK | |
157 | * - Third bit: SCROLL_LOCK | |
158 | * | |
159 | * @returns status of lock keys | |
160 | */ | |
161 | uint8_t lockStatus(); | |
162 | ||
163 | private: | |
164 | //dummy otherwise it doesn,t compile (we must define all methods of an abstract class) | |
165 | virtual int _getc() { return -1;}; | |
166 | ||
167 | uint8_t lock_status; | |
168 | }; | |
169 | ||
170 | #endif |