WindowCoveringManager.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #include "WindowCoveringManager.h"
  18. #include <app-common/zap-generated/attributes/Accessors.h>
  19. #include <platform/internal/CHIPDeviceLayerInternal.h>
  20. #include <system/SystemClock.h>
  21. using namespace chip;
  22. using namespace chip::app;
  23. using namespace chip::app::Clusters::WindowCovering;
  24. using namespace chip::System::Clock::Literals;
  25. namespace {
  26. constexpr const System::Clock::Milliseconds32 kIncrementMovementTimeout = 700_ms32;
  27. constexpr const uint16_t kDefaultMovementStep = 2000;
  28. } // namespace
  29. void WindowCoveringManager::Init(EndpointId endpoint)
  30. {
  31. mState = OperationalState::Stall;
  32. mCurrentPosition.SetNonNull(static_cast<uint16_t>(0));
  33. mTargetPosition.SetNonNull(static_cast<uint16_t>(0));
  34. SetEndpoint(endpoint);
  35. }
  36. void WindowCoveringManager::HandleMovementTimer(System::Layer * layer, void * aAppState)
  37. {
  38. WindowCoveringManager * manager = reinterpret_cast<WindowCoveringManager *>(aAppState);
  39. VerifyOrReturn(manager->mState != OperationalState::Stall);
  40. uint16_t currentPosition = manager->mCurrentPosition.Value();
  41. uint16_t targetPosition = manager->mTargetPosition.Value();
  42. ChipLogProgress(NotSpecified, "HandleMovementTimer:currentPosition:%u, targetPosition:%u", currentPosition, targetPosition);
  43. if (OperationalState::MovingUpOrOpen == manager->mState)
  44. {
  45. if (currentPosition > targetPosition)
  46. {
  47. uint16_t tempPosition =
  48. currentPosition > kDefaultMovementStep ? static_cast<uint16_t>(currentPosition - kDefaultMovementStep) : 0;
  49. currentPosition = tempPosition > targetPosition ? tempPosition : targetPosition;
  50. manager->mCurrentPosition.SetNonNull(currentPosition);
  51. }
  52. else
  53. {
  54. ChipLogProgress(NotSpecified, "Reached the target position");
  55. return;
  56. }
  57. }
  58. else
  59. {
  60. if (currentPosition < targetPosition)
  61. {
  62. uint16_t tempPosition = static_cast<uint16_t>(currentPosition + kDefaultMovementStep);
  63. currentPosition = tempPosition < targetPosition ? tempPosition : targetPosition;
  64. manager->mCurrentPosition.SetNonNull(currentPosition);
  65. }
  66. else
  67. {
  68. ChipLogProgress(NotSpecified, "Reached the target position");
  69. return;
  70. }
  71. }
  72. if (HasFeature(manager->mEndpoint, Feature::kPositionAwareLift))
  73. {
  74. LiftPositionSet(manager->mEndpoint, manager->mCurrentPosition);
  75. }
  76. if (HasFeature(manager->mEndpoint, Feature::kPositionAwareTilt))
  77. {
  78. TiltPositionSet(manager->mEndpoint, manager->mCurrentPosition);
  79. }
  80. if (manager->mCurrentPosition != manager->mTargetPosition)
  81. {
  82. LogErrorOnFailure(DeviceLayer::SystemLayer().StartTimer(kIncrementMovementTimeout, HandleMovementTimer, aAppState));
  83. }
  84. }
  85. CHIP_ERROR WindowCoveringManager::HandleMovement(WindowCoveringType type)
  86. {
  87. mState = OperationalStateGet(mEndpoint, OperationalStatus::kGlobal);
  88. if (OperationalState::Stall == mState)
  89. {
  90. ChipLogProgress(NotSpecified, "Covering is currently not moving");
  91. return CHIP_NO_ERROR;
  92. }
  93. // Cancel ongoing window covering movement timer if any.
  94. DeviceLayer::SystemLayer().CancelTimer(HandleMovementTimer, this);
  95. // At least one of the Lift and Tilt features SHALL be supported.
  96. if (type == WindowCoveringType::Lift)
  97. {
  98. Attributes::CurrentPositionLiftPercent100ths::Get(mEndpoint, mCurrentPosition);
  99. Attributes::TargetPositionLiftPercent100ths::Get(mEndpoint, mTargetPosition);
  100. }
  101. else
  102. {
  103. Attributes::CurrentPositionTiltPercent100ths::Get(mEndpoint, mCurrentPosition);
  104. Attributes::TargetPositionTiltPercent100ths::Get(mEndpoint, mTargetPosition);
  105. }
  106. VerifyOrReturnError(!mCurrentPosition.IsNull(), CHIP_ERROR_INCORRECT_STATE);
  107. VerifyOrReturnError(!mTargetPosition.IsNull(), CHIP_ERROR_INCORRECT_STATE);
  108. VerifyOrReturnError(mCurrentPosition != mTargetPosition, CHIP_ERROR_INCORRECT_STATE);
  109. return DeviceLayer::SystemLayer().StartTimer(kIncrementMovementTimeout, HandleMovementTimer, this);
  110. }
  111. CHIP_ERROR WindowCoveringManager::HandleStopMotion()
  112. {
  113. DeviceLayer::SystemLayer().CancelTimer(HandleMovementTimer, this);
  114. return CHIP_NO_ERROR;
  115. }