@@ -28,6 +28,8 @@ LOG_MODULE_REGISTER(app, CONFIG_BLE_HIDS_KEYBOARD_SAMPLE_LOG_LEVEL);
28
28
29
29
#define BASE_USB_HID_SPEC_VERSION 0x0101
30
30
31
+ #define INPUT_REPORT_KEYS_CTRL_CODE_MIN 224 /**< Control key codes - required 8 of them */
32
+ #define INPUT_REPORT_KEYS_CTRL_CODE_MAX 231 /**< Control key codes - required 8 of them */
31
33
#define INPUT_REPORT_KEYS_INDEX 0 /**< Index of Input Report. */
32
34
#define INPUT_REPORT_KEYS_MAX_LEN 8 /**< Maximum length of the Input Report characteristic. */
33
35
#define INPUT_REP_REF_ID 0 /**< Id of reference to Keyboard Input Report. */
@@ -50,6 +52,8 @@ BLE_ADV_DEF(ble_adv);
50
52
/* BLE Connection handle */
51
53
static uint16_t conn_handle = BLE_CONN_HANDLE_INVALID ;
52
54
55
+ static uint8_t keys_report [INPUT_REPORT_KEYS_MAX_LEN ];
56
+
53
57
static void on_ble_evt (const ble_evt_t * evt , void * ctx )
54
58
{
55
59
uint32_t nrf_err ;
@@ -260,44 +264,107 @@ static uint32_t hids_init(void)
260
264
return ble_hids_init (& ble_hids , & hids_config );
261
265
}
262
266
263
- static int register_key ( struct ble_hids * hids , const char key , bool pressed )
267
+ static uint8_t button_ctrl_code_get ( uint8_t key )
264
268
{
265
- uint32_t nrf_err ;
266
- uint8_t report [INPUT_REPORT_KEYS_MAX_LEN ] = {};
269
+ if (INPUT_REPORT_KEYS_CTRL_CODE_MIN <= key && key <= INPUT_REPORT_KEYS_CTRL_CODE_MAX ) {
270
+ return (uint8_t )(1U << (key - INPUT_REPORT_KEYS_CTRL_CODE_MIN ));
271
+ }
272
+ return 0 ;
273
+ }
274
+
275
+
276
+ static int key_set (struct ble_hids * hids , uint8_t * report , size_t report_size , uint8_t key )
277
+ {
278
+ uint8_t ctrl_mask = button_ctrl_code_get (key );
279
+
280
+ if (ctrl_mask ) {
281
+ report [0 ] |= ctrl_mask ;
282
+ return 0 ;
283
+ }
284
+ for (size_t i = 0 ; i < (report_size - 2 ); ++ i ) {
285
+ if (report [i + 2 ] == 0 ) {
286
+ report [i + 2 ] = key ;
287
+ return 0 ;
288
+ }
289
+ }
290
+ /* All slots busy */
291
+ return - EBUSY ;
292
+ }
293
+
267
294
268
- #define DATA_OFFSET 2
295
+ static int key_clear (struct ble_hids * hids , uint8_t * report , size_t report_size , uint8_t key )
296
+ {
297
+ uint8_t ctrl_mask = button_ctrl_code_get (key );
298
+
299
+ if (ctrl_mask ) {
300
+ report [0 ] &= ~ctrl_mask ;
301
+ return 0 ;
302
+ }
303
+ for (size_t i = 0 ; i < (report_size - 2 ); ++ i ) {
304
+ if (report [i + 2 ] == key ) {
305
+ report [i + 2 ] = 0 ;
306
+ return 0 ;
307
+ }
308
+ }
309
+
310
+ /* Key not found */
311
+ return - EINVAL ;
312
+ }
313
+
314
+ static int on_key_press (struct ble_hids * hids , const char key , bool pressed )
315
+ {
316
+ uint32_t nrf_err ;
269
317
270
318
if (pressed ) {
271
- memcpy (report + DATA_OFFSET , & key , sizeof (key ));
319
+ key_set (hids , keys_report , sizeof (keys_report ), key );
320
+ } else {
321
+ key_clear (hids , keys_report , sizeof (keys_report ), key );
272
322
}
273
323
274
- nrf_err = ble_hids_inp_rep_send (hids , conn_handle , INPUT_REPORT_KEYS_INDEX , & report ,
275
- sizeof (report ));
276
- if (nrf_err ) {
277
- printk ("Failed to send input report, nrf_error %#x" , nrf_err );
324
+ nrf_err = ble_hids_inp_rep_send (hids , conn_handle , INPUT_REPORT_KEYS_INDEX , keys_report ,
325
+ sizeof (keys_report ));
326
+ if (nrf_err != NRF_SUCCESS ) {
327
+ LOG_ERR ("Failed to send input report, nrf_error %#x" , nrf_err );
278
328
}
279
329
280
330
return 0 ;
281
331
}
282
332
283
333
static void button_handler (uint8_t pin , uint8_t action )
284
334
{
335
+ static const char hello_world_str [] = {
336
+ 0x0b , /* Key h */
337
+ 0x08 , /* Key e */
338
+ 0x0f , /* Key l */
339
+ 0x0f , /* Key l */
340
+ 0x12 , /* Key o */
341
+ 0x28 , /* Key Return */
342
+ };
343
+ static const char * chr = hello_world_str ;
344
+
285
345
if (conn_handle == BLE_CONN_HANDLE_INVALID ) {
286
346
return ;
287
347
}
288
348
289
349
switch (pin ) {
290
350
case BOARD_PIN_BTN_0 :
291
- register_key (& ble_hids , 0x04 , action == BTN_PRESSED ); /* Key a */
351
+ if (action == BTN_PRESSED ) {
352
+ on_key_press (& ble_hids , * chr , true);
353
+ } else {
354
+ on_key_press (& ble_hids , * chr , false);
355
+ if (++ chr == (hello_world_str + sizeof (hello_world_str ))) {
356
+ chr = hello_world_str ;
357
+ }
358
+ }
292
359
break ;
293
360
case BOARD_PIN_BTN_1 :
294
- register_key (& ble_hids , 0x05 , action == BTN_PRESSED ); /* Key b */
361
+ on_key_press (& ble_hids , 0xE1 , action == BTN_PRESSED ); /* Shift */
295
362
break ;
296
363
case BOARD_PIN_BTN_2 :
297
- register_key (& ble_hids , 0x06 , action == BTN_PRESSED ); /* Key c */
364
+ on_key_press (& ble_hids , 0x06 , action == BTN_PRESSED ); /* Key c */
298
365
break ;
299
366
case BOARD_PIN_BTN_3 :
300
- register_key (& ble_hids , 0x07 , action == BTN_PRESSED ); /* Key d */
367
+ on_key_press (& ble_hids , 0x07 , action == BTN_PRESSED ); /* Key d */
301
368
break ;
302
369
}
303
370
}
0 commit comments