pwm.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. static const lws_led_intensity_t sineq16[] = {
  26. /*
  27. * Quadrant at sin(270) in 16 samples, normalized so
  28. * -1 == 0 and 0 == 32767
  29. */
  30. 0, 158, 630, 1411, 2494, 3869, 5522, 7437,
  31. 9597, 11980, 14562, 17321, 20228, 23225, 26374, 29555,
  32. 32767 /* to interpolate against */
  33. };
  34. /*
  35. * Elaborate the 90 degree phase table to 360 degrees and offset to +32768,
  36. * notice for the last sample we have to interpolate against a 17th sample
  37. * reflecting full scale to avoid clipping due to interpolation against the
  38. * 16th sample again
  39. */
  40. static lws_led_intensity_t
  41. sine_lu(int n, int next)
  42. {
  43. switch ((n >> 4) & 3) {
  44. case 1:
  45. /* forwards */
  46. return 32768 + sineq16[(n & 15) + next];
  47. case 2:
  48. /* scan it backwards */
  49. return 32768 + sineq16[15 - (n & 15) + (!next)];
  50. case 3:
  51. /* forwards */
  52. return 32768 - sineq16[(n & 15) + next];
  53. default:
  54. /* scan it backwards */
  55. return 32768 - sineq16[15 - (n & 15) + (!next)];
  56. }
  57. }
  58. /*
  59. * The normalized phase resolution is 16-bit, however much table you decide to
  60. * have needs interpolating or indexing in a reduced number of significant
  61. * phase bits if it doesn't have the same phase resolution.
  62. *
  63. * In this sine table we have a 16 x 15-bit sample quadrant reflected 4 times
  64. * to make 360 degrees, so 64 accurate sample points, with the rest of the
  65. * intermediate phases generated by linear interpolation. That probably would
  66. * sound a bit funky, but for modulating light dynamically it's more than
  67. * enough.
  68. */
  69. lws_led_intensity_t
  70. lws_led_func_sine(lws_led_seq_phase_t n)
  71. {
  72. /*
  73. * 2: quadrant
  74. * 4: table entry in quadrant
  75. * 10: interp (LSB)
  76. */
  77. return (sine_lu(n >> 10, 0) * (0x3ff - (n & 0x3ff)) +
  78. sine_lu(n >> 10, 1) * (n & 0x3ff)) / 0x3ff;
  79. }
  80. lws_led_intensity_t
  81. lws_led_func_linear(lws_led_seq_phase_t n)
  82. {
  83. return (lws_led_intensity_t)n;
  84. }
  85. static lws_led_intensity_t
  86. lws_led_func_static(lws_led_seq_phase_t n)
  87. {
  88. return ((int)n * LWS_LED_MAX_INTENSITY) / 2;
  89. }
  90. const lws_led_sequence_def_t lws_pwmseq_static_off = {
  91. .func = lws_led_func_static,
  92. .ledphase_offset = 0,
  93. .ledphase_total = 0,
  94. .ms = 0
  95. };
  96. const lws_led_sequence_def_t lws_pwmseq_static_half = {
  97. .func = lws_led_func_static,
  98. .ledphase_offset = 1,
  99. .ledphase_total = 0,
  100. .ms = 0
  101. };
  102. const lws_led_sequence_def_t lws_pwmseq_static_on = {
  103. .func = lws_led_func_static,
  104. .ledphase_offset = 2,
  105. .ledphase_total = 0,
  106. .ms = 0
  107. };
  108. const lws_led_sequence_def_t lws_pwmseq_sine_up = {
  109. .func = lws_led_func_sine,
  110. .ledphase_offset = 0, /* already at 0 amp at 0 phase */
  111. .ledphase_total = LWS_LED_FUNC_PHASE / 2, /* 180 degree ./^ */
  112. .ms = 300
  113. };
  114. const lws_led_sequence_def_t lws_pwmseq_sine_down = {
  115. .func = lws_led_func_sine,
  116. .ledphase_offset = LWS_LED_FUNC_PHASE / 2, /* start at peak */
  117. .ledphase_total = LWS_LED_FUNC_PHASE / 2, /* 180 degree ./^ */
  118. .ms = 300
  119. };
  120. const lws_led_sequence_def_t lws_pwmseq_linear_wipe = {
  121. .func = lws_led_func_linear,
  122. .ledphase_offset = 0,
  123. .ledphase_total = LWS_LED_FUNC_PHASE - 1,
  124. .ms = 300
  125. };
  126. const lws_led_sequence_def_t lws_pwmseq_sine_endless_slow = {
  127. .func = lws_led_func_sine,
  128. .ledphase_offset = 0, /* already at 0 amp at 0 phase */
  129. .ledphase_total = LWS_SEQ_LEDPHASE_TOTAL_ENDLESS,
  130. .ms = 1500
  131. };
  132. const lws_led_sequence_def_t lws_pwmseq_sine_endless_fast = {
  133. .func = lws_led_func_sine,
  134. .ledphase_offset = 0, /* already at 0 amp at 0 phase */
  135. .ledphase_total = LWS_SEQ_LEDPHASE_TOTAL_ENDLESS,
  136. .ms = 750
  137. };