2 * This file is part of the libsigrok 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/>.
22 #include "libsigrok.h"
23 #include "libsigrok-internal.h"
29 static void process_packet(const struct sr_dev_inst *sdi)
31 struct dev_context *devc;
32 struct sr_datafeed_packet packet;
33 struct sr_datafeed_analog analog;
36 int checksum, mode, i;
39 if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
40 dbg = g_string_sized_new(128);
41 g_string_printf(dbg, "received packet:");
42 for (i = 0; i < 10; i++)
43 g_string_append_printf(dbg, " %.2x", (devc->buf)[i]);
44 sr_spew("%s", dbg->str);
45 g_string_free(dbg, TRUE);
48 if (devc->buf[0] != 0x08 || devc->buf[1] != 0x04) {
49 sr_dbg("invalid packet header.");
53 if (devc->buf[8] != 0x01) {
54 sr_dbg("invalid measurement.");
59 for (i = 0; i < 9; i++)
60 checksum += devc->buf[i];
61 if ((checksum & 0xff) != devc->buf[9]) {
62 sr_dbg("invalid packet checksum.");
67 for (i = 3; i < 8; i++) {
68 if (devc->buf[i] > 0x09)
71 fvalue += devc->buf[i];
75 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
76 analog.mq = SR_MQ_SOUND_PRESSURE_LEVEL;
77 analog.unit = SR_UNIT_DECIBEL_SPL;
78 analog.num_samples = 1;
79 analog.data = &fvalue;
81 /* High nibble should only have 0x01 or 0x02. */
82 mode = (devc->buf[2] >> 4) & 0x0f;
84 analog.mqflags |= SR_MQFLAG_HOLD;
85 else if (mode != 0x01) {
86 sr_dbg("unknown measurement mode 0x%.2x", mode);
90 /* Low nibble has 14 combinations of direct/long-term average,
91 * time scale of that average, frequency weighting, and time
93 mode = devc->buf[2] & 0x0f;
96 analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_A \
97 | SR_MQFLAG_SPL_TIME_WEIGHT_F;
100 analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_A \
101 | SR_MQFLAG_SPL_TIME_WEIGHT_S;
104 analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_C \
105 | SR_MQFLAG_SPL_TIME_WEIGHT_F;
108 analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_C \
109 | SR_MQFLAG_SPL_TIME_WEIGHT_S;
112 analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_FLAT \
113 | SR_MQFLAG_SPL_TIME_WEIGHT_F;
116 analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_FLAT \
117 | SR_MQFLAG_SPL_TIME_WEIGHT_S;
120 analog.mqflags |= SR_MQFLAG_SPL_PCT_OVER_ALARM \
121 | SR_MQFLAG_SPL_FREQ_WEIGHT_A \
122 | SR_MQFLAG_SPL_TIME_WEIGHT_F;
125 analog.mqflags |= SR_MQFLAG_SPL_PCT_OVER_ALARM \
126 | SR_MQFLAG_SPL_FREQ_WEIGHT_A \
127 | SR_MQFLAG_SPL_TIME_WEIGHT_S;
130 /* 10-second mean, but we don't have MQ flags to express it. */
131 analog.mqflags |= SR_MQFLAG_SPL_LAT \
132 | SR_MQFLAG_SPL_FREQ_WEIGHT_A \
133 | SR_MQFLAG_SPL_TIME_WEIGHT_F;
136 /* Mean over a time period between 11 seconds and 24 hours.
137 * Which is so silly that there's no point in expressing
138 * either this or the previous case. */
139 analog.mqflags |= SR_MQFLAG_SPL_LAT \
140 | SR_MQFLAG_SPL_FREQ_WEIGHT_A \
141 | SR_MQFLAG_SPL_TIME_WEIGHT_F;
144 /* 10-second mean. */
145 analog.mqflags |= SR_MQFLAG_SPL_LAT \
146 | SR_MQFLAG_SPL_FREQ_WEIGHT_A \
147 | SR_MQFLAG_SPL_TIME_WEIGHT_S;
150 /* Mean over a time period between 11 seconds and 24 hours. */
151 analog.mqflags |= SR_MQFLAG_SPL_LAT \
152 | SR_MQFLAG_SPL_FREQ_WEIGHT_A \
153 | SR_MQFLAG_SPL_TIME_WEIGHT_S;
156 /* Internal calibration on 1kHz sine at 94dB, not useful
157 * to anything but the device. */
158 analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_FLAT;
161 /* Internal calibration on 1kHz sine at 94dB, not useful
162 * to anything but the device. */
163 analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_FLAT;
166 sr_dbg("unknown configuration 0x%.2x", mode);
171 packet.type = SR_DF_ANALOG;
172 packet.payload = &analog;
173 sr_session_send(devc->cb_data, &packet);
179 SR_PRIV int colead_slm_receive_data(int fd, int revents, void *cb_data)
181 const struct sr_dev_inst *sdi;
182 struct dev_context *devc;
188 if (!(sdi = cb_data))
191 if (!(devc = sdi->priv))
194 if (revents != G_IO_IN)
198 if (devc->state == IDLE) {
199 if (serial_read(devc->serial, buf, 128) != 1 || buf[0] != 0x10)
200 /* Nothing there, or caught the tail end of a previous packet,
201 * or some garbage. Unless it's a single "data ready" byte,
202 * we don't want it. */
204 /* Got 0x10, "measurement ready". */
205 if (serial_write(devc->serial, "\x20", 1) == -1)
206 sr_err("unable to send command: %s", strerror(errno));
208 devc->state = COMMAND_SENT;
212 len = serial_read(devc->serial, devc->buf + devc->buflen,
217 if (devc->buflen > 10) {
218 sr_dbg("buffer overrun");
222 if (devc->buflen == 10) {
223 /* If we got here, we're sure the device sent a "data ready"
224 * notification, we asked for data, and got it. */