WindowManager.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. *
  3. * Copyright (c) 2021 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. #pragma once
  18. #include <app/clusters/window-covering-server/window-covering-server.h>
  19. #include <lib/core/CHIPError.h>
  20. #include "AppEvent.h"
  21. #include "LEDWidget.h"
  22. #include <FreeRTOS.h>
  23. #include <string>
  24. #include <task.h>
  25. #include <timers.h>
  26. #ifdef DISPLAY_ENABLED
  27. #include <LcdPainter.h>
  28. #endif
  29. using namespace chip::app::Clusters::WindowCovering;
  30. class WindowManager
  31. {
  32. public:
  33. static WindowManager sWindow;
  34. struct Timer
  35. {
  36. typedef void (*Callback)(Timer & timer);
  37. Timer(uint32_t timeoutInMs, Callback callback, void * context);
  38. void Start();
  39. void Stop();
  40. void Timeout();
  41. Callback mCallback = nullptr;
  42. void * mContext = nullptr;
  43. bool mIsActive = false;
  44. TimerHandle_t mHandler = nullptr;
  45. private:
  46. static void TimerCallback(TimerHandle_t xTimer);
  47. };
  48. struct Cover
  49. {
  50. void Init(chip::EndpointId endpoint);
  51. void LiftUpdate(bool newTarget);
  52. void LiftGoToTarget() { LiftUpdate(true); }
  53. void LiftContinueToTarget() { LiftUpdate(false); }
  54. void LiftStepToward(OperationalState direction);
  55. void LiftSchedulePositionSet(chip::Percent100ths position) { SchedulePositionSet(position, false); }
  56. void LiftScheduleOperationalStateSet(OperationalState opState) { ScheduleOperationalStateSet(opState, false); }
  57. void TiltUpdate(bool newTarget);
  58. void TiltGoToTarget() { TiltUpdate(true); }
  59. void TiltContinueToTarget() { TiltUpdate(false); }
  60. void TiltStepToward(OperationalState direction);
  61. void TiltSchedulePositionSet(chip::Percent100ths position) { SchedulePositionSet(position, true); }
  62. void TiltScheduleOperationalStateSet(OperationalState opState) { ScheduleOperationalStateSet(opState, true); }
  63. void UpdateTargetPosition(OperationalState direction, bool isTilt);
  64. Type CycleType();
  65. static void OnLiftTimeout(Timer & timer);
  66. static void OnTiltTimeout(Timer & timer);
  67. chip::EndpointId mEndpoint = 0;
  68. Timer * mLiftTimer = nullptr;
  69. Timer * mTiltTimer = nullptr;
  70. OperationalState mLiftOpState = OperationalState::Stall;
  71. OperationalState mTiltOpState = OperationalState::Stall;
  72. struct CoverWorkData
  73. {
  74. chip::EndpointId mEndpointId;
  75. bool isTilt;
  76. union
  77. {
  78. chip::Percent100ths percent100ths;
  79. OperationalState opState;
  80. };
  81. };
  82. void SchedulePositionSet(chip::Percent100ths position, bool isTilt);
  83. static void CallbackPositionSet(intptr_t arg);
  84. void ScheduleOperationalStateSet(OperationalState opState, bool isTilt);
  85. static void CallbackOperationalStateSet(intptr_t arg);
  86. };
  87. static WindowManager & Instance();
  88. WindowManager();
  89. CHIP_ERROR Init();
  90. void PostAttributeChange(chip::EndpointId endpoint, chip::AttributeId attributeId);
  91. static void ButtonEventHandler(uint8_t button, uint8_t btnAction);
  92. void UpdateLEDs();
  93. void UpdateLCD();
  94. static void GeneralEventHandler(AppEvent * aEvent);
  95. static void OnIconTimeout(WindowManager::Timer & timer);
  96. protected:
  97. struct StateFlags
  98. {
  99. #if CHIP_ENABLE_OPENTHREAD
  100. bool isThreadProvisioned = false;
  101. bool isThreadEnabled = false;
  102. #else
  103. bool isWiFiProvisioned = false;
  104. bool isWiFiEnabled = false;
  105. #endif
  106. bool haveBLEConnections = false;
  107. bool isWinking = false;
  108. };
  109. Cover & GetCover();
  110. Cover * GetCover(chip::EndpointId endpoint);
  111. static void OnLongPressTimeout(Timer & timer);
  112. Timer * mLongPressTimer = nullptr;
  113. StateFlags mState;
  114. bool mTiltMode = false;
  115. bool mUpPressed = false;
  116. bool mDownPressed = false;
  117. bool mUpSuppressed = false;
  118. bool mDownSuppressed = false;
  119. bool mResetWarning = false;
  120. private:
  121. void HandleLongPress();
  122. void DispatchEventAttributeChange(chip::EndpointId endpoint, chip::AttributeId attribute);
  123. Cover mCoverList[WINDOW_COVER_COUNT];
  124. uint8_t mCurrentCover = 0;
  125. LEDWidget mStatusLED;
  126. LEDWidget mActionLED;
  127. #ifdef DISPLAY_ENABLED
  128. Timer mIconTimer;
  129. LcdIcon mIcon = LcdIcon::None;
  130. #endif
  131. };