]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> | |
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 <string.h> | |
22 | #include "protocol.h" | |
23 | ||
24 | #define SERIALCOMM "9600/8n1" | |
25 | ||
26 | /* 23ms is the longest interval between tokens. */ | |
27 | #define MAX_SCAN_TIME_US (25 * 1000) | |
28 | ||
29 | static const uint32_t scanopts[] = { | |
30 | SR_CONF_CONN, | |
31 | }; | |
32 | ||
33 | static const uint32_t drvopts[] = { | |
34 | SR_CONF_SOUNDLEVELMETER, | |
35 | }; | |
36 | ||
37 | static const uint32_t devopts[] = { | |
38 | SR_CONF_CONTINUOUS, | |
39 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, | |
40 | SR_CONF_SPL_WEIGHT_FREQ | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
41 | SR_CONF_SPL_WEIGHT_TIME | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
42 | SR_CONF_SPL_MEASUREMENT_RANGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
43 | SR_CONF_DATALOG | SR_CONF_GET | SR_CONF_SET, | |
44 | SR_CONF_HOLD_MAX | SR_CONF_GET | SR_CONF_SET, | |
45 | SR_CONF_HOLD_MIN | SR_CONF_GET | SR_CONF_SET, | |
46 | SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET, | |
47 | SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
48 | }; | |
49 | ||
50 | static const char *weight_freq[] = { | |
51 | "A", | |
52 | "C", | |
53 | }; | |
54 | ||
55 | static const char *weight_time[] = { | |
56 | "F", | |
57 | "S", | |
58 | }; | |
59 | ||
60 | static const uint64_t meas_ranges[][2] = { | |
61 | { 30, 130 }, | |
62 | { 30, 80 }, | |
63 | { 50, 100 }, | |
64 | { 80, 130 }, | |
65 | }; | |
66 | ||
67 | static const char *data_sources[] = { | |
68 | "Live", | |
69 | "Memory", | |
70 | }; | |
71 | ||
72 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
73 | { | |
74 | struct dev_context *devc; | |
75 | struct sr_config *src; | |
76 | struct sr_serial_dev_inst *serial; | |
77 | struct sr_dev_inst *sdi; | |
78 | GSList *l, *devices; | |
79 | gint64 start; | |
80 | const char *conn; | |
81 | unsigned char c; | |
82 | ||
83 | conn = NULL; | |
84 | for (l = options; l; l = l->next) { | |
85 | src = l->data; | |
86 | if (src->key == SR_CONF_CONN) | |
87 | conn = g_variant_get_string(src->data, NULL); | |
88 | } | |
89 | if (!conn) | |
90 | return NULL; | |
91 | ||
92 | serial = sr_serial_dev_inst_new(conn, SERIALCOMM); | |
93 | ||
94 | if (serial_open(serial, SERIAL_RDONLY) != SR_OK) | |
95 | return NULL; | |
96 | ||
97 | devices = NULL; | |
98 | start = g_get_monotonic_time(); | |
99 | while (g_get_monotonic_time() - start < MAX_SCAN_TIME_US) { | |
100 | if (serial_read_nonblocking(serial, &c, 1) == 1 && c == 0xa5) { | |
101 | /* Found one. */ | |
102 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
103 | sdi->status = SR_ST_INACTIVE; | |
104 | sdi->vendor = g_strdup("CEM"); | |
105 | sdi->model = g_strdup("DT-885x"); | |
106 | devc = g_malloc0(sizeof(struct dev_context)); | |
107 | devc->cur_mqflags = 0; | |
108 | devc->recording = -1; | |
109 | devc->cur_meas_range = 0; | |
110 | devc->cur_data_source = DATA_SOURCE_LIVE; | |
111 | devc->enable_data_source_memory = FALSE; | |
112 | sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM); | |
113 | sdi->inst_type = SR_INST_SERIAL; | |
114 | sdi->priv = devc; | |
115 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "SPL"); | |
116 | devices = g_slist_append(devices, sdi); | |
117 | break; | |
118 | } | |
119 | /* It takes about 1ms for a byte to come in. */ | |
120 | g_usleep(1000); | |
121 | } | |
122 | ||
123 | serial_close(serial); | |
124 | ||
125 | return std_scan_complete(di, devices); | |
126 | } | |
127 | ||
128 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
129 | const struct sr_channel_group *cg) | |
130 | { | |
131 | struct dev_context *devc; | |
132 | GVariant *range[2]; | |
133 | uint64_t low, high; | |
134 | int tmp, ret; | |
135 | ||
136 | (void)cg; | |
137 | ||
138 | if (!sdi) | |
139 | return SR_ERR_ARG; | |
140 | ||
141 | devc = sdi->priv; | |
142 | ret = SR_OK; | |
143 | switch (key) { | |
144 | case SR_CONF_LIMIT_SAMPLES: | |
145 | *data = g_variant_new_uint64(devc->limit_samples); | |
146 | break; | |
147 | case SR_CONF_DATALOG: | |
148 | if ((ret = cem_dt_885x_recording_get(sdi, &tmp)) == SR_OK) | |
149 | *data = g_variant_new_boolean(tmp); | |
150 | break; | |
151 | case SR_CONF_SPL_WEIGHT_FREQ: | |
152 | tmp = cem_dt_885x_weight_freq_get(sdi); | |
153 | if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_A) | |
154 | *data = g_variant_new_string("A"); | |
155 | else if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_C) | |
156 | *data = g_variant_new_string("C"); | |
157 | else | |
158 | return SR_ERR; | |
159 | break; | |
160 | case SR_CONF_SPL_WEIGHT_TIME: | |
161 | tmp = cem_dt_885x_weight_time_get(sdi); | |
162 | if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_F) | |
163 | *data = g_variant_new_string("F"); | |
164 | else if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_S) | |
165 | *data = g_variant_new_string("S"); | |
166 | else | |
167 | return SR_ERR; | |
168 | break; | |
169 | case SR_CONF_HOLD_MAX: | |
170 | if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK) | |
171 | *data = g_variant_new_boolean(tmp == SR_MQFLAG_MAX); | |
172 | break; | |
173 | case SR_CONF_HOLD_MIN: | |
174 | if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK) | |
175 | *data = g_variant_new_boolean(tmp == SR_MQFLAG_MIN); | |
176 | break; | |
177 | case SR_CONF_SPL_MEASUREMENT_RANGE: | |
178 | if ((ret = cem_dt_885x_meas_range_get(sdi, &low, &high)) == SR_OK) { | |
179 | range[0] = g_variant_new_uint64(low); | |
180 | range[1] = g_variant_new_uint64(high); | |
181 | *data = g_variant_new_tuple(range, 2); | |
182 | } | |
183 | break; | |
184 | case SR_CONF_POWER_OFF: | |
185 | *data = g_variant_new_boolean(FALSE); | |
186 | break; | |
187 | case SR_CONF_DATA_SOURCE: | |
188 | if (devc->cur_data_source == DATA_SOURCE_LIVE) | |
189 | *data = g_variant_new_string("Live"); | |
190 | else | |
191 | *data = g_variant_new_string("Memory"); | |
192 | break; | |
193 | default: | |
194 | return SR_ERR_NA; | |
195 | } | |
196 | ||
197 | return ret; | |
198 | } | |
199 | ||
200 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, | |
201 | const struct sr_channel_group *cg) | |
202 | { | |
203 | struct dev_context *devc; | |
204 | uint64_t tmp_u64, low, high; | |
205 | unsigned int i; | |
206 | int tmp, ret; | |
207 | const char *tmp_str; | |
208 | ||
209 | (void)cg; | |
210 | ||
211 | if (sdi->status != SR_ST_ACTIVE) | |
212 | return SR_ERR_DEV_CLOSED; | |
213 | ||
214 | devc = sdi->priv; | |
215 | ||
216 | ret = SR_OK; | |
217 | switch (key) { | |
218 | case SR_CONF_LIMIT_SAMPLES: | |
219 | tmp_u64 = g_variant_get_uint64(data); | |
220 | devc->limit_samples = tmp_u64; | |
221 | ret = SR_OK; | |
222 | break; | |
223 | case SR_CONF_DATALOG: | |
224 | ret = cem_dt_885x_recording_set(sdi, g_variant_get_boolean(data)); | |
225 | break; | |
226 | case SR_CONF_SPL_WEIGHT_FREQ: | |
227 | tmp_str = g_variant_get_string(data, NULL); | |
228 | if (!strcmp(tmp_str, "A")) | |
229 | ret = cem_dt_885x_weight_freq_set(sdi, | |
230 | SR_MQFLAG_SPL_FREQ_WEIGHT_A); | |
231 | else if (!strcmp(tmp_str, "C")) | |
232 | ret = cem_dt_885x_weight_freq_set(sdi, | |
233 | SR_MQFLAG_SPL_FREQ_WEIGHT_C); | |
234 | else | |
235 | return SR_ERR_ARG; | |
236 | break; | |
237 | case SR_CONF_SPL_WEIGHT_TIME: | |
238 | tmp_str = g_variant_get_string(data, NULL); | |
239 | if (!strcmp(tmp_str, "F")) | |
240 | ret = cem_dt_885x_weight_time_set(sdi, | |
241 | SR_MQFLAG_SPL_TIME_WEIGHT_F); | |
242 | else if (!strcmp(tmp_str, "S")) | |
243 | ret = cem_dt_885x_weight_time_set(sdi, | |
244 | SR_MQFLAG_SPL_TIME_WEIGHT_S); | |
245 | else | |
246 | return SR_ERR_ARG; | |
247 | break; | |
248 | case SR_CONF_HOLD_MAX: | |
249 | tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MAX : 0; | |
250 | ret = cem_dt_885x_holdmode_set(sdi, tmp); | |
251 | break; | |
252 | case SR_CONF_HOLD_MIN: | |
253 | tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MIN : 0; | |
254 | ret = cem_dt_885x_holdmode_set(sdi, tmp); | |
255 | break; | |
256 | case SR_CONF_SPL_MEASUREMENT_RANGE: | |
257 | g_variant_get(data, "(tt)", &low, &high); | |
258 | ret = SR_ERR_ARG; | |
259 | for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) { | |
260 | if (meas_ranges[i][0] == low && meas_ranges[i][1] == high) { | |
261 | ret = cem_dt_885x_meas_range_set(sdi, low, high); | |
262 | break; | |
263 | } | |
264 | } | |
265 | break; | |
266 | case SR_CONF_POWER_OFF: | |
267 | if (g_variant_get_boolean(data)) | |
268 | ret = cem_dt_885x_power_off(sdi); | |
269 | break; | |
270 | case SR_CONF_DATA_SOURCE: | |
271 | tmp_str = g_variant_get_string(data, NULL); | |
272 | if (!strcmp(tmp_str, "Live")) | |
273 | devc->cur_data_source = DATA_SOURCE_LIVE; | |
274 | else if (!strcmp(tmp_str, "Memory")) | |
275 | devc->cur_data_source = DATA_SOURCE_MEMORY; | |
276 | else | |
277 | return SR_ERR; | |
278 | devc->enable_data_source_memory = devc->cur_data_source == DATA_SOURCE_MEMORY; | |
279 | break; | |
280 | default: | |
281 | ret = SR_ERR_NA; | |
282 | } | |
283 | ||
284 | return ret; | |
285 | } | |
286 | ||
287 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
288 | const struct sr_channel_group *cg) | |
289 | { | |
290 | GVariant *tuple, *range[2]; | |
291 | GVariantBuilder gvb; | |
292 | unsigned int i; | |
293 | int ret; | |
294 | ||
295 | (void)cg; | |
296 | ||
297 | ret = SR_OK; | |
298 | if (!sdi) { | |
299 | switch (key) { | |
300 | case SR_CONF_SCAN_OPTIONS: | |
301 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
302 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
303 | break; | |
304 | case SR_CONF_DEVICE_OPTIONS: | |
305 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
306 | drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); | |
307 | break; | |
308 | default: | |
309 | return SR_ERR_NA; | |
310 | } | |
311 | } else { | |
312 | switch (key) { | |
313 | case SR_CONF_DEVICE_OPTIONS: | |
314 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
315 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
316 | break; | |
317 | case SR_CONF_SPL_WEIGHT_FREQ: | |
318 | *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq)); | |
319 | break; | |
320 | case SR_CONF_SPL_WEIGHT_TIME: | |
321 | *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time)); | |
322 | break; | |
323 | case SR_CONF_SPL_MEASUREMENT_RANGE: | |
324 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
325 | for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) { | |
326 | range[0] = g_variant_new_uint64(meas_ranges[i][0]); | |
327 | range[1] = g_variant_new_uint64(meas_ranges[i][1]); | |
328 | tuple = g_variant_new_tuple(range, 2); | |
329 | g_variant_builder_add_value(&gvb, tuple); | |
330 | } | |
331 | *data = g_variant_builder_end(&gvb); | |
332 | break; | |
333 | case SR_CONF_DATA_SOURCE: | |
334 | *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources)); | |
335 | break; | |
336 | default: | |
337 | return SR_ERR_NA; | |
338 | } | |
339 | } | |
340 | ||
341 | return ret; | |
342 | } | |
343 | ||
344 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
345 | { | |
346 | struct dev_context *devc; | |
347 | struct sr_serial_dev_inst *serial; | |
348 | ||
349 | if (sdi->status != SR_ST_ACTIVE) | |
350 | return SR_ERR_DEV_CLOSED; | |
351 | ||
352 | devc = sdi->priv; | |
353 | devc->state = ST_INIT; | |
354 | devc->num_samples = 0; | |
355 | devc->buf_len = 0; | |
356 | ||
357 | std_session_send_df_header(sdi); | |
358 | ||
359 | /* Poll every 100ms, or whenever some data comes in. */ | |
360 | serial = sdi->conn; | |
361 | serial_source_add(sdi->session, serial, G_IO_IN, 150, | |
362 | cem_dt_885x_receive_data, (void *)sdi); | |
363 | ||
364 | return SR_OK; | |
365 | } | |
366 | ||
367 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
368 | { | |
369 | if (sdi->status != SR_ST_ACTIVE) | |
370 | return SR_ERR_DEV_CLOSED; | |
371 | ||
372 | return std_serial_dev_acquisition_stop(sdi); | |
373 | } | |
374 | ||
375 | static struct sr_dev_driver cem_dt_885x_driver_info = { | |
376 | .name = "cem-dt-885x", | |
377 | .longname = "CEM DT-885x", | |
378 | .api_version = 1, | |
379 | .init = std_init, | |
380 | .cleanup = std_cleanup, | |
381 | .scan = scan, | |
382 | .dev_list = std_dev_list, | |
383 | .dev_clear = NULL, | |
384 | .config_get = config_get, | |
385 | .config_set = config_set, | |
386 | .config_list = config_list, | |
387 | .dev_open = std_serial_dev_open, | |
388 | .dev_close = std_serial_dev_close, | |
389 | .dev_acquisition_start = dev_acquisition_start, | |
390 | .dev_acquisition_stop = dev_acquisition_stop, | |
391 | .context = NULL, | |
392 | }; | |
393 | SR_REGISTER_DEV_DRIVER(cem_dt_885x_driver_info); |