Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / platform / efr32 / 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 "bsp.h"
23
24 #include <platform/CHIPDeviceLayer.h>
25
26 void LEDWidget::InitGpio(void)
27 {
28     // Sets gpio pin mode for ALL board Leds.
29     BSP_LedsInit();
30 }
31
32 void LEDWidget::Init(int ledNum)
33 {
34     mLastChangeTimeUS = 0;
35     mBlinkOnTimeMS    = 0;
36     mBlinkOffTimeMS   = 0;
37     mLedNum           = ledNum;
38
39     Set(false);
40 }
41
42 void LEDWidget::Invert(void)
43 {
44     Set(!mState);
45 }
46
47 void LEDWidget::Set(bool state)
48 {
49     mLastChangeTimeUS = mBlinkOnTimeMS = mBlinkOffTimeMS = 0;
50     DoSet(state);
51 }
52
53 void LEDWidget::Blink(uint32_t changeRateMS)
54 {
55     Blink(changeRateMS, changeRateMS);
56 }
57
58 void LEDWidget::Blink(uint32_t onTimeMS, uint32_t offTimeMS)
59 {
60     mBlinkOnTimeMS  = onTimeMS;
61     mBlinkOffTimeMS = offTimeMS;
62     Animate();
63 }
64
65 void LEDWidget::Animate()
66 {
67     if (mBlinkOnTimeMS != 0 && mBlinkOffTimeMS != 0)
68     {
69         int64_t nowUS            = ::chip::System::Layer::GetClock_MonotonicHiRes();
70         int64_t stateDurUS       = ((mState) ? mBlinkOnTimeMS : mBlinkOffTimeMS) * 1000LL;
71         int64_t nextChangeTimeUS = mLastChangeTimeUS + stateDurUS;
72
73         if (nowUS > nextChangeTimeUS)
74         {
75             DoSet(!mState);
76             mLastChangeTimeUS = nowUS;
77         }
78     }
79 }
80
81 void LEDWidget::DoSet(bool state)
82 {
83     mState = state;
84
85     if (state)
86     {
87         BSP_LedSet(mLedNum);
88     }
89     else
90     {
91         BSP_LedClear(mLedNum);
92     }
93 }