Apollo 10.0
自动驾驶开放平台
py_timer.h
浏览该文件的文档.
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
28namespace apollo {
29namespace cyber {
30
31class 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_
PyTimer(uint32_t period, void(*func)(), bool oneshot)
Definition py_timer.h:35
void set_option(uint32_t period, void(*func)(), bool oneshot)
Definition py_timer.h:44
class register implement
Definition arena_queue.h:37
The options of timer
Definition timer.h:32
uint32_t period
The period of the timer, unit is ms max: 512 * 64 min: 1
Definition timer.h:54
bool oneshot
True: perform the callback only after the first timing cycle False: perform the callback every timed ...
Definition timer.h:63
std::function< void()> callback
The task that the timer needs to perform
Definition timer.h:57