| /* |
| * |
| * Copyright (c) 2020 Project CHIP Authors |
| * Copyright (c) 2019 Google LLC. |
| * All rights reserved. |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| #include "LightingManager.h" |
| #include "qvCHIP.h" |
| #include <support/logging/CHIPLogging.h> |
| |
| LightingManager LightingManager::sLight; |
| |
| int LightingManager::Init() |
| { |
| mState = kState_Off; |
| mLevel = 64; |
| return 0; |
| } |
| |
| bool LightingManager::IsTurnedOn() |
| { |
| return mState == kState_On; |
| } |
| |
| uint8_t LightingManager::GetLevel() |
| { |
| return mLevel; |
| } |
| |
| void LightingManager::SetCallbacks(LightingCallback_fn aActionInitiated_CB, LightingCallback_fn aActionCompleted_CB) |
| { |
| mActionInitiated_CB = aActionInitiated_CB; |
| mActionCompleted_CB = aActionCompleted_CB; |
| } |
| |
| bool LightingManager::InitiateAction(Action_t aAction, int32_t aActor, uint8_t size, uint8_t * value) |
| { |
| // TODO: this function is called InitiateAction because we want to implement some features such as ramping up here. |
| bool action_initiated = false; |
| State_t new_state; |
| |
| switch (aAction) |
| { |
| case ON_ACTION: |
| ChipLogProgress(NotSpecified, "LightingManager::InitiateAction(ON_ACTION)"); |
| break; |
| case OFF_ACTION: |
| ChipLogProgress(NotSpecified, "LightingManager::InitiateAction(OFF_ACTION)"); |
| break; |
| case LEVEL_ACTION: |
| ChipLogProgress(NotSpecified, "LightingManager::InitiateAction(LEVEL_ACTION)"); |
| break; |
| default: |
| ChipLogProgress(NotSpecified, "LightingManager::InitiateAction(unknown)"); |
| break; |
| } |
| |
| // Initiate On/Off Action only when the previous one is complete. |
| if (mState == kState_Off && aAction == ON_ACTION) |
| { |
| action_initiated = true; |
| new_state = kState_On; |
| } |
| else if (mState == kState_On && aAction == OFF_ACTION) |
| { |
| action_initiated = true; |
| new_state = kState_Off; |
| } |
| else if (aAction == LEVEL_ACTION && *value != mLevel) |
| { |
| action_initiated = true; |
| if (*value == 0) |
| { |
| new_state = kState_Off; |
| } |
| else |
| { |
| new_state = kState_On; |
| } |
| } |
| |
| if (action_initiated) |
| { |
| if (mActionInitiated_CB) |
| { |
| mActionInitiated_CB(aAction); |
| } |
| if (aAction == LEVEL_ACTION) |
| { |
| SetLevel(*value); |
| } |
| else |
| { |
| Set(new_state == kState_On); |
| } |
| |
| if (mActionCompleted_CB) |
| { |
| mActionCompleted_CB(aAction); |
| } |
| } |
| |
| return action_initiated; |
| } |
| |
| void LightingManager::SetLevel(uint8_t aLevel) |
| { |
| mLevel = aLevel; |
| UpdateLight(); |
| } |
| |
| void LightingManager::Set(bool aOn) |
| { |
| if (aOn) |
| { |
| mState = kState_On; |
| } |
| else |
| { |
| mState = kState_Off; |
| } |
| UpdateLight(); |
| } |
| |
| void LightingManager::UpdateLight() |
| { |
| ChipLogProgress(NotSpecified, "UpdateLight: %d %d", mState, mLevel); |
| qvCHIP_PWMSetColor(mLevel, mLevel, mLevel); |
| qvCHIP_PWMColorOnOff(mState == kState_On); |
| } |