]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> | |
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 <config.h> | |
21 | #include <string.h> | |
22 | #include "protocol.h" | |
23 | ||
24 | #define SERIALCOMM "9600/8n1" | |
25 | ||
26 | /* 23ms is the longest interval between tokens. */ | |
27 | #define MAX_SCAN_TIME_US (25 * 1000) | |
28 | ||
29 | static const uint32_t scanopts[] = { | |
30 | SR_CONF_CONN, | |
31 | }; | |
32 | ||
33 | static const uint32_t drvopts[] = { | |
34 | SR_CONF_SOUNDLEVELMETER, | |
35 | }; | |
36 | ||
37 | static const uint32_t devopts[] = { | |
38 | SR_CONF_CONTINUOUS, | |
39 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, | |
40 | SR_CONF_SPL_WEIGHT_FREQ | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
41 | SR_CONF_SPL_WEIGHT_TIME | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
42 | SR_CONF_SPL_MEASUREMENT_RANGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
43 | SR_CONF_DATALOG | SR_CONF_GET | SR_CONF_SET, | |
44 | SR_CONF_HOLD_MAX | SR_CONF_GET | SR_CONF_SET, | |
45 | SR_CONF_HOLD_MIN | SR_CONF_GET | SR_CONF_SET, | |
46 | SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET, | |
47 | SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
48 | }; | |
49 | ||
50 | static const char *weight_freq[] = { | |
51 | "A", | |
52 | "C", | |
53 | }; | |
54 | ||
55 | static const char *weight_time[] = { | |
56 | "F", | |
57 | "S", | |
58 | }; | |
59 | ||
60 | static const uint64_t meas_ranges[][2] = { | |
61 | { 30, 130 }, | |
62 | { 30, 80 }, | |
63 | { 50, 100 }, | |
64 | { 80, 130 }, | |
65 | }; | |
66 | ||
67 | static const char *data_sources[] = { | |
68 | "Live", | |
69 | "Memory", | |
70 | }; | |
71 | ||
72 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
73 | { | |
74 | struct dev_context *devc; | |
75 | struct sr_config *src; | |
76 | struct sr_serial_dev_inst *serial; | |
77 | struct sr_dev_inst *sdi; | |
78 | GSList *l, *devices; | |
79 | gint64 start; | |
80 | const char *conn; | |
81 | unsigned char c; | |
82 | ||
83 | conn = NULL; | |
84 | for (l = options; l; l = l->next) { | |
85 | src = l->data; | |
86 | if (src->key == SR_CONF_CONN) | |
87 | conn = g_variant_get_string(src->data, NULL); | |
88 | } | |
89 | if (!conn) | |
90 | return NULL; | |
91 | ||
92 | serial = sr_serial_dev_inst_new(conn, SERIALCOMM); | |
93 | ||
94 | if (serial_open(serial, SERIAL_RDONLY) != SR_OK) | |
95 | return NULL; | |
96 | ||
97 | devices = NULL; | |
98 | start = g_get_monotonic_time(); | |
99 | while (g_get_monotonic_time() - start < MAX_SCAN_TIME_US) { | |
100 | if (serial_read_nonblocking(serial, &c, 1) == 1 && c == 0xa5) { | |
101 | /* Found one. */ | |
102 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
103 | sdi->status = SR_ST_INACTIVE; | |
104 | sdi->vendor = g_strdup("CEM"); | |
105 | sdi->model = g_strdup("DT-885x"); | |
106 | devc = g_malloc0(sizeof(struct dev_context)); | |
107 | devc->cur_mqflags = 0; | |
108 | devc->recording = -1; | |
109 | devc->cur_meas_range = 0; | |
110 | devc->cur_data_source = DATA_SOURCE_LIVE; | |
111 | devc->enable_data_source_memory = FALSE; | |
112 | sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM); | |
113 | sdi->inst_type = SR_INST_SERIAL; | |
114 | sdi->priv = devc; | |
115 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "SPL"); | |
116 | devices = g_slist_append(devices, sdi); | |
117 | break; | |
118 | } | |
119 | /* It takes about 1ms for a byte to come in. */ | |
120 | g_usleep(1000); | |
121 | } | |
122 | ||
123 | serial_close(serial); | |
124 | ||
125 | return std_scan_complete(di, devices); | |
126 | } | |
127 | ||
128 | static int config_get(uint32_t key, GVariant **data, | |
129 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
130 | { | |
131 | struct dev_context *devc; | |
132 | uint64_t low, high; | |
133 | int tmp, ret; | |
134 | ||
135 | (void)cg; | |
136 | ||
137 | if (!sdi) | |
138 | return SR_ERR_ARG; | |
139 | ||
140 | devc = sdi->priv; | |
141 | ret = SR_OK; | |
142 | switch (key) { | |
143 | case SR_CONF_LIMIT_SAMPLES: | |
144 | *data = g_variant_new_uint64(devc->limit_samples); | |
145 | break; | |
146 | case SR_CONF_DATALOG: | |
147 | if ((ret = cem_dt_885x_recording_get(sdi, &tmp)) == SR_OK) | |
148 | *data = g_variant_new_boolean(tmp); | |
149 | break; | |
150 | case SR_CONF_SPL_WEIGHT_FREQ: | |
151 | tmp = cem_dt_885x_weight_freq_get(sdi); | |
152 | if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_A) | |
153 | *data = g_variant_new_string("A"); | |
154 | else if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_C) | |
155 | *data = g_variant_new_string("C"); | |
156 | else | |
157 | return SR_ERR; | |
158 | break; | |
159 | case SR_CONF_SPL_WEIGHT_TIME: | |
160 | tmp = cem_dt_885x_weight_time_get(sdi); | |
161 | if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_F) | |
162 | *data = g_variant_new_string("F"); | |
163 | else if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_S) | |
164 | *data = g_variant_new_string("S"); | |
165 | else | |
166 | return SR_ERR; | |
167 | break; | |
168 | case SR_CONF_HOLD_MAX: | |
169 | if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK) | |
170 | *data = g_variant_new_boolean(tmp == SR_MQFLAG_MAX); | |
171 | break; | |
172 | case SR_CONF_HOLD_MIN: | |
173 | if ((ret = cem_dt_885x_holdmode_get(sdi, &tmp)) == SR_OK) | |
174 | *data = g_variant_new_boolean(tmp == SR_MQFLAG_MIN); | |
175 | break; | |
176 | case SR_CONF_SPL_MEASUREMENT_RANGE: | |
177 | if ((ret = cem_dt_885x_meas_range_get(sdi, &low, &high)) == SR_OK) | |
178 | *data = std_gvar_tuple_u64(low, high); | |
179 | break; | |
180 | case SR_CONF_POWER_OFF: | |
181 | *data = g_variant_new_boolean(FALSE); | |
182 | break; | |
183 | case SR_CONF_DATA_SOURCE: | |
184 | if (devc->cur_data_source == DATA_SOURCE_LIVE) | |
185 | *data = g_variant_new_string("Live"); | |
186 | else | |
187 | *data = g_variant_new_string("Memory"); | |
188 | break; | |
189 | default: | |
190 | return SR_ERR_NA; | |
191 | } | |
192 | ||
193 | return ret; | |
194 | } | |
195 | ||
196 | static int config_set(uint32_t key, GVariant *data, | |
197 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
198 | { | |
199 | struct dev_context *devc; | |
200 | int tmp, ret, idx; | |
201 | ||
202 | (void)cg; | |
203 | ||
204 | devc = sdi->priv; | |
205 | ||
206 | ret = SR_OK; | |
207 | switch (key) { | |
208 | case SR_CONF_LIMIT_SAMPLES: | |
209 | devc->limit_samples = g_variant_get_uint64(data); | |
210 | break; | |
211 | case SR_CONF_DATALOG: | |
212 | ret = cem_dt_885x_recording_set(sdi, g_variant_get_boolean(data)); | |
213 | break; | |
214 | case SR_CONF_SPL_WEIGHT_FREQ: | |
215 | if ((idx = std_str_idx(data, ARRAY_AND_SIZE(weight_freq))) < 0) | |
216 | return SR_ERR_ARG; | |
217 | return cem_dt_885x_weight_freq_set(sdi, (weight_freq[idx][0] == 'A') ? | |
218 | SR_MQFLAG_SPL_FREQ_WEIGHT_A : SR_MQFLAG_SPL_FREQ_WEIGHT_C); | |
219 | case SR_CONF_SPL_WEIGHT_TIME: | |
220 | if ((idx = std_str_idx(data, ARRAY_AND_SIZE(weight_time))) < 0) | |
221 | return SR_ERR_ARG; | |
222 | return cem_dt_885x_weight_time_set(sdi, (weight_time[idx][0] == 'F') ? | |
223 | SR_MQFLAG_SPL_TIME_WEIGHT_F : SR_MQFLAG_SPL_TIME_WEIGHT_S); | |
224 | case SR_CONF_HOLD_MAX: | |
225 | tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MAX : 0; | |
226 | ret = cem_dt_885x_holdmode_set(sdi, tmp); | |
227 | break; | |
228 | case SR_CONF_HOLD_MIN: | |
229 | tmp = g_variant_get_boolean(data) ? SR_MQFLAG_MIN : 0; | |
230 | ret = cem_dt_885x_holdmode_set(sdi, tmp); | |
231 | break; | |
232 | case SR_CONF_SPL_MEASUREMENT_RANGE: | |
233 | if ((idx = std_u64_tuple_idx(data, ARRAY_AND_SIZE(meas_ranges))) < 0) | |
234 | return SR_ERR_ARG; | |
235 | return cem_dt_885x_meas_range_set(sdi, meas_ranges[idx][0], meas_ranges[idx][1]); | |
236 | case SR_CONF_POWER_OFF: | |
237 | if (g_variant_get_boolean(data)) | |
238 | ret = cem_dt_885x_power_off(sdi); | |
239 | break; | |
240 | case SR_CONF_DATA_SOURCE: | |
241 | if ((idx = std_str_idx(data, ARRAY_AND_SIZE(data_sources))) < 0) | |
242 | return SR_ERR_ARG; | |
243 | devc->cur_data_source = idx; | |
244 | devc->enable_data_source_memory = (idx == DATA_SOURCE_MEMORY); | |
245 | break; | |
246 | default: | |
247 | ret = SR_ERR_NA; | |
248 | } | |
249 | ||
250 | return ret; | |
251 | } | |
252 | ||
253 | static int config_list(uint32_t key, GVariant **data, | |
254 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
255 | { | |
256 | switch (key) { | |
257 | case SR_CONF_SCAN_OPTIONS: | |
258 | case SR_CONF_DEVICE_OPTIONS: | |
259 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); | |
260 | case SR_CONF_SPL_WEIGHT_FREQ: | |
261 | *data = g_variant_new_strv(ARRAY_AND_SIZE(weight_freq)); | |
262 | break; | |
263 | case SR_CONF_SPL_WEIGHT_TIME: | |
264 | *data = g_variant_new_strv(ARRAY_AND_SIZE(weight_time)); | |
265 | break; | |
266 | case SR_CONF_SPL_MEASUREMENT_RANGE: | |
267 | *data = std_gvar_tuple_array(ARRAY_AND_SIZE(meas_ranges)); | |
268 | break; | |
269 | case SR_CONF_DATA_SOURCE: | |
270 | *data = g_variant_new_strv(ARRAY_AND_SIZE(data_sources)); | |
271 | break; | |
272 | default: | |
273 | return SR_ERR_NA; | |
274 | } | |
275 | ||
276 | return SR_OK; | |
277 | } | |
278 | ||
279 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
280 | { | |
281 | struct dev_context *devc; | |
282 | struct sr_serial_dev_inst *serial; | |
283 | ||
284 | devc = sdi->priv; | |
285 | devc->state = ST_INIT; | |
286 | devc->num_samples = 0; | |
287 | devc->buf_len = 0; | |
288 | ||
289 | std_session_send_df_header(sdi); | |
290 | ||
291 | serial = sdi->conn; | |
292 | serial_source_add(sdi->session, serial, G_IO_IN, 150, | |
293 | cem_dt_885x_receive_data, (void *)sdi); | |
294 | ||
295 | return SR_OK; | |
296 | } | |
297 | ||
298 | static struct sr_dev_driver cem_dt_885x_driver_info = { | |
299 | .name = "cem-dt-885x", | |
300 | .longname = "CEM DT-885x", | |
301 | .api_version = 1, | |
302 | .init = std_init, | |
303 | .cleanup = std_cleanup, | |
304 | .scan = scan, | |
305 | .dev_list = std_dev_list, | |
306 | .dev_clear = std_dev_clear, | |
307 | .config_get = config_get, | |
308 | .config_set = config_set, | |
309 | .config_list = config_list, | |
310 | .dev_open = std_serial_dev_open, | |
311 | .dev_close = std_serial_dev_close, | |
312 | .dev_acquisition_start = dev_acquisition_start, | |
313 | .dev_acquisition_stop = std_serial_dev_acquisition_stop, | |
314 | .context = NULL, | |
315 | }; | |
316 | SR_REGISTER_DEV_DRIVER(cem_dt_885x_driver_info); |