Hello,
My application is based on nrf52832 and ble_uart example. I recently added system off function when long pressed BUTTON_1 successfully. To do that I slightly changed the code like below.
the side effect of the change make different behavior. With the previous code, the device wakes up with any 4 buttons after sleeping system off.
The current code has an issue. the device only wakes up only with BUTTON_1 or 2 press after sleeping (system off) with long press.
I have a sleep timer implementation for system off to save energy. If the device sleep with the sleep timer, any 4 button can wake up the device with current code and previous code. It also wakes up with any button when device sleep when no connection after advertising duration expires for both code. I like to wake up with any button like the previous code. Can you please advise? Thanks.
////previous code///////////////////////////////////////////////////
void bsp_event_handler(bsp_event_t event)
{
uint32_t err_code;
switch (event)
{
case BSP_EVENT_KEY_0: //PLAY
{
//do something
} break;
case BSP_EVENT_KEY_1:
{
//do something
} break;
case BSP_EVENT_KEY_2:
{
//do something
} break;
case BSP_EVENT_KEY_3:
{
//do something
} break;
case BSP_EVENT_DISCONNECT:
err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
if (err_code != NRF_ERROR_INVALID_STATE)
{
APP_ERROR_CHECK(err_code);
} break;
case BSP_EVENT_SLEEP:
sleep_mode_enter();
break;
default:
break;
}
}
static void ble_buttons_leds_init(bool * p_erase_bonds)
{
bsp_event_t startup_event;
uint32_t err_code;
err_code = bsp_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS, bsp_event_handler);
APP_ERROR_CHECK(err_code);
err_code = bsp_btn_ble_init(NULL, &startup_event);
APP_ERROR_CHECK(err_code);
*p_erase_bonds = (startup_event == BSP_EVENT_CLEAR_BONDING_DATA);
}
////current code /////////////////////////////////////////////////////////
void do_nothing_handler(bsp_event_t event)
{
UNUSED_PARAMETER(event);
}
static void m_button_event_handler(uint8_t pin_no, uint8_t button_action)
{
uint32_t err_code;
static uint8_t current_long_push_pin_no;
switch (pin_no)
{
case BUTTON_1: //long push to sleep
switch (button_action)
{
case APP_BUTTON_PUSH:
// m_press_is_button = true;
if (m_button_ticks_count == 0)
{
//do something
err_code = app_timer_start(m_button_tick_timer_id, LONG_PRESS_INTERVAL, NULL);
APP_ERROR_CHECK(err_code);
m_button_ticks_count++;
}
break;
case APP_BUTTON_RELEASE:
err_code = app_timer_stop(m_button_tick_timer_id);
APP_ERROR_CHECK(err_code);
m_button_ticks_count = 0;
break;
}
break;
case BUTTON_2:
if(button_action == APP_BUTTON_PUSH)
{
//do something
}
break;
case BUTTON_3:
if(button_action == APP_BUTTON_PUSH)
{
//do something
}
break;
case BUTTON_4:
if(button_action == APP_BUTTON_PUSH)
{
//do something
}
break;
}
}
static void ble_buttons_leds_init(bool * p_erase_bonds)
{
uint32_t err_code = 0;
static app_button_cfg_t buttons[BUTTONS_NUMBER] = {
{BUTTON_1, APP_BUTTON_ACTIVE_LOW, NRF_GPIO_PIN_PULLUP, m_button_event_handler},
{BUTTON_2, APP_BUTTON_ACTIVE_LOW, NRF_GPIO_PIN_PULLUP, m_button_event_handler},
{BUTTON_3, APP_BUTTON_ACTIVE_LOW, NRF_GPIO_PIN_PULLUP, m_button_event_handler},
{BUTTON_4, APP_BUTTON_ACTIVE_LOW, NRF_GPIO_PIN_PULLUP, m_button_event_handler}
};
err_code = app_button_init((app_button_cfg_t *)buttons,
sizeof(buttons) / sizeof(buttons[0]),
APP_TIMER_TICKS(50));
APP_ERROR_CHECK(err_code);
err_code = app_button_enable();
APP_ERROR_CHECK(err_code);
//timer for long push determination
err_code = app_timer_create(&m_button_tick_timer_id, APP_TIMER_MODE_REPEATED, m_button_timer_handler);
APP_ERROR_CHECK(err_code);
//bsp_board_init(BSP_INIT_LEDS);
bsp_init(BSP_INIT_LEDS, do_nothing_handler);
bsp_event_t startup_event;
err_code = bsp_btn_ble_init(NULL, &startup_event);
APP_ERROR_CHECK(err_code);
*p_erase_bonds = (startup_event == BSP_EVENT_CLEAR_BONDING_DATA);
}
static void long_press_shutdown_timeout_handler(void *unused)
{
uint32_t err_code;
UNUSED_PARAMETER(unused);
err_code = app_timer_stop_all();
APP_ERROR_CHECK(err_code);
sleep_mode_enter();
}
static void m_button_timer_handler(void * p_context)
{
uint32_t err_code;
UNUSED_PARAMETER(p_context);
NRF_LOG_INFO("Long Press button count %d", m_button_ticks_count);
m_button_ticks_count++;
if (m_button_ticks_count > 1)
{
app_button_disable();
err_code = app_timer_stop(m_button_tick_timer_id);
APP_ERROR_CHECK(err_code);
NRF_LOG_INFO("Prepare to shutdown the device");
err_code = app_timer_start(m_app_shutdown_timer_id,
SHUTDOWN_INTERVAL, NULL);
APP_ERROR_CHECK(err_code);
err_code = app_timer_start(m_shutdown_blink_timer_id,
LP_SHUTDOWN_BLINK_INTERVAL, NULL);
APP_ERROR_CHECK(err_code);
}
}
static void timers_init(void)
{
ret_code_t err_code = app_timer_init();
APP_ERROR_CHECK(err_code);
err_code = app_timer_create(&m_app_shutdown_timer_id,
APP_TIMER_MODE_SINGLE_SHOT,
long_press_shutdown_timeout_handler);
APP_ERROR_CHECK(err_code);
}