]>
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[] = { | |
f8195cb2 | 46 | "A", "C", |
5a2c71cc GH |
47 | }; |
48 | ||
49 | static const char *weight_time[] = { | |
f8195cb2 | 50 | "F", "S", |
5a2c71cc GH |
51 | }; |
52 | ||
53 | static const uint64_t meas_ranges[][2] = { | |
54 | { 30, 130 }, | |
55 | { 30, 80 }, | |
56 | { 50, 100 }, | |
57 | { 80, 130 }, | |
58 | }; | |
59 | ||
ae87e02f | 60 | static const char *data_sources[] = { |
f8195cb2 | 61 | "Live", "Memory", |
ae87e02f MG |
62 | }; |
63 | ||
5a2c71cc GH |
64 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
65 | { | |
5a2c71cc GH |
66 | struct dev_context *devc; |
67 | struct sr_config *src; | |
68 | struct sr_serial_dev_inst *serial; | |
69 | struct sr_dev_inst *sdi; | |
43376f33 | 70 | GSList *l; |
5a2c71cc GH |
71 | const char *conn; |
72 | ||
73 | conn = NULL; | |
74 | for (l = options; l; l = l->next) { | |
75 | src = l->data; | |
76 | if (src->key == SR_CONF_CONN) | |
77 | conn = g_variant_get_string(src->data, NULL); | |
78 | } | |
79 | if (!conn) | |
80 | return NULL; | |
81 | ||
82 | serial = sr_serial_dev_inst_new(conn, SERIALCOMM); | |
83 | ||
84 | if (serial_open(serial, SERIAL_RDONLY) != SR_OK) | |
85 | return NULL; | |
86 | ||
5a2c71cc GH |
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; | |
5a2c71cc | 96 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "SPL"); |
5a2c71cc GH |
97 | |
98 | serial_close(serial); | |
99 | ||
43376f33 | 100 | return std_scan_complete(di, g_slist_append(NULL, sdi)); |
5a2c71cc GH |
101 | } |
102 | ||
5a2c71cc GH |
103 | static int config_get(uint32_t key, GVariant **data, |
104 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
105 | { | |
106 | struct dev_context *devc; | |
107 | GVariant *range[2]; | |
108 | uint64_t low, high; | |
109 | uint64_t tmp; | |
110 | int ret; | |
111 | ||
112 | (void)cg; | |
113 | ||
114 | if (!sdi) | |
115 | return SR_ERR_ARG; | |
116 | ||
117 | devc = sdi->priv; | |
118 | ret = SR_OK; | |
119 | switch (key) { | |
120 | case SR_CONF_LIMIT_SAMPLES: | |
121 | *data = g_variant_new_uint64(devc->limit_samples); | |
122 | break; | |
123 | case SR_CONF_SPL_WEIGHT_FREQ: | |
124 | tmp = pce_322a_weight_freq_get(sdi); | |
125 | if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_A) | |
126 | *data = g_variant_new_string("A"); | |
127 | else if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_C) | |
128 | *data = g_variant_new_string("C"); | |
129 | else | |
130 | return SR_ERR; | |
131 | break; | |
132 | case SR_CONF_SPL_WEIGHT_TIME: | |
133 | tmp = pce_322a_weight_time_get(sdi); | |
134 | if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_F) | |
135 | *data = g_variant_new_string("F"); | |
136 | else if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_S) | |
137 | *data = g_variant_new_string("S"); | |
138 | else | |
139 | return SR_ERR; | |
140 | break; | |
141 | case SR_CONF_SPL_MEASUREMENT_RANGE: | |
142 | if ((ret = pce_322a_meas_range_get(sdi, &low, &high)) == SR_OK) { | |
143 | range[0] = g_variant_new_uint64(low); | |
144 | range[1] = g_variant_new_uint64(high); | |
145 | *data = g_variant_new_tuple(range, 2); | |
146 | } | |
147 | break; | |
148 | case SR_CONF_POWER_OFF: | |
149 | *data = g_variant_new_boolean(FALSE); | |
150 | break; | |
ae87e02f MG |
151 | case SR_CONF_DATA_SOURCE: |
152 | if (devc->cur_data_source == DATA_SOURCE_LIVE) | |
153 | *data = g_variant_new_string("Live"); | |
154 | else | |
155 | *data = g_variant_new_string("Memory"); | |
156 | break; | |
5a2c71cc GH |
157 | default: |
158 | return SR_ERR_NA; | |
159 | } | |
160 | ||
161 | return ret; | |
162 | } | |
163 | ||
164 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, | |
165 | const struct sr_channel_group *cg) | |
166 | { | |
167 | struct dev_context *devc; | |
168 | uint64_t tmp_u64, low, high; | |
169 | unsigned int i; | |
170 | int ret; | |
171 | const char *tmp_str; | |
172 | ||
173 | (void)cg; | |
174 | ||
5a2c71cc GH |
175 | devc = sdi->priv; |
176 | ||
177 | ret = SR_OK; | |
178 | switch (key) { | |
179 | case SR_CONF_LIMIT_SAMPLES: | |
180 | tmp_u64 = g_variant_get_uint64(data); | |
181 | devc->limit_samples = tmp_u64; | |
182 | ret = SR_OK; | |
183 | break; | |
184 | case SR_CONF_SPL_WEIGHT_FREQ: | |
185 | tmp_str = g_variant_get_string(data, NULL); | |
186 | if (!strcmp(tmp_str, "A")) | |
187 | ret = pce_322a_weight_freq_set(sdi, | |
188 | SR_MQFLAG_SPL_FREQ_WEIGHT_A); | |
189 | else if (!strcmp(tmp_str, "C")) | |
190 | ret = pce_322a_weight_freq_set(sdi, | |
191 | SR_MQFLAG_SPL_FREQ_WEIGHT_C); | |
192 | else | |
193 | return SR_ERR_ARG; | |
194 | break; | |
195 | case SR_CONF_SPL_WEIGHT_TIME: | |
196 | tmp_str = g_variant_get_string(data, NULL); | |
197 | if (!strcmp(tmp_str, "F")) | |
198 | ret = pce_322a_weight_time_set(sdi, | |
199 | SR_MQFLAG_SPL_TIME_WEIGHT_F); | |
200 | else if (!strcmp(tmp_str, "S")) | |
201 | ret = pce_322a_weight_time_set(sdi, | |
202 | SR_MQFLAG_SPL_TIME_WEIGHT_S); | |
203 | else | |
204 | return SR_ERR_ARG; | |
205 | break; | |
206 | case SR_CONF_SPL_MEASUREMENT_RANGE: | |
207 | g_variant_get(data, "(tt)", &low, &high); | |
208 | ret = SR_ERR_ARG; | |
209 | for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) { | |
210 | if (meas_ranges[i][0] == low && meas_ranges[i][1] == high) { | |
211 | ret = pce_322a_meas_range_set(sdi, low, high); | |
212 | break; | |
213 | } | |
214 | } | |
215 | break; | |
216 | case SR_CONF_POWER_OFF: | |
217 | if (g_variant_get_boolean(data)) | |
218 | ret = pce_322a_power_off(sdi); | |
219 | break; | |
ae87e02f MG |
220 | case SR_CONF_DATA_SOURCE: |
221 | tmp_str = g_variant_get_string(data, NULL); | |
222 | if (!strcmp(tmp_str, "Live")) | |
223 | devc->cur_data_source = DATA_SOURCE_LIVE; | |
224 | else if (!strcmp(tmp_str, "Memory")) | |
225 | devc->cur_data_source = DATA_SOURCE_MEMORY; | |
226 | else | |
227 | return SR_ERR; | |
228 | break; | |
5a2c71cc GH |
229 | default: |
230 | ret = SR_ERR_NA; | |
231 | } | |
232 | ||
233 | return ret; | |
234 | } | |
235 | ||
236 | static int config_list(uint32_t key, GVariant **data, | |
237 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
238 | { | |
e66d1892 UH |
239 | switch (key) { |
240 | case SR_CONF_SCAN_OPTIONS: | |
241 | case SR_CONF_DEVICE_OPTIONS: | |
242 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); | |
243 | case SR_CONF_SPL_WEIGHT_FREQ: | |
53012da6 | 244 | *data = g_variant_new_strv(ARRAY_AND_SIZE(weight_freq)); |
e66d1892 UH |
245 | break; |
246 | case SR_CONF_SPL_WEIGHT_TIME: | |
53012da6 | 247 | *data = g_variant_new_strv(ARRAY_AND_SIZE(weight_time)); |
e66d1892 UH |
248 | break; |
249 | case SR_CONF_SPL_MEASUREMENT_RANGE: | |
db944f16 | 250 | *data = std_gvar_tuple_array(&meas_ranges, ARRAY_SIZE(meas_ranges)); |
e66d1892 UH |
251 | break; |
252 | case SR_CONF_DATA_SOURCE: | |
53012da6 | 253 | *data = g_variant_new_strv(ARRAY_AND_SIZE(data_sources)); |
e66d1892 UH |
254 | break; |
255 | default: | |
256 | return SR_ERR_NA; | |
5a2c71cc GH |
257 | } |
258 | ||
e66d1892 | 259 | return SR_OK; |
5a2c71cc GH |
260 | } |
261 | ||
262 | static int dev_open(struct sr_dev_inst *sdi) | |
263 | { | |
264 | int ret; | |
265 | ||
266 | ret = std_serial_dev_open(sdi); | |
267 | if (ret != SR_OK) | |
268 | return ret; | |
269 | ||
270 | return pce_322a_connect(sdi); | |
271 | } | |
272 | ||
273 | static int dev_close(struct sr_dev_inst *sdi) | |
274 | { | |
275 | /* | |
276 | * Ensure device gets properly disconnected even when there was | |
277 | * no acquisition. | |
278 | */ | |
279 | pce_322a_disconnect(sdi); | |
280 | ||
281 | return std_serial_dev_close(sdi); | |
282 | } | |
283 | ||
284 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
285 | { | |
286 | struct dev_context *devc; | |
287 | struct sr_serial_dev_inst *serial; | |
288 | ||
5a2c71cc GH |
289 | devc = sdi->priv; |
290 | devc->buffer_len = 0; | |
ae87e02f | 291 | devc->memory_state = MEM_STATE_REQUEST_MEMORY_USAGE; |
5a2c71cc | 292 | |
bee2b016 | 293 | std_session_send_df_header(sdi); |
5a2c71cc | 294 | |
5a2c71cc GH |
295 | serial = sdi->conn; |
296 | serial_source_add(sdi->session, serial, G_IO_IN, 150, | |
297 | pce_322a_receive_data, (void *)sdi); | |
298 | ||
299 | return SR_OK; | |
300 | } | |
301 | ||
5a2c71cc GH |
302 | static struct sr_dev_driver pce_322a_driver_info = { |
303 | .name = "pce-322a", | |
304 | .longname = "PCE PCE-322A", | |
305 | .api_version = 1, | |
306 | .init = std_init, | |
307 | .cleanup = std_cleanup, | |
308 | .scan = scan, | |
309 | .dev_list = std_dev_list, | |
f778bf02 | 310 | .dev_clear = std_dev_clear, |
5a2c71cc GH |
311 | .config_get = config_get, |
312 | .config_set = config_set, | |
313 | .config_list = config_list, | |
314 | .dev_open = dev_open, | |
315 | .dev_close = dev_close, | |
316 | .dev_acquisition_start = dev_acquisition_start, | |
4b1a9d5d | 317 | .dev_acquisition_stop = std_serial_dev_acquisition_stop, |
5a2c71cc GH |
318 | .context = NULL, |
319 | }; | |
320 | SR_REGISTER_DEV_DRIVER(pce_322a_driver_info); |