bsp_btn_ant.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include "bsp_btn_ant.h"
  13. #include <stdint.h>
  14. #include "bsp.h"
  15. #include "ant_stack_handler_types.h"
  16. #include "ant_parameters.h"
  17. #include "app_trace.h"
  18. #define BTN_ID_WAKEUP 3 /**< ID of button used to wake up the application. */
  19. #define BTN_ID_SLEEP 3 /**< ID of button used to put the application into sleep mode. */
  20. #define BTN_ACTION_SLEEP BSP_BUTTON_ACTION_RELEASE /**< Button action used to put the application into sleep mode. */
  21. /**@brief This macro will return from the current function if err_code
  22. * is not NRF_SUCCESS or NRF_ERROR_INVALID_PARAM.
  23. */
  24. #define RETURN_ON_ERROR_NOT_INVALID_PARAM(err_code) \
  25. do \
  26. { \
  27. if (((err_code) != NRF_SUCCESS) && ((err_code) != NRF_ERROR_INVALID_PARAM)) \
  28. { \
  29. return err_code; \
  30. } \
  31. } \
  32. while(0)
  33. /**@brief This macro will return from the current function if err_code
  34. * is not NRF_SUCCESS or NRF_ERROR_NOT_SUPPORTED.
  35. */
  36. #define RETURN_ON_ERROR_NOT_NOT_SUPPORTED(err_code) \
  37. do \
  38. { \
  39. if (((err_code) != NRF_SUCCESS) && ((err_code) != NRF_ERROR_NOT_SUPPORTED)) \
  40. { \
  41. return err_code; \
  42. } \
  43. } \
  44. while(0)
  45. static bool m_connected = false; /**< Notify if channel is connected. */
  46. /**@brief Function for configuring the buttons for connection.
  47. *
  48. * @retval NRF_SUCCESS Configured successfully.
  49. * @return A propagated error code.
  50. */
  51. static uint32_t connection_buttons_configure(void)
  52. {
  53. uint32_t err_code;
  54. err_code = bsp_event_to_button_action_assign(BTN_ID_SLEEP,
  55. BTN_ACTION_SLEEP,
  56. BSP_EVENT_DEFAULT);
  57. RETURN_ON_ERROR_NOT_INVALID_PARAM(err_code);
  58. return NRF_SUCCESS;
  59. }
  60. /**@brief Function for configuring the buttons for searching.
  61. *
  62. * @retval NRF_SUCCESS Configured successfully.
  63. * @return A propagated error code.
  64. */
  65. static uint32_t searching_buttons_configure(void)
  66. {
  67. uint32_t err_code;
  68. err_code = bsp_event_to_button_action_assign(BTN_ID_SLEEP,
  69. BTN_ACTION_SLEEP,
  70. BSP_EVENT_SLEEP);
  71. RETURN_ON_ERROR_NOT_INVALID_PARAM(err_code);
  72. return NRF_SUCCESS;
  73. }
  74. uint32_t bsp_btn_ant_sleep_mode_prepare(void)
  75. {
  76. uint32_t err_code = bsp_wakeup_buttons_set(1 << BTN_ID_WAKEUP);
  77. RETURN_ON_ERROR_NOT_NOT_SUPPORTED(err_code);
  78. return NRF_SUCCESS;
  79. }
  80. void bsp_btn_ant_on_ant_evt(ant_evt_t * p_ant_evt)
  81. {
  82. uint32_t err_code;
  83. switch (p_ant_evt->event)
  84. {
  85. case EVENT_RX:
  86. if (!m_connected)
  87. {
  88. err_code = connection_buttons_configure();
  89. APP_ERROR_CHECK(err_code);
  90. }
  91. m_connected = true;
  92. break;
  93. case EVENT_RX_FAIL_GO_TO_SEARCH:
  94. m_connected = false;
  95. err_code = searching_buttons_configure();
  96. APP_ERROR_CHECK(err_code);
  97. break;
  98. default:
  99. break;
  100. }
  101. }
  102. uint32_t bsp_btn_ant_init(void)
  103. {
  104. uint32_t err_code = NRF_SUCCESS;
  105. if (!m_connected)
  106. {
  107. err_code = searching_buttons_configure();
  108. }
  109. else
  110. {
  111. err_code = connection_buttons_configure();
  112. }
  113. return err_code;
  114. }