2 * This file is part of the libsigrokdecode 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 "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
21 #include "libsigrokdecode.h"
26 static PyObject *srd_logic_iter(PyObject *self)
31 static PyObject *srd_logic_iternext(PyObject *self)
34 PyObject *py_samplenum, *py_samples;
35 uint8_t *sample_pos, sample;
36 int byte_offset, bit_offset, i;
38 logic = (srd_logic *)self;
39 if (logic->itercnt >= logic->inbuflen / logic->di->data_unitsize) {
40 /* End iteration loop. */
45 * Convert the bit-packed sample to an array of bytes, with only 0x01
46 * and 0x00 values, so the PD doesn't need to do any bitshifting.
48 sample_pos = logic->inbuf + logic->itercnt * logic->di->data_unitsize;
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;
55 byte_offset = logic->di->dec_channelmap[i] / 8;
56 bit_offset = logic->di->dec_channelmap[i] % 8;
57 sample = *(sample_pos + byte_offset) & (1 << bit_offset) ? 1 : 0;
58 logic->di->channel_samples[i] = sample;
62 /* Prepare the next samplenum/sample list in this iteration. */
64 PyLong_FromUnsignedLongLong(logic->start_samplenum +
66 PyList_SetItem(logic->sample, 0, py_samplenum);
67 py_samples = PyBytes_FromStringAndSize((const char *)logic->di->channel_samples,
68 logic->di->dec_num_channels);
69 PyList_SetItem(logic->sample, 1, py_samples);
70 Py_INCREF(logic->sample);
77 SRD_PRIV PyTypeObject srd_logic_type = {
78 PyVarObject_HEAD_INIT(NULL, 0)
79 .tp_name = "srd_logic",
80 .tp_basicsize = sizeof(srd_logic),
81 .tp_flags = Py_TPFLAGS_DEFAULT,
82 .tp_doc = "Sigrokdecode logic sample object",
83 .tp_iter = srd_logic_iter,
84 .tp_iternext = srd_logic_iternext,