3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2019 Google LLC.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "LEDWidget.h"
22 #include <system/SystemClock.h>
24 void LEDWidget::Init(LED_t led)
26 mLastChangeTimeUS = 0;
35 void LEDWidget::Invert(void)
40 void LEDWidget::Set(bool state)
42 mLastChangeTimeUS = mBlinkOnTimeMS = mBlinkOffTimeMS = 0;
46 void LEDWidget::Blink(uint32_t changeRateMS)
48 Blink(changeRateMS, changeRateMS);
51 void LEDWidget::Blink(uint32_t onTimeMS, uint32_t offTimeMS)
53 mBlinkOnTimeMS = onTimeMS;
54 mBlinkOffTimeMS = offTimeMS;
58 void LEDWidget::Animate()
60 if (mBlinkOnTimeMS != 0 && mBlinkOffTimeMS != 0)
62 int64_t nowUS = chip::System::Platform::Layer::GetClock_Monotonic();
64 int64_t stateDurUS = ((mState) ? mBlinkOnTimeMS : mBlinkOffTimeMS) * 1000LL;
65 int64_t nextChangeTimeUS = mLastChangeTimeUS + stateDurUS;
67 if (nowUS > nextChangeTimeUS)
70 mLastChangeTimeUS = nowUS;
75 void LEDWidget::DoSet(bool state)
81 LED_TurnOnLed(mGPIONum);
85 LED_TurnOffLed(mGPIONum);