Apollo  6.0
Open source self driving car software
py_timer.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2019 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 
17 #ifndef CYBER_PYTHON_INTERNAL_PY_TIMER_H_
18 #define CYBER_PYTHON_INTERNAL_PY_TIMER_H_
19 
20 #include <unistd.h>
21 #include <functional>
22 #include <memory>
23 
24 #include "cyber/cyber.h"
25 #include "cyber/init.h"
26 #include "cyber/timer/timer.h"
27 
28 namespace apollo {
29 namespace cyber {
30 
31 class PyTimer {
32  public:
33  PyTimer() { timer_ = std::make_shared<Timer>(); }
34 
35  PyTimer(uint32_t period, void (*func)(), bool oneshot) {
36  std::function<void()> bound_f = std::bind(func);
37  timer_ = std::make_shared<Timer>(period, bound_f, oneshot);
38  }
39 
40  void start() { timer_->Start(); }
41 
42  void stop() { timer_->Stop(); }
43 
44  void set_option(uint32_t period, void (*func)(), bool oneshot) {
45  std::function<void()> bound_f = std::bind(func);
46  TimerOption time_opt;
47  time_opt.period = period;
48  time_opt.callback = bound_f;
49  time_opt.oneshot = oneshot;
50  timer_->SetTimerOption(time_opt);
51  }
52 
53  private:
54  std::shared_ptr<Timer> timer_ = nullptr;
55 };
56 
57 } // namespace cyber
58 } // namespace apollo
59 
60 #endif // CYBER_PYTHON_INTERNAL_PY_TIMER_H_
void(* func)(void *)
Definition: routine_context.h:41
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
std::function< void()> callback
Definition: timer.h:57
bool oneshot
Definition: timer.h:63
void start()
Definition: py_timer.h:40
void set_option(uint32_t period, void(*func)(), bool oneshot)
Definition: py_timer.h:44
void stop()
Definition: py_timer.h:42
PyTimer()
Definition: py_timer.h:33
uint32_t period
The period of the timer, unit is ms max: 512 * 64 min: 1.
Definition: timer.h:54
PyTimer(uint32_t period, void(*func)(), bool oneshot)
Definition: py_timer.h:35
Definition: py_timer.h:31
The options of timer.
Definition: timer.h:32