USB CDC (serial) and USB MSC (strage) Composite Device. http://mbed.org/users/okini3939/notebook/USB_Device/

Dependencies:   ChaNFSSD mbed ChaNFS

Committer:
okini3939
Date:
Fri Dec 23 16:37:58 2011 +0000
Revision:
2:5db90410bb90
Parent:
0:9b1d17d54055

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okini3939 0:9b1d17d54055 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
okini3939 0:9b1d17d54055 2 *
okini3939 0:9b1d17d54055 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
okini3939 0:9b1d17d54055 4 * and associated documentation files (the "Software"), to deal in the Software without
okini3939 0:9b1d17d54055 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
okini3939 0:9b1d17d54055 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
okini3939 0:9b1d17d54055 7 * Software is furnished to do so, subject to the following conditions:
okini3939 0:9b1d17d54055 8 *
okini3939 0:9b1d17d54055 9 * The above copyright notice and this permission notice shall be included in all copies or
okini3939 0:9b1d17d54055 10 * substantial portions of the Software.
okini3939 0:9b1d17d54055 11 *
okini3939 0:9b1d17d54055 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
okini3939 0:9b1d17d54055 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
okini3939 0:9b1d17d54055 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
okini3939 0:9b1d17d54055 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
okini3939 0:9b1d17d54055 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
okini3939 0:9b1d17d54055 17 */
okini3939 0:9b1d17d54055 18
okini3939 0:9b1d17d54055 19 #ifndef USBMOUSEKEYBOARD_H
okini3939 0:9b1d17d54055 20 #define USBMOUSEKEYBOARD_H
okini3939 0:9b1d17d54055 21
okini3939 0:9b1d17d54055 22 #define REPORT_ID_KEYBOARD 1
okini3939 0:9b1d17d54055 23 #define REPORT_ID_MOUSE 2
okini3939 0:9b1d17d54055 24 #define REPORT_ID_VOLUME 3
okini3939 0:9b1d17d54055 25
okini3939 0:9b1d17d54055 26 #include "USBMouse.h"
okini3939 0:9b1d17d54055 27 #include "USBKeyboard.h"
okini3939 0:9b1d17d54055 28 #include "Stream.h"
okini3939 0:9b1d17d54055 29 #include "USBHID.h"
okini3939 0:9b1d17d54055 30
okini3939 0:9b1d17d54055 31 /**
okini3939 0:9b1d17d54055 32 * USBMouseKeyboard example
okini3939 0:9b1d17d54055 33 * @code
okini3939 0:9b1d17d54055 34 *
okini3939 0:9b1d17d54055 35 * #include "mbed.h"
okini3939 0:9b1d17d54055 36 * #include "USBMouseKeyboard.h"
okini3939 0:9b1d17d54055 37 *
okini3939 0:9b1d17d54055 38 * USBMouseKeyboard key_mouse;
okini3939 0:9b1d17d54055 39 *
okini3939 0:9b1d17d54055 40 * int main(void)
okini3939 0:9b1d17d54055 41 * {
okini3939 0:9b1d17d54055 42 * while(1)
okini3939 0:9b1d17d54055 43 * {
okini3939 0:9b1d17d54055 44 * key_mouse.move(20, 0);
okini3939 0:9b1d17d54055 45 * key_mouse.printf("Hello From MBED\r\n");
okini3939 0:9b1d17d54055 46 * wait(1);
okini3939 0:9b1d17d54055 47 * }
okini3939 0:9b1d17d54055 48 * }
okini3939 0:9b1d17d54055 49 * @endcode
okini3939 0:9b1d17d54055 50 *
okini3939 0:9b1d17d54055 51 *
okini3939 0:9b1d17d54055 52 * @code
okini3939 0:9b1d17d54055 53 *
okini3939 0:9b1d17d54055 54 * #include "mbed.h"
okini3939 0:9b1d17d54055 55 * #include "USBMouseKeyboard.h"
okini3939 0:9b1d17d54055 56 *
okini3939 0:9b1d17d54055 57 * USBMouseKeyboard key_mouse(ABS_MOUSE);
okini3939 0:9b1d17d54055 58 *
okini3939 0:9b1d17d54055 59 * int main(void)
okini3939 0:9b1d17d54055 60 * {
okini3939 0:9b1d17d54055 61 * while(1)
okini3939 0:9b1d17d54055 62 * {
okini3939 0:9b1d17d54055 63 * key_mouse.move(X_MAX_ABS/2, Y_MAX_ABS/2);
okini3939 0:9b1d17d54055 64 * key_mouse.printf("Hello from MBED\r\n");
okini3939 0:9b1d17d54055 65 * wait(1);
okini3939 0:9b1d17d54055 66 * }
okini3939 0:9b1d17d54055 67 * }
okini3939 0:9b1d17d54055 68 * @endcode
okini3939 0:9b1d17d54055 69 */
okini3939 0:9b1d17d54055 70 class USBMouseKeyboard: public USBHID, public Stream
okini3939 0:9b1d17d54055 71 {
okini3939 0:9b1d17d54055 72 public:
okini3939 0:9b1d17d54055 73
okini3939 0:9b1d17d54055 74 /**
okini3939 0:9b1d17d54055 75 * Constructor
okini3939 0:9b1d17d54055 76 *
okini3939 0:9b1d17d54055 77 * @param mouse_type Mouse type: ABS_MOUSE (absolute mouse) or REL_MOUSE (relative mouse) (default: REL_MOUSE)
okini3939 0:9b1d17d54055 78 * @param leds Leds bus: first: NUM_LOCK, second: CAPS_LOCK, third: SCROLL_LOCK
okini3939 0:9b1d17d54055 79 * @param vendor_id Your vendor_id (default: 0x1234)
okini3939 0:9b1d17d54055 80 * @param product_id Your product_id (default: 0x0001)
okini3939 0:9b1d17d54055 81 * @param product_release Your preoduct_release (default: 0x0001)
okini3939 0:9b1d17d54055 82 *
okini3939 0:9b1d17d54055 83 */
okini3939 0:9b1d17d54055 84 USBMouseKeyboard(MOUSE_TYPE mouse_type = REL_MOUSE, uint16_t vendor_id = 0x0021, uint16_t product_id = 0x0011, uint16_t product_release = 0x0001):
okini3939 0:9b1d17d54055 85 USBHID(0, 0, vendor_id, product_id, product_release, false)
okini3939 0:9b1d17d54055 86 {
okini3939 0:9b1d17d54055 87 lock_status = 0;
okini3939 0:9b1d17d54055 88 button = 0;
okini3939 0:9b1d17d54055 89 this->mouse_type = mouse_type;
okini3939 0:9b1d17d54055 90 connect();
okini3939 0:9b1d17d54055 91 };
okini3939 0:9b1d17d54055 92
okini3939 0:9b1d17d54055 93 /**
okini3939 0:9b1d17d54055 94 * Write a state of the mouse
okini3939 0:9b1d17d54055 95 *
okini3939 0:9b1d17d54055 96 * @param x x-axis position
okini3939 0:9b1d17d54055 97 * @param y y-axis position
okini3939 0:9b1d17d54055 98 * @param buttons buttons state (first bit represents MOUSE_LEFT, second bit MOUSE_RIGHT and third bit MOUSE_MIDDLE)
okini3939 0:9b1d17d54055 99 * @param z wheel state (>0 to scroll down, <0 to scroll up)
okini3939 0:9b1d17d54055 100 * @returns true if there is no error, false otherwise
okini3939 0:9b1d17d54055 101 */
okini3939 0:9b1d17d54055 102 bool update(int16_t x, int16_t y, uint8_t buttons, int8_t z);
okini3939 0:9b1d17d54055 103
okini3939 0:9b1d17d54055 104
okini3939 0:9b1d17d54055 105 /**
okini3939 0:9b1d17d54055 106 * Move the cursor to (x, y)
okini3939 0:9b1d17d54055 107 *
okini3939 0:9b1d17d54055 108 * @param x x-axis position
okini3939 0:9b1d17d54055 109 * @param y y-axis position
okini3939 0:9b1d17d54055 110 * @returns true if there is no error, false otherwise
okini3939 0:9b1d17d54055 111 */
okini3939 0:9b1d17d54055 112 bool move(int16_t x, int16_t y);
okini3939 0:9b1d17d54055 113
okini3939 0:9b1d17d54055 114 /**
okini3939 0:9b1d17d54055 115 * Press one or several buttons
okini3939 0:9b1d17d54055 116 *
okini3939 0:9b1d17d54055 117 * @param button button state (ex: press(MOUSE_LEFT))
okini3939 0:9b1d17d54055 118 * @returns true if there is no error, false otherwise
okini3939 0:9b1d17d54055 119 */
okini3939 0:9b1d17d54055 120 bool press(uint8_t button);
okini3939 0:9b1d17d54055 121
okini3939 0:9b1d17d54055 122 /**
okini3939 0:9b1d17d54055 123 * Release one or several buttons
okini3939 0:9b1d17d54055 124 *
okini3939 0:9b1d17d54055 125 * @param button button state (ex: release(MOUSE_LEFT))
okini3939 0:9b1d17d54055 126 * @returns true if there is no error, false otherwise
okini3939 0:9b1d17d54055 127 */
okini3939 0:9b1d17d54055 128 bool release(uint8_t button);
okini3939 0:9b1d17d54055 129
okini3939 0:9b1d17d54055 130 /**
okini3939 0:9b1d17d54055 131 * Double click (MOUSE_LEFT)
okini3939 0:9b1d17d54055 132 *
okini3939 0:9b1d17d54055 133 * @returns true if there is no error, false otherwise
okini3939 0:9b1d17d54055 134 */
okini3939 0:9b1d17d54055 135 bool doubleClick();
okini3939 0:9b1d17d54055 136
okini3939 0:9b1d17d54055 137 /**
okini3939 0:9b1d17d54055 138 * Click
okini3939 0:9b1d17d54055 139 *
okini3939 0:9b1d17d54055 140 * @param button state of the buttons ( ex: clic(MOUSE_LEFT))
okini3939 0:9b1d17d54055 141 * @returns true if there is no error, false otherwise
okini3939 0:9b1d17d54055 142 */
okini3939 0:9b1d17d54055 143 bool click(uint8_t button);
okini3939 0:9b1d17d54055 144
okini3939 0:9b1d17d54055 145 /**
okini3939 0:9b1d17d54055 146 * Scrolling
okini3939 0:9b1d17d54055 147 *
okini3939 0:9b1d17d54055 148 * @param z value of the wheel (>0 to go down, <0 to go up)
okini3939 0:9b1d17d54055 149 * @returns true if there is no error, false otherwise
okini3939 0:9b1d17d54055 150 */
okini3939 0:9b1d17d54055 151 bool scroll(int8_t z);
okini3939 0:9b1d17d54055 152
okini3939 0:9b1d17d54055 153 /**
okini3939 0:9b1d17d54055 154 * To send a character defined by a modifier(CTRL, SHIFT, ALT) and the key
okini3939 0:9b1d17d54055 155 *
okini3939 0:9b1d17d54055 156 * @code
okini3939 0:9b1d17d54055 157 * //To send CTRL + s (save)
okini3939 0:9b1d17d54055 158 * keyboard.keyCode('s', KEY_CTRL);
okini3939 0:9b1d17d54055 159 * @endcode
okini3939 0:9b1d17d54055 160 *
okini3939 0:9b1d17d54055 161 * @param modifier bit 0: KEY_CTRL, bit 1: KEY_SHIFT, bit 2: KEY_ALT (default: 0)
okini3939 0:9b1d17d54055 162 * @param key character to send
okini3939 0:9b1d17d54055 163 * @returns true if there is no error, false otherwise
okini3939 0:9b1d17d54055 164 */
okini3939 0:9b1d17d54055 165 bool keyCode(uint8_t key, uint8_t modifier = 0);
okini3939 0:9b1d17d54055 166
okini3939 0:9b1d17d54055 167 /**
okini3939 0:9b1d17d54055 168 * Send a character
okini3939 0:9b1d17d54055 169 *
okini3939 0:9b1d17d54055 170 * @param c character to be sent
okini3939 0:9b1d17d54055 171 * @returns true if there is no error, false otherwise
okini3939 0:9b1d17d54055 172 */
okini3939 0:9b1d17d54055 173 virtual int _putc(int c);
okini3939 0:9b1d17d54055 174
okini3939 0:9b1d17d54055 175 /**
okini3939 0:9b1d17d54055 176 * Control media keys
okini3939 0:9b1d17d54055 177 *
okini3939 0:9b1d17d54055 178 * @param key media key pressed (KEY_NEXT_TRACK, KEY_PREVIOUS_TRACK, KEY_STOP, KEY_PLAY_PAUSE, KEY_MUTE, KEY_VOLUME_UP, KEY_VOLUME_DOWN)
okini3939 0:9b1d17d54055 179 * @returns true if there is no error, false otherwise
okini3939 0:9b1d17d54055 180 */
okini3939 0:9b1d17d54055 181 bool mediaControl(MEDIA_KEY key);
okini3939 0:9b1d17d54055 182
okini3939 0:9b1d17d54055 183 /**
okini3939 0:9b1d17d54055 184 * 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:
okini3939 0:9b1d17d54055 185 * - First bit: NUM_LOCK
okini3939 0:9b1d17d54055 186 * - Second bit: CAPS_LOCK
okini3939 0:9b1d17d54055 187 * - Third bit: SCROLL_LOCK
okini3939 0:9b1d17d54055 188 *
okini3939 0:9b1d17d54055 189 * @returns status of lock keys
okini3939 0:9b1d17d54055 190 */
okini3939 0:9b1d17d54055 191 uint8_t lockStatus();
okini3939 0:9b1d17d54055 192
okini3939 0:9b1d17d54055 193 /*
okini3939 0:9b1d17d54055 194 * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
okini3939 0:9b1d17d54055 195 *
okini3939 0:9b1d17d54055 196 * @returns pointer to the report descriptor
okini3939 0:9b1d17d54055 197 */
okini3939 0:9b1d17d54055 198 virtual uint8_t * reportDesc();
okini3939 0:9b1d17d54055 199
okini3939 0:9b1d17d54055 200 /*
okini3939 0:9b1d17d54055 201 * Called when a data is received on the OUT endpoint. Useful to switch on LED of LOCK keys
okini3939 0:9b1d17d54055 202 *
okini3939 0:9b1d17d54055 203 * @returns if handle by subclass, return true
okini3939 0:9b1d17d54055 204 */
okini3939 0:9b1d17d54055 205 virtual bool EP1_OUT_callback();
okini3939 0:9b1d17d54055 206
okini3939 0:9b1d17d54055 207
okini3939 0:9b1d17d54055 208 private:
okini3939 0:9b1d17d54055 209 bool mouseWrite(int8_t x, int8_t y, uint8_t buttons, int8_t z);
okini3939 0:9b1d17d54055 210 MOUSE_TYPE mouse_type;
okini3939 0:9b1d17d54055 211 uint8_t button;
okini3939 0:9b1d17d54055 212 bool mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z);
okini3939 0:9b1d17d54055 213
okini3939 0:9b1d17d54055 214 uint8_t lock_status;
okini3939 0:9b1d17d54055 215
okini3939 0:9b1d17d54055 216 //dummy otherwise it doesn't compile (we must define all methods of an abstract class)
okini3939 0:9b1d17d54055 217 virtual int _getc() { return -1;}
okini3939 0:9b1d17d54055 218 };
okini3939 0:9b1d17d54055 219
okini3939 0:9b1d17d54055 220 #endif