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