lws-display.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * lws abstract display
  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 <libwebsockets.h>
  25. static void
  26. sul_autodim_cb(lws_sorted_usec_list_t *sul)
  27. {
  28. lws_display_state_t *lds = lws_container_of(sul, lws_display_state_t,
  29. sul_autodim);
  30. int next_ms = -1;
  31. /* we fire both to dim and to blank... if already in dim state, blank */
  32. switch (lds->state) {
  33. case LWSDISPS_BECOMING_ACTIVE:
  34. lws_display_state_set_brightness(lds, lds->disp->bl_active);
  35. lds->state = LWSDISPS_ACTIVE;
  36. next_ms = lds->autodim_ms;
  37. break;
  38. case LWSDISPS_ACTIVE:
  39. /* active -> autodimmed */
  40. lds->state = LWSDISPS_AUTODIMMED;
  41. next_ms = lds->off_ms;
  42. lws_display_state_set_brightness(lds, lds->disp->bl_dim);
  43. break;
  44. case LWSDISPS_AUTODIMMED:
  45. /* dimmed -> OFF */
  46. lws_display_state_set_brightness(lds, &lws_pwmseq_static_off);
  47. lds->state = LWSDISPS_GOING_OFF;
  48. next_ms = 600;
  49. break;
  50. case LWSDISPS_GOING_OFF:
  51. /* off dimming completed, actual display OFF */
  52. lws_display_state_off(lds);
  53. return;
  54. default:
  55. return;
  56. }
  57. if (next_ms >= 0)
  58. lws_sul_schedule(lds->ctx, 0, &lds->sul_autodim, sul_autodim_cb,
  59. next_ms * LWS_US_PER_MS);
  60. }
  61. void
  62. lws_display_state_init(lws_display_state_t *lds, struct lws_context *ctx,
  63. int dim_ms, int off_ms, struct lws_led_state *bl_lcs,
  64. const lws_display_t *disp)
  65. {
  66. memset(lds, 0, sizeof(*lds));
  67. lds->disp = disp;
  68. lds->ctx = ctx;
  69. lds->autodim_ms = dim_ms;
  70. lds->off_ms = off_ms;
  71. lds->bl_lcs = bl_lcs;
  72. lds->state = LWSDISPS_OFF;
  73. lws_led_transition(lds->bl_lcs, "backlight", &lws_pwmseq_static_off,
  74. &lws_pwmseq_static_on);
  75. disp->init(disp);
  76. }
  77. void
  78. lws_display_state_set_brightness(lws_display_state_t *lds,
  79. const lws_led_sequence_def_t *pwmseq)
  80. {
  81. lws_led_transition(lds->bl_lcs, "backlight", pwmseq,
  82. lds->disp->bl_transition);
  83. }
  84. void
  85. lws_display_state_active(lws_display_state_t *lds)
  86. {
  87. int waiting_ms;
  88. if (lds->state == LWSDISPS_OFF) {
  89. /* power us up */
  90. lds->disp->power(lds->disp, 1);
  91. lds->state = LWSDISPS_BECOMING_ACTIVE;
  92. waiting_ms = lds->disp->latency_wake_ms;
  93. } else {
  94. if (lds->state != LWSDISPS_ACTIVE)
  95. lws_display_state_set_brightness(lds,
  96. lds->disp->bl_active);
  97. lds->state = LWSDISPS_ACTIVE;
  98. waiting_ms = lds->autodim_ms;
  99. }
  100. /* reset the autodim timer */
  101. if (waiting_ms >= 0)
  102. lws_sul_schedule(lds->ctx, 0, &lds->sul_autodim, sul_autodim_cb,
  103. waiting_ms * LWS_US_PER_MS);
  104. }
  105. void
  106. lws_display_state_off(lws_display_state_t *lds)
  107. {
  108. lds->disp->power(lds->disp, 0);
  109. lws_sul_cancel(&lds->sul_autodim);
  110. lds->state = LWSDISPS_OFF;
  111. }