USBHost : USBHostHID , USBHostHub fix indentation.

pull/4231/head
Michel Jaouen 2017-05-04 11:40:59 +02:00
parent bae762eb24
commit fa2abd22c7
6 changed files with 145 additions and 97 deletions

View File

@ -19,71 +19,81 @@
#if USBHOST_KEYBOARD
static uint8_t keymap[4][0x39] = {
{ 0, 0, 0, 0, 'a', 'b' /*0x05*/,
'c', 'd', 'e', 'f', 'g' /*0x0a*/,
'h', 'i', 'j', 'k', 'l'/*0x0f*/,
'm', 'n', 'o', 'p', 'q'/*0x14*/,
'r', 's', 't', 'u', 'v'/*0x19*/,
'w', 'x', 'y', 'z', '1'/*0x1E*/,
'2', '3', '4', '5', '6'/*0x23*/,
'7', '8', '9', '0', 0x0A /*enter*/, /*0x28*/
0x1B /*escape*/, 0x08 /*backspace*/, 0x09/*tab*/, 0x20/*space*/, '-', /*0x2d*/
'=', '[', ']', '\\', '#', /*0x32*/
';', '\'', 0, ',', '.', /*0x37*/
'/'},
{
0, 0, 0, 0, 'a', 'b' /*0x05*/,
'c', 'd', 'e', 'f', 'g' /*0x0a*/,
'h', 'i', 'j', 'k', 'l'/*0x0f*/,
'm', 'n', 'o', 'p', 'q'/*0x14*/,
'r', 's', 't', 'u', 'v'/*0x19*/,
'w', 'x', 'y', 'z', '1'/*0x1E*/,
'2', '3', '4', '5', '6'/*0x23*/,
'7', '8', '9', '0', 0x0A /*enter*/, /*0x28*/
0x1B /*escape*/, 0x08 /*backspace*/, 0x09/*tab*/, 0x20/*space*/, '-', /*0x2d*/
'=', '[', ']', '\\', '#', /*0x32*/
';', '\'', 0, ',', '.', /*0x37*/
'/'
},
/* CTRL MODIFIER */
{ 0, 0, 0, 0, 0, 0 /*0x05*/,
0, 0, 0, 0, 0 /*0x0a*/,
0, 0, 0, 0, 0/*0x0f*/,
0, 0, 0, 0, 0/*0x14*/,
0, 0, 0, 0, 0/*0x19*/,
0, 0, 0, 0, 0/*0x1E*/,
0, 0, 0, 0, 0/*0x23*/,
0, 0, 0, 0, 0 /*enter*/, /*0x28*/
0, 0, 0, 0, 0, /*0x2d*/
0, 0, 0, 0, 0, /*0x32*/
0, 0, 0, 0, 0, /*0x37*/
0},
{
0, 0, 0, 0, 0, 0 /*0x05*/,
0, 0, 0, 0, 0 /*0x0a*/,
0, 0, 0, 0, 0/*0x0f*/,
0, 0, 0, 0, 0/*0x14*/,
0, 0, 0, 0, 0/*0x19*/,
0, 0, 0, 0, 0/*0x1E*/,
0, 0, 0, 0, 0/*0x23*/,
0, 0, 0, 0, 0 /*enter*/, /*0x28*/
0, 0, 0, 0, 0, /*0x2d*/
0, 0, 0, 0, 0, /*0x32*/
0, 0, 0, 0, 0, /*0x37*/
0
},
/* SHIFT MODIFIER */
{ 0, 0, 0, 0, 'A', 'B' /*0x05*/,
'C', 'D', 'E', 'F', 'G' /*0x0a*/,
'H', 'I', 'J', 'K', 'L'/*0x0f*/,
'M', 'N', 'O', 'P', 'Q'/*0x14*/,
'R', 'S', 'T', 'U', 'V'/*0x19*/,
'W', 'X', 'Y', 'Z', '!'/*0x1E*/,
'@', '#', '$', '%', '^'/*0x23*/,
'&', '*', '(', ')', 0, /*0x28*/
0, 0, 0, 0, 0, /*0x2d*/
'+', '{', '}', '|', '~', /*0x32*/
':', '"', 0, '<', '>', /*0x37*/
'?'},
{
0, 0, 0, 0, 'A', 'B' /*0x05*/,
'C', 'D', 'E', 'F', 'G' /*0x0a*/,
'H', 'I', 'J', 'K', 'L'/*0x0f*/,
'M', 'N', 'O', 'P', 'Q'/*0x14*/,
'R', 'S', 'T', 'U', 'V'/*0x19*/,
'W', 'X', 'Y', 'Z', '!'/*0x1E*/,
'@', '#', '$', '%', '^'/*0x23*/,
'&', '*', '(', ')', 0, /*0x28*/
0, 0, 0, 0, 0, /*0x2d*/
'+', '{', '}', '|', '~', /*0x32*/
':', '"', 0, '<', '>', /*0x37*/
'?'
},
/* ALT MODIFIER */
{ 0, 0, 0, 0, 0, 0 /*0x05*/,
0, 0, 0, 0, 0 /*0x0a*/,
0, 0, 0, 0, 0/*0x0f*/,
0, 0, 0, 0, 0/*0x14*/,
0, 0, 0, 0, 0/*0x19*/,
0, 0, 0, 0, 0/*0x1E*/,
0, 0, 0, 0, 0/*0x23*/,
0, 0, 0, 0, 0 /*enter*/, /*0x28*/
0, 0, 0, 0, 0, /*0x2d*/
0, 0, 0, 0, 0, /*0x32*/
0, 0, 0, 0, 0, /*0x37*/
0}
{
0, 0, 0, 0, 0, 0 /*0x05*/,
0, 0, 0, 0, 0 /*0x0a*/,
0, 0, 0, 0, 0/*0x0f*/,
0, 0, 0, 0, 0/*0x14*/,
0, 0, 0, 0, 0/*0x19*/,
0, 0, 0, 0, 0/*0x1E*/,
0, 0, 0, 0, 0/*0x23*/,
0, 0, 0, 0, 0 /*enter*/, /*0x28*/
0, 0, 0, 0, 0, /*0x2d*/
0, 0, 0, 0, 0, /*0x32*/
0, 0, 0, 0, 0, /*0x37*/
0
}
};
USBHostKeyboard::USBHostKeyboard() {
USBHostKeyboard::USBHostKeyboard()
{
host = USBHost::getHostInst();
init();
}
void USBHostKeyboard::init() {
void USBHostKeyboard::init()
{
dev = NULL;
int_in = NULL;
report_id = 0;
@ -94,7 +104,8 @@ void USBHostKeyboard::init() {
keyboard_device_found = false;
}
bool USBHostKeyboard::connected() {
bool USBHostKeyboard::connected()
{
return dev_connected;
}
@ -109,8 +120,9 @@ bool USBHostKeyboard::connect()
for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
if ((dev = host->getDevice(i)) != NULL) {
if (host->enumerate(dev, this))
if (host->enumerate(dev, this)) {
break;
}
if (keyboard_device_found) {
{
@ -121,8 +133,9 @@ bool USBHostKeyboard::connect()
int_in = dev->getEndpoint(keyboard_intf, INTERRUPT_ENDPOINT, IN);
if (!int_in)
if (!int_in) {
break;
}
USB_INFO("New Keyboard device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, keyboard_intf);
dev->setName("Keyboard", keyboard_intf);
@ -141,7 +154,8 @@ bool USBHostKeyboard::connect()
return false;
}
void USBHostKeyboard::rxHandler() {
void USBHostKeyboard::rxHandler()
{
int len = int_in->getLengthTransferred();
int index = (len == 9) ? 1 : 0;
int len_listen = int_in->getSize();
@ -157,8 +171,9 @@ void USBHostKeyboard::rxHandler() {
(*onKeyCode)(report[index + 2], modifier);
}
}
if (dev && int_in)
if (dev && int_in) {
host->interruptRead(dev, int_in, report, len_listen, false);
}
}
/*virtual*/ void USBHostKeyboard::setVidPid(uint16_t vid, uint16_t pid)
@ -169,9 +184,9 @@ void USBHostKeyboard::rxHandler() {
/*virtual*/ bool USBHostKeyboard::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
{
if ((keyboard_intf == -1) &&
(intf_class == HID_CLASS) &&
(intf_subclass == 0x01) &&
(intf_protocol == 0x01)) {
(intf_class == HID_CLASS) &&
(intf_subclass == 0x01) &&
(intf_protocol == 0x01)) {
keyboard_intf = intf_nb;
return true;
}

View File

@ -26,7 +26,8 @@
/**
* A class to communicate a USB keyboard
*/
class USBHostKeyboard : public IUSBEnumerator {
class USBHostKeyboard : public IUSBEnumerator
{
public:
/**
@ -53,7 +54,8 @@ public:
*
* @param ptr function pointer
*/
inline void attach(void (*ptr)(uint8_t key)) {
inline void attach(void (*ptr)(uint8_t key))
{
if (ptr != NULL) {
onKey = ptr;
}
@ -64,7 +66,8 @@ public:
*
* @param ptr function pointer
*/
inline void attach(void (*ptr)(uint8_t keyCode, uint8_t modifier)) {
inline void attach(void (*ptr)(uint8_t keyCode, uint8_t modifier))
{
if (ptr != NULL) {
onKeyCode = ptr;
}

View File

@ -18,12 +18,14 @@
#if USBHOST_MOUSE
USBHostMouse::USBHostMouse() {
USBHostMouse::USBHostMouse()
{
host = USBHost::getHostInst();
init();
}
void USBHostMouse::init() {
void USBHostMouse::init()
{
dev = NULL;
int_in = NULL;
onUpdate = NULL;
@ -42,7 +44,8 @@ void USBHostMouse::init() {
z = 0;
}
bool USBHostMouse::connected() {
bool USBHostMouse::connected()
{
return dev_connected;
}
@ -57,8 +60,9 @@ bool USBHostMouse::connect()
for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
if ((dev = host->getDevice(i)) != NULL) {
if(host->enumerate(dev, this))
if(host->enumerate(dev, this)) {
break;
}
if (mouse_device_found) {
{
/* As this is done in a specific thread
@ -66,8 +70,9 @@ bool USBHostMouse::connect()
* disconnect in usb process during the device registering */
USBHost::Lock Lock(host);
int_in = dev->getEndpoint(mouse_intf, INTERRUPT_ENDPOINT, IN);
if (!int_in)
if (!int_in) {
break;
}
USB_INFO("New Mouse device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, mouse_intf);
dev->setName("Mouse", mouse_intf);
@ -81,10 +86,12 @@ bool USBHostMouse::connect()
}
int ret=host->interruptRead(dev, int_in, report, len_listen, false);
MBED_ASSERT((ret==USB_TYPE_OK) || (ret ==USB_TYPE_PROCESSING) || (ret == USB_TYPE_FREE));
if ((ret==USB_TYPE_OK) || (ret ==USB_TYPE_PROCESSING))
if ((ret==USB_TYPE_OK) || (ret ==USB_TYPE_PROCESSING)) {
dev_connected = true;
if (ret == USB_TYPE_FREE)
}
if (ret == USB_TYPE_FREE) {
dev_connected = false;
}
return true;
}
}
@ -93,7 +100,8 @@ bool USBHostMouse::connect()
return false;
}
void USBHostMouse::rxHandler() {
void USBHostMouse::rxHandler()
{
int len_listen = int_in->getSize();
if (onUpdate) {
@ -126,8 +134,9 @@ void USBHostMouse::rxHandler() {
len_listen = sizeof(report);
}
if (dev)
if (dev) {
host->interruptRead(dev, int_in, report, len_listen, false);
}
}
/*virtual*/ void USBHostMouse::setVidPid(uint16_t vid, uint16_t pid)
@ -138,9 +147,9 @@ void USBHostMouse::rxHandler() {
/*virtual*/ bool USBHostMouse::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
{
if ((mouse_intf == -1) &&
(intf_class == HID_CLASS) &&
(intf_subclass == 0x01) &&
(intf_protocol == 0x02)) {
(intf_class == HID_CLASS) &&
(intf_subclass == 0x01) &&
(intf_protocol == 0x02)) {
mouse_intf = intf_nb;
return true;
}

View File

@ -26,7 +26,8 @@
/**
* A class to communicate a USB mouse
*/
class USBHostMouse : public IUSBEnumerator {
class USBHostMouse : public IUSBEnumerator
{
public:
/**
@ -53,7 +54,8 @@ public:
*
* @param ptr function pointer
*/
inline void attachEvent(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;
}
@ -64,7 +66,8 @@ public:
*
* @param ptr function pointer
*/
inline void attachButtonEvent(void (*ptr)(uint8_t buttons)) {
inline void attachButtonEvent(void (*ptr)(uint8_t buttons))
{
if (ptr != NULL) {
onButtonUpdate = ptr;
}
@ -75,7 +78,8 @@ public:
*
* @param ptr function pointer
*/
inline void attachXEvent(void (*ptr)(int8_t x)) {
inline void attachXEvent(void (*ptr)(int8_t x))
{
if (ptr != NULL) {
onXUpdate = ptr;
}
@ -86,7 +90,8 @@ public:
*
* @param ptr function pointer
*/
inline void attachYEvent(void (*ptr)(int8_t y)) {
inline void attachYEvent(void (*ptr)(int8_t y))
{
if (ptr != NULL) {
onYUpdate = ptr;
}
@ -97,7 +102,8 @@ public:
*
* @param ptr function pointer
*/
inline void attachZEvent(void (*ptr)(int8_t z)) {
inline void attachZEvent(void (*ptr)(int8_t z))
{
if (ptr != NULL) {
onZUpdate = ptr;
}

View File

@ -50,12 +50,14 @@
#define C_PORT_OVER_CURRENT (1 << 19)
#define C_PORT_RESET (1 << 20)
USBHostHub::USBHostHub() {
USBHostHub::USBHostHub()
{
host = NULL;
init();
}
void USBHostHub::init() {
void USBHostHub::init()
{
dev_connected = false;
dev = NULL;
int_in = NULL;
@ -70,7 +72,8 @@ void USBHostHub::init() {
}
}
void USBHostHub::setHost(USBHost * host_) {
void USBHostHub::setHost(USBHost * host_)
{
host = host_;
}
@ -129,7 +132,8 @@ bool USBHostHub::connect(USBDeviceConnected * dev)
return false;
}
void USBHostHub::disconnect() {
void USBHostHub::disconnect()
{
init();
}
@ -141,9 +145,9 @@ void USBHostHub::disconnect() {
/*virtual*/ bool USBHostHub::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
{
if ((hub_intf == -1) &&
(intf_class == HUB_CLASS) &&
(intf_subclass == 0) &&
(intf_protocol == 0)) {
(intf_class == HUB_CLASS) &&
(intf_subclass == 0) &&
(intf_protocol == 0)) {
hub_intf = intf_nb;
return true;
}
@ -161,15 +165,18 @@ void USBHostHub::disconnect() {
return false;
}
void USBHostHub::deviceConnected(USBDeviceConnected * dev) {
void USBHostHub::deviceConnected(USBDeviceConnected * dev)
{
device_children[dev->getPort() - 1] = dev;
}
void USBHostHub::deviceDisconnected(USBDeviceConnected * dev) {
void USBHostHub::deviceDisconnected(USBDeviceConnected * dev)
{
device_children[dev->getPort() - 1] = NULL;
}
void USBHostHub::hubDisconnected() {
void USBHostHub::hubDisconnected()
{
for (uint8_t i = 0; i < MAX_HUB_PORT; i++) {
if (device_children[i] != NULL) {
host->freeDevice(device_children[i]);
@ -177,7 +184,8 @@ void USBHostHub::hubDisconnected() {
}
}
void USBHostHub::rxHandler() {
void USBHostHub::rxHandler()
{
uint32_t status;
if (int_in) {
if (int_in->getState() == USB_TYPE_IDLE) {
@ -217,7 +225,8 @@ void USBHostHub::rxHandler() {
}
}
void USBHostHub::portReset(uint8_t port) {
void USBHostHub::portReset(uint8_t port)
{
// reset port
uint32_t status;
USB_DBG("reset port %d on hub: %p [this: %p]", port, dev, this)
@ -228,10 +237,12 @@ void USBHostHub::portReset(uint8_t port) {
while(1) {
status = getPortStatus(port);
/* disconnection since reset request */
if (!(status & PORT_CONNECTION))
if (!(status & PORT_CONNECTION)) {
break;
if (status & (PORT_ENABLE | PORT_RESET))
}
if (status & (PORT_ENABLE | PORT_RESET)) {
break;
}
if (status & PORT_OVER_CURRENT) {
USB_ERR("OVER CURRENT DETECTED\r\n");
clearPortFeature(PORT_OVER_CURRENT, port);
@ -242,7 +253,8 @@ void USBHostHub::portReset(uint8_t port) {
}
}
void USBHostHub::setPortFeature(uint32_t feature, uint8_t port) {
void USBHostHub::setPortFeature(uint32_t feature, uint8_t port)
{
host->controlWrite( dev,
USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE | USB_RECIPIENT_ENDPOINT,
SET_FEATURE,
@ -252,7 +264,8 @@ void USBHostHub::setPortFeature(uint32_t feature, uint8_t port) {
0);
}
void USBHostHub::clearPortFeature(uint32_t feature, uint8_t port) {
void USBHostHub::clearPortFeature(uint32_t feature, uint8_t port)
{
host->controlWrite( dev,
USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE | USB_RECIPIENT_ENDPOINT,
CLEAR_FEATURE,
@ -262,7 +275,8 @@ void USBHostHub::clearPortFeature(uint32_t feature, uint8_t port) {
0);
}
uint32_t USBHostHub::getPortStatus(uint8_t port) {
uint32_t USBHostHub::getPortStatus(uint8_t port)
{
uint32_t st;
host->controlRead( dev,
USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE | USB_RECIPIENT_ENDPOINT,

View File

@ -31,7 +31,8 @@ class USBEndpoint;
/**
* A class to use a USB Hub
*/
class USBHostHub : public IUSBEnumerator {
class USBHostHub : public IUSBEnumerator
{
public:
/**
* Constructor