From 546f2a1165555819bf3ef09fe1afcfbcbb084dd9 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 24 Jul 2018 00:10:22 +0700 Subject: nrf52840 example work with boot keyboard --- examples/device/nrf52840/src/main.c | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) (limited to 'examples/device') diff --git a/examples/device/nrf52840/src/main.c b/examples/device/nrf52840/src/main.c index 04d07f0a6..cc18cce24 100644 --- a/examples/device/nrf52840/src/main.c +++ b/examples/device/nrf52840/src/main.c @@ -106,10 +106,29 @@ void usb_hid_task(void) { if ( !tud_hid_keyboard_busy() ) { - static bool toggle = false; // send either A or B + // Poll every 10ms + static tu_timeout_t tm = { .start = 0, .interval = 10 }; - tud_hid_keyboard_send_char( toggle ? 'A' : 'B' ); - toggle = !toggle; + if ( !tu_timeout_expired(&tm) ) return; // not enough time + tu_timeout_reset(&tm); + + uint32_t bt = board_buttons(); + + if ( bt ) + { + uint8_t keycode[6] = { 0 }; + + for(uint8_t i=0; i < 6; i++) + { + if ( bt & (1 << i) ) keycode[i] = HID_KEY_A + i; + } + + tud_hid_keyboard_send_keycode(0, keycode); + }else + { + // Null means empty (all zeroes) report + tud_hid_keyboard_send_report(NULL); + } } if ( !tud_hid_mouse_busy() ) @@ -143,15 +162,11 @@ void tud_cdc_rx_cb(uint8_t itf) //--------------------------------------------------------------------+ void led_blinking_task(void) { - enum { BLINK_INTEVAL = 1000 }; - + static tu_timeout_t tm = { .start = 0, .interval = 1000 }; // Blink every 1000 ms static bool led_state = false; - static uint32_t last_blink = 0; - - // not enough time - if ( last_blink + BLINK_INTEVAL > tusb_hal_millis() ) return; - last_blink += BLINK_INTEVAL; + if ( !tu_timeout_expired(&tm) ) return; // not enough time + tu_timeout_reset(&tm); board_led_control(BOARD_LED0, led_state); led_state = 1 - led_state; // toggle -- cgit v1.3.1