3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2019 Google LLC.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 #include "LightingManager.h"
22 #include "AppConfig.h"
26 LightingManager LightingManager::sLight;
28 TimerHandle_t sLightTimer;
30 int LightingManager::Init()
32 // Create FreeRTOS sw timer for light timer.
33 sLightTimer = xTimerCreate("lightTmr", // Just a text name, not used by the RTOS kernel
34 1, // == default timer period (mS)
35 false, // no timer reload (==one-shot)
36 (void *) this, // init timer id = light obj context
37 TimerEventHandler // timer callback handler
40 if (sLightTimer == NULL)
42 EFR32_LOG("sLightTimer timer create failed");
43 appError(CHIP_ERROR_MAX);
46 mState = kState_OffCompleted;
47 mAutoTurnOffTimerArmed = false;
49 mAutoTurnOffDuration = 0;
54 void LightingManager::SetCallbacks(Callback_fn_initiated aActionInitiated_CB, Callback_fn_completed aActionCompleted_CB)
56 mActionInitiated_CB = aActionInitiated_CB;
57 mActionCompleted_CB = aActionCompleted_CB;
60 bool LightingManager::IsActionInProgress()
62 return (mState == kState_OffInitiated || mState == kState_OnInitiated);
65 bool LightingManager::IsLightOn()
67 return (mState == kState_OnCompleted);
70 void LightingManager::EnableAutoTurnOff(bool aOn)
75 void LightingManager::SetAutoTurnOffDuration(uint32_t aDurationInSecs)
77 mAutoTurnOffDuration = aDurationInSecs;
80 bool LightingManager::InitiateAction(int32_t aActor, Action_t aAction)
82 bool action_initiated = false;
85 // Initiate Turn On/Off Action only when the previous one is complete.
86 if (mState == kState_OffCompleted && aAction == ON_ACTION)
88 action_initiated = true;
90 new_state = kState_OnInitiated;
92 else if (mState == kState_OnCompleted && aAction == OFF_ACTION)
94 action_initiated = true;
96 new_state = kState_OffInitiated;
101 if (mAutoTurnOffTimerArmed && new_state == kState_OffInitiated)
103 // If auto turn off timer has been armed and someone initiates turning off,
104 // cancel the timer and continue as normal.
105 mAutoTurnOffTimerArmed = false;
110 StartTimer(ACTUATOR_MOVEMENT_PERIOS_MS);
112 // Since the timer started successfully, update the state and trigger callback
115 if (mActionInitiated_CB)
117 mActionInitiated_CB(aAction, aActor);
121 return action_initiated;
124 void LightingManager::StartTimer(uint32_t aTimeoutMs)
126 if (xTimerIsTimerActive(sLightTimer))
128 EFR32_LOG("app timer already started!");
132 // timer is not active, change its period to required value (== restart).
133 // FreeRTOS- Block for a maximum of 100 ticks if the change period command
134 // cannot immediately be sent to the timer command queue.
135 if (xTimerChangePeriod(sLightTimer, (aTimeoutMs / portTICK_PERIOD_MS), 100) != pdPASS)
137 EFR32_LOG("sLightTimer timer start() failed");
138 appError(CHIP_ERROR_MAX);
142 void LightingManager::CancelTimer(void)
144 if (xTimerStop(sLightTimer, 0) == pdFAIL)
146 EFR32_LOG("sLightTimer stop() failed");
147 appError(CHIP_ERROR_MAX);
151 void LightingManager::TimerEventHandler(TimerHandle_t xTimer)
153 // Get light obj context from timer id.
154 LightingManager * light = static_cast<LightingManager *>(pvTimerGetTimerID(xTimer));
156 // The timer event handler will be called in the context of the timer task
157 // once sLightTimer expires. Post an event to apptask queue with the actual handler
158 // so that the event can be handled in the context of the apptask.
160 event.Type = AppEvent::kEventType_Timer;
161 event.TimerEvent.Context = light;
162 if (light->mAutoTurnOffTimerArmed)
164 event.Handler = AutoTurnOffTimerEventHandler;
168 event.Handler = ActuatorMovementTimerEventHandler;
170 GetAppTask().PostEvent(&event);
173 void LightingManager::AutoTurnOffTimerEventHandler(AppEvent * aEvent)
175 LightingManager * light = static_cast<LightingManager *>(aEvent->TimerEvent.Context);
178 // Make sure auto turn off timer is still armed.
179 if (!light->mAutoTurnOffTimerArmed)
184 light->mAutoTurnOffTimerArmed = false;
186 EFR32_LOG("Auto Turn Off has been triggered!");
188 light->InitiateAction(actor, OFF_ACTION);
191 void LightingManager::ActuatorMovementTimerEventHandler(AppEvent * aEvent)
193 Action_t actionCompleted = INVALID_ACTION;
195 LightingManager * light = static_cast<LightingManager *>(aEvent->TimerEvent.Context);
197 if (light->mState == kState_OffInitiated)
199 light->mState = kState_OffCompleted;
200 actionCompleted = OFF_ACTION;
202 else if (light->mState == kState_OnInitiated)
204 light->mState = kState_OnCompleted;
205 actionCompleted = ON_ACTION;
208 if (actionCompleted != INVALID_ACTION)
210 if (light->mActionCompleted_CB)
212 light->mActionCompleted_CB(actionCompleted);
215 if (light->mAutoTurnOff && actionCompleted == ON_ACTION)
217 // Start the timer for auto turn off
218 light->StartTimer(light->mAutoTurnOffDuration * 1000);
220 light->mAutoTurnOffTimerArmed = true;
222 EFR32_LOG("Auto Turn off enabled. Will be triggered in %u seconds", light->mAutoTurnOffDuration);