LEDWidget.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. *
  3. * Copyright (c) 2020 Project CHIP Authors
  4. * Copyright (c) 2019 Google LLC.
  5. * All rights reserved.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "LEDWidget.h"
  20. #include <dk_buttons_and_leds.h>
  21. #include <zephyr/kernel.h>
  22. static LEDWidget::LEDWidgetStateUpdateHandler sStateUpdateCallback;
  23. void LEDWidget::InitGpio()
  24. {
  25. dk_leds_init();
  26. }
  27. void LEDWidget::SetStateUpdateCallback(LEDWidgetStateUpdateHandler stateUpdateCb)
  28. {
  29. if (stateUpdateCb)
  30. sStateUpdateCallback = stateUpdateCb;
  31. }
  32. void LEDWidget::Init(uint32_t gpioNum)
  33. {
  34. mBlinkOnTimeMS = 0;
  35. mBlinkOffTimeMS = 0;
  36. mGPIONum = gpioNum;
  37. mState = false;
  38. k_timer_init(&mLedTimer, &LEDWidget::LedStateTimerHandler, nullptr);
  39. k_timer_user_data_set(&mLedTimer, this);
  40. Set(false);
  41. }
  42. void LEDWidget::Invert(void)
  43. {
  44. Set(!mState);
  45. }
  46. void LEDWidget::Set(bool state)
  47. {
  48. k_timer_stop(&mLedTimer);
  49. mBlinkOnTimeMS = mBlinkOffTimeMS = 0;
  50. DoSet(state);
  51. }
  52. void LEDWidget::Blink(uint32_t changeRateMS)
  53. {
  54. Blink(changeRateMS, changeRateMS);
  55. }
  56. void LEDWidget::Blink(uint32_t onTimeMS, uint32_t offTimeMS)
  57. {
  58. k_timer_stop(&mLedTimer);
  59. mBlinkOnTimeMS = onTimeMS;
  60. mBlinkOffTimeMS = offTimeMS;
  61. if (mBlinkOnTimeMS != 0 && mBlinkOffTimeMS != 0)
  62. {
  63. DoSet(!mState);
  64. ScheduleStateChange();
  65. }
  66. }
  67. void LEDWidget::ScheduleStateChange()
  68. {
  69. k_timer_start(&mLedTimer, K_MSEC(mState ? mBlinkOnTimeMS : mBlinkOffTimeMS), K_NO_WAIT);
  70. }
  71. void LEDWidget::DoSet(bool state)
  72. {
  73. mState = state;
  74. dk_set_led(mGPIONum, state);
  75. }
  76. void LEDWidget::UpdateState()
  77. {
  78. /* Prevent from keep updating the state if LED was set to solid On/Off value */
  79. if (mBlinkOnTimeMS != 0 && mBlinkOffTimeMS != 0)
  80. {
  81. DoSet(!mState);
  82. ScheduleStateChange();
  83. }
  84. }
  85. void LEDWidget::LedStateTimerHandler(k_timer * timer)
  86. {
  87. if (sStateUpdateCallback)
  88. sStateUpdateCallback(*reinterpret_cast<LEDWidget *>(timer->user_data));
  89. }