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 /* USBMouse.h */
okini3939 0:9b1d17d54055 2 /* USB device example: relative mouse */
okini3939 0:9b1d17d54055 3 /* Copyright (c) 2011 ARM Limited. All rights reserved. */
okini3939 0:9b1d17d54055 4
okini3939 0:9b1d17d54055 5 #ifndef USBMOUSE_H
okini3939 0:9b1d17d54055 6 #define USBMOUSE_H
okini3939 0:9b1d17d54055 7
okini3939 0:9b1d17d54055 8 #include "USBHID.h"
okini3939 0:9b1d17d54055 9
okini3939 0:9b1d17d54055 10 #define REPORT_ID_MOUSE 2
okini3939 0:9b1d17d54055 11
okini3939 0:9b1d17d54055 12 /* Common usage */
okini3939 0:9b1d17d54055 13
okini3939 0:9b1d17d54055 14 enum MOUSE_BUTTON
okini3939 0:9b1d17d54055 15 {
okini3939 0:9b1d17d54055 16 MOUSE_LEFT = 1,
okini3939 0:9b1d17d54055 17 MOUSE_RIGHT = 2,
okini3939 0:9b1d17d54055 18 MOUSE_MIDDLE = 4,
okini3939 0:9b1d17d54055 19 };
okini3939 0:9b1d17d54055 20
okini3939 0:9b1d17d54055 21 /* X and Y limits */
okini3939 0:9b1d17d54055 22 /* These values do not directly map to screen pixels */
okini3939 0:9b1d17d54055 23 /* Zero may be interpreted as meaning 'no movement' */
okini3939 0:9b1d17d54055 24 #define X_MIN_ABS (1) /*!< Minimum value on x-axis */
okini3939 0:9b1d17d54055 25 #define Y_MIN_ABS (1) /*!< Minimum value on y-axis */
okini3939 0:9b1d17d54055 26 #define X_MAX_ABS (0x7fff) /*!< Maximum value on x-axis */
okini3939 0:9b1d17d54055 27 #define Y_MAX_ABS (0x7fff) /*!< Maximum value on y-axis */
okini3939 0:9b1d17d54055 28
okini3939 0:9b1d17d54055 29 #define X_MIN_REL (-127) /*!< The maximum value that we can move to the left on the x-axis */
okini3939 0:9b1d17d54055 30 #define Y_MIN_REL (-127) /*!< The maximum value that we can move up on the y-axis */
okini3939 0:9b1d17d54055 31 #define X_MAX_REL (127) /*!< The maximum value that we can move to the right on the x-axis */
okini3939 0:9b1d17d54055 32 #define Y_MAX_REL (127) /*!< The maximum value that we can move down on the y-axis */
okini3939 0:9b1d17d54055 33
okini3939 0:9b1d17d54055 34 enum MOUSE_TYPE
okini3939 0:9b1d17d54055 35 {
okini3939 0:9b1d17d54055 36 ABS_MOUSE,
okini3939 0:9b1d17d54055 37 REL_MOUSE,
okini3939 0:9b1d17d54055 38 };
okini3939 0:9b1d17d54055 39
okini3939 0:9b1d17d54055 40 /**
okini3939 0:9b1d17d54055 41 *
okini3939 0:9b1d17d54055 42 * USBMouse example
okini3939 0:9b1d17d54055 43 * @code
okini3939 0:9b1d17d54055 44 * #include "mbed.h"
okini3939 0:9b1d17d54055 45 * #include "USBMouse.h"
okini3939 0:9b1d17d54055 46 *
okini3939 0:9b1d17d54055 47 * USBMouse mouse;
okini3939 0:9b1d17d54055 48 *
okini3939 0:9b1d17d54055 49 * int main(void)
okini3939 0:9b1d17d54055 50 * {
okini3939 0:9b1d17d54055 51 * while (1)
okini3939 0:9b1d17d54055 52 * {
okini3939 0:9b1d17d54055 53 * mouse.move(20, 0);
okini3939 0:9b1d17d54055 54 * wait(0.5);
okini3939 0:9b1d17d54055 55 * }
okini3939 0:9b1d17d54055 56 * }
okini3939 0:9b1d17d54055 57 *
okini3939 0:9b1d17d54055 58 * @endcode
okini3939 0:9b1d17d54055 59 *
okini3939 0:9b1d17d54055 60 *
okini3939 0:9b1d17d54055 61 * @code
okini3939 0:9b1d17d54055 62 * #include "mbed.h"
okini3939 0:9b1d17d54055 63 * #include "USBMouse.h"
okini3939 0:9b1d17d54055 64 * #include <math.h>
okini3939 0:9b1d17d54055 65 *
okini3939 0:9b1d17d54055 66 * USBMouse mouse(ABS_MOUSE);
okini3939 0:9b1d17d54055 67 *
okini3939 0:9b1d17d54055 68 * int main(void)
okini3939 0:9b1d17d54055 69 * {
okini3939 0:9b1d17d54055 70 * uint16_t x_center = (X_MAX_ABS - X_MIN_ABS)/2;
okini3939 0:9b1d17d54055 71 * uint16_t y_center = (Y_MAX_ABS - Y_MIN_ABS)/2;
okini3939 0:9b1d17d54055 72 * uint16_t x_screen = 0;
okini3939 0:9b1d17d54055 73 * uint16_t y_screen = 0;
okini3939 0:9b1d17d54055 74 *
okini3939 0:9b1d17d54055 75 * uint32_t x_origin = x_center;
okini3939 0:9b1d17d54055 76 * uint32_t y_origin = y_center;
okini3939 0:9b1d17d54055 77 * uint32_t radius = 5000;
okini3939 0:9b1d17d54055 78 * uint32_t angle = 0;
okini3939 0:9b1d17d54055 79 *
okini3939 0:9b1d17d54055 80 * while (1)
okini3939 0:9b1d17d54055 81 * {
okini3939 0:9b1d17d54055 82 * x_screen = x_origin + cos((double)angle*3.14/180.0)*radius;
okini3939 0:9b1d17d54055 83 * y_screen = y_origin + sin((double)angle*3.14/180.0)*radius;
okini3939 0:9b1d17d54055 84 *
okini3939 0:9b1d17d54055 85 * mouse.move(x_screen, y_screen);
okini3939 0:9b1d17d54055 86 * angle += 3;
okini3939 0:9b1d17d54055 87 * wait(0.01);
okini3939 0:9b1d17d54055 88 * }
okini3939 0:9b1d17d54055 89 * }
okini3939 0:9b1d17d54055 90 *
okini3939 0:9b1d17d54055 91 * @endcode
okini3939 0:9b1d17d54055 92 */
okini3939 0:9b1d17d54055 93 class USBMouse: public USBHID
okini3939 0:9b1d17d54055 94 {
okini3939 0:9b1d17d54055 95 public:
okini3939 0:9b1d17d54055 96
okini3939 0:9b1d17d54055 97 /**
okini3939 0:9b1d17d54055 98 * Constructor
okini3939 0:9b1d17d54055 99 *
okini3939 0:9b1d17d54055 100 * @param mouse_type Mouse type: ABS_MOUSE (absolute mouse) or REL_MOUSE (relative mouse) (default: REL_MOUSE)
okini3939 0:9b1d17d54055 101 * @param vendor_id Your vendor_id (default: 0x1234)
okini3939 0:9b1d17d54055 102 * @param product_id Your product_id (default: 0x0001)
okini3939 0:9b1d17d54055 103 * @param product_release Your preoduct_release (default: 0x0001)
okini3939 0:9b1d17d54055 104 *
okini3939 0:9b1d17d54055 105 */
okini3939 0:9b1d17d54055 106 USBMouse(MOUSE_TYPE mouse_type = REL_MOUSE, uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0001, uint16_t product_release = 0x0001):
okini3939 0:9b1d17d54055 107 USBHID(0, 0, vendor_id, product_id, product_release, false)
okini3939 0:9b1d17d54055 108 {
okini3939 0:9b1d17d54055 109 button = 0;
okini3939 0:9b1d17d54055 110 this->mouse_type = mouse_type;
okini3939 0:9b1d17d54055 111 connect();
okini3939 0:9b1d17d54055 112 };
okini3939 0:9b1d17d54055 113
okini3939 0:9b1d17d54055 114 /**
okini3939 0:9b1d17d54055 115 * Write a state of the mouse
okini3939 0:9b1d17d54055 116 *
okini3939 0:9b1d17d54055 117 * @param x x-axis position
okini3939 0:9b1d17d54055 118 * @param y y-axis position
okini3939 0:9b1d17d54055 119 * @param buttons buttons state (first bit represents MOUSE_LEFT, second bit MOUSE_RIGHT and third bit MOUSE_MIDDLE)
okini3939 0:9b1d17d54055 120 * @param z wheel state (>0 to scroll down, <0 to scroll up)
okini3939 0:9b1d17d54055 121 * @returns true if there is no error, false otherwise
okini3939 0:9b1d17d54055 122 */
okini3939 0:9b1d17d54055 123 bool update(int16_t x, int16_t y, uint8_t buttons, int8_t z);
okini3939 0:9b1d17d54055 124
okini3939 0:9b1d17d54055 125
okini3939 0:9b1d17d54055 126 /**
okini3939 0:9b1d17d54055 127 * Move the cursor to (x, y)
okini3939 0:9b1d17d54055 128 *
okini3939 0:9b1d17d54055 129 * @param x-axis position
okini3939 0:9b1d17d54055 130 * @param y-axis position
okini3939 0:9b1d17d54055 131 * @returns true if there is no error, false otherwise
okini3939 0:9b1d17d54055 132 */
okini3939 0:9b1d17d54055 133 bool move(int16_t x, int16_t y);
okini3939 0:9b1d17d54055 134
okini3939 0:9b1d17d54055 135 /**
okini3939 0:9b1d17d54055 136 * Press one or several buttons
okini3939 0:9b1d17d54055 137 *
okini3939 0:9b1d17d54055 138 * @param button button state (ex: press(MOUSE_LEFT))
okini3939 0:9b1d17d54055 139 * @returns true if there is no error, false otherwise
okini3939 0:9b1d17d54055 140 */
okini3939 0:9b1d17d54055 141 bool press(uint8_t button);
okini3939 0:9b1d17d54055 142
okini3939 0:9b1d17d54055 143 /**
okini3939 0:9b1d17d54055 144 * Release one or several buttons
okini3939 0:9b1d17d54055 145 *
okini3939 0:9b1d17d54055 146 * @param button button state (ex: release(MOUSE_LEFT))
okini3939 0:9b1d17d54055 147 * @returns true if there is no error, false otherwise
okini3939 0:9b1d17d54055 148 */
okini3939 0:9b1d17d54055 149 bool release(uint8_t button);
okini3939 0:9b1d17d54055 150
okini3939 0:9b1d17d54055 151 /**
okini3939 0:9b1d17d54055 152 * Double click (MOUSE_LEFT)
okini3939 0:9b1d17d54055 153 *
okini3939 0:9b1d17d54055 154 * @returns true if there is no error, false otherwise
okini3939 0:9b1d17d54055 155 */
okini3939 0:9b1d17d54055 156 bool doubleClick();
okini3939 0:9b1d17d54055 157
okini3939 0:9b1d17d54055 158 /**
okini3939 0:9b1d17d54055 159 * Click
okini3939 0:9b1d17d54055 160 *
okini3939 0:9b1d17d54055 161 * @param button state of the buttons ( ex: clic(MOUSE_LEFT))
okini3939 0:9b1d17d54055 162 * @returns true if there is no error, false otherwise
okini3939 0:9b1d17d54055 163 */
okini3939 0:9b1d17d54055 164 bool click(uint8_t button);
okini3939 0:9b1d17d54055 165
okini3939 0:9b1d17d54055 166 /**
okini3939 0:9b1d17d54055 167 * Scrolling
okini3939 0:9b1d17d54055 168 *
okini3939 0:9b1d17d54055 169 * @param z value of the wheel (>0 to go down, <0 to go up)
okini3939 0:9b1d17d54055 170 * @returns true if there is no error, false otherwise
okini3939 0:9b1d17d54055 171 */
okini3939 0:9b1d17d54055 172 bool scroll(int8_t z);
okini3939 0:9b1d17d54055 173
okini3939 0:9b1d17d54055 174 /*
okini3939 0:9b1d17d54055 175 * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
okini3939 0:9b1d17d54055 176 *
okini3939 0:9b1d17d54055 177 * @returns pointer to the report descriptor
okini3939 0:9b1d17d54055 178 */
okini3939 0:9b1d17d54055 179 virtual uint8_t * reportDesc();
okini3939 0:9b1d17d54055 180
okini3939 0:9b1d17d54055 181 private:
okini3939 0:9b1d17d54055 182 MOUSE_TYPE mouse_type;
okini3939 0:9b1d17d54055 183 uint8_t button;
okini3939 0:9b1d17d54055 184 bool mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z);
okini3939 0:9b1d17d54055 185 };
okini3939 0:9b1d17d54055 186
okini3939 0:9b1d17d54055 187 #endif