LEDWidget.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 "sl_simple_led_instances.h"
  21. #include <platform/CHIPDeviceLayer.h>
  22. using namespace ::chip::System;
  23. void LEDWidget::InitGpio(void)
  24. {
  25. // Sets gpio pin mode for ALL board Leds.
  26. sl_simple_led_init_instances();
  27. }
  28. void LEDWidget::Init(const sl_led_t * led)
  29. {
  30. mLastChangeTimeMS = 0;
  31. mBlinkOnTimeMS = 0;
  32. mBlinkOffTimeMS = 0;
  33. mLed = led;
  34. Set(false);
  35. }
  36. void LEDWidget::Invert(void)
  37. {
  38. if (mLed)
  39. {
  40. sl_led_toggle(mLed);
  41. }
  42. }
  43. void LEDWidget::Set(bool state)
  44. {
  45. mLastChangeTimeMS = mBlinkOnTimeMS = mBlinkOffTimeMS = 0;
  46. if (mLed)
  47. {
  48. state ? sl_led_turn_on(mLed) : sl_led_turn_off(mLed);
  49. }
  50. }
  51. void LEDWidget::Blink(uint32_t changeRateMS)
  52. {
  53. Blink(changeRateMS, changeRateMS);
  54. }
  55. void LEDWidget::Blink(uint32_t onTimeMS, uint32_t offTimeMS)
  56. {
  57. mBlinkOnTimeMS = onTimeMS;
  58. mBlinkOffTimeMS = offTimeMS;
  59. Animate();
  60. }
  61. void LEDWidget::Animate()
  62. {
  63. if (mBlinkOnTimeMS != 0 && mBlinkOffTimeMS != 0)
  64. {
  65. uint64_t nowMS = chip::System::SystemClock().GetMonotonicMilliseconds64().count();
  66. uint64_t stateDurMS = sl_led_get_state(mLed) ? mBlinkOnTimeMS : mBlinkOffTimeMS;
  67. uint64_t nextChangeTimeMS = mLastChangeTimeMS + stateDurMS;
  68. if (nextChangeTimeMS < nowMS)
  69. {
  70. Invert();
  71. mLastChangeTimeMS = nowMS;
  72. }
  73. }
  74. }