Dieter Graef / USBHost_DISCO-F746NG

Dependents:   DISCO-F746NG_USB_Host

Fork of KL46Z-USBHost by Norimasa Okamoto

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBHostMouseKeyboard.h Source File

USBHostMouseKeyboard.h

00001 #ifndef USBHOSTMOUSEKEYBOARD_H_INCLUDED
00002 #define USBHOSTMOUSEKEYBOARD_H_INCLUDED
00003 
00004 #include "USBHostConf.h"
00005 
00006 #if USBHOST_MOUSE_KEYBOARD
00007 
00008 #include "USBHost.h"
00009 
00010 #define REPORT_ID_IDLE 0
00011 #define REPORT_ID_KEYBOARD 1
00012 #define REPORT_ID_MOUSE 2
00013 #define REPORT_ID_VOLUME 3
00014 
00015 
00016 /**
00017  * A class to communicate a USB mouse
00018  */
00019 class USBHostMouseKb : public IUSBEnumerator {
00020 public:
00021 
00022     /**
00023     * Constructor
00024     */
00025     USBHostMouseKb(int Interface);
00026 
00027     /**
00028      * Try to connect a mouse device
00029      *
00030      * @return true if connection was successful
00031      */
00032     bool connect();
00033 
00034     /**
00035     * Check if a mouse is connected
00036     *
00037     * @returns true if a mouse is connected
00038     */
00039     bool connected();
00040 
00041     void poll();
00042 
00043     /**
00044      * Attach a callback called when a mouse event is received
00045      *
00046      * @param ptr function pointer
00047      */
00048     inline void attachMouseEvent(void (*ptr)(uint8_t buttons, int8_t x, int8_t y, int8_t z)) {
00049         if (ptr != NULL) {
00050             onUpdate = ptr;
00051         }
00052     }
00053 
00054     /**
00055      * Attach a callback called when the button state changes
00056      *
00057      * @param ptr function pointer
00058      */
00059     inline void attachButtonEvent(void (*ptr)(uint8_t buttons)) {
00060         if (ptr != NULL) {
00061             onButtonUpdate = ptr;
00062         }
00063     }
00064 
00065     /**
00066      * Attach a callback called when the X axis value changes
00067      *
00068      * @param ptr function pointer
00069      */
00070     inline void attachXEvent(void (*ptr)(int8_t x)) {
00071         if (ptr != NULL) {
00072             onXUpdate = ptr;
00073         }
00074     }
00075 
00076     /**
00077      * Attach a callback called when the Y axis value changes
00078      *
00079      * @param ptr function pointer
00080      */
00081     inline void attachYEvent(void (*ptr)(int8_t y)) {
00082         if (ptr != NULL) {
00083             onYUpdate = ptr;
00084         }
00085     }
00086 
00087     /**
00088      * Attach a callback called when the Z axis value changes (scrolling)
00089      *
00090      * @param ptr function pointer
00091      */
00092     inline void attachZEvent(void (*ptr)(int8_t z)) {
00093         if (ptr != NULL) {
00094             onZUpdate = ptr;
00095         }
00096     }
00097 
00098     /**
00099      * Attach a callback called when a keyboard event is received
00100      *
00101      * @param ptr function pointer
00102      */
00103 
00104     inline void attachKb(void (*ptr)(uint8_t key)) {
00105         if (ptr != NULL) {
00106             onKey = ptr;
00107         }
00108     }
00109 
00110     /**
00111      * Attach a callback called when a keyboard event is received
00112      *
00113      * @param ptr function pointer
00114      */
00115     inline void attachKb(void (*ptr)(uint8_t keyCode, uint8_t modifier)) {
00116         if (ptr != NULL) {
00117             onKeyCode = ptr;
00118         }
00119     }
00120 
00121 protected:
00122     //From IUSBEnumerator
00123     virtual void setVidPid(uint16_t vid, uint16_t pid);
00124     virtual bool parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol); //Must return true if the interface should be parsed
00125     virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used
00126 
00127 private:
00128     USBHost * host;
00129     USBDeviceConnected * dev;
00130     USBEndpoint * int_in_mouse;
00131     USBEndpoint * int_in_kb;
00132     uint8_t report[20];
00133     bool dev_connected;
00134     bool mouse_device_found;
00135     bool kb_device_found;
00136     int mouse_intf;
00137     int kb_intf;
00138 
00139     uint8_t buttons;
00140     int8_t x;
00141     int8_t y;
00142     int8_t z;
00143 
00144     void rxHandlerMouse();
00145     void rxHandlerKb();
00146     void (*onUpdate)(uint8_t buttons, int8_t x, int8_t y, int8_t z);
00147     void (*onButtonUpdate)(uint8_t buttons);
00148     void (*onXUpdate)(int8_t x);
00149     void (*onYUpdate)(int8_t y);
00150     void (*onZUpdate)(int8_t z);
00151     void (*onKey)(uint8_t key);
00152     void (*onKeyCode)(uint8_t key, uint8_t modifier);
00153 
00154     int report_id;
00155     void init();
00156 };
00157 
00158 #endif
00159 
00160 
00161 #endif /* USBHOSTMOUSEKEYBOARD_H_INCLUDED */