]>
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> | |
ae87e02f | 5 | * Copyright (C) 2016 Matthieu Guillaumin <matthieu@guillaum.in> |
5a2c71cc GH |
6 | * |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
21 | #include <string.h> | |
22 | #include <config.h> | |
23 | #include "protocol.h" | |
24 | ||
25 | #define SERIALCOMM "115200/8n1" | |
26 | ||
27 | static const uint32_t scanopts[] = { | |
28 | SR_CONF_CONN, | |
29 | }; | |
30 | ||
31 | static const uint32_t drvopts[] = { | |
32 | SR_CONF_SOUNDLEVELMETER, | |
33 | }; | |
34 | ||
35 | static const uint32_t devopts[] = { | |
36 | SR_CONF_CONTINUOUS | SR_CONF_SET, | |
37 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, | |
38 | SR_CONF_SPL_WEIGHT_FREQ | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
39 | SR_CONF_SPL_WEIGHT_TIME | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
40 | SR_CONF_SPL_MEASUREMENT_RANGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
41 | SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET, | |
ae87e02f | 42 | SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, |
5a2c71cc GH |
43 | }; |
44 | ||
45 | static const char *weight_freq[] = { | |
46 | "A", | |
47 | "C", | |
48 | }; | |
49 | ||
50 | static const char *weight_time[] = { | |
51 | "F", | |
52 | "S", | |
53 | }; | |
54 | ||
55 | static const uint64_t meas_ranges[][2] = { | |
56 | { 30, 130 }, | |
57 | { 30, 80 }, | |
58 | { 50, 100 }, | |
59 | { 80, 130 }, | |
60 | }; | |
61 | ||
ae87e02f MG |
62 | static const char *data_sources[] = { |
63 | "Live", | |
64 | "Memory", | |
65 | }; | |
66 | ||
5a2c71cc GH |
67 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
68 | { | |
5a2c71cc GH |
69 | struct dev_context *devc; |
70 | struct sr_config *src; | |
71 | struct sr_serial_dev_inst *serial; | |
72 | struct sr_dev_inst *sdi; | |
43376f33 | 73 | GSList *l; |
5a2c71cc GH |
74 | const char *conn; |
75 | ||
76 | conn = NULL; | |
77 | for (l = options; l; l = l->next) { | |
78 | src = l->data; | |
79 | if (src->key == SR_CONF_CONN) | |
80 | conn = g_variant_get_string(src->data, NULL); | |
81 | } | |
82 | if (!conn) | |
83 | return NULL; | |
84 | ||
85 | serial = sr_serial_dev_inst_new(conn, SERIALCOMM); | |
86 | ||
87 | if (serial_open(serial, SERIAL_RDONLY) != SR_OK) | |
88 | return NULL; | |
89 | ||
5a2c71cc GH |
90 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
91 | sdi->status = SR_ST_INACTIVE; | |
92 | sdi->vendor = g_strdup("PCE"); | |
93 | sdi->model = g_strdup("PCE-322A"); | |
94 | devc = g_malloc0(sizeof(struct dev_context)); | |
95 | devc->cur_mqflags = SR_MQFLAG_SPL_TIME_WEIGHT_F | SR_MQFLAG_SPL_FREQ_WEIGHT_A; | |
96 | sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM); | |
97 | sdi->inst_type = SR_INST_SERIAL; | |
98 | sdi->priv = devc; | |
5a2c71cc | 99 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "SPL"); |
5a2c71cc GH |
100 | |
101 | serial_close(serial); | |
102 | ||
43376f33 | 103 | return std_scan_complete(di, g_slist_append(NULL, sdi)); |
5a2c71cc GH |
104 | } |
105 | ||
106 | static int dev_clear(const struct sr_dev_driver *di) | |
107 | { | |
108 | return std_dev_clear(di, NULL); | |
109 | } | |
110 | ||
111 | static int config_get(uint32_t key, GVariant **data, | |
112 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
113 | { | |
114 | struct dev_context *devc; | |
115 | GVariant *range[2]; | |
116 | uint64_t low, high; | |
117 | uint64_t tmp; | |
118 | int ret; | |
119 | ||
120 | (void)cg; | |
121 | ||
122 | if (!sdi) | |
123 | return SR_ERR_ARG; | |
124 | ||
125 | devc = sdi->priv; | |
126 | ret = SR_OK; | |
127 | switch (key) { | |
128 | case SR_CONF_LIMIT_SAMPLES: | |
129 | *data = g_variant_new_uint64(devc->limit_samples); | |
130 | break; | |
131 | case SR_CONF_SPL_WEIGHT_FREQ: | |
132 | tmp = pce_322a_weight_freq_get(sdi); | |
133 | if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_A) | |
134 | *data = g_variant_new_string("A"); | |
135 | else if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_C) | |
136 | *data = g_variant_new_string("C"); | |
137 | else | |
138 | return SR_ERR; | |
139 | break; | |
140 | case SR_CONF_SPL_WEIGHT_TIME: | |
141 | tmp = pce_322a_weight_time_get(sdi); | |
142 | if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_F) | |
143 | *data = g_variant_new_string("F"); | |
144 | else if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_S) | |
145 | *data = g_variant_new_string("S"); | |
146 | else | |
147 | return SR_ERR; | |
148 | break; | |
149 | case SR_CONF_SPL_MEASUREMENT_RANGE: | |
150 | if ((ret = pce_322a_meas_range_get(sdi, &low, &high)) == SR_OK) { | |
151 | range[0] = g_variant_new_uint64(low); | |
152 | range[1] = g_variant_new_uint64(high); | |
153 | *data = g_variant_new_tuple(range, 2); | |
154 | } | |
155 | break; | |
156 | case SR_CONF_POWER_OFF: | |
157 | *data = g_variant_new_boolean(FALSE); | |
158 | break; | |
ae87e02f MG |
159 | case SR_CONF_DATA_SOURCE: |
160 | if (devc->cur_data_source == DATA_SOURCE_LIVE) | |
161 | *data = g_variant_new_string("Live"); | |
162 | else | |
163 | *data = g_variant_new_string("Memory"); | |
164 | break; | |
5a2c71cc GH |
165 | default: |
166 | return SR_ERR_NA; | |
167 | } | |
168 | ||
169 | return ret; | |
170 | } | |
171 | ||
172 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, | |
173 | const struct sr_channel_group *cg) | |
174 | { | |
175 | struct dev_context *devc; | |
176 | uint64_t tmp_u64, low, high; | |
177 | unsigned int i; | |
178 | int ret; | |
179 | const char *tmp_str; | |
180 | ||
181 | (void)cg; | |
182 | ||
183 | if (sdi->status != SR_ST_ACTIVE) | |
184 | return SR_ERR_DEV_CLOSED; | |
185 | ||
186 | devc = sdi->priv; | |
187 | ||
188 | ret = SR_OK; | |
189 | switch (key) { | |
190 | case SR_CONF_LIMIT_SAMPLES: | |
191 | tmp_u64 = g_variant_get_uint64(data); | |
192 | devc->limit_samples = tmp_u64; | |
193 | ret = SR_OK; | |
194 | break; | |
195 | case SR_CONF_SPL_WEIGHT_FREQ: | |
196 | tmp_str = g_variant_get_string(data, NULL); | |
197 | if (!strcmp(tmp_str, "A")) | |
198 | ret = pce_322a_weight_freq_set(sdi, | |
199 | SR_MQFLAG_SPL_FREQ_WEIGHT_A); | |
200 | else if (!strcmp(tmp_str, "C")) | |
201 | ret = pce_322a_weight_freq_set(sdi, | |
202 | SR_MQFLAG_SPL_FREQ_WEIGHT_C); | |
203 | else | |
204 | return SR_ERR_ARG; | |
205 | break; | |
206 | case SR_CONF_SPL_WEIGHT_TIME: | |
207 | tmp_str = g_variant_get_string(data, NULL); | |
208 | if (!strcmp(tmp_str, "F")) | |
209 | ret = pce_322a_weight_time_set(sdi, | |
210 | SR_MQFLAG_SPL_TIME_WEIGHT_F); | |
211 | else if (!strcmp(tmp_str, "S")) | |
212 | ret = pce_322a_weight_time_set(sdi, | |
213 | SR_MQFLAG_SPL_TIME_WEIGHT_S); | |
214 | else | |
215 | return SR_ERR_ARG; | |
216 | break; | |
217 | case SR_CONF_SPL_MEASUREMENT_RANGE: | |
218 | g_variant_get(data, "(tt)", &low, &high); | |
219 | ret = SR_ERR_ARG; | |
220 | for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) { | |
221 | if (meas_ranges[i][0] == low && meas_ranges[i][1] == high) { | |
222 | ret = pce_322a_meas_range_set(sdi, low, high); | |
223 | break; | |
224 | } | |
225 | } | |
226 | break; | |
227 | case SR_CONF_POWER_OFF: | |
228 | if (g_variant_get_boolean(data)) | |
229 | ret = pce_322a_power_off(sdi); | |
230 | break; | |
ae87e02f MG |
231 | case SR_CONF_DATA_SOURCE: |
232 | tmp_str = g_variant_get_string(data, NULL); | |
233 | if (!strcmp(tmp_str, "Live")) | |
234 | devc->cur_data_source = DATA_SOURCE_LIVE; | |
235 | else if (!strcmp(tmp_str, "Memory")) | |
236 | devc->cur_data_source = DATA_SOURCE_MEMORY; | |
237 | else | |
238 | return SR_ERR; | |
239 | break; | |
5a2c71cc GH |
240 | default: |
241 | ret = SR_ERR_NA; | |
242 | } | |
243 | ||
244 | return ret; | |
245 | } | |
246 | ||
247 | static int config_list(uint32_t key, GVariant **data, | |
248 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
249 | { | |
250 | GVariant *tuple, *range[2]; | |
251 | GVariantBuilder gvb; | |
252 | unsigned int i; | |
253 | int ret; | |
254 | ||
255 | (void)cg; | |
256 | ||
257 | ret = SR_OK; | |
258 | if (!sdi) { | |
259 | switch (key) { | |
260 | case SR_CONF_SCAN_OPTIONS: | |
261 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
262 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
263 | break; | |
264 | case SR_CONF_DEVICE_OPTIONS: | |
265 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
266 | drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); | |
267 | break; | |
268 | default: | |
269 | return SR_ERR_NA; | |
270 | } | |
271 | } else { | |
272 | switch (key) { | |
273 | case SR_CONF_DEVICE_OPTIONS: | |
274 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
275 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
276 | break; | |
277 | case SR_CONF_SPL_WEIGHT_FREQ: | |
278 | *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq)); | |
279 | break; | |
280 | case SR_CONF_SPL_WEIGHT_TIME: | |
281 | *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time)); | |
282 | break; | |
283 | case SR_CONF_SPL_MEASUREMENT_RANGE: | |
284 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
285 | for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) { | |
286 | range[0] = g_variant_new_uint64(meas_ranges[i][0]); | |
287 | range[1] = g_variant_new_uint64(meas_ranges[i][1]); | |
288 | tuple = g_variant_new_tuple(range, 2); | |
289 | g_variant_builder_add_value(&gvb, tuple); | |
290 | } | |
291 | *data = g_variant_builder_end(&gvb); | |
292 | break; | |
ae87e02f MG |
293 | case SR_CONF_DATA_SOURCE: |
294 | *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources)); | |
295 | break; | |
5a2c71cc GH |
296 | default: |
297 | return SR_ERR_NA; | |
298 | } | |
299 | } | |
300 | ||
301 | return ret; | |
302 | } | |
303 | ||
304 | static int dev_open(struct sr_dev_inst *sdi) | |
305 | { | |
306 | int ret; | |
307 | ||
308 | ret = std_serial_dev_open(sdi); | |
309 | if (ret != SR_OK) | |
310 | return ret; | |
311 | ||
312 | return pce_322a_connect(sdi); | |
313 | } | |
314 | ||
315 | static int dev_close(struct sr_dev_inst *sdi) | |
316 | { | |
317 | /* | |
318 | * Ensure device gets properly disconnected even when there was | |
319 | * no acquisition. | |
320 | */ | |
321 | pce_322a_disconnect(sdi); | |
322 | ||
323 | return std_serial_dev_close(sdi); | |
324 | } | |
325 | ||
326 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
327 | { | |
328 | struct dev_context *devc; | |
329 | struct sr_serial_dev_inst *serial; | |
330 | ||
331 | if (sdi->status != SR_ST_ACTIVE) | |
332 | return SR_ERR_DEV_CLOSED; | |
333 | ||
334 | devc = sdi->priv; | |
335 | devc->buffer_len = 0; | |
ae87e02f | 336 | devc->memory_state = MEM_STATE_REQUEST_MEMORY_USAGE; |
5a2c71cc | 337 | |
bee2b016 | 338 | std_session_send_df_header(sdi); |
5a2c71cc GH |
339 | |
340 | /* Poll every 150ms, or whenever some data comes in. */ | |
341 | serial = sdi->conn; | |
342 | serial_source_add(sdi->session, serial, G_IO_IN, 150, | |
343 | pce_322a_receive_data, (void *)sdi); | |
344 | ||
345 | return SR_OK; | |
346 | } | |
347 | ||
5a2c71cc GH |
348 | static struct sr_dev_driver pce_322a_driver_info = { |
349 | .name = "pce-322a", | |
350 | .longname = "PCE PCE-322A", | |
351 | .api_version = 1, | |
352 | .init = std_init, | |
353 | .cleanup = std_cleanup, | |
354 | .scan = scan, | |
355 | .dev_list = std_dev_list, | |
356 | .dev_clear = dev_clear, | |
357 | .config_get = config_get, | |
358 | .config_set = config_set, | |
359 | .config_list = config_list, | |
360 | .dev_open = dev_open, | |
361 | .dev_close = dev_close, | |
362 | .dev_acquisition_start = dev_acquisition_start, | |
4b1a9d5d | 363 | .dev_acquisition_stop = std_serial_dev_acquisition_stop, |
5a2c71cc GH |
364 | .context = NULL, |
365 | }; | |
366 | SR_REGISTER_DEV_DRIVER(pce_322a_driver_info); |