library

Dependents:   USB_CDC_MSD_Hello

Committer:
sherckuith
Date:
Fri Aug 24 02:01:51 2012 +0000
Revision:
0:d5bb9a9c3e24
[mbed] converted /USB_CDC_MSD_Hello/USBDevice

Who changed what in which revision?

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