Simple USBHost library for STM32F746NG Discovery board. Only either the Fastspeed or the Highspeed port can be used( not both together)

Dependents:   DISCO-F746NG_USB_Host

Fork of KL46Z-USBHost by Norimasa Okamoto

Committer:
DieterGraef
Date:
Mon Jun 13 17:21:07 2016 +0000
Revision:
24:5396b6a93262
USB Host for STM32F746 DISCO Board. At the moment you can only use either the High Speed Port or the Fast Speed Port.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DieterGraef 24:5396b6a93262 1 #ifndef USBHOSTMOUSEKEYBOARD_H_INCLUDED
DieterGraef 24:5396b6a93262 2 #define USBHOSTMOUSEKEYBOARD_H_INCLUDED
DieterGraef 24:5396b6a93262 3
DieterGraef 24:5396b6a93262 4 #include "USBHostConf.h"
DieterGraef 24:5396b6a93262 5
DieterGraef 24:5396b6a93262 6 #if USBHOST_MOUSE_KEYBOARD
DieterGraef 24:5396b6a93262 7
DieterGraef 24:5396b6a93262 8 #include "USBHost.h"
DieterGraef 24:5396b6a93262 9
DieterGraef 24:5396b6a93262 10 #define REPORT_ID_IDLE 0
DieterGraef 24:5396b6a93262 11 #define REPORT_ID_KEYBOARD 1
DieterGraef 24:5396b6a93262 12 #define REPORT_ID_MOUSE 2
DieterGraef 24:5396b6a93262 13 #define REPORT_ID_VOLUME 3
DieterGraef 24:5396b6a93262 14
DieterGraef 24:5396b6a93262 15
DieterGraef 24:5396b6a93262 16 /**
DieterGraef 24:5396b6a93262 17 * A class to communicate a USB mouse
DieterGraef 24:5396b6a93262 18 */
DieterGraef 24:5396b6a93262 19 class USBHostMouseKb : public IUSBEnumerator {
DieterGraef 24:5396b6a93262 20 public:
DieterGraef 24:5396b6a93262 21
DieterGraef 24:5396b6a93262 22 /**
DieterGraef 24:5396b6a93262 23 * Constructor
DieterGraef 24:5396b6a93262 24 */
DieterGraef 24:5396b6a93262 25 USBHostMouseKb(int Interface);
DieterGraef 24:5396b6a93262 26
DieterGraef 24:5396b6a93262 27 /**
DieterGraef 24:5396b6a93262 28 * Try to connect a mouse device
DieterGraef 24:5396b6a93262 29 *
DieterGraef 24:5396b6a93262 30 * @return true if connection was successful
DieterGraef 24:5396b6a93262 31 */
DieterGraef 24:5396b6a93262 32 bool connect();
DieterGraef 24:5396b6a93262 33
DieterGraef 24:5396b6a93262 34 /**
DieterGraef 24:5396b6a93262 35 * Check if a mouse is connected
DieterGraef 24:5396b6a93262 36 *
DieterGraef 24:5396b6a93262 37 * @returns true if a mouse is connected
DieterGraef 24:5396b6a93262 38 */
DieterGraef 24:5396b6a93262 39 bool connected();
DieterGraef 24:5396b6a93262 40
DieterGraef 24:5396b6a93262 41 void poll();
DieterGraef 24:5396b6a93262 42
DieterGraef 24:5396b6a93262 43 /**
DieterGraef 24:5396b6a93262 44 * Attach a callback called when a mouse event is received
DieterGraef 24:5396b6a93262 45 *
DieterGraef 24:5396b6a93262 46 * @param ptr function pointer
DieterGraef 24:5396b6a93262 47 */
DieterGraef 24:5396b6a93262 48 inline void attachMouseEvent(void (*ptr)(uint8_t buttons, int8_t x, int8_t y, int8_t z)) {
DieterGraef 24:5396b6a93262 49 if (ptr != NULL) {
DieterGraef 24:5396b6a93262 50 onUpdate = ptr;
DieterGraef 24:5396b6a93262 51 }
DieterGraef 24:5396b6a93262 52 }
DieterGraef 24:5396b6a93262 53
DieterGraef 24:5396b6a93262 54 /**
DieterGraef 24:5396b6a93262 55 * Attach a callback called when the button state changes
DieterGraef 24:5396b6a93262 56 *
DieterGraef 24:5396b6a93262 57 * @param ptr function pointer
DieterGraef 24:5396b6a93262 58 */
DieterGraef 24:5396b6a93262 59 inline void attachButtonEvent(void (*ptr)(uint8_t buttons)) {
DieterGraef 24:5396b6a93262 60 if (ptr != NULL) {
DieterGraef 24:5396b6a93262 61 onButtonUpdate = ptr;
DieterGraef 24:5396b6a93262 62 }
DieterGraef 24:5396b6a93262 63 }
DieterGraef 24:5396b6a93262 64
DieterGraef 24:5396b6a93262 65 /**
DieterGraef 24:5396b6a93262 66 * Attach a callback called when the X axis value changes
DieterGraef 24:5396b6a93262 67 *
DieterGraef 24:5396b6a93262 68 * @param ptr function pointer
DieterGraef 24:5396b6a93262 69 */
DieterGraef 24:5396b6a93262 70 inline void attachXEvent(void (*ptr)(int8_t x)) {
DieterGraef 24:5396b6a93262 71 if (ptr != NULL) {
DieterGraef 24:5396b6a93262 72 onXUpdate = ptr;
DieterGraef 24:5396b6a93262 73 }
DieterGraef 24:5396b6a93262 74 }
DieterGraef 24:5396b6a93262 75
DieterGraef 24:5396b6a93262 76 /**
DieterGraef 24:5396b6a93262 77 * Attach a callback called when the Y axis value changes
DieterGraef 24:5396b6a93262 78 *
DieterGraef 24:5396b6a93262 79 * @param ptr function pointer
DieterGraef 24:5396b6a93262 80 */
DieterGraef 24:5396b6a93262 81 inline void attachYEvent(void (*ptr)(int8_t y)) {
DieterGraef 24:5396b6a93262 82 if (ptr != NULL) {
DieterGraef 24:5396b6a93262 83 onYUpdate = ptr;
DieterGraef 24:5396b6a93262 84 }
DieterGraef 24:5396b6a93262 85 }
DieterGraef 24:5396b6a93262 86
DieterGraef 24:5396b6a93262 87 /**
DieterGraef 24:5396b6a93262 88 * Attach a callback called when the Z axis value changes (scrolling)
DieterGraef 24:5396b6a93262 89 *
DieterGraef 24:5396b6a93262 90 * @param ptr function pointer
DieterGraef 24:5396b6a93262 91 */
DieterGraef 24:5396b6a93262 92 inline void attachZEvent(void (*ptr)(int8_t z)) {
DieterGraef 24:5396b6a93262 93 if (ptr != NULL) {
DieterGraef 24:5396b6a93262 94 onZUpdate = ptr;
DieterGraef 24:5396b6a93262 95 }
DieterGraef 24:5396b6a93262 96 }
DieterGraef 24:5396b6a93262 97
DieterGraef 24:5396b6a93262 98 /**
DieterGraef 24:5396b6a93262 99 * Attach a callback called when a keyboard event is received
DieterGraef 24:5396b6a93262 100 *
DieterGraef 24:5396b6a93262 101 * @param ptr function pointer
DieterGraef 24:5396b6a93262 102 */
DieterGraef 24:5396b6a93262 103
DieterGraef 24:5396b6a93262 104 inline void attachKb(void (*ptr)(uint8_t key)) {
DieterGraef 24:5396b6a93262 105 if (ptr != NULL) {
DieterGraef 24:5396b6a93262 106 onKey = ptr;
DieterGraef 24:5396b6a93262 107 }
DieterGraef 24:5396b6a93262 108 }
DieterGraef 24:5396b6a93262 109
DieterGraef 24:5396b6a93262 110 /**
DieterGraef 24:5396b6a93262 111 * Attach a callback called when a keyboard event is received
DieterGraef 24:5396b6a93262 112 *
DieterGraef 24:5396b6a93262 113 * @param ptr function pointer
DieterGraef 24:5396b6a93262 114 */
DieterGraef 24:5396b6a93262 115 inline void attachKb(void (*ptr)(uint8_t keyCode, uint8_t modifier)) {
DieterGraef 24:5396b6a93262 116 if (ptr != NULL) {
DieterGraef 24:5396b6a93262 117 onKeyCode = ptr;
DieterGraef 24:5396b6a93262 118 }
DieterGraef 24:5396b6a93262 119 }
DieterGraef 24:5396b6a93262 120
DieterGraef 24:5396b6a93262 121 protected:
DieterGraef 24:5396b6a93262 122 //From IUSBEnumerator
DieterGraef 24:5396b6a93262 123 virtual void setVidPid(uint16_t vid, uint16_t pid);
DieterGraef 24:5396b6a93262 124 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
DieterGraef 24:5396b6a93262 125 virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used
DieterGraef 24:5396b6a93262 126
DieterGraef 24:5396b6a93262 127 private:
DieterGraef 24:5396b6a93262 128 USBHost * host;
DieterGraef 24:5396b6a93262 129 USBDeviceConnected * dev;
DieterGraef 24:5396b6a93262 130 USBEndpoint * int_in_mouse;
DieterGraef 24:5396b6a93262 131 USBEndpoint * int_in_kb;
DieterGraef 24:5396b6a93262 132 uint8_t report[20];
DieterGraef 24:5396b6a93262 133 bool dev_connected;
DieterGraef 24:5396b6a93262 134 bool mouse_device_found;
DieterGraef 24:5396b6a93262 135 bool kb_device_found;
DieterGraef 24:5396b6a93262 136 int mouse_intf;
DieterGraef 24:5396b6a93262 137 int kb_intf;
DieterGraef 24:5396b6a93262 138
DieterGraef 24:5396b6a93262 139 uint8_t buttons;
DieterGraef 24:5396b6a93262 140 int8_t x;
DieterGraef 24:5396b6a93262 141 int8_t y;
DieterGraef 24:5396b6a93262 142 int8_t z;
DieterGraef 24:5396b6a93262 143
DieterGraef 24:5396b6a93262 144 void rxHandlerMouse();
DieterGraef 24:5396b6a93262 145 void rxHandlerKb();
DieterGraef 24:5396b6a93262 146 void (*onUpdate)(uint8_t buttons, int8_t x, int8_t y, int8_t z);
DieterGraef 24:5396b6a93262 147 void (*onButtonUpdate)(uint8_t buttons);
DieterGraef 24:5396b6a93262 148 void (*onXUpdate)(int8_t x);
DieterGraef 24:5396b6a93262 149 void (*onYUpdate)(int8_t y);
DieterGraef 24:5396b6a93262 150 void (*onZUpdate)(int8_t z);
DieterGraef 24:5396b6a93262 151 void (*onKey)(uint8_t key);
DieterGraef 24:5396b6a93262 152 void (*onKeyCode)(uint8_t key, uint8_t modifier);
DieterGraef 24:5396b6a93262 153
DieterGraef 24:5396b6a93262 154 int report_id;
DieterGraef 24:5396b6a93262 155 void init();
DieterGraef 24:5396b6a93262 156 };
DieterGraef 24:5396b6a93262 157
DieterGraef 24:5396b6a93262 158 #endif
DieterGraef 24:5396b6a93262 159
DieterGraef 24:5396b6a93262 160
DieterGraef 24:5396b6a93262 161 #endif /* USBHOSTMOUSEKEYBOARD_H_INCLUDED */