WindowApp.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. using namespace chip::app::Clusters::WindowCovering;
  21. class WindowApp
  22. {
  23. public:
  24. struct Timer
  25. {
  26. typedef void (*Callback)(Timer & timer);
  27. Timer(const char * name, uint32_t timeoutInMs, Callback callback, void * context) :
  28. mName(name), mTimeoutInMs(timeoutInMs), mCallback(callback), mContext(context)
  29. {}
  30. virtual ~Timer() = default;
  31. virtual void Start() = 0;
  32. virtual void Stop() = 0;
  33. void Timeout();
  34. const char * mName = nullptr;
  35. uint32_t mTimeoutInMs = 0;
  36. Callback mCallback = nullptr;
  37. void * mContext = nullptr;
  38. bool mIsActive = false;
  39. };
  40. struct Button
  41. {
  42. enum class Id
  43. {
  44. Up = 0,
  45. Down = 1
  46. };
  47. Button(Id id, const char * name) : mId(id), mName(name) {}
  48. virtual ~Button() = default;
  49. void Press();
  50. void Release();
  51. Id mId;
  52. const char * mName = nullptr;
  53. };
  54. enum class EventId
  55. {
  56. None = 0,
  57. Reset,
  58. ResetPressed,
  59. ResetWarning,
  60. ResetCanceled,
  61. // Button events
  62. UpPressed,
  63. UpReleased,
  64. DownPressed,
  65. DownReleased,
  66. // Cover events
  67. CoverChange,
  68. CoverTypeChange,
  69. TiltModeChange,
  70. // Cover Attribute update events
  71. AttributeChange,
  72. // Provisioning events
  73. ProvisionedStateChanged,
  74. ConnectivityStateChanged,
  75. BLEConnectionsChanged,
  76. WinkOff,
  77. WinkOn,
  78. };
  79. struct Event
  80. {
  81. Event(EventId id) : mId(id), mEndpoint(0) {}
  82. Event(EventId id, chip::EndpointId endpoint) : mId(id), mEndpoint(endpoint) {}
  83. Event(EventId id, chip::EndpointId endpoint, chip::AttributeId attributeId) :
  84. mId(id), mEndpoint(endpoint), mAttributeId(attributeId)
  85. {}
  86. EventId mId;
  87. chip::EndpointId mEndpoint;
  88. chip::AttributeId mAttributeId;
  89. };
  90. struct Cover
  91. {
  92. void Init(chip::EndpointId endpoint);
  93. void Finish();
  94. void LiftUpdate(bool newTarget);
  95. void LiftGoToTarget() { LiftUpdate(true); }
  96. void LiftContinueToTarget() { LiftUpdate(false); }
  97. void LiftStepToward(OperationalState direction);
  98. void LiftSchedulePositionSet(chip::Percent100ths position) { SchedulePositionSet(position, false); }
  99. void LiftScheduleOperationalStateSet(OperationalState opState) { ScheduleOperationalStateSet(opState, false); }
  100. void TiltUpdate(bool newTarget);
  101. void TiltGoToTarget() { TiltUpdate(true); }
  102. void TiltContinueToTarget() { TiltUpdate(false); }
  103. void TiltStepToward(OperationalState direction);
  104. void TiltSchedulePositionSet(chip::Percent100ths position) { SchedulePositionSet(position, true); }
  105. void TiltScheduleOperationalStateSet(OperationalState opState) { ScheduleOperationalStateSet(opState, true); }
  106. void UpdateTargetPosition(OperationalState direction, bool isTilt);
  107. void StepToward(OperationalState direction, bool isTilt);
  108. Type CycleType();
  109. static void OnLiftTimeout(Timer & timer);
  110. static void OnTiltTimeout(Timer & timer);
  111. chip::EndpointId mEndpoint = 0;
  112. Timer * mLiftTimer = nullptr;
  113. Timer * mTiltTimer = nullptr;
  114. OperationalState mLiftOpState = OperationalState::Stall;
  115. OperationalState mTiltOpState = OperationalState::Stall;
  116. struct CoverWorkData
  117. {
  118. chip::EndpointId mEndpointId;
  119. bool isTilt;
  120. union
  121. {
  122. chip::Percent100ths percent100ths;
  123. OperationalState opState;
  124. };
  125. };
  126. void SchedulePositionSet(chip::Percent100ths position, bool isTilt);
  127. static void CallbackPositionSet(intptr_t arg);
  128. void ScheduleOperationalStateSet(OperationalState opState, bool isTilt);
  129. static void CallbackOperationalStateSet(intptr_t arg);
  130. };
  131. static WindowApp & Instance();
  132. virtual ~WindowApp() = default;
  133. virtual CHIP_ERROR Init();
  134. virtual CHIP_ERROR Start() = 0;
  135. virtual CHIP_ERROR Run();
  136. virtual void Finish();
  137. virtual void PostEvent(const Event & event) = 0;
  138. virtual void PostAttributeChange(chip::EndpointId endpoint, chip::AttributeId attributeId) = 0;
  139. protected:
  140. struct StateFlags
  141. {
  142. #if CHIP_ENABLE_OPENTHREAD
  143. bool isThreadProvisioned = false;
  144. bool isThreadEnabled = false;
  145. #else
  146. bool isWiFiProvisioned = false;
  147. bool isWiFiEnabled = false;
  148. #endif
  149. bool haveBLEConnections = false;
  150. bool isWinking = false;
  151. };
  152. Cover & GetCover();
  153. Cover * GetCover(chip::EndpointId endpoint);
  154. virtual Button * CreateButton(Button::Id id, const char * name) = 0;
  155. virtual void DestroyButton(Button * b);
  156. virtual Timer * CreateTimer(const char * name, uint32_t timeoutInMs, Timer::Callback callback, void * context) = 0;
  157. virtual void DestroyTimer(Timer * timer);
  158. virtual void ProcessEvents() = 0;
  159. virtual void DispatchEvent(const Event & event);
  160. virtual void OnMainLoop() = 0;
  161. static void OnLongPressTimeout(Timer & timer);
  162. Timer * mLongPressTimer = nullptr;
  163. Button * mButtonUp = nullptr;
  164. Button * mButtonDown = nullptr;
  165. StateFlags mState;
  166. bool mTiltMode = false;
  167. bool mUpPressed = false;
  168. bool mDownPressed = false;
  169. bool mUpSuppressed = false;
  170. bool mDownSuppressed = false;
  171. bool mResetWarning = false;
  172. private:
  173. void HandleLongPress();
  174. void DispatchEventAttributeChange(chip::EndpointId endpoint, chip::AttributeId attribute);
  175. Cover mCoverList[WINDOW_COVER_COUNT];
  176. uint8_t mCurrentCover = 0;
  177. };