led-gpio.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Generic GPIO led
  3. *
  4. * Copyright (C) 2019 - 2020 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #include "private-lib-core.h"
  25. #include "drivers/led/private-lib-drivers-led.h"
  26. #if defined(LWS_PLAT_TIMER_CB)
  27. static LWS_PLAT_TIMER_CB(lws_led_timer_cb, th)
  28. {
  29. lws_led_state_t *lcs = LWS_PLAT_TIMER_CB_GET_OPAQUE(th);
  30. lws_seq_timer_handle(lcs);
  31. }
  32. #endif
  33. struct lws_led_state *
  34. lws_led_gpio_create(const lws_led_ops_t *led_ops)
  35. {
  36. lws_led_gpio_controller_t *lgc = (lws_led_gpio_controller_t *)led_ops;
  37. /*
  38. * We allocate the main state object, and a 3 x seq dynamic footprint
  39. * for each led, since it may be sequencing the transition between two
  40. * other sequences.
  41. */
  42. lws_led_state_t *lcs = lws_zalloc(sizeof(lws_led_state_t) +
  43. (lgc->count_leds * sizeof(lws_led_state_chs_t)),
  44. __func__);
  45. int n;
  46. if (!lcs)
  47. return NULL;
  48. lcs->controller = lgc;
  49. #if defined(LWS_PLAT_TIMER_CREATE)
  50. lcs->timer = LWS_PLAT_TIMER_CREATE("leds",
  51. LWS_LED_SEQUENCER_UPDATE_INTERVAL_MS, 1, lcs,
  52. (TimerCallbackFunction_t)lws_led_timer_cb);
  53. if (!lcs->timer)
  54. return NULL;
  55. #endif
  56. for (n = 0; n < lgc->count_leds; n++) {
  57. const lws_led_gpio_map_t *map = &lgc->led_map[n];
  58. if (map->pwm_ops) {
  59. lgc->gpio_ops->mode(map->gpio, LWSGGPIO_FL_READ);
  60. lgc->gpio_ops->set(map->gpio, 0);
  61. } else {
  62. lgc->gpio_ops->mode(map->gpio, LWSGGPIO_FL_WRITE);
  63. lgc->gpio_ops->set(map->gpio,
  64. !lgc->led_map[n].active_level);
  65. }
  66. }
  67. return lcs;
  68. }
  69. void
  70. lws_led_gpio_destroy(struct lws_led_state *lcs)
  71. {
  72. #if defined(LWS_PLAT_TIMER_DELETE)
  73. LWS_PLAT_TIMER_DELETE(&lcs->timer);
  74. #endif
  75. lws_free(lcs);
  76. }
  77. int
  78. lws_led_gpio_lookup(const struct lws_led_ops *lo, const char *name)
  79. {
  80. const lws_led_gpio_controller_t *lgc = (lws_led_gpio_controller_t *)lo;
  81. int n;
  82. for (n = 0; n < lgc->count_leds; n++)
  83. if (!strcmp(name, lgc->led_map[n].name))
  84. return n;
  85. return -1;
  86. }
  87. void
  88. lws_led_gpio_intensity(const struct lws_led_ops *lo, const char *name,
  89. lws_led_intensity_t inten)
  90. {
  91. const lws_led_gpio_controller_t *lgc = (lws_led_gpio_controller_t *)lo;
  92. int idx = lws_led_gpio_lookup(lo, name);
  93. const lws_led_gpio_map_t *map;
  94. if (idx < 0)
  95. return;
  96. map = &lgc->led_map[idx];
  97. if (map->pwm_ops)
  98. map->pwm_ops->intensity(map->pwm_ops, map->gpio, inten);
  99. else
  100. lgc->gpio_ops->set(map->gpio,
  101. (!!map->active_level) ^ !(inten & 0x8000));
  102. }