Chris Majoros / USBDevice

Dependents:   loststone

Fork of USBDevice by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBMouse.h Source File

USBMouse.h

00001 /* Copyright (c) 2010-2011 mbed.org, MIT License
00002 *
00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004 * and associated documentation files (the "Software"), to deal in the Software without
00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00007 * Software is furnished to do so, subject to the following conditions:
00008 *
00009 * The above copyright notice and this permission notice shall be included in all copies or
00010 * substantial portions of the Software.
00011 *
00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 */
00018 
00019 #ifndef USBMOUSE_H
00020 #define USBMOUSE_H
00021 
00022 #include "USBHID.h"
00023 
00024 #define REPORT_ID_MOUSE   2
00025 
00026 /* Common usage */
00027 
00028 enum MOUSE_BUTTON
00029 {
00030     MOUSE_LEFT = 1,
00031     MOUSE_RIGHT = 2,
00032     MOUSE_MIDDLE = 4,
00033     MOUSE_FORWORD = 8,
00034     MOUSE_BACK = 16
00035 };
00036 
00037 /* X and Y limits */
00038 /* These values do not directly map to screen pixels */
00039 /* Zero may be interpreted as meaning 'no movement' */
00040 #define X_MIN_ABS    (1)        /*!< Minimum value on x-axis */  
00041 #define Y_MIN_ABS    (1)        /*!< Minimum value on y-axis */
00042 #define X_MAX_ABS    (0x7fff)   /*!< Maximum value on x-axis */
00043 #define Y_MAX_ABS    (0x7fff)   /*!< Maximum value on y-axis */
00044 
00045 #define X_MIN_REL    (-127)     /*!< The maximum value that we can move to the left on the x-axis */
00046 #define Y_MIN_REL    (-127)     /*!< The maximum value that we can move up on the y-axis */
00047 #define X_MAX_REL    (127)      /*!< The maximum value that we can move to the right on the x-axis */
00048 #define Y_MAX_REL    (127)      /*!< The maximum value that we can move down on the y-axis */
00049 
00050 enum MOUSE_TYPE
00051 {
00052     ABS_MOUSE,
00053     REL_MOUSE,
00054 };
00055 
00056 /**
00057  *
00058  * USBMouse example
00059  * @code
00060  * #include "mbed.h"
00061  * #include "USBMouse.h"
00062  *
00063  * USBMouse mouse;
00064  *
00065  * int main(void)
00066  * {
00067  *   while (1)
00068  *   {
00069  *      mouse.move(20, 0);
00070  *      wait(0.5);
00071  *   }
00072  * }
00073  *
00074  * @endcode
00075  *
00076  *
00077  * @code
00078  * #include "mbed.h"
00079  * #include "USBMouse.h"
00080  * #include <math.h>
00081  *
00082  * USBMouse mouse(ABS_MOUSE);
00083  *
00084  * int main(void)
00085  * {
00086  *   uint16_t x_center = (X_MAX_ABS - X_MIN_ABS)/2;
00087  *   uint16_t y_center = (Y_MAX_ABS - Y_MIN_ABS)/2;
00088  *   uint16_t x_screen = 0;
00089  *   uint16_t y_screen = 0;
00090  *   
00091  *   uint32_t x_origin = x_center;
00092  *   uint32_t y_origin = y_center;
00093  *   uint32_t radius = 5000;
00094  *   uint32_t angle = 0;
00095  *
00096  *   while (1)
00097  *   {
00098  *       x_screen = x_origin + cos((double)angle*3.14/180.0)*radius;
00099  *       y_screen = y_origin + sin((double)angle*3.14/180.0)*radius;
00100  *       
00101  *       mouse.move(x_screen, y_screen);
00102  *       angle += 3;
00103  *       wait(0.01);
00104  *   }
00105  * }
00106  *
00107  * @endcode
00108  */
00109 class USBMouse: public USBHID
00110 {
00111     public:
00112         
00113         /**
00114         *   Constructor
00115         *
00116         * @param mouse_type Mouse type: ABS_MOUSE (absolute mouse) or REL_MOUSE (relative mouse) (default: REL_MOUSE)
00117         * @param vendor_id Your vendor_id (default: 0x1234)
00118         * @param product_id Your product_id (default: 0x0001)
00119         * @param product_release Your preoduct_release (default: 0x0001)
00120         *
00121         */
00122         USBMouse(MOUSE_TYPE mouse_type = REL_MOUSE, uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0001, uint16_t product_release = 0x0001): 
00123             USBHID(0, 0, vendor_id, product_id, product_release, false)
00124             { 
00125                 button = 0;
00126                 this->mouse_type = mouse_type;
00127                 connect();
00128             };
00129         
00130         /**
00131         * Write a state of the mouse
00132         *
00133         * @param x x-axis position
00134         * @param y y-axis position
00135         * @param buttons buttons state (first bit represents MOUSE_LEFT, second bit MOUSE_RIGHT and third bit MOUSE_MIDDLE)
00136         * @param z wheel state (>0 to scroll down, <0 to scroll up)
00137         * @returns true if there is no error, false otherwise
00138         */
00139         bool update(int16_t x, int16_t y, uint8_t buttons, int8_t z, int8_t h);
00140         
00141         
00142         /**
00143         * Move the cursor to (x, y)
00144         *
00145         * @param x-axis position
00146         * @param y-axis position
00147         * @returns true if there is no error, false otherwise
00148         */
00149         bool move(int16_t x, int16_t y);
00150         
00151         /**
00152         * Press one or several buttons
00153         *
00154         * @param button button state (ex: press(MOUSE_LEFT))
00155         * @returns true if there is no error, false otherwise
00156         */
00157         bool press(uint8_t button);
00158         
00159         /**
00160         * Release one or several buttons
00161         *
00162         * @param button button state (ex: release(MOUSE_LEFT))
00163         * @returns true if there is no error, false otherwise
00164         */
00165         bool release(uint8_t button);
00166         
00167         /**
00168         * Double click (MOUSE_LEFT)
00169         *
00170         * @returns true if there is no error, false otherwise
00171         */
00172         bool doubleClick();
00173         
00174         /**
00175         * Click
00176         *
00177         * @param button state of the buttons ( ex: clic(MOUSE_LEFT))
00178         * @returns true if there is no error, false otherwise
00179         */
00180         bool click(uint8_t button); 
00181         
00182         /**
00183         * Scrolling
00184         *
00185         * @param z value of the wheel (>0 to go down, <0 to go up)
00186         * @returns true if there is no error, false otherwise
00187         */
00188         bool scroll(int8_t z, int8_t h);
00189         
00190         /*
00191         * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
00192         *
00193         * @returns pointer to the report descriptor
00194         */
00195         virtual uint8_t * reportDesc();
00196 
00197     protected:
00198         /*
00199         * Get configuration descriptor
00200         *
00201         * @returns pointer to the configuration descriptor
00202         */
00203         virtual uint8_t * configurationDesc();
00204         
00205     private:
00206         MOUSE_TYPE mouse_type;
00207         uint8_t button;
00208         bool mouseSend(int16_t x, int16_t y, uint8_t buttons, int8_t z, int8_t h);
00209 };
00210 
00211 #endif