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) );
}
}