]>
Commit | Line | Data |
---|---|---|
bc5f5a43 | 1 | /* |
50bd5d25 | 2 | * This file is part of the libsigrokdecode project. |
bc5f5a43 BV |
3 | * |
4 | * Copyright (C) 2012 Bert Vermeulen <bert@biot.com> | |
5 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
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/>. | |
18 | */ | |
19 | ||
36784362 | 20 | #include <config.h> |
f6c7eade MC |
21 | #include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */ |
22 | #include "libsigrokdecode.h" | |
bc5f5a43 BV |
23 | #include <inttypes.h> |
24 | #include <string.h> | |
25 | ||
55c3c5f4 | 26 | static PyObject *srd_logic_iter(PyObject *self) |
bc5f5a43 | 27 | { |
bc5f5a43 BV |
28 | return self; |
29 | } | |
30 | ||
55c3c5f4 | 31 | static PyObject *srd_logic_iternext(PyObject *self) |
bc5f5a43 | 32 | { |
bc5f5a43 | 33 | srd_logic *logic; |
69075817 | 34 | PyObject *py_samplenum, *py_samples; |
37b94c20 BV |
35 | uint8_t *sample_pos, sample; |
36 | int byte_offset, bit_offset, i; | |
bc5f5a43 | 37 | |
c9bfccc6 | 38 | logic = (srd_logic *)self; |
f38ec285 | 39 | if (logic->itercnt >= logic->inbuflen / logic->di->data_unitsize) { |
bc5f5a43 BV |
40 | /* End iteration loop. */ |
41 | return NULL; | |
42 | } | |
43 | ||
c9bfccc6 UH |
44 | /* |
45 | * Convert the bit-packed sample to an array of bytes, with only 0x01 | |
bc5f5a43 BV |
46 | * and 0x00 values, so the PD doesn't need to do any bitshifting. |
47 | */ | |
37b94c20 | 48 | sample_pos = logic->inbuf + logic->itercnt * logic->di->data_unitsize; |
6a15597a UH |
49 | for (i = 0; i < logic->di->dec_num_channels; i++) { |
50 | /* A channelmap value of -1 means "unused optional channel". */ | |
51 | if (logic->di->dec_channelmap[i] == -1) { | |
52 | /* Value of unused channel is 0xff, instead of 0 or 1. */ | |
53 | logic->di->channel_samples[i] = 0xff; | |
37b94c20 | 54 | } else { |
6a15597a UH |
55 | byte_offset = logic->di->dec_channelmap[i] / 8; |
56 | bit_offset = logic->di->dec_channelmap[i] % 8; | |
37b94c20 | 57 | sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0; |
6a15597a | 58 | logic->di->channel_samples[i] = sample; |
37b94c20 | 59 | } |
38ff5046 | 60 | } |
bc5f5a43 | 61 | |
bc5f5a43 | 62 | /* Prepare the next samplenum/sample list in this iteration. */ |
c9bfccc6 UH |
63 | py_samplenum = |
64 | PyLong_FromUnsignedLongLong(logic->start_samplenum + | |
65 | logic->itercnt); | |
bc5f5a43 | 66 | PyList_SetItem(logic->sample, 0, py_samplenum); |
6a15597a UH |
67 | py_samples = PyBytes_FromStringAndSize((const char *)logic->di->channel_samples, |
68 | logic->di->dec_num_channels); | |
bc5f5a43 BV |
69 | PyList_SetItem(logic->sample, 1, py_samples); |
70 | Py_INCREF(logic->sample); | |
86528298 | 71 | logic->itercnt++; |
bc5f5a43 BV |
72 | |
73 | return logic->sample; | |
74 | } | |
75 | ||
201a85a8 DE |
76 | /** Create the srd_logic type. |
77 | * @return The new type object. | |
78 | * @private | |
79 | */ | |
80 | SRD_PRIV PyObject *srd_logic_type_new(void) | |
81 | { | |
82 | PyType_Spec spec; | |
83 | PyType_Slot slots[] = { | |
84 | { Py_tp_doc, "sigrokdecode logic sample object" }, | |
85 | { Py_tp_iter, (void *)&srd_logic_iter }, | |
86 | { Py_tp_iternext, (void *)&srd_logic_iternext }, | |
87 | { Py_tp_new, (void *)&PyType_GenericNew }, | |
88 | { 0, NULL } | |
89 | }; | |
90 | spec.name = "srd_logic"; | |
91 | spec.basicsize = sizeof(srd_logic); | |
92 | spec.itemsize = 0; | |
93 | spec.flags = Py_TPFLAGS_DEFAULT; | |
94 | spec.slots = slots; | |
95 | ||
96 | return PyType_FromSpec(&spec); | |
97 | } |