Apollo 10.0
自动驾驶开放平台
py_timer.cc
浏览该文件的文档.
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
18
19#include <set>
20#include <string>
21
22#include <Python.h>
23
25
26#define PyInt_AsLong PyLong_AsLong
27
28template <typename T>
29T PyObjectToPtr(PyObject* pyobj, const std::string& type_ptr) {
30 T obj_ptr = (T)PyCapsule_GetPointer(pyobj, type_ptr.c_str());
31 if (obj_ptr == nullptr) {
32 AERROR << "PyObjectToPtr failed,type->" << type_ptr << "pyobj: " << pyobj;
33 }
34 return obj_ptr;
35}
36
37PyObject* cyber_new_PyTimer(PyObject* self, PyObject* args) {
38 uint32_t period = 0;
39 PyObject* pyobj_regist_fun = nullptr;
40 unsigned int oneshot = 0;
41 if (!PyArg_ParseTuple(args, const_cast<char*>("kOI:cyber_new_PyTimer"),
42 &period, &pyobj_regist_fun, &oneshot)) {
43 AERROR << "cyber_new_PyTimer parsetuple failed!";
44 Py_INCREF(Py_None);
45 return Py_None;
46 }
47
48 void (*callback_fun)() = (void (*)())0;
49 callback_fun = (void (*)())PyInt_AsLong(pyobj_regist_fun);
50 PyTimer* pytimer = new PyTimer(period, callback_fun, oneshot != 0);
51 return PyCapsule_New(pytimer, "apollo_cybertron_pytimer", nullptr);
52}
53
54PyObject* cyber_new_PyTimer_noparam(PyObject* self, PyObject* args) {
55 PyTimer* pytimer = new PyTimer();
56 return PyCapsule_New(pytimer, "apollo_cybertron_pytimer", nullptr);
57}
58
59PyObject* cyber_delete_PyTimer(PyObject* self, PyObject* args) {
60 PyObject* pyobj_timer = nullptr;
61 if (!PyArg_ParseTuple(args, const_cast<char*>("O:cyber_delete_PyTimer"),
62 &pyobj_timer)) {
63 Py_INCREF(Py_None);
64 return Py_None;
65 }
66
67 auto* pytimer = reinterpret_cast<PyTimer*>(
68 PyCapsule_GetPointer(pyobj_timer, "apollo_cybertron_pytimer"));
69 if (nullptr == pytimer) {
70 AERROR << "cyber_delete_PyTimer:timer ptr is null!";
71 Py_INCREF(Py_None);
72 return Py_None;
73 }
74 delete pytimer;
75 Py_INCREF(Py_None);
76 return Py_None;
77}
78
79PyObject* cyber_PyTimer_start(PyObject* self, PyObject* args) {
80 PyObject* pyobj_timer = nullptr;
81 if (!PyArg_ParseTuple(args, const_cast<char*>("O:cyber_delete_PyTimer"),
82 &pyobj_timer)) {
83 Py_INCREF(Py_None);
84 return Py_None;
85 }
86
87 auto* pytimer = reinterpret_cast<PyTimer*>(
88 PyCapsule_GetPointer(pyobj_timer, "apollo_cybertron_pytimer"));
89 if (nullptr == pytimer) {
90 AERROR << "cyber_delete_PyTimer:timer ptr is null!";
91 Py_INCREF(Py_None);
92 return Py_None;
93 }
94 pytimer->start();
95 Py_INCREF(Py_None);
96 return Py_None;
97}
98
99PyObject* cyber_PyTimer_stop(PyObject* self, PyObject* args) {
100 PyObject* pyobj_timer = nullptr;
101 if (!PyArg_ParseTuple(args, const_cast<char*>("O:cyber_delete_PyTimer"),
102 &pyobj_timer)) {
103 Py_INCREF(Py_None);
104 return Py_None;
105 }
106
107 auto* pytimer = reinterpret_cast<PyTimer*>(
108 PyCapsule_GetPointer(pyobj_timer, "apollo_cybertron_pytimer"));
109 if (nullptr == pytimer) {
110 AERROR << "cyber_delete_PyTimer:timer ptr is null!";
111 Py_INCREF(Py_None);
112 return Py_None;
113 }
114 pytimer->stop();
115 Py_INCREF(Py_None);
116 return Py_None;
117}
118
119PyObject* cyber_PyTimer_set_option(PyObject* self, PyObject* args) {
120 PyObject* pyobj_timer = nullptr;
121 uint32_t period = 0;
122 PyObject* pyobj_regist_fun = nullptr;
123 unsigned int oneshot = 0;
124
125 void (*callback_fun)() = (void (*)())0;
126
127 if (!PyArg_ParseTuple(args,
128 const_cast<char*>("OkOI:cyber_PyTimer_set_option"),
129 &pyobj_timer, &period, &pyobj_regist_fun, &oneshot)) {
130 Py_INCREF(Py_None);
131 return Py_None;
132 }
133
134 PyTimer* pytimer =
135 PyObjectToPtr<PyTimer*>(pyobj_timer, "apollo_cybertron_pytimer");
136 callback_fun = (void (*)())PyInt_AsLong(pyobj_regist_fun);
137 if (nullptr == pytimer) {
138 AERROR << "cyber_PyTimer_set_option ptr is null!";
139 Py_INCREF(Py_None);
140 return Py_None;
141 }
142
143 pytimer->set_option(period, callback_fun, oneshot != 0);
144
145 Py_INCREF(Py_None);
146 return Py_None;
147}
148
149static PyMethodDef _cyber_timer_methods[] = {
150 {"new_PyTimer_noparam", cyber_new_PyTimer_noparam, METH_NOARGS, ""},
151 {"new_PyTimer", cyber_new_PyTimer, METH_VARARGS, ""},
152 {"delete_PyTimer", cyber_delete_PyTimer, METH_VARARGS, ""},
153 {"PyTimer_start", cyber_PyTimer_start, METH_VARARGS, ""},
154 {"PyTimer_stop", cyber_PyTimer_stop, METH_VARARGS, ""},
155 {"PyTimer_set_option", cyber_PyTimer_set_option, METH_VARARGS, ""},
156
157 {nullptr, nullptr, 0, nullptr} /* sentinel */
158};
159
161PyMODINIT_FUNC PyInit__cyber_timer_wrapper(void) {
162 static struct PyModuleDef module_def = {
163 PyModuleDef_HEAD_INIT,
164 "_cyber_timer_wrapper", // Module name.
165 "CyberTimer module", // Module doc.
166 -1, // Module size.
167 _cyber_timer_methods, // Module methods.
168 nullptr,
169 nullptr,
170 nullptr,
171 nullptr,
172 };
173
174 return PyModule_Create(&module_def);
175}
void set_option(uint32_t period, void(*func)(), bool oneshot)
Definition py_timer.h:44
#define AERROR
Definition log.h:44
PyObject * cyber_new_PyTimer(PyObject *self, PyObject *args)
Definition py_timer.cc:37
PyObject * cyber_delete_PyTimer(PyObject *self, PyObject *args)
Definition py_timer.cc:59
#define PyInt_AsLong
Definition py_timer.cc:26
PyObject * cyber_PyTimer_start(PyObject *self, PyObject *args)
Definition py_timer.cc:79
T PyObjectToPtr(PyObject *pyobj, const std::string &type_ptr)
Definition py_timer.cc:29
PyObject * cyber_PyTimer_set_option(PyObject *self, PyObject *args)
Definition py_timer.cc:119
PyObject * cyber_new_PyTimer_noparam(PyObject *self, PyObject *args)
Definition py_timer.cc:54
PyMODINIT_FUNC PyInit__cyber_timer_wrapper(void)
Init function of this module
Definition py_timer.cc:161
PyObject * cyber_PyTimer_stop(PyObject *self, PyObject *args)
Definition py_timer.cc:99