2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "sigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
23 /* lives in type_logic.c */
24 extern PyTypeObject srd_logic_type;
27 /* TODO: not used, doesn't work actually */
28 static PyObject *Decoder_init(PyObject *self, PyObject *args)
32 printf("init Decoder object %p\n", self);
37 static PyObject *Decoder_put(PyObject *self, PyObject *args)
41 struct srd_decoder_instance *di;
42 struct srd_pd_output *pdo;
43 uint64_t timeoffset, duration;
46 if (!(di = get_di_by_decobject(self)))
49 if (!PyArg_ParseTuple(args, "KKiO", &timeoffset, &duration, &output_id, &data))
52 if (!(l = g_slist_nth(di->pd_output, output_id))) {
53 /* PD supplied invalid output id */
54 /* TODO: better error message */
59 /* TODO: SRD_OUTPUT_ANNOTATION should go back up to the caller,
60 * and SRD_OUTPUT_PROTOCOL should go up the PD stack.
62 printf("stream %d: ", pdo->output_type);
63 PyObject_Print(data, stdout, Py_PRINT_RAW);
70 static PyObject *Decoder_output_new(PyObject *self, PyObject *py_output_type)
73 struct srd_decoder_instance *di;
74 char *protocol_id, *description;
75 int output_type, pdo_id;
77 if (!(di = get_di_by_decobject(self)))
80 printf("output_new di %s\n", di->decoder->name);
82 if (!PyArg_ParseTuple(py_output_type, "i:output_type", &output_type))
87 pdo_id = pd_output_new(di, output_type, protocol_id, description);
91 ret = Py_BuildValue("i", pdo_id);
96 static PyMethodDef no_methods[] = { {NULL, NULL, 0, NULL} };
97 static PyMethodDef Decoder_methods[] = {
98 {"put", Decoder_put, METH_VARARGS,
99 "Accepts a dictionary with the following keys: time, duration, data"},
100 {"output_new", Decoder_output_new, METH_VARARGS,
101 "Create a new output stream"},
102 {NULL, NULL, 0, NULL}
108 } sigrok_Decoder_object;
110 static PyTypeObject srd_Decoder_type = {
111 PyVarObject_HEAD_INIT(NULL, 0)
112 .tp_name = "sigrokdecode.Decoder",
113 .tp_basicsize = sizeof(sigrok_Decoder_object),
114 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
115 .tp_doc = "Sigrok Decoder object",
116 .tp_methods = Decoder_methods,
117 .tp_init = (initproc) Decoder_init,
120 static struct PyModuleDef sigrokdecode_module = {
121 PyModuleDef_HEAD_INIT,
122 .m_name = "sigrokdecode",
123 .m_doc = "sigrokdecode base class",
125 .m_methods = no_methods,
128 PyMODINIT_FUNC PyInit_sigrokdecode(void)
132 /* tp_new needs to be assigned here for compiler portability */
133 srd_Decoder_type.tp_new = PyType_GenericNew;
134 if (PyType_Ready(&srd_Decoder_type) < 0)
137 srd_logic_type.tp_new = PyType_GenericNew;
138 if (PyType_Ready(&srd_logic_type) < 0)
141 mod = PyModule_Create(&sigrokdecode_module);
142 Py_INCREF(&srd_Decoder_type);
143 if (PyModule_AddObject(mod, "Decoder", (PyObject *)&srd_Decoder_type) == -1)
145 Py_INCREF(&srd_logic_type);
146 if (PyModule_AddObject(mod, "srd_logic", (PyObject *)&srd_logic_type) == -1)