led_manager.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright (c) 2019 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file led manager interface
  8. */
  9. #include <os_common_api.h>
  10. #include <mem_manager.h>
  11. #include <led_manager.h>
  12. #include <kernel.h>
  13. #include <led_hal.h>
  14. #include <string.h>
  15. #include <sys_monitor.h>
  16. #if defined(CONFIG_SYS_LOG)
  17. #define SYS_LOG_NO_NEWLINE
  18. #ifdef SYS_LOG_DOMAIN
  19. #undef SYS_LOG_DOMAIN
  20. #endif
  21. #define SYS_LOG_DOMAIN "led_manager"
  22. #endif
  23. #ifdef BOARD_LED_MAP
  24. const led_map led_maps[] = {
  25. BOARD_LED_MAP
  26. };
  27. #else
  28. #define BOARD_LED_MAP
  29. const led_map led_maps[8] = {
  30. };
  31. #endif
  32. #define MAX_LED_NUM ARRAY_SIZE(led_maps)
  33. OS_MUTEX_DEFINE(led_manager_mutex);
  34. struct led_state_t {
  35. uint8_t mode;
  36. uint16_t timeout;
  37. union{
  38. struct {
  39. uint16_t blink_period;
  40. uint16_t blink_pulse;
  41. uint8_t start_state;
  42. };
  43. struct {
  44. pwm_breath_ctrl_t ctrl;
  45. };
  46. };
  47. led_display_callback cb;
  48. };
  49. struct led_image {
  50. sys_snode_t snode;
  51. struct led_state_t led_state[MAX_LED_NUM];
  52. };
  53. struct led_manager_ctx_t {
  54. struct device *dev;
  55. struct led_image image;
  56. uint16_t timeout;
  57. uint8_t update_direct:1;
  58. uint8_t timeout_event_lock:1;
  59. led_timeout_callback timeout_cb;
  60. sys_slist_t image_list;
  61. };
  62. static struct led_manager_ctx_t global_led_manager_ctx;
  63. static struct led_manager_ctx_t *_led_manager_get_ctx(void)
  64. {
  65. return &global_led_manager_ctx;
  66. }
  67. #ifdef BOARD_LED_MAP
  68. static int _led_manager_update_state(void)
  69. {
  70. struct led_manager_ctx_t *ctx = _led_manager_get_ctx();
  71. for (int led_index = 0 ; led_index < MAX_LED_NUM; led_index++) {
  72. struct led_state_t *led_state = &ctx->image.led_state[led_index];
  73. switch (led_state->mode) {
  74. case LED_BLINK:
  75. led_blink(led_index, led_state->blink_period, led_state->blink_pulse, led_state->start_state);
  76. break;
  77. case LED_BREATH:
  78. if (led_state->ctrl.rise_time_ms || led_state->ctrl.down_time_ms
  79. || led_state->ctrl.high_time_ms || led_state->ctrl.low_time_ms) {
  80. led_breath(led_index, &led_state->ctrl);
  81. } else {
  82. led_breath(led_index, NULL);
  83. }
  84. break;
  85. case LED_ON:
  86. led_on(led_index);
  87. break;
  88. case LED_OFF:
  89. led_off(led_index);
  90. break;
  91. }
  92. }
  93. return 0;
  94. }
  95. #endif
  96. int led_manager_set_timeout_event_start(void)
  97. {
  98. struct led_manager_ctx_t *ctx = _led_manager_get_ctx();
  99. os_mutex_lock(&led_manager_mutex, OS_FOREVER);
  100. ctx->update_direct = 1;
  101. ctx->timeout_event_lock = 1;
  102. os_mutex_unlock(&led_manager_mutex);
  103. return 0;
  104. }
  105. int led_manager_set_timeout_event(int timeout, led_timeout_callback cb)
  106. {
  107. struct led_manager_ctx_t *ctx = _led_manager_get_ctx();
  108. os_mutex_lock(&led_manager_mutex, OS_FOREVER);
  109. ctx->timeout_cb = cb;
  110. ctx->timeout = timeout / CONFIG_MONITOR_PERIOD;
  111. ctx->update_direct = 0;
  112. os_mutex_unlock(&led_manager_mutex);
  113. return 0;
  114. }
  115. #ifdef BOARD_LED_MAP
  116. static int _led_manager_work_handle(void)
  117. {
  118. struct led_manager_ctx_t *ctx = _led_manager_get_ctx();
  119. os_mutex_lock(&led_manager_mutex, OS_FOREVER);
  120. for (int led_index = 0; led_index < MAX_LED_NUM; led_index++) {
  121. struct led_state_t *led_state = &ctx->image.led_state[led_index];
  122. if (led_state->timeout != DISPLAY_FOREVER) {
  123. led_state->timeout--;
  124. if (!led_state->timeout) {
  125. led_on(led_index);
  126. led_state->timeout = DISPLAY_FOREVER;
  127. if (led_state->cb) {
  128. os_mutex_unlock(&led_manager_mutex);
  129. led_state->cb();
  130. os_mutex_lock(&led_manager_mutex, OS_FOREVER);
  131. }
  132. }
  133. }
  134. }
  135. if (ctx->timeout != DISPLAY_FOREVER) {
  136. if (ctx->timeout-- == 0) {
  137. ctx->timeout = DISPLAY_FOREVER;
  138. ctx->update_direct = 0;
  139. ctx->timeout_event_lock = 0;
  140. _led_manager_update_state();
  141. if (ctx->timeout_cb) {
  142. os_mutex_unlock(&led_manager_mutex);
  143. ctx->timeout_cb();
  144. os_mutex_lock(&led_manager_mutex, OS_FOREVER);
  145. ctx->timeout_cb = NULL;
  146. }
  147. }
  148. }
  149. os_mutex_unlock(&led_manager_mutex);
  150. return 0;
  151. }
  152. #endif
  153. int led_manager_set_display(uint16_t led_index, uint8_t onoff, uint32_t timeout, led_display_callback cb)
  154. {
  155. #ifdef BOARD_LED_MAP
  156. struct led_manager_ctx_t *ctx = _led_manager_get_ctx();
  157. struct led_state_t *led_state = &ctx->image.led_state[led_index];
  158. os_mutex_lock(&led_manager_mutex, OS_FOREVER);
  159. if (!ctx->update_direct) {
  160. led_state->mode = onoff;
  161. led_state->cb = cb;
  162. if (timeout != OS_FOREVER) {
  163. led_state->timeout = timeout / CONFIG_MONITOR_PERIOD;
  164. } else {
  165. led_state->timeout = DISPLAY_FOREVER;
  166. }
  167. }
  168. if (!ctx->timeout_event_lock || ctx->update_direct) {
  169. if (onoff == LED_ON) {
  170. led_on(led_index);
  171. } else {
  172. led_off(led_index);
  173. }
  174. }
  175. os_mutex_unlock(&led_manager_mutex);
  176. #endif
  177. return 0;
  178. }
  179. int led_manager_set_breath(uint16_t led_index, pwm_breath_ctrl_t *ctrl, uint32_t timeout, led_display_callback cb)
  180. {
  181. #ifdef BOARD_LED_MAP
  182. struct led_manager_ctx_t *ctx = _led_manager_get_ctx();
  183. struct led_state_t *led_state = &ctx->image.led_state[led_index];
  184. os_mutex_lock(&led_manager_mutex, OS_FOREVER);
  185. if (!ctx->update_direct) {
  186. led_state->mode = LED_BREATH;
  187. if (ctrl) {
  188. memcpy(&led_state->ctrl, ctrl, sizeof(pwm_breath_ctrl_t));
  189. } else {
  190. memset(&led_state->ctrl, 0, sizeof(pwm_breath_ctrl_t));
  191. }
  192. if (timeout != OS_FOREVER) {
  193. led_state->timeout = timeout / CONFIG_MONITOR_PERIOD;
  194. } else {
  195. led_state->timeout = DISPLAY_FOREVER;
  196. }
  197. led_state->cb = cb;
  198. }
  199. if (!ctx->timeout_event_lock || ctx->update_direct) {
  200. led_breath(led_index, ctrl);
  201. }
  202. os_mutex_unlock(&led_manager_mutex);
  203. #endif
  204. return 0;
  205. }
  206. int led_manager_set_blink(uint8_t led_index, uint16_t blink_period, uint16_t blink_pulse, uint32_t timeout, uint8_t start_state, led_display_callback cb)
  207. {
  208. #ifdef BOARD_LED_MAP
  209. struct led_manager_ctx_t *ctx = _led_manager_get_ctx();
  210. struct led_state_t *led_state = &ctx->image.led_state[led_index];
  211. os_mutex_lock(&led_manager_mutex, OS_FOREVER);
  212. if (!ctx->update_direct) {
  213. led_state->mode = LED_BLINK;
  214. if (timeout != OS_FOREVER) {
  215. led_state->timeout = timeout / CONFIG_MONITOR_PERIOD;
  216. } else {
  217. led_state->timeout = DISPLAY_FOREVER;
  218. }
  219. led_state->blink_period = blink_period;
  220. led_state->blink_pulse = blink_pulse;
  221. led_state->start_state = start_state;
  222. led_state->cb = cb;
  223. }
  224. if (!ctx->timeout_event_lock || ctx->update_direct) {
  225. led_blink(led_index, blink_period, blink_pulse, start_state);
  226. }
  227. os_mutex_unlock(&led_manager_mutex);
  228. #endif
  229. return 0;
  230. }
  231. int led_manager_set_all(uint8_t onoff)
  232. {
  233. for (int led_index = 0 ; led_index < MAX_LED_NUM; led_index++) {
  234. led_manager_set_display(led_index, onoff, DISPLAY_FOREVER, NULL);
  235. }
  236. return 0;
  237. }
  238. int led_manager_sleep(void)
  239. {
  240. #ifdef BOARD_LED_MAP
  241. led_manager_store();
  242. led_manager_set_all(LED_OFF);
  243. #endif
  244. return 0;
  245. }
  246. int led_manager_wake_up(void)
  247. {
  248. #ifdef BOARD_LED_MAP
  249. led_manager_restore();
  250. #endif
  251. return 0;
  252. }
  253. int led_manager_store(void)
  254. {
  255. struct led_manager_ctx_t *ctx = _led_manager_get_ctx();
  256. struct led_image *image_history = mem_malloc(sizeof(struct led_image));
  257. if (!image_history)
  258. return -ENOMEM;
  259. os_mutex_lock(&led_manager_mutex, OS_FOREVER);
  260. memcpy(image_history, &ctx->image, sizeof(struct led_image));
  261. sys_slist_append(&ctx->image_list, (sys_snode_t *)image_history);
  262. os_mutex_unlock(&led_manager_mutex);
  263. return 0;
  264. }
  265. int led_manager_restore(void)
  266. {
  267. #ifdef BOARD_LED_MAP
  268. struct led_manager_ctx_t *ctx = _led_manager_get_ctx();
  269. struct led_image *image_history = NULL;
  270. os_mutex_lock(&led_manager_mutex, OS_FOREVER);
  271. image_history = (struct led_image *)sys_slist_peek_tail(&ctx->image_list);
  272. if (image_history) {
  273. memcpy(&ctx->image, image_history, sizeof(struct led_image));
  274. sys_slist_find_and_remove(&ctx->image_list, (sys_snode_t *)image_history);
  275. mem_free(image_history);
  276. } else {
  277. SYS_LOG_ERR("no history");
  278. return -ENODEV;
  279. }
  280. _led_manager_update_state();
  281. os_mutex_unlock(&led_manager_mutex);
  282. #endif
  283. return 0;
  284. }
  285. int led_manager_init(void)
  286. {
  287. #ifdef BOARD_LED_MAP
  288. struct led_manager_ctx_t *led_manager_ctx = _led_manager_get_ctx();
  289. memset(led_manager_ctx, 0, sizeof(struct led_manager_ctx_t));
  290. sys_slist_init(&led_manager_ctx->image_list);
  291. if (sys_monitor_add_work(_led_manager_work_handle)) {
  292. SYS_LOG_ERR("add work failed\n");
  293. return -EFAULT;
  294. }
  295. led_manager_ctx->timeout = DISPLAY_FOREVER;
  296. #endif
  297. return 0;
  298. }
  299. int led_manager_deinit(void)
  300. {
  301. return 0;
  302. }