This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

how to emulate input one key and then release it for USB HID keyboard emulation using cc2540

Other Parts Discussed in Thread: CC2540

we need to input some CC2540 ADC results into PC as text into a text file, and we use the USB HID keyboard emulation to input the ADC results.

we start from 3 example projects: HIDAdvRemoteDongle , HIDAdvRemote and HIDEmuKbd.  we only use the USB HID part and neglected BLE functionalities

and the resultant USB HID part of th project is as the following codes.

when ADC result comes out,  osal_set_event(hidappTaskId, HIDAPP_EVT_REPORT_RETRY) is called to send the result as  key press and release sequence: as key pressed for a digit,  call hidKeyboardSendReport( HID_KEYBOARD_RESERVED, k ), and followed by a  hidKeyboardSendReport( HID_KEYBOARD_RESERVED, 0 )  as key released for the same digit.

but the the USB HID keyboard input one key/digit continuously for the int16 ADC result, as if one key is pressed and never released. how to input one key/digit and then release it ?

if ( events & HIDAPP_EVT_REPORT_RETRY )
  {
    uint8 i, k, str[7];
    sprintf(str,"%d",adcValue);       // int16 to string, at maximum need 1 - sign, 5 digit, 1 \0 as end. 7 bytes is enough
    for (i=0;str[i] != 0 && i < 7;i++) {
      // Report retries event
      if (str[i] == '-') k = HID_KEYBOARD_MINUS;  // - for negative result
      else k = (str[i]+1)%10+30;    //numbers to keys
      if(hidKeyboardSendReport( HID_KEYBOARD_RESERVED, k ))  // key pressed
        hidKeyboardSendReport( HID_KEYBOARD_RESERVED, 0 );   // key released
    }

    return (events ^ HIDAPP_EVT_REPORT_RETRY);
  }


/*********************************************************************
 * @fn      hidKeyboardSendReport
 *
 * @brief   Build and send a HID Keyboard report.
 *
 * @param   modifiers - HID modifiers.
 * @param   keycode - HID keycode.
 *
 * @return  none
 */
static uint8 hidKeyboardSendReport( uint8 modifiers, uint8 keycode )
{
  static uint8 prevKeycode = HAL_KEY_CODE_NOKEY;
  static uint8 prevModifiers = HID_KEYBOARD_RESERVED;

  // Only send the report if something meaningful to report
  if ( ( prevKeycode != keycode ) || ( prevModifiers != modifiers ) )
  {
    uint8 buf[HID_KEYBOARD_IN_RPT_LEN];

    // No need to include Report Id
    buf[0] = modifiers;         // Modifier keys
    buf[1] = 0;                 // Reserved
    buf[2] = keycode;           // Keycode 1
    buf[3] = 0;                 // Keycode 2
    buf[4] = 0;                 // Keycode 3
    buf[5] = 0;                 // Keycode 4
    buf[6] = 0;                 // Keycode 5
    buf[7] = 0;                 // Keycode 6

    // Save the new values
    prevKeycode = keycode;
    prevModifiers = modifiers;
    return ( hidSendHidInReport(buf, USB_HID_KBD_EP, HID_KEYBOARD_IN_RPT_LEN) );
  }
}

  • I will have someone from the HealthTech team get back to you on this.
  • Hi,

    I am not sure about how you handle reports in USB HID, but have tried sending the HID_KEYBOARD_RESERVED key after some delay(approximately 20 ms) after you send the required key? If the reports are not queued inside(or queue not flushed one by one) USB HID task event, then the reserve key report might be lost(as it may not get send after the key report) and that may be the reason the host is printing the key continuously.

    Having a small delay would help in keeping the HID reports distinct for both Key report and Release report.

    Regards,
    Arun
  • thank you very much! 20ms delay resolves the problem.