]>
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 | ||
c1f86f02 | 20 | #include "libsigrokdecode.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */ |
bc5f5a43 BV |
21 | #include "config.h" |
22 | #include <inttypes.h> | |
23 | #include <string.h> | |
24 | ||
55c3c5f4 | 25 | static PyObject *srd_logic_iter(PyObject *self) |
bc5f5a43 | 26 | { |
bc5f5a43 BV |
27 | return self; |
28 | } | |
29 | ||
55c3c5f4 | 30 | static PyObject *srd_logic_iternext(PyObject *self) |
bc5f5a43 | 31 | { |
bc5f5a43 | 32 | srd_logic *logic; |
69075817 | 33 | PyObject *py_samplenum, *py_samples; |
37b94c20 BV |
34 | uint8_t *sample_pos, sample; |
35 | int byte_offset, bit_offset, i; | |
bc5f5a43 | 36 | |
c9bfccc6 | 37 | logic = (srd_logic *)self; |
f38ec285 | 38 | if (logic->itercnt >= logic->inbuflen / logic->di->data_unitsize) { |
bc5f5a43 BV |
39 | /* End iteration loop. */ |
40 | return NULL; | |
41 | } | |
42 | ||
c9bfccc6 UH |
43 | /* |
44 | * Convert the bit-packed sample to an array of bytes, with only 0x01 | |
bc5f5a43 BV |
45 | * and 0x00 values, so the PD doesn't need to do any bitshifting. |
46 | */ | |
37b94c20 | 47 | sample_pos = logic->inbuf + logic->itercnt * logic->di->data_unitsize; |
38ff5046 UH |
48 | for (i = 0; i < logic->di->dec_num_probes; i++) { |
49 | /* A probemap value of -1 means "unused optional probe". */ | |
37b94c20 BV |
50 | if (logic->di->dec_probemap[i] == -1) { |
51 | /* Value of unused probe is 0xff, instead of 0 or 1. */ | |
52 | logic->di->probe_samples[i] = 0xff; | |
53 | } else { | |
54 | byte_offset = logic->di->dec_probemap[i] / 8; | |
55 | bit_offset = logic->di->dec_probemap[i] % 8; | |
56 | sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0; | |
57 | logic->di->probe_samples[i] = sample; | |
58 | } | |
38ff5046 | 59 | } |
bc5f5a43 | 60 | |
bc5f5a43 | 61 | /* Prepare the next samplenum/sample list in this iteration. */ |
c9bfccc6 UH |
62 | py_samplenum = |
63 | PyLong_FromUnsignedLongLong(logic->start_samplenum + | |
64 | logic->itercnt); | |
bc5f5a43 | 65 | PyList_SetItem(logic->sample, 0, py_samplenum); |
37b94c20 | 66 | py_samples = PyBytes_FromStringAndSize((const char *)logic->di->probe_samples, |
c9bfccc6 | 67 | logic->di->dec_num_probes); |
bc5f5a43 BV |
68 | PyList_SetItem(logic->sample, 1, py_samples); |
69 | Py_INCREF(logic->sample); | |
86528298 | 70 | logic->itercnt++; |
bc5f5a43 BV |
71 | |
72 | return logic->sample; | |
73 | } | |
74 | ||
57790bc8 | 75 | /** @cond PRIVATE */ |
55c3c5f4 | 76 | SRD_PRIV PyTypeObject srd_logic_type = { |
bc5f5a43 BV |
77 | PyVarObject_HEAD_INIT(NULL, 0) |
78 | .tp_name = "srd_logic", | |
79 | .tp_basicsize = sizeof(srd_logic), | |
80 | .tp_flags = Py_TPFLAGS_DEFAULT, | |
81 | .tp_doc = "Sigrokdecode logic sample object", | |
82 | .tp_iter = srd_logic_iter, | |
83 | .tp_iternext = srd_logic_iternext, | |
84 | }; | |
57790bc8 | 85 | /** @endcond */ |