LightingManager.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. *
  3. * Copyright (c) 2019 Google LLC.
  4. * All rights reserved.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #pragma once
  19. #include <stdbool.h>
  20. #include <stdint.h>
  21. #include "AppEvent.h"
  22. #include "FreeRTOS.h"
  23. #include "timers.h" // provides FreeRTOS timer support
  24. #include <lib/core/CHIPError.h>
  25. class LightingManager
  26. {
  27. public:
  28. enum Action_t
  29. {
  30. ON_ACTION = 0,
  31. OFF_ACTION,
  32. INVALID_ACTION
  33. } Action;
  34. enum State_t
  35. {
  36. kState_OffInitiated = 0,
  37. kState_OffCompleted,
  38. kState_OnInitiated,
  39. kState_OnCompleted,
  40. } State;
  41. CHIP_ERROR Init();
  42. bool IsLightOn();
  43. void EnableAutoTurnOff(bool aOn);
  44. void SetAutoTurnOffDuration(uint32_t aDurationInSecs);
  45. bool IsActionInProgress();
  46. bool InitiateAction(int32_t aActor, Action_t aAction);
  47. typedef void (*Callback_fn_initiated)(Action_t, int32_t aActor);
  48. typedef void (*Callback_fn_completed)(Action_t);
  49. void SetCallbacks(Callback_fn_initiated aActionInitiated_CB, Callback_fn_completed aActionCompleted_CB);
  50. private:
  51. friend LightingManager & LightMgr(void);
  52. State_t mState;
  53. Callback_fn_initiated mActionInitiated_CB;
  54. Callback_fn_completed mActionCompleted_CB;
  55. bool mAutoTurnOff;
  56. uint32_t mAutoTurnOffDuration;
  57. bool mAutoTurnOffTimerArmed;
  58. void CancelTimer(void);
  59. void StartTimer(uint32_t aTimeoutMs);
  60. static void TimerEventHandler(TimerHandle_t xTimer);
  61. static void AutoTurnOffTimerEventHandler(AppEvent * aEvent);
  62. static void ActuatorMovementTimerEventHandler(AppEvent * aEvent);
  63. static LightingManager sLight;
  64. };
  65. inline LightingManager & LightMgr(void)
  66. {
  67. return LightingManager::sLight;
  68. }