|
| 1 | +/** |
| 2 | + * |
| 3 | + * Copyright (c) 2022 Project CHIP Authors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "WindowCoveringManager.h" |
| 19 | +#include <app-common/zap-generated/attributes/Accessors.h> |
| 20 | +#include <platform/internal/CHIPDeviceLayerInternal.h> |
| 21 | +#include <system/SystemClock.h> |
| 22 | + |
| 23 | +using namespace chip; |
| 24 | +using namespace chip::app; |
| 25 | +using namespace chip::app::Clusters::WindowCovering; |
| 26 | +using namespace chip::System::Clock::Literals; |
| 27 | + |
| 28 | +namespace { |
| 29 | + |
| 30 | +constexpr const System::Clock::Milliseconds32 kIncrementMovementTimeout = 700_ms32; |
| 31 | +constexpr const uint16_t kDefaultMovementStep = 2000; |
| 32 | + |
| 33 | +} // namespace |
| 34 | + |
| 35 | +void WindowCoveringManager::Init(EndpointId endpoint) |
| 36 | +{ |
| 37 | + mState = OperationalState::Stall; |
| 38 | + mCurrentPosition.SetNonNull(static_cast<uint16_t>(0)); |
| 39 | + mTargetPosition.SetNonNull(static_cast<uint16_t>(0)); |
| 40 | + SetEndpoint(endpoint); |
| 41 | +} |
| 42 | + |
| 43 | +void WindowCoveringManager::HandleMovementTimer(System::Layer * layer, void * aAppState) |
| 44 | +{ |
| 45 | + WindowCoveringManager * manager = reinterpret_cast<WindowCoveringManager *>(aAppState); |
| 46 | + |
| 47 | + VerifyOrReturn(manager->mState != OperationalState::Stall); |
| 48 | + |
| 49 | + uint16_t currentPosition = manager->mCurrentPosition.Value(); |
| 50 | + uint16_t targetPosition = manager->mTargetPosition.Value(); |
| 51 | + |
| 52 | + ChipLogProgress(NotSpecified, "HandleMovementTimer:currentPosition:%u, targetPosition:%u", currentPosition, targetPosition); |
| 53 | + |
| 54 | + if (OperationalState::MovingUpOrOpen == manager->mState) |
| 55 | + { |
| 56 | + if (currentPosition > targetPosition) |
| 57 | + { |
| 58 | + uint16_t tempPosition = |
| 59 | + currentPosition > kDefaultMovementStep ? static_cast<uint16_t>(currentPosition - kDefaultMovementStep) : 0; |
| 60 | + currentPosition = tempPosition > targetPosition ? tempPosition : targetPosition; |
| 61 | + |
| 62 | + manager->mCurrentPosition.SetNonNull(currentPosition); |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + ChipLogProgress(NotSpecified, "Reached the target position"); |
| 67 | + return; |
| 68 | + } |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + if (currentPosition < targetPosition) |
| 73 | + { |
| 74 | + uint16_t tempPosition = static_cast<uint16_t>(currentPosition + kDefaultMovementStep); |
| 75 | + currentPosition = tempPosition < targetPosition ? tempPosition : targetPosition; |
| 76 | + manager->mCurrentPosition.SetNonNull(currentPosition); |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + ChipLogProgress(NotSpecified, "Reached the target position"); |
| 81 | + return; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if (HasFeature(manager->mEndpoint, Feature::kPositionAwareLift)) |
| 86 | + { |
| 87 | + LiftPositionSet(manager->mEndpoint, manager->mCurrentPosition); |
| 88 | + } |
| 89 | + |
| 90 | + if (HasFeature(manager->mEndpoint, Feature::kPositionAwareTilt)) |
| 91 | + { |
| 92 | + TiltPositionSet(manager->mEndpoint, manager->mCurrentPosition); |
| 93 | + } |
| 94 | + |
| 95 | + if (manager->mCurrentPosition != manager->mTargetPosition) |
| 96 | + { |
| 97 | + LogErrorOnFailure(DeviceLayer::SystemLayer().StartTimer(kIncrementMovementTimeout, HandleMovementTimer, aAppState)); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +CHIP_ERROR WindowCoveringManager::HandleMovement(WindowCoveringType type) |
| 102 | +{ |
| 103 | + mState = OperationalStateGet(mEndpoint, OperationalStatus::kGlobal); |
| 104 | + |
| 105 | + if (OperationalState::Stall == mState) |
| 106 | + { |
| 107 | + ChipLogProgress(NotSpecified, "Covering is currently not moving"); |
| 108 | + return CHIP_NO_ERROR; |
| 109 | + } |
| 110 | + |
| 111 | + // Cancel ongoing window covering movement timer if any. |
| 112 | + DeviceLayer::SystemLayer().CancelTimer(HandleMovementTimer, this); |
| 113 | + |
| 114 | + // At least one of the Lift and Tilt features SHALL be supported. |
| 115 | + if (type == WindowCoveringType::Lift) |
| 116 | + { |
| 117 | + Attributes::CurrentPositionLiftPercent100ths::Get(mEndpoint, mCurrentPosition); |
| 118 | + Attributes::TargetPositionLiftPercent100ths::Get(mEndpoint, mTargetPosition); |
| 119 | + } |
| 120 | + else |
| 121 | + { |
| 122 | + Attributes::CurrentPositionTiltPercent100ths::Get(mEndpoint, mCurrentPosition); |
| 123 | + Attributes::TargetPositionTiltPercent100ths::Get(mEndpoint, mTargetPosition); |
| 124 | + } |
| 125 | + |
| 126 | + VerifyOrReturnError(!mCurrentPosition.IsNull(), CHIP_ERROR_INCORRECT_STATE); |
| 127 | + VerifyOrReturnError(!mTargetPosition.IsNull(), CHIP_ERROR_INCORRECT_STATE); |
| 128 | + VerifyOrReturnError(mCurrentPosition != mTargetPosition, CHIP_ERROR_INCORRECT_STATE); |
| 129 | + |
| 130 | + return DeviceLayer::SystemLayer().StartTimer(kIncrementMovementTimeout, HandleMovementTimer, this); |
| 131 | +} |
| 132 | + |
| 133 | +CHIP_ERROR WindowCoveringManager::HandleStopMotion() |
| 134 | +{ |
| 135 | + |
| 136 | + DeviceLayer::SystemLayer().CancelTimer(HandleMovementTimer, this); |
| 137 | + return CHIP_NO_ERROR; |
| 138 | +} |
0 commit comments