hathach 5 лет назад
Родитель
Сommit
35aee4a6af
3 измененных файлов с 65 добавлено и 22 удалено
  1. 58 5
      examples/host/cdc_msc_hid/src/main.c
  2. 6 16
      src/class/hid/hid_host.c
  3. 1 1
      src/host/usbh.c

+ 58 - 5
examples/host/cdc_msc_hid/src/main.c

@@ -110,21 +110,74 @@ void cdc_task(void)
 // USB HID
 //--------------------------------------------------------------------+
 #if CFG_TUH_HID_KEYBOARD
-void hid_task(void)
+
+uint8_t const keycode2ascii[128][2] =  { HID_KEYCODE_TO_ASCII };
+
+// look up new key in previous keys
+static inline bool find_key_in_report(hid_keyboard_report_t const *p_report, uint8_t keycode)
+{
+  for(uint8_t i=0; i<6; i++)
+  {
+    if (p_report->keycode[i] == keycode)  return true;
+  }
+
+  return false;
+}
+
+static inline void process_kbd_report(hid_keyboard_report_t const *p_new_report)
 {
+  static hid_keyboard_report_t prev_report = { 0, 0, {0} }; // previous report to check key released
+
+  //------------- example code ignore control (non-printable) key affects -------------//
+  for(uint8_t i=0; i<6; i++)
+  {
+    if ( p_new_report->keycode[i] )
+    {
+      if ( find_key_in_report(&prev_report, p_new_report->keycode[i]) )
+      {
+        // exist in previous report means the current key is holding
+      }else
+      {
+        // not existed in previous report means the current key is pressed
+        bool const is_shift =  p_new_report->modifier & (KEYBOARD_MODIFIER_LEFTSHIFT | KEYBOARD_MODIFIER_RIGHTSHIFT);
+        uint8_t ch = keycode2ascii[p_new_report->keycode[i]][is_shift ? 1 : 0];
+        putchar(ch);
+        if ( ch == '\r' ) putchar('\n'); // added new line for enter key
+      }
+    }
+    // TODO example skips key released
+  }
+
+  prev_report = *p_new_report;
+}
+
+CFG_TUSB_MEM_SECTION static hid_keyboard_report_t usb_keyboard_report;
 
+void hid_task(void)
+{
+  uint8_t const addr = 1;
+  if ( tuh_hid_keyboard_is_mounted(addr) )
+  {
+    if ( !tuh_hid_keyboard_is_busy(addr) )
+    {
+      process_kbd_report(&usb_keyboard_report);
+      tuh_hid_keyboard_get_report(addr, &usb_keyboard_report);
+    }
+  }
 }
 
 void tuh_hid_keyboard_mounted_cb(uint8_t dev_addr)
 {
   // application set-up
-  printf("\na Keyboard device (address %d) is mounted\n", dev_addr);
+  printf("A Keyboard device (address %d) is mounted\r\n", dev_addr);
+
+  tuh_hid_keyboard_get_report(dev_addr, &usb_keyboard_report);
 }
 
 void tuh_hid_keyboard_unmounted_cb(uint8_t dev_addr)
 {
   // application tear-down
-  printf("\na Keyboard device (address %d) is unmounted\n", dev_addr);
+  printf("A Keyboard device (address %d) is unmounted\r\n", dev_addr);
 }
 
 // invoked ISR context
@@ -139,13 +192,13 @@ void tuh_hid_keyboard_isr(uint8_t dev_addr, xfer_result_t event)
 void tuh_hid_mouse_mounted_cb(uint8_t dev_addr)
 {
   // application set-up
-  printf("\na Mouse device (address %d) is mounted\n", dev_addr);
+  printf("A Mouse device (address %d) is mounted\r\n", dev_addr);
 }
 
 void tuh_hid_mouse_unmounted_cb(uint8_t dev_addr)
 {
   // application tear-down
-  printf("\na Mouse device (address %d) is unmounted\n", dev_addr);
+  printf("A Mouse device (address %d) is unmounted\r\n", dev_addr);
 }
 
 // invoked ISR context

+ 6 - 16
src/class/hid/hid_host.c

@@ -67,11 +67,11 @@ tusb_error_t hidh_interface_get_report(uint8_t dev_addr, void * report, hidh_int
 {
   //------------- parameters validation -------------//
   // TODO change to use is configured function
-  TU_ASSERT (TUSB_DEVICE_STATE_CONFIGURED == tuh_device_get_state(dev_addr), TUSB_ERROR_DEVICE_NOT_READY);
-  TU_VERIFY (report, TUSB_ERROR_INVALID_PARA);
-  TU_ASSERT (!hcd_edpt_busy(dev_addr, p_hid->ep_in), TUSB_ERROR_INTERFACE_IS_BUSY);
+  TU_ASSERT(TUSB_DEVICE_STATE_CONFIGURED == tuh_device_get_state(dev_addr), TUSB_ERROR_DEVICE_NOT_READY);
+  TU_VERIFY(report, TUSB_ERROR_INVALID_PARA);
+  TU_ASSERT(!hcd_edpt_busy(dev_addr, p_hid->ep_in), TUSB_ERROR_INTERFACE_IS_BUSY);
 
-  TU_ASSERT_ERR( hcd_pipe_xfer(dev_addr, p_hid->ep_in, report, p_hid->report_size, true) ) ;
+  TU_ASSERT( hcd_pipe_xfer(dev_addr, p_hid->ep_in, report, p_hid->report_size, true) ) ;
 
   return TUSB_ERROR_NONE;
 }
@@ -81,18 +81,6 @@ tusb_error_t hidh_interface_get_report(uint8_t dev_addr, void * report, hidh_int
 //--------------------------------------------------------------------+
 #if CFG_TUH_HID_KEYBOARD
 
-#if 0
-#define EXPAND_KEYCODE_TO_ASCII(keycode, ascii, shift_modified)  \
-  [0][keycode] = ascii,\
-  [1][keycode] = shift_modified,\
-
-// TODO size of table should be a macro for application to check boundary
-uint8_t const hid_keycode_to_ascii_tbl[2][128] =
-{
-    HID_KEYCODE_TABLE(EXPAND_KEYCODE_TO_ASCII)
-};
-#endif
-
 static hidh_interface_t keyboardh_data[CFG_TUSB_HOST_DEVICE_MAX]; // does not have addr0, index = dev_address-1
 
 //------------- KEYBOARD PUBLIC API (parameter validation required) -------------//
@@ -215,6 +203,7 @@ bool hidh_open_subtask(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t c
     if ( HID_PROTOCOL_KEYBOARD == p_interface_desc->bInterfaceProtocol)
     {
       TU_ASSERT( hidh_interface_open(rhport, dev_addr, p_interface_desc->bInterfaceNumber, p_endpoint_desc, &keyboardh_data[dev_addr-1]) );
+      TU_LOG2_HEX(keyboardh_data[dev_addr-1].ep_in);
       tuh_hid_keyboard_mounted_cb(dev_addr);
     } else
     #endif
@@ -223,6 +212,7 @@ bool hidh_open_subtask(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t c
     if ( HID_PROTOCOL_MOUSE == p_interface_desc->bInterfaceProtocol)
     {
       TU_ASSERT ( hidh_interface_open(rhport, dev_addr, p_interface_desc->bInterfaceNumber, p_endpoint_desc, &mouseh_data[dev_addr-1]) );
+      TU_LOG2_HEX(mouseh_data[dev_addr-1].ep_in);
       tuh_hid_mouse_mounted_cb(dev_addr);
     } else
     #endif

+ 1 - 1
src/host/usbh.c

@@ -284,7 +284,7 @@ void hcd_event_xfer_complete(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t ev
 
     if (usbh_class_drivers[drv_id].isr)
     {
-      TU_LOG2("%s isr\r\n", usbh_class_drivers[drv_id].name);
+      //TU_LOG2("%s isr\r\n", usbh_class_drivers[drv_id].name);
       usbh_class_drivers[drv_id].isr(dev_addr, ep_addr, event, xferred_bytes);
     }
     else