]> sigrok.org Git - libsigrok.git/blame - hardware/cem-dt-885x/protocol.c
cem-dt-885x: Live SPL acquisition
[libsigrok.git] / hardware / cem-dt-885x / protocol.c
CommitLineData
8fa9368e
BV
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
e37c4b39 20#include <string.h>
8fa9368e
BV
21#include "protocol.h"
22
e37c4b39
BV
23/* Length of expected payload for each token. */
24static 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 },
29 { TOKEN_TIME, 3 },
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 },
49};
50
51static int find_token_payload_len(unsigned char c)
8fa9368e 52{
e37c4b39 53 unsigned int i;
8fa9368e 54
e37c4b39
BV
55 for (i = 0; i < ARRAY_SIZE(token_payloads); i++) {
56 if (token_payloads[i][0] == c)
57 return token_payloads[i][1];
58 }
59
60 return -1;
61}
62
63/* Process measurement or setting (0xa5 command). */
64static void process_mset(const struct sr_dev_inst *sdi)
65{
8fa9368e 66 struct dev_context *devc;
e37c4b39
BV
67 struct sr_datafeed_packet packet;
68 struct sr_datafeed_analog analog;
69 GString *dbg;
70 float fvalue;
71 int i;
8fa9368e 72
e37c4b39
BV
73 devc = sdi->priv;
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);
78 if (devc->buf_len) {
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]);
82 }
83 sr_spew("%s", dbg->str);
84 g_string_free(dbg, TRUE);
85 }
86
87 switch(devc->token) {
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;
91 break;
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;
95 break;
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;
99 break;
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;
103 break;
104 case TOKEN_HOLD_MAX:
105 devc->cur_mqflags |= SR_MQFLAG_HOLD | SR_MQFLAG_MAX;
106 devc->cur_mqflags &= ~SR_MQFLAG_MIN;
107 break;
108 case TOKEN_HOLD_MIN:
109 devc->cur_mqflags |= SR_MQFLAG_HOLD | SR_MQFLAG_MIN;
110 devc->cur_mqflags &= ~SR_MQFLAG_MAX;
111 break;
112 case TOKEN_HOLD_NONE:
113 devc->cur_mqflags &= ~(SR_MQFLAG_MAX | SR_MQFLAG_MIN | SR_MQFLAG_HOLD);
114 break;
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 memset(&analog, 0, sizeof(struct sr_datafeed_analog));
121 analog.mq = SR_MQ_SOUND_PRESSURE_LEVEL;
122 analog.mqflags = devc->cur_mqflags;
123 analog.unit = SR_UNIT_DECIBEL_SPL;
124 analog.probes = sdi->probes;
125 analog.num_samples = 1;
126 analog.data = &fvalue;
127 packet.type = SR_DF_ANALOG;
128 packet.payload = &analog;
129 sr_session_send(devc->cb_data, &packet);
130
131 devc->num_samples++;
132 if (devc->limit_samples && devc->num_samples >= devc->limit_samples)
133 sdi->driver->dev_acquisition_stop((struct sr_dev_inst *)sdi,
134 devc->cb_data);
135 break;
136 case TOKEN_TIME:
137 case TOKEN_STORE_OK:
138 case TOKEN_STORE_FULL:
139 case TOKEN_RECORDING_ON:
140 case TOKEN_RECORDING_OFF:
141 case TOKEN_MEAS_WAS_READOUT:
142 case TOKEN_MEAS_WAS_BARGRAPH:
143 case TOKEN_BATTERY_OK:
144 case TOKEN_BATTERY_LOW:
145 case TOKEN_MEAS_RANGE_OK:
146 case TOKEN_MEAS_RANGE_OVER:
147 case TOKEN_MEAS_RANGE_UNDER:
148 case TOKEN_MEAS_RANGE_30_80:
149 case TOKEN_MEAS_RANGE_30_130:
150 case TOKEN_MEAS_RANGE_50_100:
151 case TOKEN_MEAS_RANGE_80_130:
152 /* Not useful, or not expressable in sigrok. */
153 break;
154 }
155
156}
157
158static void process_byte(const struct sr_dev_inst *sdi, const unsigned char c)
159{
160 struct dev_context *devc;
161 int len;
8fa9368e
BV
162
163 if (!(devc = sdi->priv))
e37c4b39
BV
164 return;
165
166 if (c == 0xff) {
167 /* Device is in hold mode */
168 devc->cur_mqflags |= SR_MQFLAG_HOLD;
169
170 /* TODO: send out the last measurement at the same
171 * rate as it normally gets sent */
172
173 /* When the device leaves hold mode, it starts from scratch. */
174 devc->state = ST_INIT;
175 return;
176 }
177 devc->cur_mqflags &= ~SR_MQFLAG_HOLD;
178
179 if (devc->state == ST_INIT) {
180 if (c == 0xa5) {
181 devc->cmd = c;
182 devc->token = 0x00;
183 devc->state = ST_GET_TOKEN;
184 } else if (c == 0xbb) {
185 devc->cmd = c;
186 devc->buf_len = 0;
187 devc->state = ST_GET_LOG;
188 }
189 } else if (devc->state == ST_GET_TOKEN) {
190 devc->token = c;
191 devc->buf_len = 0;
192 len = find_token_payload_len(devc->token);
193 if (len == -1 || len > 0) {
194 devc->buf_len = 0;
195 devc->state = ST_GET_DATA;
196 } else {
197 process_mset(sdi);
198 devc->state = ST_INIT;
199 }
200 } else if (devc->state == ST_GET_DATA) {
201 len = find_token_payload_len(devc->token);
202 if (len == -1) {
203 /* We don't know this token. */
204 sr_dbg("Unknown 0xa5 token 0x%.2x", devc->token);
205 if (c == 0xa5 || c == 0xbb) {
206 /* Looks like a new command however. */
207 process_mset(sdi);
208 devc->state = ST_INIT;
209 } else {
210 devc->buf[devc->buf_len++] = c;
211 if (devc->buf_len > BUF_SIZE) {
212 /* Shouldn't happen, ignore. */
213 devc->state = ST_INIT;
214 }
215 }
216 } else {
217 devc->buf[devc->buf_len++] = c;
218 if (devc->buf_len == len) {
219 process_mset(sdi);
220 devc->state = ST_INIT;
221 } else if (devc->buf_len > BUF_SIZE) {
222 /* Shouldn't happen, ignore. */
223 devc->state = ST_INIT;
224 }
225 }
226 } else if (devc->state == ST_GET_LOG) {
227 }
228
229}
230
231SR_PRIV int cem_dt_885x_receive_data(int fd, int revents, void *cb_data)
232{
233 const struct sr_dev_inst *sdi;
234 struct sr_serial_dev_inst *serial;
235 unsigned char c;
236
237 (void)fd;
238
239 if (!(sdi = cb_data))
8fa9368e
BV
240 return TRUE;
241
e37c4b39 242 serial = sdi->conn;
8fa9368e 243 if (revents == G_IO_IN) {
e37c4b39
BV
244 if (serial_read(serial, &c, 1) != 1)
245 return TRUE;
246 process_byte(sdi, c);
8fa9368e
BV
247 }
248
249 return TRUE;
250}