LEDWidget.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. *
  3. * Copyright (c) 2022 Project CHIP Authors
  4. * All rights reserved.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "LEDWidget.h"
  19. #include "AppConfig.h"
  20. #include <platform/CHIPDeviceLayer.h>
  21. /*******************************************************************************
  22. * Macro Definitions
  23. *******************************************************************************/
  24. /* Allowed TCPWM compare value for maximum brightness */
  25. #define LED_MAX_BRIGHTNESS (100u)
  26. /* Allowed TCPWM compare value for minimum brightness*/
  27. #define LED_MIN_BRIGHTNESS (0u)
  28. #define PWM_LED_FREQ_HZ (1000u) /* in Hz */
  29. /* subtracting from 100 since the LED is connected in active low configuration */
  30. #define GET_DUTY_CYCLE(x) (100 - x)
  31. // PWM period in micro seconds
  32. #define LED_PWM_PERIOD_US (255u)
  33. #ifdef LIGHT_SELECT_RGB
  34. static void show_pwm(duet_pwm_dev_t * pwm_dev, uint8_t val)
  35. {
  36. duet_pwm_config_t pwm_cfg;
  37. if (val < LED_MIN_BRIGHTNESS)
  38. val = LED_MIN_BRIGHTNESS;
  39. pwm_cfg.duty_cycle = (float) (val) / LED_PWM_PERIOD_US;
  40. pwm_cfg.freq = PWM_LED_FREQ_HZ;
  41. duet_pwm_para_chg(pwm_dev, pwm_cfg);
  42. }
  43. static void init_pwm(duet_pwm_dev_t * pwm_dev, uint8_t ledNum, uint8_t val)
  44. {
  45. pwm_dev->port = ledNum;
  46. pwm_dev->config.duty_cycle = val;
  47. pwm_dev->config.freq = PWM_LED_FREQ_HZ;
  48. pwm_dev->priv = NULL;
  49. duet_pwm_init(pwm_dev);
  50. }
  51. #endif
  52. void LEDWidget::Init(uint8_t ledNum)
  53. {
  54. mLastChangeTimeMS = 0;
  55. mBlinkOnTimeMS = 0;
  56. mBlinkOffTimeMS = 0;
  57. mState = 0;
  58. mbrightness = LED_MIN_BRIGHTNESS;
  59. mHue = 0;
  60. mSaturation = 0;
  61. #ifdef LIGHT_SELECT_RGB
  62. if (ledNum == LIGHT_RGB_GREEN)
  63. {
  64. #ifdef CFG_PLF_RV32
  65. asr_pinmux_config(LIGHT_RGB_RED_PAD, PF_PWM);
  66. #endif
  67. init_pwm(&pwm_led_g, ledNum, LED_MIN_BRIGHTNESS);
  68. }
  69. else if (ledNum == LIGHT_RGB_BLUE)
  70. {
  71. #ifdef CFG_PLF_RV32
  72. asr_pinmux_config(LIGHT_RGB_BLUE_PAD, PF_PWM);
  73. #endif
  74. init_pwm(&pwm_led_b, ledNum, LED_MIN_BRIGHTNESS);
  75. }
  76. else
  77. {
  78. #ifdef CFG_PLF_RV32
  79. asr_pinmux_config(LIGHT_RGB_GREEN_PAD, PF_PWM);
  80. #endif
  81. init_pwm(&pwm_led, ledNum, LED_MIN_BRIGHTNESS);
  82. }
  83. #else
  84. gpio.port = ledNum;
  85. gpio.config = DUET_OUTPUT_PUSH_PULL;
  86. duet_gpio_init(&gpio);
  87. #endif
  88. }
  89. void LEDWidget::Invert(void)
  90. {
  91. Set(!mState);
  92. }
  93. void LEDWidget::Set(bool state)
  94. {
  95. mLastChangeTimeMS = mBlinkOnTimeMS = mBlinkOffTimeMS = 0;
  96. DoSet(state);
  97. }
  98. bool LEDWidget::Get()
  99. {
  100. return mState;
  101. }
  102. void LEDWidget::Blink(uint32_t changeRateMS)
  103. {
  104. Blink(changeRateMS, changeRateMS);
  105. }
  106. void LEDWidget::Blink(uint32_t onTimeMS, uint32_t offTimeMS)
  107. {
  108. mBlinkOnTimeMS = onTimeMS;
  109. mBlinkOffTimeMS = offTimeMS;
  110. Animate();
  111. }
  112. void LEDWidget::Animate()
  113. {
  114. if (mBlinkOnTimeMS != 0 && mBlinkOffTimeMS != 0)
  115. {
  116. uint64_t nowMS = chip::System::SystemClock().GetMonotonicMilliseconds64().count();
  117. uint64_t stateDurMS = ((mState) ? mBlinkOnTimeMS : mBlinkOffTimeMS);
  118. uint64_t nextChangeTimeMS = mLastChangeTimeMS + stateDurMS;
  119. if (nextChangeTimeMS < nowMS)
  120. {
  121. DoSet(!mState);
  122. mLastChangeTimeMS = nowMS;
  123. }
  124. }
  125. }
  126. void LEDWidget::DoSet(bool state)
  127. {
  128. if (mState != state)
  129. {
  130. #ifdef LIGHT_SELECT_RGB
  131. (state) ? PWM_start() : PWM_stop();
  132. #else
  133. (state) ? duet_gpio_output_low(&gpio) : duet_gpio_output_high(&gpio);
  134. #endif
  135. }
  136. mState = state;
  137. }
  138. #ifdef LIGHT_SELECT_RGB
  139. void LEDWidget::RGB_init()
  140. {
  141. Init(LIGHT_RGB_RED); // red light of RGB
  142. Init(LIGHT_RGB_GREEN); // green light of RGB
  143. Init(LIGHT_RGB_BLUE); // blue light of RGB
  144. }
  145. void LEDWidget::PWM_start()
  146. {
  147. if (!mState)
  148. {
  149. /* Start PWM to turn the LED on */
  150. if (0 != duet_pwm_start(&pwm_led))
  151. {
  152. ASR_LOG("PWM failed to start!");
  153. }
  154. /* Start PWM to turn the LED on */
  155. if (0 != duet_pwm_start(&pwm_led_g))
  156. {
  157. ASR_LOG("PWM failed to start!");
  158. }
  159. /* Start PWM to turn the LED on */
  160. if (0 != duet_pwm_start(&pwm_led_b))
  161. {
  162. ASR_LOG("PWM failed to start!");
  163. }
  164. mState = 1;
  165. }
  166. }
  167. void LEDWidget::PWM_stop()
  168. {
  169. SetBrightness(LED_MIN_BRIGHTNESS);
  170. mbrightness = LED_MIN_BRIGHTNESS;
  171. mState = 0;
  172. }
  173. void LEDWidget::SetBrightness(uint8_t brightness)
  174. {
  175. mbrightness = brightness;
  176. SetColor(mHue, mSaturation);
  177. }
  178. void LEDWidget::SetColor(uint8_t Hue, uint8_t Saturation)
  179. {
  180. uint8_t red, green, blue;
  181. uint8_t sSaturation, sbrightness;
  182. uint16_t sHue;
  183. sbrightness = (mState) ? mbrightness : 0;
  184. sHue = static_cast<uint16_t>(Hue) * 360 / 254; // sHue [0, 360]
  185. sSaturation = static_cast<uint8_t>(Saturation) * 100 / 254; // sSaturation [0 , 100]
  186. HSB2rgb(sHue, sSaturation, sbrightness, red, green, blue);
  187. ASR_LOG("brightness: %d, red: %d, green: %d, blue: %d", sbrightness, red, green, blue);
  188. mHue = Hue;
  189. mSaturation = Saturation;
  190. showRGB(red, green, blue);
  191. }
  192. void LEDWidget::HSB2rgb(uint16_t Hue, uint8_t Saturation, uint8_t brightness, uint8_t & red, uint8_t & green, uint8_t & blue)
  193. {
  194. uint16_t i = Hue / 60;
  195. uint16_t rgb_max = brightness;
  196. uint16_t rgb_min = rgb_max * (100 - Saturation) / 100;
  197. uint16_t diff = Hue % 60;
  198. uint16_t rgb_adj = (rgb_max - rgb_min) * diff / 60;
  199. switch (i)
  200. {
  201. case 0:
  202. red = rgb_max;
  203. green = rgb_min + rgb_adj;
  204. blue = rgb_min;
  205. break;
  206. case 1:
  207. red = rgb_max - rgb_adj;
  208. green = rgb_max;
  209. blue = rgb_min;
  210. break;
  211. case 2:
  212. red = rgb_min;
  213. green = rgb_max;
  214. blue = rgb_min + rgb_adj;
  215. break;
  216. case 3:
  217. red = rgb_min;
  218. green = rgb_max - rgb_adj;
  219. blue = rgb_max;
  220. break;
  221. case 4:
  222. red = rgb_min + rgb_adj;
  223. green = rgb_min;
  224. blue = rgb_max;
  225. break;
  226. default:
  227. red = rgb_max;
  228. green = rgb_min;
  229. blue = rgb_max - rgb_adj;
  230. break;
  231. }
  232. }
  233. void LEDWidget::showRGB(uint8_t red, uint8_t green, uint8_t blue)
  234. {
  235. show_pwm(&pwm_led, red);
  236. show_pwm(&pwm_led_g, green);
  237. show_pwm(&pwm_led_b, blue);
  238. }
  239. #endif