Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / platform / k32w / util / LEDWidget.cpp
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
20 #include "LEDWidget.h"
21
22 #include <system/SystemClock.h>
23
24 void LEDWidget::Init(LED_t led)
25 {
26     mLastChangeTimeUS = 0;
27     mBlinkOnTimeMS    = 0;
28     mBlinkOffTimeMS   = 0;
29     mGPIONum          = led;
30     mState            = false;
31
32     Set(false);
33 }
34
35 void LEDWidget::Invert(void)
36 {
37     Set(!mState);
38 }
39
40 void LEDWidget::Set(bool state)
41 {
42     mLastChangeTimeUS = mBlinkOnTimeMS = mBlinkOffTimeMS = 0;
43     DoSet(state);
44 }
45
46 void LEDWidget::Blink(uint32_t changeRateMS)
47 {
48     Blink(changeRateMS, changeRateMS);
49 }
50
51 void LEDWidget::Blink(uint32_t onTimeMS, uint32_t offTimeMS)
52 {
53     mBlinkOnTimeMS  = onTimeMS;
54     mBlinkOffTimeMS = offTimeMS;
55     Animate();
56 }
57
58 void LEDWidget::Animate()
59 {
60     if (mBlinkOnTimeMS != 0 && mBlinkOffTimeMS != 0)
61     {
62         int64_t nowUS = chip::System::Platform::Layer::GetClock_Monotonic();
63         ;
64         int64_t stateDurUS       = ((mState) ? mBlinkOnTimeMS : mBlinkOffTimeMS) * 1000LL;
65         int64_t nextChangeTimeUS = mLastChangeTimeUS + stateDurUS;
66
67         if (nowUS > nextChangeTimeUS)
68         {
69             DoSet(!mState);
70             mLastChangeTimeUS = nowUS;
71         }
72     }
73 }
74
75 void LEDWidget::DoSet(bool state)
76 {
77     mState = state;
78
79     if (state)
80     {
81         LED_TurnOnLed(mGPIONum);
82     }
83     else
84     {
85         LED_TurnOffLed(mGPIONum);
86     }
87 }