]>
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 | { | |
5a2c71cc GH |
62 | struct dev_context *devc; |
63 | struct sr_config *src; | |
64 | struct sr_serial_dev_inst *serial; | |
65 | struct sr_dev_inst *sdi; | |
66 | GSList *l, *devices; | |
67 | const char *conn; | |
68 | ||
69 | conn = NULL; | |
70 | for (l = options; l; l = l->next) { | |
71 | src = l->data; | |
72 | if (src->key == SR_CONF_CONN) | |
73 | conn = g_variant_get_string(src->data, NULL); | |
74 | } | |
75 | if (!conn) | |
76 | return NULL; | |
77 | ||
78 | serial = sr_serial_dev_inst_new(conn, SERIALCOMM); | |
79 | ||
80 | if (serial_open(serial, SERIAL_RDONLY) != SR_OK) | |
81 | return NULL; | |
82 | ||
83 | devices = NULL; | |
5a2c71cc GH |
84 | |
85 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
86 | sdi->status = SR_ST_INACTIVE; | |
87 | sdi->vendor = g_strdup("PCE"); | |
88 | sdi->model = g_strdup("PCE-322A"); | |
89 | devc = g_malloc0(sizeof(struct dev_context)); | |
90 | devc->cur_mqflags = SR_MQFLAG_SPL_TIME_WEIGHT_F | SR_MQFLAG_SPL_FREQ_WEIGHT_A; | |
91 | sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM); | |
92 | sdi->inst_type = SR_INST_SERIAL; | |
93 | sdi->priv = devc; | |
5a2c71cc | 94 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "SPL"); |
5a2c71cc GH |
95 | devices = g_slist_append(devices, sdi); |
96 | ||
97 | serial_close(serial); | |
98 | ||
15a5bfe4 | 99 | return std_scan_complete(di, devices); |
5a2c71cc GH |
100 | } |
101 | ||
102 | static int dev_clear(const struct sr_dev_driver *di) | |
103 | { | |
104 | return std_dev_clear(di, NULL); | |
105 | } | |
106 | ||
107 | static int config_get(uint32_t key, GVariant **data, | |
108 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
109 | { | |
110 | struct dev_context *devc; | |
111 | GVariant *range[2]; | |
112 | uint64_t low, high; | |
113 | uint64_t tmp; | |
114 | int ret; | |
115 | ||
116 | (void)cg; | |
117 | ||
118 | if (!sdi) | |
119 | return SR_ERR_ARG; | |
120 | ||
121 | devc = sdi->priv; | |
122 | ret = SR_OK; | |
123 | switch (key) { | |
124 | case SR_CONF_LIMIT_SAMPLES: | |
125 | *data = g_variant_new_uint64(devc->limit_samples); | |
126 | break; | |
127 | case SR_CONF_SPL_WEIGHT_FREQ: | |
128 | tmp = pce_322a_weight_freq_get(sdi); | |
129 | if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_A) | |
130 | *data = g_variant_new_string("A"); | |
131 | else if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_C) | |
132 | *data = g_variant_new_string("C"); | |
133 | else | |
134 | return SR_ERR; | |
135 | break; | |
136 | case SR_CONF_SPL_WEIGHT_TIME: | |
137 | tmp = pce_322a_weight_time_get(sdi); | |
138 | if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_F) | |
139 | *data = g_variant_new_string("F"); | |
140 | else if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_S) | |
141 | *data = g_variant_new_string("S"); | |
142 | else | |
143 | return SR_ERR; | |
144 | break; | |
145 | case SR_CONF_SPL_MEASUREMENT_RANGE: | |
146 | if ((ret = pce_322a_meas_range_get(sdi, &low, &high)) == SR_OK) { | |
147 | range[0] = g_variant_new_uint64(low); | |
148 | range[1] = g_variant_new_uint64(high); | |
149 | *data = g_variant_new_tuple(range, 2); | |
150 | } | |
151 | break; | |
152 | case SR_CONF_POWER_OFF: | |
153 | *data = g_variant_new_boolean(FALSE); | |
154 | break; | |
155 | default: | |
156 | return SR_ERR_NA; | |
157 | } | |
158 | ||
159 | return ret; | |
160 | } | |
161 | ||
162 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, | |
163 | const struct sr_channel_group *cg) | |
164 | { | |
165 | struct dev_context *devc; | |
166 | uint64_t tmp_u64, low, high; | |
167 | unsigned int i; | |
168 | int ret; | |
169 | const char *tmp_str; | |
170 | ||
171 | (void)cg; | |
172 | ||
173 | if (sdi->status != SR_ST_ACTIVE) | |
174 | return SR_ERR_DEV_CLOSED; | |
175 | ||
176 | devc = sdi->priv; | |
177 | ||
178 | ret = SR_OK; | |
179 | switch (key) { | |
180 | case SR_CONF_LIMIT_SAMPLES: | |
181 | tmp_u64 = g_variant_get_uint64(data); | |
182 | devc->limit_samples = tmp_u64; | |
183 | ret = SR_OK; | |
184 | break; | |
185 | case SR_CONF_SPL_WEIGHT_FREQ: | |
186 | tmp_str = g_variant_get_string(data, NULL); | |
187 | if (!strcmp(tmp_str, "A")) | |
188 | ret = pce_322a_weight_freq_set(sdi, | |
189 | SR_MQFLAG_SPL_FREQ_WEIGHT_A); | |
190 | else if (!strcmp(tmp_str, "C")) | |
191 | ret = pce_322a_weight_freq_set(sdi, | |
192 | SR_MQFLAG_SPL_FREQ_WEIGHT_C); | |
193 | else | |
194 | return SR_ERR_ARG; | |
195 | break; | |
196 | case SR_CONF_SPL_WEIGHT_TIME: | |
197 | tmp_str = g_variant_get_string(data, NULL); | |
198 | if (!strcmp(tmp_str, "F")) | |
199 | ret = pce_322a_weight_time_set(sdi, | |
200 | SR_MQFLAG_SPL_TIME_WEIGHT_F); | |
201 | else if (!strcmp(tmp_str, "S")) | |
202 | ret = pce_322a_weight_time_set(sdi, | |
203 | SR_MQFLAG_SPL_TIME_WEIGHT_S); | |
204 | else | |
205 | return SR_ERR_ARG; | |
206 | break; | |
207 | case SR_CONF_SPL_MEASUREMENT_RANGE: | |
208 | g_variant_get(data, "(tt)", &low, &high); | |
209 | ret = SR_ERR_ARG; | |
210 | for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) { | |
211 | if (meas_ranges[i][0] == low && meas_ranges[i][1] == high) { | |
212 | ret = pce_322a_meas_range_set(sdi, low, high); | |
213 | break; | |
214 | } | |
215 | } | |
216 | break; | |
217 | case SR_CONF_POWER_OFF: | |
218 | if (g_variant_get_boolean(data)) | |
219 | ret = pce_322a_power_off(sdi); | |
220 | break; | |
221 | default: | |
222 | ret = SR_ERR_NA; | |
223 | } | |
224 | ||
225 | return ret; | |
226 | } | |
227 | ||
228 | static int config_list(uint32_t key, GVariant **data, | |
229 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
230 | { | |
231 | GVariant *tuple, *range[2]; | |
232 | GVariantBuilder gvb; | |
233 | unsigned int i; | |
234 | int ret; | |
235 | ||
236 | (void)cg; | |
237 | ||
238 | ret = SR_OK; | |
239 | if (!sdi) { | |
240 | switch (key) { | |
241 | case SR_CONF_SCAN_OPTIONS: | |
242 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
243 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
244 | break; | |
245 | case SR_CONF_DEVICE_OPTIONS: | |
246 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
247 | drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); | |
248 | break; | |
249 | default: | |
250 | return SR_ERR_NA; | |
251 | } | |
252 | } else { | |
253 | switch (key) { | |
254 | case SR_CONF_DEVICE_OPTIONS: | |
255 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
256 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
257 | break; | |
258 | case SR_CONF_SPL_WEIGHT_FREQ: | |
259 | *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq)); | |
260 | break; | |
261 | case SR_CONF_SPL_WEIGHT_TIME: | |
262 | *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time)); | |
263 | break; | |
264 | case SR_CONF_SPL_MEASUREMENT_RANGE: | |
265 | g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); | |
266 | for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) { | |
267 | range[0] = g_variant_new_uint64(meas_ranges[i][0]); | |
268 | range[1] = g_variant_new_uint64(meas_ranges[i][1]); | |
269 | tuple = g_variant_new_tuple(range, 2); | |
270 | g_variant_builder_add_value(&gvb, tuple); | |
271 | } | |
272 | *data = g_variant_builder_end(&gvb); | |
273 | break; | |
274 | default: | |
275 | return SR_ERR_NA; | |
276 | } | |
277 | } | |
278 | ||
279 | return ret; | |
280 | } | |
281 | ||
282 | static int dev_open(struct sr_dev_inst *sdi) | |
283 | { | |
284 | int ret; | |
285 | ||
286 | ret = std_serial_dev_open(sdi); | |
287 | if (ret != SR_OK) | |
288 | return ret; | |
289 | ||
290 | return pce_322a_connect(sdi); | |
291 | } | |
292 | ||
293 | static int dev_close(struct sr_dev_inst *sdi) | |
294 | { | |
295 | /* | |
296 | * Ensure device gets properly disconnected even when there was | |
297 | * no acquisition. | |
298 | */ | |
299 | pce_322a_disconnect(sdi); | |
300 | ||
301 | return std_serial_dev_close(sdi); | |
302 | } | |
303 | ||
304 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
305 | { | |
306 | struct dev_context *devc; | |
307 | struct sr_serial_dev_inst *serial; | |
308 | ||
309 | if (sdi->status != SR_ST_ACTIVE) | |
310 | return SR_ERR_DEV_CLOSED; | |
311 | ||
312 | devc = sdi->priv; | |
313 | devc->buffer_len = 0; | |
314 | ||
315 | std_session_send_df_header(sdi, LOG_PREFIX); | |
316 | ||
317 | /* Poll every 150ms, or whenever some data comes in. */ | |
318 | serial = sdi->conn; | |
319 | serial_source_add(sdi->session, serial, G_IO_IN, 150, | |
320 | pce_322a_receive_data, (void *)sdi); | |
321 | ||
322 | return SR_OK; | |
323 | } | |
324 | ||
325 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
326 | { | |
327 | pce_322a_disconnect(sdi); | |
328 | ||
329 | return std_serial_dev_acquisition_stop(sdi, std_serial_dev_close, | |
330 | sdi->conn, LOG_PREFIX); | |
331 | } | |
332 | ||
333 | static struct sr_dev_driver pce_322a_driver_info = { | |
334 | .name = "pce-322a", | |
335 | .longname = "PCE PCE-322A", | |
336 | .api_version = 1, | |
337 | .init = std_init, | |
338 | .cleanup = std_cleanup, | |
339 | .scan = scan, | |
340 | .dev_list = std_dev_list, | |
341 | .dev_clear = dev_clear, | |
342 | .config_get = config_get, | |
343 | .config_set = config_set, | |
344 | .config_list = config_list, | |
345 | .dev_open = dev_open, | |
346 | .dev_close = dev_close, | |
347 | .dev_acquisition_start = dev_acquisition_start, | |
348 | .dev_acquisition_stop = dev_acquisition_stop, | |
349 | .context = NULL, | |
350 | }; | |
351 | SR_REGISTER_DEV_DRIVER(pce_322a_driver_info); |