]>
Commit | Line | Data |
---|---|---|
5a2c71cc GH |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2016 George Hopkins <george-hopkins@null.net> | |
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 3 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, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <config.h> | |
21 | #include "protocol.h" | |
22 | ||
23 | static int send_command(const struct sr_dev_inst *sdi, uint16_t command) | |
24 | { | |
25 | struct sr_serial_dev_inst *serial; | |
26 | uint8_t buffer[2]; | |
27 | ||
28 | buffer[0] = command >> 8; | |
29 | buffer[1] = command; | |
30 | ||
31 | if (!(serial = sdi->conn)) | |
32 | return SR_ERR; | |
33 | ||
34 | if (serial_write_nonblocking(serial, (const void *)buffer, 2) != 2) | |
35 | return SR_ERR; | |
36 | ||
37 | return SR_OK; | |
38 | } | |
39 | ||
40 | static void send_data(const struct sr_dev_inst *sdi, float sample) | |
41 | { | |
42 | struct dev_context *devc; | |
43 | struct sr_datafeed_packet packet; | |
44 | struct sr_datafeed_analog analog; | |
45 | struct sr_analog_encoding encoding; | |
46 | struct sr_analog_meaning meaning; | |
47 | struct sr_analog_spec spec; | |
48 | ||
49 | devc = sdi->priv; | |
50 | ||
51 | sr_analog_init(&analog, &encoding, &meaning, &spec, 1); | |
52 | meaning.mq = SR_MQ_SOUND_PRESSURE_LEVEL; | |
53 | meaning.mqflags = devc->cur_mqflags; | |
54 | meaning.unit = SR_UNIT_DECIBEL_SPL; | |
55 | meaning.channels = sdi->channels; | |
56 | analog.num_samples = 1; | |
57 | analog.data = &sample; | |
58 | packet.type = SR_DF_ANALOG; | |
59 | packet.payload = &analog; | |
60 | sr_session_send(sdi, &packet); | |
61 | ||
62 | devc->num_samples++; | |
63 | if (devc->limit_samples && devc->num_samples >= devc->limit_samples) | |
64 | sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi); | |
65 | } | |
66 | ||
67 | static void process_measurement(const struct sr_dev_inst *sdi) | |
68 | { | |
69 | struct dev_context *devc; | |
70 | unsigned short value; | |
71 | ||
72 | devc = sdi->priv; | |
73 | ||
74 | if (devc->buffer[3] & (1 << 0)) { | |
75 | devc->cur_mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_C; | |
76 | devc->cur_mqflags &= ~SR_MQFLAG_SPL_FREQ_WEIGHT_A; | |
77 | } else { | |
78 | devc->cur_mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_A; | |
79 | devc->cur_mqflags &= ~SR_MQFLAG_SPL_FREQ_WEIGHT_C; | |
80 | } | |
81 | ||
82 | if (devc->buffer[3] & (1 << 1)) { | |
83 | devc->cur_mqflags |= SR_MQFLAG_SPL_TIME_WEIGHT_S; | |
84 | devc->cur_mqflags &= ~SR_MQFLAG_SPL_TIME_WEIGHT_F; | |
85 | } else { | |
86 | devc->cur_mqflags |= SR_MQFLAG_SPL_TIME_WEIGHT_F; | |
87 | devc->cur_mqflags &= ~SR_MQFLAG_SPL_TIME_WEIGHT_S; | |
88 | } | |
89 | ||
90 | devc->cur_meas_range = devc->buffer[4] & 3; | |
91 | ||
92 | if (devc->buffer[4] & (1 << 2)) { | |
93 | devc->cur_mqflags |= SR_MQFLAG_MAX; | |
94 | devc->cur_mqflags &= ~SR_MQFLAG_MIN; | |
95 | } else if (devc->buffer[4] & (1 << 3)) { | |
96 | devc->cur_mqflags |= SR_MQFLAG_MIN; | |
97 | devc->cur_mqflags &= ~SR_MQFLAG_MAX; | |
98 | } else { | |
99 | devc->cur_mqflags &= ~SR_MQFLAG_MIN; | |
100 | devc->cur_mqflags &= ~SR_MQFLAG_MAX; | |
101 | } | |
102 | ||
103 | value = devc->buffer[1] << 8 | devc->buffer[2]; | |
104 | send_data(sdi, value / 10.0); | |
105 | } | |
106 | ||
107 | static void process_byte(const struct sr_dev_inst *sdi, const unsigned char c) | |
108 | { | |
109 | struct dev_context *devc; | |
110 | unsigned int i; | |
111 | ||
112 | devc = sdi->priv; | |
113 | ||
114 | if (devc->buffer_len < BUFFER_SIZE) { | |
115 | devc->buffer[devc->buffer_len++] = c; | |
116 | } else { | |
117 | for (i = 1; i < BUFFER_SIZE; i++) | |
118 | devc->buffer[i - 1] = devc->buffer[i]; | |
119 | devc->buffer[BUFFER_SIZE - 1] = c; | |
120 | if (devc->buffer[0] == 0x7f && devc->buffer[BUFFER_SIZE - 1] == 0x00) { | |
121 | process_measurement(sdi); | |
122 | devc->buffer_len = 0; | |
123 | } | |
124 | } | |
125 | } | |
126 | ||
127 | SR_PRIV int pce_322a_receive_data(int fd, int revents, void *cb_data) | |
128 | { | |
129 | const struct sr_dev_inst *sdi; | |
130 | struct dev_context *devc; | |
131 | struct sr_serial_dev_inst *serial; | |
132 | unsigned char c; | |
133 | ||
134 | (void)fd; | |
135 | ||
136 | if (!(sdi = cb_data)) | |
137 | return TRUE; | |
138 | ||
139 | if (!(devc = sdi->priv)) | |
140 | return TRUE; | |
141 | ||
142 | if (!(serial = sdi->conn)) | |
143 | return TRUE; | |
144 | ||
145 | if (revents == G_IO_IN) { | |
146 | if (serial_read_nonblocking(serial, &c, 1) != 1) | |
147 | return TRUE; | |
148 | process_byte(sdi, c); | |
149 | } | |
150 | ||
151 | return TRUE; | |
152 | } | |
153 | ||
154 | SR_PRIV int pce_322a_connect(const struct sr_dev_inst *sdi) | |
155 | { | |
156 | return send_command(sdi, CMD_CONNECT); | |
157 | } | |
158 | ||
159 | SR_PRIV int pce_322a_disconnect(const struct sr_dev_inst *sdi) | |
160 | { | |
161 | return send_command(sdi, CMD_DISCONNECT); | |
162 | } | |
163 | ||
164 | SR_PRIV uint64_t pce_322a_weight_freq_get(const struct sr_dev_inst *sdi) | |
165 | { | |
166 | struct dev_context *devc; | |
167 | ||
168 | devc = sdi->priv; | |
169 | ||
170 | return devc->cur_mqflags & (SR_MQFLAG_SPL_FREQ_WEIGHT_A | SR_MQFLAG_SPL_FREQ_WEIGHT_C); | |
171 | } | |
172 | ||
173 | SR_PRIV int pce_322a_weight_freq_set(const struct sr_dev_inst *sdi, uint64_t freqw) | |
174 | { | |
175 | struct dev_context *devc; | |
176 | ||
177 | devc = sdi->priv; | |
178 | ||
179 | if (devc->cur_mqflags & freqw) | |
180 | return SR_OK; | |
181 | ||
182 | return send_command(sdi, CMD_TOGGLE_WEIGHT_FREQ); | |
183 | } | |
184 | ||
185 | SR_PRIV uint64_t pce_322a_weight_time_get(const struct sr_dev_inst *sdi) | |
186 | { | |
187 | struct dev_context *devc; | |
188 | ||
189 | devc = sdi->priv; | |
190 | ||
191 | return devc->cur_mqflags & (SR_MQFLAG_SPL_TIME_WEIGHT_F | SR_MQFLAG_SPL_TIME_WEIGHT_S); | |
192 | } | |
193 | ||
194 | SR_PRIV int pce_322a_weight_time_set(const struct sr_dev_inst *sdi, uint64_t timew) | |
195 | { | |
196 | struct dev_context *devc; | |
197 | ||
198 | devc = sdi->priv; | |
199 | ||
200 | if (devc->cur_mqflags & timew) | |
201 | return SR_OK; | |
202 | ||
203 | return send_command(sdi, CMD_TOGGLE_WEIGHT_TIME); | |
204 | } | |
205 | ||
206 | SR_PRIV int pce_322a_meas_range_get(const struct sr_dev_inst *sdi, | |
207 | uint64_t *low, uint64_t *high) | |
208 | { | |
209 | struct dev_context *devc; | |
210 | ||
211 | devc = sdi->priv; | |
212 | ||
213 | switch (devc->cur_meas_range) { | |
214 | case MEAS_RANGE_30_130: | |
215 | *low = 30; | |
216 | *high = 130; | |
217 | break; | |
218 | case MEAS_RANGE_30_80: | |
219 | *low = 30; | |
220 | *high = 80; | |
221 | break; | |
222 | case MEAS_RANGE_50_100: | |
223 | *low = 50; | |
224 | *high = 100; | |
225 | break; | |
226 | case MEAS_RANGE_80_130: | |
227 | *low = 80; | |
228 | *high = 130; | |
229 | break; | |
230 | default: | |
231 | return SR_ERR; | |
232 | } | |
233 | ||
234 | return SR_OK; | |
235 | } | |
236 | ||
237 | SR_PRIV int pce_322a_meas_range_set(const struct sr_dev_inst *sdi, | |
238 | uint64_t low, uint64_t high) | |
239 | { | |
240 | struct dev_context *devc; | |
241 | uint8_t range; | |
242 | int ret = SR_OK; | |
243 | ||
244 | devc = sdi->priv; | |
245 | ||
246 | if (low == 30 && high == 130) | |
247 | range = MEAS_RANGE_30_130; | |
248 | else if (low == 30 && high == 80) | |
249 | range = MEAS_RANGE_30_80; | |
250 | else if (low == 50 && high == 100) | |
251 | range = MEAS_RANGE_50_100; | |
252 | else if (low == 80 && high == 130) | |
253 | range = MEAS_RANGE_80_130; | |
254 | else | |
255 | return SR_ERR; | |
256 | ||
257 | while (range != devc->cur_meas_range) { | |
258 | ret = send_command(sdi, CMD_TOGGLE_MEAS_RANGE); | |
259 | if (ret != SR_OK) | |
260 | break; | |
261 | range = (range - 1) & 3; | |
262 | } | |
263 | ||
264 | return ret; | |
265 | } | |
266 | ||
267 | SR_PRIV int pce_322a_power_off(const struct sr_dev_inst *sdi) | |
268 | { | |
269 | return send_command(sdi, CMD_POWER_OFF); | |
270 | } |