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/>.
23 #include "libsigrok.h"
24 #include "libsigrok-internal.h"
27 static void process_packet(const struct sr_dev_inst *sdi)
29 struct dev_context *devc;
30 struct sr_datafeed_packet packet;
31 struct sr_datafeed_analog analog;
34 int checksum, mode, i;
37 if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
38 dbg = g_string_sized_new(128);
39 g_string_printf(dbg, "received packet:");
40 for (i = 0; i < 10; i++)
41 g_string_append_printf(dbg, " %.2x", (devc->buf)[i]);
42 sr_spew("%s", dbg->str);
43 g_string_free(dbg, TRUE);
46 if (devc->buf[0] != 0x08 || devc->buf[1] != 0x04) {
47 sr_dbg("invalid packet header.");
51 if (devc->buf[8] != 0x01) {
52 sr_dbg("invalid measurement.");
57 for (i = 0; i < 9; i++)
58 checksum += devc->buf[i];
59 if ((checksum & 0xff) != devc->buf[9]) {
60 sr_dbg("invalid packet checksum.");
65 for (i = 3; i < 8; i++) {
66 if (devc->buf[i] > 0x09)
69 fvalue += devc->buf[i];
73 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
74 analog.mq = SR_MQ_SOUND_PRESSURE_LEVEL;
75 analog.unit = SR_UNIT_DECIBEL_SPL;
76 analog.channels = sdi->channels;
77 analog.num_samples = 1;
78 analog.data = &fvalue;
80 /* High nibble should only have 0x01 or 0x02. */
81 mode = (devc->buf[2] >> 4) & 0x0f;
83 analog.mqflags |= SR_MQFLAG_HOLD;
84 else if (mode != 0x01) {
85 sr_dbg("unknown measurement mode 0x%.2x", mode);
89 /* Low nibble has 14 combinations of direct/long-term average,
90 * time scale of that average, frequency weighting, and time
92 mode = devc->buf[2] & 0x0f;
95 analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_A \
96 | SR_MQFLAG_SPL_TIME_WEIGHT_F;
99 analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_A \
100 | SR_MQFLAG_SPL_TIME_WEIGHT_S;
103 analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_C \
104 | SR_MQFLAG_SPL_TIME_WEIGHT_F;
107 analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_C \
108 | SR_MQFLAG_SPL_TIME_WEIGHT_S;
111 analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_FLAT \
112 | SR_MQFLAG_SPL_TIME_WEIGHT_F;
115 analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_FLAT \
116 | SR_MQFLAG_SPL_TIME_WEIGHT_S;
119 analog.mqflags |= SR_MQFLAG_SPL_PCT_OVER_ALARM \
120 | SR_MQFLAG_SPL_FREQ_WEIGHT_A \
121 | SR_MQFLAG_SPL_TIME_WEIGHT_F;
124 analog.mqflags |= SR_MQFLAG_SPL_PCT_OVER_ALARM \
125 | SR_MQFLAG_SPL_FREQ_WEIGHT_A \
126 | SR_MQFLAG_SPL_TIME_WEIGHT_S;
129 /* 10-second mean, but we don't have MQ flags to express it. */
130 analog.mqflags |= SR_MQFLAG_SPL_LAT \
131 | SR_MQFLAG_SPL_FREQ_WEIGHT_A \
132 | SR_MQFLAG_SPL_TIME_WEIGHT_F;
135 /* Mean over a time period between 11 seconds and 24 hours.
136 * Which is so silly that there's no point in expressing
137 * either this or the previous case. */
138 analog.mqflags |= SR_MQFLAG_SPL_LAT \
139 | SR_MQFLAG_SPL_FREQ_WEIGHT_A \
140 | SR_MQFLAG_SPL_TIME_WEIGHT_F;
143 /* 10-second mean. */
144 analog.mqflags |= SR_MQFLAG_SPL_LAT \
145 | SR_MQFLAG_SPL_FREQ_WEIGHT_A \
146 | SR_MQFLAG_SPL_TIME_WEIGHT_S;
149 /* Mean over a time period between 11 seconds and 24 hours. */
150 analog.mqflags |= SR_MQFLAG_SPL_LAT \
151 | SR_MQFLAG_SPL_FREQ_WEIGHT_A \
152 | SR_MQFLAG_SPL_TIME_WEIGHT_S;
155 /* Internal calibration on 1kHz sine at 94dB, not useful
156 * to anything but the device. */
157 analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_FLAT;
160 /* Internal calibration on 1kHz sine at 94dB, not useful
161 * to anything but the device. */
162 analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_FLAT;
165 sr_dbg("unknown configuration 0x%.2x", mode);
169 packet.type = SR_DF_ANALOG;
170 packet.payload = &analog;
171 sr_session_send(devc->cb_data, &packet);
174 if (devc->num_samples >= devc->limit_samples)
175 sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
180 SR_PRIV int colead_slm_receive_data(int fd, int revents, void *cb_data)
182 const struct sr_dev_inst *sdi;
183 struct dev_context *devc;
184 struct sr_serial_dev_inst *serial;
190 if (!(sdi = cb_data))
193 if (!(devc = sdi->priv))
196 if (revents != G_IO_IN)
201 if (devc->state == IDLE) {
202 if (serial_read_nonblocking(serial, buf, 128) != 1 || buf[0] != 0x10)
203 /* Nothing there, or caught the tail end of a previous packet,
204 * or some garbage. Unless it's a single "data ready" byte,
205 * we don't want it. */
207 /* Got 0x10, "measurement ready". */
208 delay_ms = serial_timeout(serial, 1);
209 if (serial_write_blocking(serial, "\x20", 1, delay_ms) < 1)
210 sr_err("unable to send command");
212 devc->state = COMMAND_SENT;
216 len = serial_read_nonblocking(serial, devc->buf + devc->buflen,
221 if (devc->buflen > 10) {
222 sr_dbg("buffer overrun");
226 if (devc->buflen == 10) {
227 /* If we got here, we're sure the device sent a "data ready"
228 * notification, we asked for data, and got it. */