More accurate calculation of the time button pressed

Change-Id: Idebf6bbf43fcf8ec06f12f20092451f96f541d7b
This commit is contained in:
Andrey Perminov 2020-04-17 13:03:21 -07:00
parent 06b99d7e18
commit 4be3f43031
4 changed files with 37 additions and 51 deletions

View File

@ -44,7 +44,7 @@ typedef enum {
} platform_led_status_t; } platform_led_status_t;
/** /**
* @brief Initializes board functions * @brief Initializes board
* *
* @param[in] speed_hz Baudrate. * @param[in] speed_hz Baudrate.
*/ */

View File

@ -90,8 +90,3 @@ void platform_set_led(platform_led_status_t color)
{ {
board_set_led(color); board_set_led(color);
} }
uint32_t platform_get_button_press_time(void)
{
return board_get_button_press_time();
}

View File

@ -28,8 +28,6 @@
/** Board button used for app control */ /** Board button used for app control */
#define BMLITE_BUTTON 3 #define BMLITE_BUTTON 3
static uint32_t btn_press_start;
static uint32_t btn_pressed;
static void _board_set_leds(uint8_t color) static void _board_set_leds(uint8_t color)
{ {
@ -48,45 +46,4 @@ static void _board_set_leds(uint8_t color)
void board_set_led(platform_led_status_t color) void board_set_led(platform_led_status_t color)
{ {
_board_set_leds(color); _board_set_leds(color);
// if (status == FPC_HAL_LED_STATUS_OFF) {
// board_set_leds(0);
// } else if (status == FPC_HAL_LED_STATUS_SOLID) {
// board_set_leds(color);
// platform_timebase_busy_wait(LED_SOLID_ON_TIME_MS);
// board_set_leds(0);
// } else if (status == FPC_HAL_LED_STATUS_ON) {
// board_set_leds(color);
// } else {
// uint32_t led_status = (uint32_t)(status - FPC_HAL_LED_STATUS_ONE_BLINK + 1);
// for (uint32_t i = 0; i < (led_status * 2); i++) {
// if (i % 2 == 0) {
// board_set_leds(0x8 + color);
// } else {
// board_set_leds(0x8);
// }
// platform_timebase_busy_wait(LED_BLINK_TIME_MS);
// }
// }
}
uint32_t board_get_button_press_time()
{
uint32_t time = 0;
if (bsp_board_button_state_get(BMLITE_BUTTON)) {
if (btn_pressed == 0) {
btn_press_start = platform_timebase_get_tick();
btn_pressed = 1;
}
} else { // Btn released
if (btn_pressed) {
uint32_t curr_time = platform_timebase_get_tick();
if (curr_time > btn_press_start) {
time = curr_time - btn_press_start;
} else {
time = curr_time + ~btn_press_start + 1;
}
btn_pressed = 0;
}
}
return time;
} }

View File

@ -26,10 +26,19 @@
#include "nrf.h" #include "nrf.h"
#include "nrf_drv_timer.h" #include "nrf_drv_timer.h"
#include "nrfx_config.h" #include "nrfx_config.h"
#include "boards.h"
const nrf_drv_timer_t TIMER_LED = NRF_DRV_TIMER_INSTANCE(0); const nrf_drv_timer_t TIMER_LED = NRF_DRV_TIMER_INSTANCE(0);
static volatile uint32_t systick; static volatile uint32_t systick;
volatile uint32_t button_pressed_time = 0;
static uint32_t btn_press_start;
static uint32_t btn_pressed = 0;
#define BMLITE_BUTTON 3
static void check_buttons();
/** /**
* @brief Handler for timer events. * @brief Handler for timer events.
@ -41,6 +50,7 @@ static void timer_event_handler(nrf_timer_event_t event_type, void* p_context)
{ {
case NRF_TIMER_EVENT_COMPARE0: case NRF_TIMER_EVENT_COMPARE0:
systick++; systick++;
check_buttons();
break; break;
default: default:
@ -49,7 +59,6 @@ static void timer_event_handler(nrf_timer_event_t event_type, void* p_context)
} }
} }
void platform_timebase_init(void) void platform_timebase_init(void)
{ {
uint32_t time_ms = 1; //Time (in miliseconds) between consecutive compare events. uint32_t time_ms = 1; //Time (in miliseconds) between consecutive compare events.
@ -66,7 +75,6 @@ void platform_timebase_init(void)
&TIMER_LED, NRF_TIMER_CC_CHANNEL0, time_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true); &TIMER_LED, NRF_TIMER_CC_CHANNEL0, time_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);
nrf_drv_timer_enable(&TIMER_LED); nrf_drv_timer_enable(&TIMER_LED);
} }
void platform_timebase_busy_wait(uint32_t delay) void platform_timebase_busy_wait(uint32_t delay)
@ -87,3 +95,29 @@ uint32_t platform_timebase_get_tick(void)
{ {
return systick; return systick;
} }
static void check_buttons()
{
if (bsp_board_button_state_get(BMLITE_BUTTON)) {
if (btn_pressed == 0) {
btn_press_start = systick;
btn_pressed = 1;
}
} else { // Btn released
if (btn_pressed) {
if (systick > btn_press_start) {
button_pressed_time = systick - btn_press_start;
} else {
button_pressed_time = systick + ~btn_press_start + 1;
}
btn_pressed = 0;
}
}
}
uint32_t platform_get_button_press_time()
{
uint32_t time = button_pressed_time;
button_pressed_time = 0;
return time;
}