USBHost custom library.
Dependents: GR-PEACH_HVC-P2_sample_client mbed-os-storage-access GR-PEACH_Digital_Signage GR-PEACH_Audio_Playback_Sample ... more
Fork of USBHost by
Revision 5:e48791a1ef18, committed 2013-03-13
- Comitter:
- samux
- Date:
- Wed Mar 13 10:23:01 2013 +0000
- Parent:
- 4:b320d68e98e7
- Child:
- 6:1571e517a91b
- Commit message:
- add button, x, y, z event callbacks
Changed in this revision
--- a/USBHostHID/USBHostKeyboard.cpp Tue Mar 12 17:23:37 2013 +0000
+++ b/USBHostHID/USBHostKeyboard.cpp Wed Mar 13 10:23:01 2013 +0000
@@ -103,13 +103,11 @@
bool USBHostKeyboard::connect() {
- U8 i;
-
if (dev_connected) {
return true;
}
- for (i = 0; i < MAX_DEVICE_CONNECTED; i++) {
+ for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
if ((dev = host->getDevice(i)) != NULL) {
if (host->enumerate(dev, this))
@@ -149,11 +147,11 @@
if (key && onKey) {
(*onKey)(key);
}
- if (onKeyCode) {
+ if ((report[index + 2] || modifier) && onKeyCode) {
(*onKeyCode)(report[index + 2], modifier);
}
}
- if (dev != NULL)
+ if (dev && int_in)
host->interruptRead(dev, int_in, report, len_listen, false);
}
--- a/USBHostHID/USBHostMouse.cpp Tue Mar 12 17:23:37 2013 +0000
+++ b/USBHostHID/USBHostMouse.cpp Wed Mar 13 10:23:01 2013 +0000
@@ -29,10 +29,19 @@
dev = NULL;
int_in = NULL;
onUpdate = NULL;
+ onButtonUpdate = NULL;
+ onXUpdate = NULL;
+ onYUpdate = NULL;
+ onZUpdate = NULL;
report_id = 0;
dev_connected = false;
mouse_device_found = false;
mouse_intf = -1;
+
+ buttons = 0;
+ x = 0;
+ y = 0;
+ z = 0;
}
bool USBHostMouse::connected() {
@@ -40,14 +49,12 @@
}
bool USBHostMouse::connect() {
-
- U8 i;
-
+
if (dev_connected) {
return true;
}
- for (i = 0; i < MAX_DEVICE_CONNECTED; i++) {
+ for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
if ((dev = host->getDevice(i)) != NULL) {
if(host->enumerate(dev, this))
@@ -78,10 +85,34 @@
void USBHostMouse::rxHandler() {
int len_listen = int_in->getSize();
int len = int_in->getLengthTransferred();
+
if (onUpdate) {
(*onUpdate)(report[0] & 0x07, report[1], report[2], report[3]);
}
- if (dev != NULL)
+
+ if (onButtonUpdate && (buttons != (report[0] & 0x07))) {
+ (*onButtonUpdate)(report[0] & 0x07);
+ }
+
+ if (onXUpdate && (x != report[1])) {
+ (*onXUpdate)(report[1]);
+ }
+
+ if (onYUpdate && (y != report[2])) {
+ (*onYUpdate)(report[2]);
+ }
+
+ if (onZUpdate && (z != report[3])) {
+ (*onZUpdate)(report[3]);
+ }
+
+ // update mouse state
+ buttons = report[0] & 0x07;
+ x = report[1];
+ y = report[2];
+ z = report[3];
+
+ if (dev)
host->interruptRead(dev, int_in, report, len_listen, false);
}
--- a/USBHostHID/USBHostMouse.h Tue Mar 12 17:23:37 2013 +0000
+++ b/USBHostHID/USBHostMouse.h Wed Mar 13 10:23:01 2013 +0000
@@ -55,11 +55,55 @@
*
* @param ptr function pointer
*/
- inline void attach(void (*ptr)(uint8_t buttons, int8_t x, int8_t y, int8_t z)) {
+ inline void attachEvent(void (*ptr)(uint8_t buttons, int8_t x, int8_t y, int8_t z)) {
if (ptr != NULL) {
onUpdate = ptr;
}
}
+
+ /**
+ * Attach a callback called when the button state changes
+ *
+ * @param ptr function pointer
+ */
+ inline void attachButtonEvent(void (*ptr)(uint8_t buttons)) {
+ if (ptr != NULL) {
+ onButtonUpdate = ptr;
+ }
+ }
+
+ /**
+ * Attach a callback called when the X axis value changes
+ *
+ * @param ptr function pointer
+ */
+ inline void attachXEvent(void (*ptr)(int8_t x)) {
+ if (ptr != NULL) {
+ onXUpdate = ptr;
+ }
+ }
+
+ /**
+ * Attach a callback called when the Y axis value changes
+ *
+ * @param ptr function pointer
+ */
+ inline void attachYEvent(void (*ptr)(int8_t y)) {
+ if (ptr != NULL) {
+ onYUpdate = ptr;
+ }
+ }
+
+ /**
+ * Attach a callback called when the Z axis value changes (scrolling)
+ *
+ * @param ptr function pointer
+ */
+ inline void attachZEvent(void (*ptr)(int8_t z)) {
+ if (ptr != NULL) {
+ onZUpdate = ptr;
+ }
+ }
protected:
//From IUSBEnumerator
@@ -77,8 +121,17 @@
bool mouse_device_found;
int mouse_intf;
+ uint8_t buttons;
+ int8_t x;
+ int8_t y;
+ int8_t z;
+
void rxHandler();
void (*onUpdate)(uint8_t buttons, int8_t x, int8_t y, int8_t z);
+ void (*onButtonUpdate)(uint8_t buttons);
+ void (*onXUpdate)(int8_t x);
+ void (*onYUpdate)(int8_t y);
+ void (*onZUpdate)(int8_t z);
int report_id;
void init();
};
--- a/USBHostMSD/USBHostMSD.cpp Tue Mar 12 17:23:37 2013 +0000
+++ b/USBHostMSD/USBHostMSD.cpp Wed Mar 13 10:23:01 2013 +0000
@@ -60,13 +60,12 @@
bool USBHostMSD::connect()
{
- U8 i;
if (dev_connected) {
return true;
}
- for (i = 0; i < MAX_DEVICE_CONNECTED; i++) {
+ for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
if ((dev = host->getDevice(i)) != NULL) {
USB_DBG("Trying to connect MSD device\r\n");
--- a/USBHostSerial/USBHostSerial.cpp Tue Mar 12 17:23:37 2013 +0000
+++ b/USBHostSerial/USBHostSerial.cpp Wed Mar 13 10:23:01 2013 +0000
@@ -52,7 +52,7 @@
if (dev_connected) {
return true;
}
- for (int i = 0; i < MAX_DEVICE_CONNECTED; i++) {
+ for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
if ((dev = host->getDevice(i)) != NULL) {
USB_DBG("Trying to connect serial device\r\n");
