2 * This file is part of the libsigrok project.
4 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
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 2 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/libsigrok.h>
24 #include "libsigrok-internal.h"
35 static void parse_packet(const uint8_t *buf, float *floatval,
36 struct sr_datafeed_analog *analog)
38 gboolean is_a, is_fast;
40 uint8_t level = 0, level_bits;
42 /* Byte 0 [7:7]: 0 = A, 1 = C */
43 is_a = ((buf[0] & (1 << 7)) == 0);
45 /* Byte 0 [6:6]: Unknown/unused? */
47 /* Byte 0 [5:4]: Level (00 = 40, 01 = 60, 10 = 80, 11 = 100) */
48 level_bits = (buf[0] >> 4) & 0x03;
51 else if (level_bits == 1)
53 else if (level_bits == 2)
55 else if (level_bits == 3)
58 /* Byte 0 [3:3]: 0 = fast, 1 = slow */
59 is_fast = ((buf[0] & (1 << 3)) == 0);
61 /* Byte 0 [2:0]: value[10..8] */
62 /* Byte 1 [7:0]: value[7..0] */
63 intval = (buf[0] & 0x7) << 8;
66 *floatval = (float)intval;
68 /* The value on the display always has one digit after the comma. */
71 analog->meaning->mq = SR_MQ_SOUND_PRESSURE_LEVEL;
72 analog->meaning->unit = SR_UNIT_DECIBEL_SPL;
75 analog->meaning->mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_A;
77 analog->meaning->mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_C;
80 analog->meaning->mqflags |= SR_MQFLAG_SPL_TIME_WEIGHT_F;
82 analog->meaning->mqflags |= SR_MQFLAG_SPL_TIME_WEIGHT_S;
84 /* TODO: How to handle level? */
88 static void decode_packet(struct sr_dev_inst *sdi)
90 struct sr_datafeed_packet packet;
91 struct sr_datafeed_analog analog;
92 struct sr_analog_encoding encoding;
93 struct sr_analog_meaning meaning;
94 struct sr_analog_spec spec;
95 struct dev_context *devc;
100 sr_analog_init(&analog, &encoding, &meaning, &spec, 1);
102 parse_packet(devc->buf, &floatval, &analog);
104 /* Send a sample packet with one analog value. */
105 analog.meaning->channels = sdi->channels;
106 analog.num_samples = 1;
107 analog.data = &floatval;
108 packet.type = SR_DF_ANALOG;
109 packet.payload = &analog;
110 sr_session_send(sdi, &packet);
112 sr_sw_limits_update_samples_read(&devc->limits, 1);
115 SR_PRIV int tondaj_sl_814_receive_data(int fd, int revents, void *cb_data)
117 struct sr_dev_inst *sdi;
118 struct dev_context *devc;
119 struct sr_serial_dev_inst *serial;
130 /* TODO: Parts of this code need to be improved later. */
133 if (devc->state == SEND_INIT) {
134 /* On the first run, send the "init" command. */
138 sr_spew("Sending init command: %02x %02x %02x.",
139 buf[0], buf[1], buf[2]);
140 if ((ret = serial_write_blocking(serial, buf, 3,
141 serial_timeout(serial, 3))) < 0) {
142 sr_err("Error sending init command: %d.", ret);
145 devc->state = GET_INIT_REPLY;
146 } else if (devc->state == GET_INIT_REPLY) {
147 /* If we just sent the "init" command, get its reply. */
148 if ((ret = serial_read_blocking(serial, buf, 2, 0)) < 0) {
149 sr_err("Error reading init reply: %d.", ret);
152 sr_spew("Received init reply: %02x %02x.", buf[0], buf[1]);
153 /* Expected reply: 0x05 0x0d */
154 if (buf[0] != 0x05 || buf[1] != 0x0d) {
155 sr_err("Received incorrect init reply, retrying.");
156 devc->state = SEND_INIT;
159 devc->state = SEND_PACKET_REQUEST;
160 } else if (devc->state == SEND_PACKET_REQUEST) {
161 /* Request a packet (send 0x30 ZZ 0x0d). */
163 buf[1] = 0x00; /* ZZ */
165 sr_spew("Sending data request command: %02x %02x %02x.",
166 buf[0], buf[1], buf[2]);
167 if ((ret = serial_write_blocking(serial, buf, 3,
168 serial_timeout(serial, 3))) < 0) {
169 sr_err("Error sending request command: %d.", ret);
173 devc->state = GET_PACKET;
174 } else if (devc->state == GET_PACKET) {
175 /* Read a packet from the device. */
176 ret = serial_read_nonblocking(serial, devc->buf + devc->buflen,
179 sr_err("Error reading packet: %d.", ret);
185 /* Didn't receive all 4 bytes, yet. */
186 if (devc->buflen != 4)
189 sr_spew("Received packet: %02x %02x %02x %02x.", devc->buf[0],
190 devc->buf[1], devc->buf[2], devc->buf[3]);
192 /* Expected reply: AA BB ZZ+1 0x0d */
193 if (devc->buf[2] != 0x01 || devc->buf[3] != 0x0d) {
194 sr_err("Received incorrect request reply, retrying.");
195 devc->state = SEND_PACKET_REQUEST;
201 devc->state = SEND_PACKET_REQUEST;
203 sr_err("Invalid state: %d.", devc->state);
207 if (sr_sw_limits_check(&devc->limits))
208 sr_dev_acquisition_stop(sdi);