This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Writing words to usb composite keyboard in usbd_hid_composite

Hello,

I want to write multiple words using the usb keyboard. I am working with usbd_hid_composite example on SDK_15.0.0_a53641a. I am able to write just a letter using API app_usbd_hid_kbd_key_control, but I am looking for an API to write a buffer (e.g., uint8_t Buffer[SIZE]).

Thanks

Parents
  • Hi,

    The USB-HID spec sets several limitations when it comes to sending words. You can max send 6 letters/keys in one HID transfer(Input Report), AND it's not possible to send two of the same letters in one HID transfer. Because of this, we have not made any API to do something like what you describe.

    That said, it should be possible to do something like this:

    Snippet from bsp_event_callback():

            case CONCAT_2(BSP_EVENT_KEY_, BTN_KBD_LETTER):
                UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, APP_USBD_HID_KBD_A, true));
                UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, APP_USBD_HID_KBD_B, true));
                UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, APP_USBD_HID_KBD_C, true));
                UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, APP_USBD_HID_KBD_D, true));
                break;
            case CONCAT_2(BSP_USER_EVENT_RELEASE_, BTN_KBD_LETTER):
                UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, APP_USBD_HID_KBD_A, false));
                UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, APP_USBD_HID_KBD_B, false));
                UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, APP_USBD_HID_KBD_C, false));
                UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, APP_USBD_HID_KBD_D, false));
                break;
    
            default:
                return; // no implementation needed

  • Thank you for the reply. I tested this and it worked for usbd_hid_composite example. However, I need an API to just type "ABCD" when BLE is connected. The problem is that I do not have any event like button release. I first called the above APIs with true on BLE connection, and after five seconds, call the APIs with false. Did not work. I then put nrf_delay_ms(2) between true and false, didn't work either (Just wrote the first letter).

    UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, APP_USBD_HID_KBD_A, true));
    UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, APP_USBD_HID_KBD_B, true));
    UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, APP_USBD_HID_KBD_C, true));
    UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, APP_USBD_HID_KBD_D, true));

    nrf_delay_ms(2)

    UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, APP_USBD_HID_KBD_A, false));
    UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, APP_USBD_HID_KBD_B, false));
    UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, APP_USBD_HID_KBD_C, false));
    UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, APP_USBD_HID_KBD_D, false));

    Can you please give me some hints how to write "ABCD" on connection event? Is there any trick to overcome the limitations and write the same letters? My ultimate goal is to write "Successful". 

    Thanks

Reply
  • Thank you for the reply. I tested this and it worked for usbd_hid_composite example. However, I need an API to just type "ABCD" when BLE is connected. The problem is that I do not have any event like button release. I first called the above APIs with true on BLE connection, and after five seconds, call the APIs with false. Did not work. I then put nrf_delay_ms(2) between true and false, didn't work either (Just wrote the first letter).

    UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, APP_USBD_HID_KBD_A, true));
    UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, APP_USBD_HID_KBD_B, true));
    UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, APP_USBD_HID_KBD_C, true));
    UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, APP_USBD_HID_KBD_D, true));

    nrf_delay_ms(2)

    UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, APP_USBD_HID_KBD_A, false));
    UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, APP_USBD_HID_KBD_B, false));
    UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, APP_USBD_HID_KBD_C, false));
    UNUSED_RETURN_VALUE(app_usbd_hid_kbd_key_control(&m_app_hid_kbd, APP_USBD_HID_KBD_D, false));

    Can you please give me some hints how to write "ABCD" on connection event? Is there any trick to overcome the limitations and write the same letters? My ultimate goal is to write "Successful". 

    Thanks

Children
  • Hi,

    Can you please give me some hints how to write "ABCD" on connection event? Is there any trick to overcome the limitations and write the same letters? My ultimate goal is to write "Successful".

    You need to split things up in multiple transfers. Let’s say that on the connection event, call a function send_string(). In the send string function, you start sending the first transaction and set some flag high, let’s call it m_pending_transaction together with a counter variable of where you are in your buffer. When this transaction is finished, you will get the event APP_USBD_HID_USER_EVT_IN_REPORT_DONE in hid_kbd_user_ev_handler(). When you get this event, you check if m_pending_transaction is true, and if it’s true, you check the counter to see what transaction you need to send next, you increment the counter and start the next transfer. When the counter reaches the end of your buffer, you know that you are finished with sending the string, and you set counter=0 and  m_pending_transaction to false.

  • Thanks for the hints. I am now able to write 6 characters.

Related