Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / platform / nrfconnect / 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 <dk_buttons_and_leds.h>
23 #include <zephyr.h>
24
25 void LEDWidget::InitGpio()
26 {
27     dk_leds_init();
28 }
29
30 void LEDWidget::Init(uint32_t gpioNum)
31 {
32     mLastChangeTimeMS = 0;
33     mBlinkOnTimeMS    = 0;
34     mBlinkOffTimeMS   = 0;
35     mGPIONum          = gpioNum;
36     mState            = false;
37     Set(false);
38 }
39
40 void LEDWidget::Invert(void)
41 {
42     Set(!mState);
43 }
44
45 void LEDWidget::Set(bool state)
46 {
47     mLastChangeTimeMS = mBlinkOnTimeMS = mBlinkOffTimeMS = 0;
48     DoSet(state);
49 }
50
51 void LEDWidget::Blink(uint32_t changeRateMS)
52 {
53     Blink(changeRateMS, changeRateMS);
54 }
55
56 void LEDWidget::Blink(uint32_t onTimeMS, uint32_t offTimeMS)
57 {
58     mBlinkOnTimeMS  = onTimeMS;
59     mBlinkOffTimeMS = offTimeMS;
60     Animate();
61 }
62
63 void LEDWidget::Animate()
64 {
65     if (mBlinkOnTimeMS != 0 && mBlinkOffTimeMS != 0)
66     {
67         int64_t nowMS      = k_uptime_get();
68         int64_t stateDurMS = mState ? mBlinkOnTimeMS : mBlinkOffTimeMS;
69
70         if (nowMS > mLastChangeTimeMS + stateDurMS)
71         {
72             DoSet(!mState);
73             mLastChangeTimeMS = nowMS;
74         }
75     }
76 }
77
78 void LEDWidget::DoSet(bool state)
79 {
80     mState = state;
81     dk_set_led(mGPIONum, state);
82 }