]>
Commit | Line | Data |
---|---|---|
aa2af324 | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
aa2af324 UH |
3 | * |
4 | * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de> | |
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 2 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, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
6ec6c43b | 21 | #include <config.h> |
c073af80 | 22 | #include <string.h> |
aa2af324 | 23 | #include <glib.h> |
c1aae900 | 24 | #include <libsigrok/libsigrok.h> |
aa2af324 UH |
25 | #include "libsigrok-internal.h" |
26 | #include "protocol.h" | |
27 | ||
c073af80 UH |
28 | /* States */ |
29 | enum { | |
30 | SEND_INIT, | |
31 | GET_INIT_REPLY, | |
32 | SEND_PACKET_REQUEST, | |
33 | GET_PACKET, | |
34 | }; | |
35 | ||
36 | static void parse_packet(const uint8_t *buf, float *floatval, | |
5faebab2 | 37 | struct sr_datafeed_analog_old *analog) |
c073af80 UH |
38 | { |
39 | gboolean is_a, is_fast; | |
40 | uint16_t intval; | |
41 | uint8_t level = 0, level_bits; | |
42 | ||
43 | /* Byte 0 [7:7]: 0 = A, 1 = C */ | |
44 | is_a = ((buf[0] & (1 << 7)) == 0); | |
45 | ||
46 | /* Byte 0 [6:6]: Unknown/unused? */ | |
47 | ||
48 | /* Byte 0 [5:4]: Level (00 = 40, 01 = 60, 10 = 80, 11 = 100) */ | |
49 | level_bits = (buf[0] >> 4) & 0x03; | |
50 | if (level_bits == 0) | |
51 | level = 40; | |
52 | else if (level_bits == 1) | |
53 | level = 60; | |
54 | else if (level_bits == 2) | |
55 | level = 80; | |
56 | else if (level_bits == 3) | |
57 | level = 100; | |
58 | ||
59 | /* Byte 0 [3:3]: 0 = fast, 1 = slow */ | |
60 | is_fast = ((buf[0] & (1 << 3)) == 0); | |
61 | ||
62 | /* Byte 0 [2:0]: value[10..8] */ | |
63 | /* Byte 1 [7:0]: value[7..0] */ | |
64 | intval = (buf[0] & 0x7) << 8; | |
65 | intval |= buf[1]; | |
66 | ||
67 | *floatval = (float)intval; | |
68 | ||
69 | /* The value on the display always has one digit after the comma. */ | |
70 | *floatval /= 10; | |
71 | ||
72 | analog->mq = SR_MQ_SOUND_PRESSURE_LEVEL; | |
73 | analog->unit = SR_UNIT_DECIBEL_SPL; | |
74 | ||
75 | if (is_a) | |
76 | analog->mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_A; | |
77 | else | |
78 | analog->mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_C; | |
79 | ||
80 | if (is_fast) | |
81 | analog->mqflags |= SR_MQFLAG_SPL_TIME_WEIGHT_F; | |
82 | else | |
83 | analog->mqflags |= SR_MQFLAG_SPL_TIME_WEIGHT_S; | |
84 | ||
85 | /* TODO: How to handle level? */ | |
86 | (void)level; | |
87 | } | |
88 | ||
69e19dd7 | 89 | static void decode_packet(struct sr_dev_inst *sdi) |
aa2af324 | 90 | { |
c073af80 | 91 | struct sr_datafeed_packet packet; |
5faebab2 | 92 | struct sr_datafeed_analog_old analog; |
69e19dd7 | 93 | struct dev_context *devc; |
c073af80 UH |
94 | float floatval; |
95 | ||
69e19dd7 | 96 | devc = sdi->priv; |
5faebab2 | 97 | memset(&analog, 0, sizeof(struct sr_datafeed_analog_old)); |
c073af80 UH |
98 | |
99 | parse_packet(devc->buf, &floatval, &analog); | |
100 | ||
101 | /* Send a sample packet with one analog value. */ | |
ba7dd8bb | 102 | analog.channels = sdi->channels; |
c073af80 UH |
103 | analog.num_samples = 1; |
104 | analog.data = &floatval; | |
5faebab2 | 105 | packet.type = SR_DF_ANALOG_OLD; |
c073af80 UH |
106 | packet.payload = &analog; |
107 | sr_session_send(devc->cb_data, &packet); | |
108 | ||
109 | devc->num_samples++; | |
110 | } | |
111 | ||
e8be6169 | 112 | SR_PRIV int tondaj_sl_814_receive_data(int fd, int revents, void *cb_data) |
c073af80 UH |
113 | { |
114 | struct sr_dev_inst *sdi; | |
aa2af324 | 115 | struct dev_context *devc; |
a5e44c32 | 116 | struct sr_serial_dev_inst *serial; |
c073af80 UH |
117 | uint8_t buf[3]; |
118 | int ret; | |
119 | ||
f4284934 | 120 | (void)fd; |
c073af80 UH |
121 | (void)revents; |
122 | ||
123 | sdi = cb_data; | |
a5e44c32 | 124 | serial = sdi->conn; |
c073af80 | 125 | devc = sdi->priv; |
aa2af324 | 126 | |
c073af80 | 127 | /* TODO: Parts of this code need to be improved later. */ |
aa2af324 | 128 | |
c073af80 UH |
129 | /* State machine. */ |
130 | if (devc->state == SEND_INIT) { | |
131 | /* On the first run, send the "init" command. */ | |
132 | buf[0] = 0x10; | |
133 | buf[1] = 0x04; | |
134 | buf[2] = 0x0d; | |
135 | sr_spew("Sending init command: %02x %02x %02x.", | |
136 | buf[0], buf[1], buf[2]); | |
32c45845 UH |
137 | if ((ret = serial_write_blocking(serial, buf, 3, |
138 | serial_timeout(serial, 3))) < 0) { | |
c073af80 UH |
139 | sr_err("Error sending init command: %d.", ret); |
140 | return FALSE; | |
141 | } | |
142 | devc->state = GET_INIT_REPLY; | |
143 | } else if (devc->state == GET_INIT_REPLY) { | |
144 | /* If we just sent the "init" command, get its reply. */ | |
eead2782 | 145 | if ((ret = serial_read_blocking(serial, buf, 2, 0)) < 0) { |
c073af80 UH |
146 | sr_err("Error reading init reply: %d.", ret); |
147 | return FALSE; | |
148 | } | |
149 | sr_spew("Received init reply: %02x %02x.", buf[0], buf[1]); | |
150 | /* Expected reply: 0x05 0x0d */ | |
151 | if (buf[0] != 0x05 || buf[1] != 0x0d) { | |
152 | sr_err("Received incorrect init reply, retrying."); | |
153 | devc->state = SEND_INIT; | |
154 | return TRUE; | |
155 | } | |
156 | devc->state = SEND_PACKET_REQUEST; | |
157 | } else if (devc->state == SEND_PACKET_REQUEST) { | |
158 | /* Request a packet (send 0x30 ZZ 0x0d). */ | |
159 | buf[0] = 0x30; | |
160 | buf[1] = 0x00; /* ZZ */ | |
161 | buf[2] = 0x0d; | |
162 | sr_spew("Sending data request command: %02x %02x %02x.", | |
163 | buf[0], buf[1], buf[2]); | |
32c45845 UH |
164 | if ((ret = serial_write_blocking(serial, buf, 3, |
165 | serial_timeout(serial, 3))) < 0) { | |
c073af80 UH |
166 | sr_err("Error sending request command: %d.", ret); |
167 | return FALSE; | |
168 | } | |
169 | devc->buflen = 0; | |
170 | devc->state = GET_PACKET; | |
171 | } else if (devc->state == GET_PACKET) { | |
172 | /* Read a packet from the device. */ | |
47d98603 | 173 | ret = serial_read_nonblocking(serial, devc->buf + devc->buflen, |
c073af80 UH |
174 | 4 - devc->buflen); |
175 | if (ret < 0) { | |
176 | sr_err("Error reading packet: %d.", ret); | |
177 | return TRUE; | |
178 | } | |
179 | ||
180 | devc->buflen += ret; | |
181 | ||
182 | /* Didn't receive all 4 bytes, yet. */ | |
183 | if (devc->buflen != 4) | |
184 | return TRUE; | |
185 | ||
186 | sr_spew("Received packet: %02x %02x %02x %02x.", devc->buf[0], | |
187 | devc->buf[1], devc->buf[2], devc->buf[3]); | |
188 | ||
189 | /* Expected reply: AA BB ZZ+1 0x0d */ | |
190 | if (devc->buf[2] != 0x01 || devc->buf[3] != 0x0d) { | |
191 | sr_err("Received incorrect request reply, retrying."); | |
192 | devc->state = SEND_PACKET_REQUEST; | |
193 | return TRUE; | |
194 | } | |
195 | ||
69e19dd7 | 196 | decode_packet(sdi); |
c073af80 UH |
197 | |
198 | devc->state = SEND_PACKET_REQUEST; | |
199 | } else { | |
200 | sr_err("Invalid state: %d.", devc->state); | |
201 | return FALSE; | |
202 | } | |
aa2af324 | 203 | |
c073af80 | 204 | /* Stop acquisition if we acquired enough samples. */ |
35e199da UH |
205 | if (devc->limit_samples && devc->num_samples >= devc->limit_samples) { |
206 | sr_info("Requested number of samples reached."); | |
207 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
aa2af324 UH |
208 | } |
209 | ||
210 | return TRUE; | |
211 | } |