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