led_softblink.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * Copyright (c) 2015 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /** @file
  41. *
  42. * @defgroup led_softblink LED softblink
  43. * @{
  44. * @ingroup app_common
  45. *
  46. * @brief Module for generating a changing pulse-width modulated output signal that is used to smoothly blink LEDs.
  47. *
  48. * @details This module provides an LED softblink implementation using timers and GPIO.
  49. *
  50. * LED softblink needs one timer. It can use any number of output channels that are available.
  51. *
  52. * Only one instance of LED softblink can run at a time.
  53. */
  54. #ifndef LED_SOFTBLINK_H__
  55. #define LED_SOFTBLINK_H__
  56. #include <stdbool.h>
  57. #include <stdint.h>
  58. #include "sdk_errors.h"
  59. #include "nrf_gpio.h"
  60. #ifdef __cplusplus
  61. extern "C" {
  62. #endif
  63. /**
  64. * @brief Structure holding the initialization parameters.
  65. */
  66. typedef struct
  67. {
  68. bool active_high; /**< Activate negative polarity. */
  69. uint8_t duty_cycle_max; /**< Maximum impulse width. */
  70. uint8_t duty_cycle_min; /**< Minimum impulse width. */
  71. uint8_t duty_cycle_step; /**< Cycle step. */
  72. uint32_t off_time_ticks; /**< Ticks to stay in low impulse state. */
  73. uint32_t on_time_ticks; /**< Ticks to stay in high impulse state. */
  74. uint32_t leds_pin_bm; /**< Mask of used LEDs. */
  75. NRF_GPIO_Type * p_leds_port; /**< Port of used LEDs mask. */
  76. }led_sb_init_params_t;
  77. /**
  78. * @name Default settings
  79. * @brief Default settings for LED softblink.
  80. * @{
  81. */
  82. #define LED_SB_INIT_PARAMS_ACTIVE_HIGH false
  83. #define LED_SB_INIT_PARAMS_DUTY_CYCLE_MAX 220
  84. #define LED_SB_INIT_PARAMS_DUTY_CYCLE_MIN 0
  85. #define LED_SB_INIT_PARAMS_DUTY_CYCLE_STEP 5
  86. #define LED_SB_INIT_PARAMS_OFF_TIME_TICKS 65536
  87. #define LED_SB_INIT_PARAMS_ON_TIME_TICKS 0
  88. #define LED_SB_INIT_PARAMS_LEDS_PIN_BM(mask) (mask)
  89. #define LED_SB_INIT_PARAMS_LEDS_PORT NRF_GPIO
  90. /** @} */
  91. /**
  92. * @brief LED softblink default configuration.
  93. */
  94. #define LED_SB_INIT_DEFAULT_PARAMS(mask) \
  95. { \
  96. .active_high = LED_SB_INIT_PARAMS_ACTIVE_HIGH, \
  97. .duty_cycle_max = LED_SB_INIT_PARAMS_DUTY_CYCLE_MAX, \
  98. .duty_cycle_min = LED_SB_INIT_PARAMS_DUTY_CYCLE_MIN, \
  99. .duty_cycle_step = LED_SB_INIT_PARAMS_DUTY_CYCLE_STEP, \
  100. .off_time_ticks = LED_SB_INIT_PARAMS_OFF_TIME_TICKS, \
  101. .on_time_ticks = LED_SB_INIT_PARAMS_ON_TIME_TICKS, \
  102. .leds_pin_bm = LED_SB_INIT_PARAMS_LEDS_PIN_BM(mask), \
  103. .p_leds_port = LED_SB_INIT_PARAMS_LEDS_PORT \
  104. }
  105. /**
  106. * @brief Function for initializing LED softblink.
  107. *
  108. * @param[in] p_init_params Pointer to the initialization structure.
  109. *
  110. * @return Values returned by @ref app_timer_create.
  111. */
  112. ret_code_t led_softblink_init(led_sb_init_params_t const * p_init_params);
  113. /**
  114. * @brief Function for starting to blink LEDs.
  115. *
  116. * @param[in] leds_pin_bit_mask Bit mask containing the pins for the LEDs to be blinked.
  117. *
  118. * @return Values returned by @ref app_timer_start.
  119. */
  120. ret_code_t led_softblink_start(uint32_t leds_pin_bit_mask);
  121. /**
  122. * @brief Function for stopping to blink LEDs.
  123. *
  124. * @return Values returned by @ref app_timer_stop.
  125. */
  126. ret_code_t led_softblink_stop(void);
  127. /**
  128. * @brief Function for setting the off time.
  129. *
  130. * This function configures the time that the LEDs will be off between each blink.
  131. *
  132. * @param[in] off_time_ticks Off time in ticks.
  133. *
  134. */
  135. void led_softblink_off_time_set(uint32_t off_time_ticks);
  136. /**
  137. * @brief Function for setting the on time.
  138. *
  139. * This function configures the time that the LEDs will be on between each blink.
  140. *
  141. * @param[in] on_time_ticks On time in ticks.
  142. *
  143. */
  144. void led_softblink_on_time_set(uint32_t on_time_ticks);
  145. /**
  146. * @brief Function for uninitializing LED softblink.
  147. *
  148. * @retval NRF_SUCCESS If LED softblink was uninitialized successfully.
  149. */
  150. ret_code_t led_softblink_uninit(void);
  151. #ifdef __cplusplus
  152. }
  153. #endif
  154. #endif // LED_SOFTBLINK_H__
  155. /** @} */