2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
23 /* Length of expected payload for each token. */
24 static int token_payloads[][2] = {
25 { TOKEN_WEIGHT_TIME_FAST, 0 },
26 { TOKEN_WEIGHT_TIME_SLOW, 0 },
27 { TOKEN_HOLD_MAX, 0 },
28 { TOKEN_HOLD_MIN, 0 },
30 { TOKEN_MEAS_RANGE_OVER, 0 },
31 { TOKEN_MEAS_RANGE_UNDER, 0 },
32 { TOKEN_STORE_FULL, 0 },
33 { TOKEN_RECORDING_ON, 0 },
34 { TOKEN_MEAS_WAS_READOUT, 1 },
35 { TOKEN_MEAS_WAS_BARGRAPH, 0 },
36 { TOKEN_MEASUREMENT, 2 },
37 { TOKEN_HOLD_NONE, 0 },
38 { TOKEN_BATTERY_LOW, 0 },
39 { TOKEN_MEAS_RANGE_OK, 0 },
40 { TOKEN_STORE_OK, 0 },
41 { TOKEN_RECORDING_OFF, 0 },
42 { TOKEN_WEIGHT_FREQ_A, 1 },
43 { TOKEN_WEIGHT_FREQ_C, 1 },
44 { TOKEN_BATTERY_OK, 0 },
45 { TOKEN_MEAS_RANGE_30_80, 0 },
46 { TOKEN_MEAS_RANGE_30_130, 0 },
47 { TOKEN_MEAS_RANGE_50_100, 0 },
48 { TOKEN_MEAS_RANGE_80_130, 0 },
51 static int find_token_payload_len(unsigned char c)
55 for (i = 0; i < ARRAY_SIZE(token_payloads); i++) {
56 if (token_payloads[i][0] == c)
57 return token_payloads[i][1];
63 /* Process measurement or setting (0xa5 command). */
64 static void process_mset(const struct sr_dev_inst *sdi)
66 struct dev_context *devc;
67 struct sr_datafeed_packet packet;
68 struct sr_datafeed_analog analog;
74 if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
75 dbg = g_string_sized_new(128);
76 g_string_printf(dbg, "got command 0x%.2x token 0x%.2x",
77 devc->cmd, devc->token);
79 g_string_append_printf(dbg, " payload");
80 for (i = 0; i < devc->buf_len; i++)
81 g_string_append_printf(dbg, " %.2x", devc->buf[i]);
83 sr_spew("%s", dbg->str);
84 g_string_free(dbg, TRUE);
88 case TOKEN_WEIGHT_TIME_FAST:
89 devc->cur_mqflags |= SR_MQFLAG_SPL_TIME_WEIGHT_F;
90 devc->cur_mqflags &= ~SR_MQFLAG_SPL_TIME_WEIGHT_S;
92 case TOKEN_WEIGHT_TIME_SLOW:
93 devc->cur_mqflags |= SR_MQFLAG_SPL_TIME_WEIGHT_S;
94 devc->cur_mqflags &= ~SR_MQFLAG_SPL_TIME_WEIGHT_F;
96 case TOKEN_WEIGHT_FREQ_A:
97 devc->cur_mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_A;
98 devc->cur_mqflags &= ~SR_MQFLAG_SPL_FREQ_WEIGHT_C;
100 case TOKEN_WEIGHT_FREQ_C:
101 devc->cur_mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_C;
102 devc->cur_mqflags &= ~SR_MQFLAG_SPL_FREQ_WEIGHT_A;
105 devc->cur_mqflags |= SR_MQFLAG_HOLD | SR_MQFLAG_MAX;
106 devc->cur_mqflags &= ~SR_MQFLAG_MIN;
109 devc->cur_mqflags |= SR_MQFLAG_HOLD | SR_MQFLAG_MIN;
110 devc->cur_mqflags &= ~SR_MQFLAG_MAX;
112 case TOKEN_HOLD_NONE:
113 devc->cur_mqflags &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN | SR_MQFLAG_HOLD);
115 case TOKEN_MEASUREMENT:
116 fvalue = ((devc->buf[0] & 0xf0) >> 4) * 100;
117 fvalue += (devc->buf[0] & 0x0f) * 10;
118 fvalue += ((devc->buf[1] & 0xf0) >> 4);
119 fvalue += (devc->buf[1] & 0x0f) / 10.0;
120 devc->last_spl = fvalue;
122 case TOKEN_MEAS_WAS_READOUT:
123 case TOKEN_MEAS_WAS_BARGRAPH:
124 if (devc->cur_mqflags & (SR_MQFLAG_MAX | SR_MQFLAG_MIN)) {
125 if (devc->token == TOKEN_MEAS_WAS_BARGRAPH) {
126 /* The device still sends bargraph measurements even
127 * when in max/min hold mode. Suppress them here, unless
128 * they're readout values. This duplicates the behavior
129 * of the device display exactly. */
133 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
134 analog.mq = SR_MQ_SOUND_PRESSURE_LEVEL;
135 analog.mqflags = devc->cur_mqflags;
136 analog.unit = SR_UNIT_DECIBEL_SPL;
137 analog.probes = sdi->probes;
138 analog.num_samples = 1;
139 analog.data = &devc->last_spl;
140 packet.type = SR_DF_ANALOG;
141 packet.payload = &analog;
142 sr_session_send(devc->cb_data, &packet);
145 if (devc->limit_samples && devc->num_samples >= devc->limit_samples)
146 sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
149 case TOKEN_RECORDING_ON:
150 devc->recording = TRUE;
152 case TOKEN_RECORDING_OFF:
153 devc->recording = FALSE;
157 case TOKEN_STORE_FULL:
158 case TOKEN_BATTERY_OK:
159 case TOKEN_BATTERY_LOW:
160 case TOKEN_MEAS_RANGE_OK:
161 case TOKEN_MEAS_RANGE_OVER:
162 case TOKEN_MEAS_RANGE_UNDER:
163 case TOKEN_MEAS_RANGE_30_80:
164 case TOKEN_MEAS_RANGE_30_130:
165 case TOKEN_MEAS_RANGE_50_100:
166 case TOKEN_MEAS_RANGE_80_130:
167 /* Not useful, or not expressable in sigrok. */
173 static void process_byte(const struct sr_dev_inst *sdi, const unsigned char c,
176 struct dev_context *devc;
180 if (!(devc = sdi->priv))
184 /* Device is in hold mode */
185 devc->cur_mqflags |= SR_MQFLAG_HOLD;
187 if (devc->hold_last_sent == 0) {
188 /* First hold notification. */
189 devc->hold_last_sent = g_get_monotonic_time();
190 /* When the device leaves hold mode, it starts from scratch. */
191 devc->state = ST_INIT;
193 cur_time = g_get_monotonic_time();
194 if (cur_time - devc->hold_last_sent > HOLD_REPEAT_INTERVAL) {
195 /* Force the last measurement out again. */
197 devc->token = TOKEN_MEAS_WAS_READOUT;
200 devc->hold_last_sent = cur_time;
206 devc->cur_mqflags &= ~SR_MQFLAG_HOLD;
207 devc->hold_last_sent = 0;
209 if (devc->state == ST_INIT) {
213 devc->state = ST_GET_TOKEN;
214 } else if (c == 0xbb) {
217 devc->state = ST_GET_LOG;
219 } else if (devc->state == ST_GET_TOKEN) {
222 len = find_token_payload_len(devc->token);
223 if (len == -1 || len > 0) {
225 devc->state = ST_GET_DATA;
229 devc->state = ST_INIT;
231 } else if (devc->state == ST_GET_DATA) {
232 len = find_token_payload_len(devc->token);
234 /* We don't know this token. */
235 sr_dbg("Unknown 0xa5 token 0x%.2x", devc->token);
236 if (c == 0xa5 || c == 0xbb) {
237 /* Looks like a new command however. */
240 devc->state = ST_INIT;
242 devc->buf[devc->buf_len++] = c;
243 if (devc->buf_len > BUF_SIZE) {
244 /* Shouldn't happen, ignore. */
245 devc->state = ST_INIT;
249 devc->buf[devc->buf_len++] = c;
250 if (devc->buf_len == len) {
253 devc->state = ST_INIT;
254 } else if (devc->buf_len > BUF_SIZE) {
255 /* Shouldn't happen, ignore. */
256 devc->state = ST_INIT;
259 } else if (devc->state == ST_GET_LOG) {
264 SR_PRIV int cem_dt_885x_receive_data(int fd, int revents, void *cb_data)
266 const struct sr_dev_inst *sdi;
267 struct sr_serial_dev_inst *serial;
272 if (!(sdi = cb_data))
276 if (revents == G_IO_IN) {
277 if (serial_read(serial, &c, 1) != 1)
279 process_byte(sdi, c, TRUE);
286 static int wait_for_token(const struct sr_dev_inst *sdi, char *tokens, int timeout)
288 struct dev_context *devc;
289 struct sr_serial_dev_inst *serial;
296 devc->state = ST_INIT;
297 start_time = g_get_monotonic_time() / 1000;
299 if (serial_read(serial, &c, 1) != 1)
300 /* Device might have gone away. */
302 process_byte(sdi, c, FALSE);
303 if (devc->state != ST_INIT)
304 /* Wait for a whole packet to get processed. */
306 for (i = 0; tokens[i] != -1; i++) {
307 if (devc->token == tokens[i]) {
308 sr_spew("wait_for_token: got token 0x%.2x", devc->token);
312 if (timeout && g_get_monotonic_time() / 1000 - start_time > timeout)
313 return SR_ERR_TIMEOUT;
319 /* cmd is the command to send, tokens are the tokens that denote the state
320 * which the command affects. The first token is the desired state. */
321 SR_PRIV int cem_dt_885x_toggle(const struct sr_dev_inst *sdi, uint8_t cmd, char *tokens)
323 struct dev_context *devc;
324 struct sr_serial_dev_inst *serial;
329 /* The device doesn't respond to commands very well. The
330 * only thing to do is wait for the token that will confirm
331 * whether the command worked or not, and resend if needed. */
333 if (serial_write(serial, (const void *)&cmd, 1) != 1)
335 /* Notifications are sent at 2Hz minimum */
336 if (wait_for_token(sdi, tokens, 510) == SR_ERR)
338 if (devc->token == tokens[0])
346 SR_PRIV gboolean cem_dt_885x_recording_get(const struct sr_dev_inst *sdi)
348 struct dev_context *devc;
353 if (devc->recording == -1) {
354 /* Didn't pick up device state yet. */
355 tokens[0] = TOKEN_RECORDING_ON;
356 tokens[1] = TOKEN_RECORDING_OFF;
358 if (wait_for_token(sdi, tokens, 0) != SR_OK)
362 return devc->token == TOKEN_RECORDING_ON;
365 SR_PRIV int cem_dt_885x_recording_set(const struct sr_dev_inst *sdi, gboolean start)
367 struct dev_context *devc;
373 /* The toggle below needs the desired state in first position. */
375 tokens[0] = TOKEN_RECORDING_ON;
376 tokens[1] = TOKEN_RECORDING_OFF;
378 tokens[0] = TOKEN_RECORDING_OFF;
379 tokens[1] = TOKEN_RECORDING_ON;
383 if (devc->recording == -1) {
384 /* Didn't pick up device state yet. */
385 if (wait_for_token(sdi, tokens, 0) != SR_OK)
387 if (devc->token == tokens[0])
390 } else if (devc->recording == start)
394 ret = cem_dt_885x_toggle(sdi, CMD_TOGGLE_RECORDING, tokens);