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