]>
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 <string.h> | |
21 | #include <config.h> | |
22 | #include "protocol.h" | |
23 | ||
24 | #define SERIALCOMM "115200/8n1" | |
25 | ||
26 | static const uint32_t scanopts[] = { | |
27 | SR_CONF_CONN, | |
28 | }; | |
29 | ||
30 | static const uint32_t drvopts[] = { | |
31 | SR_CONF_SOUNDLEVELMETER, | |
32 | }; | |
33 | ||
34 | static const uint32_t devopts[] = { | |
35 | SR_CONF_CONTINUOUS | SR_CONF_SET, | |
36 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, | |
37 | SR_CONF_SPL_WEIGHT_FREQ | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
38 | SR_CONF_SPL_WEIGHT_TIME | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
39 | SR_CONF_SPL_MEASUREMENT_RANGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
40 | SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET, | |
41 | }; | |
42 | ||
43 | static const char *weight_freq[] = { | |
44 | "A", | |
45 | "C", | |
46 | }; | |
47 | ||
48 | static const char *weight_time[] = { | |
49 | "F", | |
50 | "S", | |
51 | }; | |
52 | ||
53 | static const uint64_t meas_ranges[][2] = { | |
54 | { 30, 130 }, | |
55 | { 30, 80 }, | |
56 | { 50, 100 }, | |
57 | { 80, 130 }, | |
58 | }; | |
59 | ||
60 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
61 | { | |
62 | struct drv_context *drvc; | |
63 | struct dev_context *devc; | |
64 | struct sr_config *src; | |
65 | struct sr_serial_dev_inst *serial; | |
66 | struct sr_dev_inst *sdi; | |
67 | GSList *l, *devices; | |
68 | const char *conn; | |
69 | ||
70 | conn = NULL; | |
71 | for (l = options; l; l = l->next) { | |
72 | src = l->data; | |
73 | if (src->key == SR_CONF_CONN) | |
74 | conn = g_variant_get_string(src->data, NULL); | |
75 | } | |
76 | if (!conn) | |
77 | return NULL; | |
78 | ||
79 | serial = sr_serial_dev_inst_new(conn, SERIALCOMM); | |
80 | ||
81 | if (serial_open(serial, SERIAL_RDONLY) != SR_OK) | |
82 | return NULL; | |
83 | ||
84 | devices = NULL; | |
85 | drvc = di->context; | |
86 | ||
87 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
88 | sdi->status = SR_ST_INACTIVE; | |
89 | sdi->vendor = g_strdup("PCE"); | |
90 | sdi->model = g_strdup("PCE-322A"); | |
91 | devc = g_malloc0(sizeof(struct dev_context)); | |
92 | devc->cur_mqflags = SR_MQFLAG_SPL_TIME_WEIGHT_F | SR_MQFLAG_SPL_FREQ_WEIGHT_A; | |
93 | sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM); | |
94 | sdi->inst_type = SR_INST_SERIAL; | |
95 | sdi->priv = devc; | |
96 | sdi->driver = di; | |
97 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "SPL"); | |
98 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
99 | devices = g_slist_append(devices, sdi); | |
100 | ||
101 | serial_close(serial); | |
102 | ||
103 | return devices; | |
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; | |
159 | default: | |
160 | return SR_ERR_NA; | |
161 | } | |
162 | ||
163 | return ret; | |
164 | } | |
165 | ||
166 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, | |
167 | const struct sr_channel_group *cg) | |
168 | { | |
169 | struct dev_context *devc; | |
170 | uint64_t tmp_u64, low, high; | |
171 | unsigned int i; | |
172 | int ret; | |
173 | const char *tmp_str; | |
174 | ||
175 | (void)cg; | |
176 | ||
177 | if (sdi->status != SR_ST_ACTIVE) | |
178 | return SR_ERR_DEV_CLOSED; | |
179 | ||
180 | devc = sdi->priv; | |
181 | ||
182 | ret = SR_OK; | |
183 | switch (key) { | |
184 | case SR_CONF_LIMIT_SAMPLES: | |
185 | tmp_u64 = g_variant_get_uint64(data); | |
186 | devc->limit_samples = tmp_u64; | |
187 | ret = SR_OK; | |
188 | break; | |
189 | case SR_CONF_SPL_WEIGHT_FREQ: | |
190 | tmp_str = g_variant_get_string(data, NULL); | |
191 | if (!strcmp(tmp_str, "A")) | |
192 | ret = pce_322a_weight_freq_set(sdi, | |
193 | SR_MQFLAG_SPL_FREQ_WEIGHT_A); | |
194 | else if (!strcmp(tmp_str, "C")) | |
195 | ret = pce_322a_weight_freq_set(sdi, | |
196 | SR_MQFLAG_SPL_FREQ_WEIGHT_C); | |
197 | else | |
198 | return SR_ERR_ARG; | |
199 | break; | |
200 | case SR_CONF_SPL_WEIGHT_TIME: | |
201 | tmp_str = g_variant_get_string(data, NULL); | |
202 | if (!strcmp(tmp_str, "F")) | |
203 | ret = pce_322a_weight_time_set(sdi, | |
204 | SR_MQFLAG_SPL_TIME_WEIGHT_F); | |
205 | else if (!strcmp(tmp_str, "S")) | |
206 | ret = pce_322a_weight_time_set(sdi, | |
207 | SR_MQFLAG_SPL_TIME_WEIGHT_S); | |
208 | else | |
209 | return SR_ERR_ARG; | |
210 | break; | |
211 | case SR_CONF_SPL_MEASUREMENT_RANGE: | |
212 | g_variant_get(data, "(tt)", &low, &high); | |
213 | ret = SR_ERR_ARG; | |
214 | for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) { | |
215 | if (meas_ranges[i][0] == low && meas_ranges[i][1] == high) { | |
216 | ret = pce_322a_meas_range_set(sdi, low, high); | |
217 | break; | |
218 | } | |
219 | } | |
220 | break; | |
221 | case SR_CONF_POWER_OFF: | |
222 | if (g_variant_get_boolean(data)) | |
223 | ret = pce_322a_power_off(sdi); | |
224 | break; | |
225 | default: | |
226 | ret = SR_ERR_NA; | |
227 | } | |
228 | ||
229 | return ret; | |
230 | } | |
231 | ||
232 | static int config_list(uint32_t key, GVariant **data, | |
233 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
234 | { | |
235 | GVariant *tuple, *range[2]; | |
236 | GVariantBuilder gvb; | |
237 | unsigned int i; | |
238 | int ret; | |
239 | ||
240 | (void)cg; | |
241 | ||
242 | ret = SR_OK; | |
243 | if (!sdi) { | |
244 | switch (key) { | |
245 | case SR_CONF_SCAN_OPTIONS: | |
246 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
247 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
248 | break; | |
249 | case SR_CONF_DEVICE_OPTIONS: | |
250 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
251 | drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); | |
252 | break; | |
253 | default: | |
254 | return SR_ERR_NA; | |
255 | } | |
256 | } else { | |
257 | switch (key) { | |
258 | case SR_CONF_DEVICE_OPTIONS: | |
259 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
260 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
261 | break; | |
262 | case SR_CONF_SPL_WEIGHT_FREQ: | |
263 | *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq)); | |
264 | break; | |
265 | case SR_CONF_SPL_WEIGHT_TIME: | |
266 | *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time)); | |
267 | break; | |
268 | case SR_CONF_SPL_MEASUREMENT_RANGE: | |
269 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
270 | for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) { | |
271 | range[0] = g_variant_new_uint64(meas_ranges[i][0]); | |
272 | range[1] = g_variant_new_uint64(meas_ranges[i][1]); | |
273 | tuple = g_variant_new_tuple(range, 2); | |
274 | g_variant_builder_add_value(&gvb, tuple); | |
275 | } | |
276 | *data = g_variant_builder_end(&gvb); | |
277 | break; | |
278 | default: | |
279 | return SR_ERR_NA; | |
280 | } | |
281 | } | |
282 | ||
283 | return ret; | |
284 | } | |
285 | ||
286 | static int dev_open(struct sr_dev_inst *sdi) | |
287 | { | |
288 | int ret; | |
289 | ||
290 | ret = std_serial_dev_open(sdi); | |
291 | if (ret != SR_OK) | |
292 | return ret; | |
293 | ||
294 | return pce_322a_connect(sdi); | |
295 | } | |
296 | ||
297 | static int dev_close(struct sr_dev_inst *sdi) | |
298 | { | |
299 | /* | |
300 | * Ensure device gets properly disconnected even when there was | |
301 | * no acquisition. | |
302 | */ | |
303 | pce_322a_disconnect(sdi); | |
304 | ||
305 | return std_serial_dev_close(sdi); | |
306 | } | |
307 | ||
308 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
309 | { | |
310 | struct dev_context *devc; | |
311 | struct sr_serial_dev_inst *serial; | |
312 | ||
313 | if (sdi->status != SR_ST_ACTIVE) | |
314 | return SR_ERR_DEV_CLOSED; | |
315 | ||
316 | devc = sdi->priv; | |
317 | devc->buffer_len = 0; | |
318 | ||
319 | std_session_send_df_header(sdi, LOG_PREFIX); | |
320 | ||
321 | /* Poll every 150ms, or whenever some data comes in. */ | |
322 | serial = sdi->conn; | |
323 | serial_source_add(sdi->session, serial, G_IO_IN, 150, | |
324 | pce_322a_receive_data, (void *)sdi); | |
325 | ||
326 | return SR_OK; | |
327 | } | |
328 | ||
329 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
330 | { | |
331 | pce_322a_disconnect(sdi); | |
332 | ||
333 | return std_serial_dev_acquisition_stop(sdi, std_serial_dev_close, | |
334 | sdi->conn, LOG_PREFIX); | |
335 | } | |
336 | ||
337 | static struct sr_dev_driver pce_322a_driver_info = { | |
338 | .name = "pce-322a", | |
339 | .longname = "PCE PCE-322A", | |
340 | .api_version = 1, | |
341 | .init = std_init, | |
342 | .cleanup = std_cleanup, | |
343 | .scan = scan, | |
344 | .dev_list = std_dev_list, | |
345 | .dev_clear = dev_clear, | |
346 | .config_get = config_get, | |
347 | .config_set = config_set, | |
348 | .config_list = config_list, | |
349 | .dev_open = dev_open, | |
350 | .dev_close = dev_close, | |
351 | .dev_acquisition_start = dev_acquisition_start, | |
352 | .dev_acquisition_stop = dev_acquisition_stop, | |
353 | .context = NULL, | |
354 | }; | |
355 | SR_REGISTER_DEV_DRIVER(pce_322a_driver_info); |