Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / lighting-app / nrfconnect / main / LightingManager.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    All rights reserved.
5  *
6  *    Licensed under the Apache License, Version 2.0 (the "License");
7  *    you may not use this file except in compliance with the License.
8  *    You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *    Unless required by applicable law or agreed to in writing, software
13  *    distributed under the License is distributed on an "AS IS" BASIS,
14  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *    See the License for the specific language governing permissions and
16  *    limitations under the License.
17  */
18
19 #include "LightingManager.h"
20
21 #include "AppConfig.h"
22
23 #include <drivers/pwm.h>
24 #include <logging/log.h>
25 #include <zephyr.h>
26
27 LOG_MODULE_DECLARE(app);
28
29 LightingManager LightingManager::sLight;
30
31 int LightingManager::Init(const device * pwmDevice, uint32_t pwmChannel)
32 {
33     // We use a gpioPin instead of a LEDWidget here because we want to use PWM
34     // and other features instead of just on/off.
35
36     mState      = kState_On;
37     mLevel      = kMaxLevel;
38     mPwmDevice  = pwmDevice;
39     mPwmChannel = pwmChannel;
40
41     if (!device_is_ready(mPwmDevice))
42     {
43         LOG_ERR("PWM device %s is not ready", mPwmDevice->name);
44         return -ENODEV;
45     }
46
47     Set(false);
48     return 0;
49 }
50
51 void LightingManager::SetCallbacks(LightingCallback_fn aActionInitiated_CB, LightingCallback_fn aActionCompleted_CB)
52 {
53     mActionInitiated_CB = aActionInitiated_CB;
54     mActionCompleted_CB = aActionCompleted_CB;
55 }
56
57 bool LightingManager::InitiateAction(Action_t aAction, int32_t aActor, uint8_t size, uint8_t * value)
58 {
59     // TODO: this function is called InitiateAction because we want to implement some features such as ramping up here.
60     bool action_initiated = false;
61     State_t new_state;
62
63     // Initiate On/Off Action only when the previous one is complete.
64     if (mState == kState_Off && aAction == ON_ACTION)
65     {
66         action_initiated = true;
67         new_state        = kState_On;
68     }
69     else if (mState == kState_On && aAction == OFF_ACTION)
70     {
71         action_initiated = true;
72         new_state        = kState_Off;
73     }
74     else if (aAction == LEVEL_ACTION && *value != mLevel)
75     {
76         action_initiated = true;
77         if (*value == 0)
78         {
79             new_state = kState_Off;
80         }
81         else
82         {
83             new_state = kState_On;
84         }
85     }
86
87     if (action_initiated)
88     {
89         if (mActionInitiated_CB)
90         {
91             mActionInitiated_CB(aAction, aActor);
92         }
93
94         if (aAction == ON_ACTION || aAction == OFF_ACTION)
95         {
96             Set(new_state == kState_On);
97         }
98         else if (aAction == LEVEL_ACTION)
99         {
100             SetLevel(*value);
101         }
102
103         if (mActionCompleted_CB)
104         {
105             mActionCompleted_CB(aAction, aActor);
106         }
107     }
108
109     return action_initiated;
110 }
111
112 void LightingManager::SetLevel(uint8_t aLevel)
113 {
114     LOG_INF("Setting brightness level to %u", aLevel);
115     mLevel = aLevel;
116     UpdateLight();
117 }
118
119 void LightingManager::Set(bool aOn)
120 {
121     mState = aOn ? kState_On : kState_Off;
122     UpdateLight();
123 }
124
125 void LightingManager::UpdateLight()
126 {
127     constexpr uint32_t kPwmWidthUs = 20000u;
128     const uint8_t level            = mState == kState_On ? mLevel : 0;
129     pwm_pin_set_usec(mPwmDevice, mPwmChannel, kPwmWidthUs, kPwmWidthUs * level / kMaxLevel, 0);
130 }