Skip to content

Commit 947d0a8

Browse files
samples: bluetooth: hids_keyboard: update button fetaures to match NCS
Update button features to match NCS sample. Work in progress. Signed-off-by: Eivind Jølsgard <[email protected]>
1 parent 3dd8292 commit 947d0a8

File tree

1 file changed

+80
-13
lines changed
  • samples/bluetooth/ble_hids_keyboard/src

1 file changed

+80
-13
lines changed

samples/bluetooth/ble_hids_keyboard/src/main.c

Lines changed: 80 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ LOG_MODULE_REGISTER(app, CONFIG_BLE_HIDS_KEYBOARD_SAMPLE_LOG_LEVEL);
2828

2929
#define BASE_USB_HID_SPEC_VERSION 0x0101
3030

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 */
3133
#define INPUT_REPORT_KEYS_INDEX 0 /**< Index of Input Report. */
3234
#define INPUT_REPORT_KEYS_MAX_LEN 8 /**< Maximum length of the Input Report characteristic. */
3335
#define INPUT_REP_REF_ID 0 /**< Id of reference to Keyboard Input Report. */
@@ -50,6 +52,8 @@ BLE_ADV_DEF(ble_adv);
5052
/* BLE Connection handle */
5153
static uint16_t conn_handle = BLE_CONN_HANDLE_INVALID;
5254

55+
static uint8_t keys_report[INPUT_REPORT_KEYS_MAX_LEN];
56+
5357
static void on_ble_evt(const ble_evt_t *evt, void *ctx)
5458
{
5559
uint32_t nrf_err;
@@ -260,44 +264,107 @@ static uint32_t hids_init(void)
260264
return ble_hids_init(&ble_hids, &hids_config);
261265
}
262266

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)
264268
{
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+
267294

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;
269317

270318
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);
272322
}
273323

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

280330
return 0;
281331
}
282332

283333
static void button_handler(uint8_t pin, uint8_t action)
284334
{
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+
285345
if (conn_handle == BLE_CONN_HANDLE_INVALID) {
286346
return;
287347
}
288348

289349
switch (pin) {
290350
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+
}
292359
break;
293360
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 */
295362
break;
296363
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 */
298365
break;
299366
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 */
301368
break;
302369
}
303370
}

0 commit comments

Comments
 (0)