LEDWidget.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #pragma once
  19. #include <stdint.h>
  20. #ifdef CFG_PLF_RV32
  21. #include "asr_gpio.h"
  22. #include "asr_pinmux.h"
  23. #include "asr_pwm.h"
  24. #define duet_pwm_dev_t asr_pwm_dev_t
  25. #define duet_pwm_config_t asr_pwm_config_t
  26. #define duet_pwm_para_chg asr_pwm_para_chg
  27. #define duet_pwm_init asr_pwm_init
  28. #define duet_pwm_start asr_pwm_start
  29. #define duet_gpio_dev_t asr_gpio_dev_t
  30. #define duet_gpio_init asr_gpio_init
  31. #define duet_gpio_output_low asr_gpio_output_low
  32. #define duet_gpio_output_high asr_gpio_output_high
  33. #define DUET_OUTPUT_PUSH_PULL ASR_OUTPUT_PUSH_PULL
  34. #elif defined CFG_PLF_DUET
  35. #include "duet_gpio.h"
  36. #include "duet_pinmux.h"
  37. #include "duet_pwm.h"
  38. #else
  39. #include "lega_gpio.h"
  40. #include "lega_pinmux.h"
  41. #include "lega_pwm.h"
  42. #define duet_pwm_dev_t lega_pwm_dev_t
  43. #define duet_pwm_config_t lega_pwm_config_t
  44. #define duet_pwm_para_chg lega_pwm_para_chg
  45. #define duet_pwm_init lega_pwm_init
  46. #define duet_pwm_start lega_pwm_start
  47. #define duet_gpio_dev_t lega_gpio_dev_t
  48. #define duet_gpio_init lega_gpio_init
  49. #define duet_gpio_output_low lega_gpio_output_low
  50. #define duet_gpio_output_high lega_gpio_output_high
  51. #define DUET_OUTPUT_PUSH_PULL LEGA_OUTPUT_PUSH_PULL
  52. #endif
  53. #ifdef __cplusplus
  54. extern "C" {
  55. #endif
  56. void RGB_setup_HSB(uint8_t Hue, uint8_t Saturation);
  57. #ifdef __cplusplus
  58. }
  59. #endif
  60. #define LIGHT_RGB_RED PWM_OUTPUT_CH6
  61. #define LIGHT_RGB_GREEN PWM_OUTPUT_CH4
  62. #define LIGHT_RGB_BLUE PWM_OUTPUT_CH1
  63. #define LIGHT_RGB_RED_PAD PAD7
  64. #define LIGHT_RGB_GREEN_PAD PAD6
  65. #define LIGHT_RGB_BLUE_PAD PAD10
  66. #define LIGHT_LED GPIO6_INDEX
  67. #define STATE_LED GPIO7_INDEX
  68. class LEDWidget
  69. {
  70. public:
  71. void Init(uint8_t port);
  72. void Set(bool state);
  73. bool Get(void);
  74. void Invert(void);
  75. void Blink(uint32_t changeRateMS);
  76. void Blink(uint32_t onTimeMS, uint32_t offTimeMS);
  77. void Animate();
  78. void PWM_start();
  79. void PWM_stop();
  80. #ifdef LIGHT_SELECT_RGB
  81. void RGB_init();
  82. void SetBrightness(uint8_t brightness);
  83. void SetColor(uint8_t Hue, uint8_t Saturation);
  84. void HSB2rgb(uint16_t Hue, uint8_t Saturation, uint8_t brightness, uint8_t & red, uint8_t & green, uint8_t & blue);
  85. void showRGB(uint8_t red, uint8_t green, uint8_t blue);
  86. #endif
  87. private:
  88. uint64_t mLastChangeTimeMS = 0;
  89. uint32_t mBlinkOnTimeMS = 0;
  90. uint32_t mBlinkOffTimeMS = 0;
  91. bool mState = 0;
  92. uint8_t mbrightness = 0;
  93. uint8_t mHue;
  94. uint8_t mSaturation;
  95. #ifdef LIGHT_SELECT_RGB
  96. duet_pwm_dev_t pwm_led;
  97. duet_pwm_dev_t pwm_led_g;
  98. duet_pwm_dev_t pwm_led_b;
  99. #else
  100. duet_gpio_dev_t gpio;
  101. #endif
  102. void DoSet(bool state);
  103. };