1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef GRAPHIC_LITE_TIMER_H 17 #define GRAPHIC_LITE_TIMER_H 18 19 #include <cstdint> 20 #ifdef _WIN32 21 #elif defined(_LITEOS) 22 #include "cmsis_os2.h" 23 #else 24 #include <ctime> 25 #endif 26 27 namespace OHOS { 28 class GraphicTimer { 29 public: 30 using GraphicTimerCb = void (*)(void*); 31 GraphicTimer(int32_t periodMs, GraphicTimerCb cb, void* arg, bool isPeriodic = false); 32 ~GraphicTimer(); 33 34 bool Start(); 35 bool SetPeriod(int32_t periodMs); GetPeriod()36 int32_t GetPeriod() 37 { 38 return periodMs_; 39 } 40 41 #ifdef _WIN32 GetNativeTimer()42 void* GetNativeTimer() 43 #elif defined(_LITEOS) 44 osTimerId_t GetNativeTimer() 45 #else 46 timer_t GetNativeTimer() 47 #endif 48 { 49 return timer_; 50 } 51 52 void Stop(); IsPeriodic()53 bool IsPeriodic() 54 { 55 return isPeriodic_; 56 } 57 Callback()58 void Callback() 59 { 60 if (cb_ != nullptr) { 61 cb_(arg_); 62 } 63 } 64 65 static constexpr int32_t MAX_PERIOD_MS = 36E5; 66 67 private: 68 int32_t periodMs_ = -1; 69 GraphicTimerCb cb_ = nullptr; 70 void* arg_ = nullptr; 71 bool isPeriodic_ = false; 72 73 #ifdef _WIN32 74 void* timer_ = nullptr; 75 #elif defined(_LITEOS) 76 osTimerId_t timer_; 77 #else 78 timer_t timer_; 79 #endif 80 81 GraphicTimer() = delete; 82 GraphicTimer(const GraphicTimer&) = delete; 83 GraphicTimer& operator=(const GraphicTimer&) = delete; 84 GraphicTimer(GraphicTimer&&) = delete; 85 GraphicTimer& operator=(GraphicTimer&&) = delete; 86 }; 87 }; // namespace OHOS 88 #endif // GRAPHIC_LITE_TIMER_H 89