2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.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/>.
25 #include "libsigrok.h"
26 #include "libsigrok-internal.h"
29 static gboolean lcd14_is_sync_valid(const struct lcd14_packet *packet)
34 /* Check the syncronization nibbles, and make sure they all match. */
35 for (i = 0; i < LCD14_PACKET_SIZE; i++) {
36 sync = (packet->raw[i] & LCD14_SYNC_MASK) >> 4;
43 static gboolean lcd14_is_selection_good(const struct lcd14_data *data)
45 int n_postfix = 0, n_type = 0;
47 /* Does the packet have more than one multiplier? */
48 if (data->flags & LCD14_NANO)
50 if (data->flags & LCD14_MICRO)
52 if (data->flags & LCD14_MILLI)
54 if (data->flags & LCD14_KILO)
56 if (data->flags & LCD14_MEGA)
62 /* Does the packet "measure" more than one type of value? */
63 if (data->flags & LCD14_HZ)
65 if (data->flags & LCD14_OHM)
67 if (data->flags & LCD14_FARAD)
69 if (data->flags & LCD14_AMP)
71 if (data->flags & LCD14_VOLT)
73 if (data->flags & LCD14_DUTY)
75 if (data->flags & LCD14_CELSIUS)
77 /* Do not test for hFE. hFE is not implemented and always '1'. */
82 if ((data->flags & LCD14_AC) && (data->flags & LCD14_DC))
85 /* OK, no duplicates. */
89 /* We "cook" a raw lcd14_pcaket into a more pallatable form, lcd14_data. */
90 static void lcd14_cook_raw(const struct lcd14_packet *packet,
91 struct lcd14_data *data)
95 /* Get the digits out. */
96 for (i = 0; i < 4; i++) {
98 data->digit[i] = ((packet->raw[j] & ~LCD14_SYNC_MASK) << 4) |
99 ((packet->raw[j + 1] & ~LCD14_SYNC_MASK));
102 /* Now extract the flags. */
103 data->flags = ((packet->raw[0] & ~LCD14_SYNC_MASK) << 20) |
104 ((packet->raw[9] & ~LCD14_SYNC_MASK) << 16) |
105 ((packet->raw[10] & ~LCD14_SYNC_MASK) << 12) |
106 ((packet->raw[11] & ~LCD14_SYNC_MASK) << 8) |
107 ((packet->raw[12] & ~LCD14_SYNC_MASK) << 4) |
108 ((packet->raw[13] & ~LCD14_SYNC_MASK));
112 * Since the DMM does not identify itself in any way shape, or form, we really
113 * don't know for sure who is sending the data. We must use every possible
114 * check to filter out bad packets, especially since the detection mechanism
115 * depends on how well we can filter out bad packets packets.
117 SR_PRIV gboolean lcd14_is_packet_valid(const struct lcd14_packet *packet,
118 struct lcd14_data *data)
120 struct lcd14_data placeholder;
122 /* Callers not interested in the data, pass NULL. */
126 if (!lcd14_is_sync_valid(packet))
129 lcd14_cook_raw(packet, data);
131 if (!lcd14_is_selection_good(data))
134 /* If we made it here, this looks to be a valid packet. */
138 static uint8_t lcd14_to_digit(uint8_t raw_digit)
140 /* Take out the decimal point, so we can use a simple switch(). */
141 raw_digit &= ~LCD14_DP_MASK;
166 return LCD14_LCD_INVALID;
170 /* Get a raw floating point value from the data. */
171 static double lcdraw_to_double(struct lcd14_data *data)
174 double multiplier = 1;
175 uint8_t digit, raw_digit;
176 gboolean dp_reached = FALSE;
179 /* We have 4 digits, and we start from the most significant. */
180 for (i = 0; i < 4; i++) {
181 raw_digit = data->digit[i];
182 digit = lcd14_to_digit(raw_digit);
183 if (digit == LCD14_LCD_INVALID) {
189 * Digit 1 does not have a decimal point. Instead, the decimal
190 * point is used to indicate MAX, so we must avoid testing it.
192 if ((i > 0) && (raw_digit & LCD14_DP_MASK))
196 rawval = rawval * 10 + digit;
198 rawval *= multiplier;
199 if (data->digit[0] & LCD14_D0_NEG)
202 /* See if we need to multiply our raw value by anything. */
203 if (data->flags & LCD14_NANO)
205 else if (data->flags & LCD14_MICRO)
207 else if (data->flags & LCD14_MILLI)
209 else if (data->flags & LCD14_KILO)
211 else if (data->flags & LCD14_MEGA)
217 /* Now see what the value means, and pass that on. */
218 static void lcd14_handle_packet(struct lcd14_data *data,
219 struct dev_context *devc)
222 struct sr_datafeed_packet packet;
223 struct sr_datafeed_analog *analog;
225 if (!(analog = g_try_malloc0(sizeof(struct sr_datafeed_analog)))) {
226 sr_err("Failed to malloc packet.");
230 if (!(analog->data = g_try_malloc(sizeof(float)))) {
231 sr_err("Failed to malloc data.");
236 rawval = lcdraw_to_double(data);
238 analog->num_samples = 1;
239 *analog->data = (float)rawval;
243 /* What does the data mean? */
244 if (data->flags & LCD14_VOLT) {
245 analog->mq = SR_MQ_VOLTAGE;
246 analog->unit = SR_UNIT_VOLT;
247 if (data->flags & LCD14_AC)
248 analog->mqflags |= SR_MQFLAG_AC;
250 analog->mqflags |= SR_MQFLAG_DC;
251 } else if (data->flags & LCD14_AMP) {
252 analog->mq = SR_MQ_CURRENT;
253 analog->unit = SR_UNIT_AMPERE;
254 if (data->flags & LCD14_AC)
255 analog->mqflags |= SR_MQFLAG_AC;
257 analog->mqflags |= SR_MQFLAG_DC;
258 } else if (data->flags & LCD14_OHM) {
259 if (data->flags & LCD14_BEEP)
260 analog->mq = SR_MQ_CONTINUITY;
262 analog->mq = SR_MQ_RESISTANCE;
264 analog->unit = SR_UNIT_OHM;
266 analog->unit = SR_UNIT_BOOLEAN;
267 *analog->data = FALSE;
269 } else if (data->flags & LCD14_FARAD) {
270 analog->mq = SR_MQ_CAPACITANCE;
271 analog->unit = SR_UNIT_FARAD;
272 } else if (data->flags & LCD14_CELSIUS) {
273 analog->mq = SR_MQ_TEMPERATURE;
274 /* No Kelvin or Fahrenheit from the device, just Celsius. */
275 analog->unit = SR_UNIT_CELSIUS;
276 } else if (data->flags & LCD14_HZ) {
277 analog->mq = SR_MQ_FREQUENCY;
278 analog->unit = SR_UNIT_HERTZ;
279 } else if (data->flags & LCD14_DUTY) {
280 analog->mq = SR_MQ_DUTY_CYCLE;
281 analog->unit = SR_UNIT_PERCENTAGE;
282 } else if (data->flags & LCD14_HFE) {
283 analog->mq = SR_MQ_GAIN;
284 analog->unit = SR_UNIT_UNITLESS;
285 } else if (data->flags & LCD14_DIODE) {
286 analog->mq = SR_MQ_VOLTAGE;
287 analog->unit = SR_UNIT_VOLT;
288 analog->mqflags |= SR_MQFLAG_DIODE | SR_MQFLAG_DC;
290 sr_warn("Unable to identify measurement mode.");
293 /* What other flags are associated with the data? */
294 if (data->flags & LCD14_HOLD)
295 analog->mqflags |= SR_MQFLAG_HOLD;
296 if (data->flags & LCD14_AUTO)
297 analog->mqflags |= SR_MQFLAG_AUTORANGE;
298 if (data->flags & LCD14_REL)
299 analog->mqflags |= SR_MQFLAG_RELATIVE;
301 if (analog->mq != -1) {
302 /* Got a measurement. */
303 sr_spew("Measurement value is %f.", rawval);
304 packet.type = SR_DF_ANALOG;
305 packet.payload = analog;
306 sr_session_send(devc->cb_data, &packet);
310 g_free(analog->data);
314 static void handle_new_data(struct dev_context *devc, int fd)
316 int len, i, offset = 0;
317 struct lcd14_packet *packet;
318 struct lcd14_data data;
320 /* Try to get as much data as the buffer can hold. */
321 len = DMM_BUFSIZE - devc->buflen;
322 len = serial_read(fd, devc->buf + devc->buflen, len);
324 sr_err("Serial port read error: %d.", len);
329 /* Now look for packets in that data. */
330 while ((devc->buflen - offset) >= LCD14_PACKET_SIZE) {
331 packet = (void *)(devc->buf + offset);
332 if (lcd14_is_packet_valid(packet, &data)) {
333 lcd14_handle_packet(&data, devc);
334 offset += LCD14_PACKET_SIZE;
340 /* If we have any data left, move it to the beginning of our buffer. */
341 for (i = 0; i < devc->buflen - offset; i++)
342 devc->buf[i] = devc->buf[offset + i];
343 devc->buflen -= offset;
346 SR_PRIV int tekpower_dmm_receive_data(int fd, int revents, void *cb_data)
348 const struct sr_dev_inst *sdi;
349 struct dev_context *devc;
351 if (!(sdi = cb_data))
354 if (!(devc = sdi->priv))
357 if (revents == G_IO_IN) {
358 /* Serial data arrived. */
359 handle_new_data(devc, fd);
362 if (devc->num_samples >= devc->limit_samples) {
363 sdi->driver->dev_acquisition_stop(sdi, cb_data);