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/>.
21 #include "libsigrok.h"
22 #include "libsigrok-internal.h"
31 static gboolean lcd14_is_sync_valid(const lcd14_packet *packet)
34 /* Check the syncronization nibbles, and make sure they all match */
35 for(i = 0; i < LCD14_PACKET_SIZE; i++)
37 uint8_t sync = (packet->raw[i] & LCD14_SYNC_MASK) >> 4;
44 static gboolean lcd14_is_selection_good(const lcd14_data *data)
48 /* Does the packet have more than one multiplier ? */
49 if(data->flags & LCD14_NANO)
51 if(data->flags & LCD14_MICRO)
53 if(data->flags & LCD14_MILLI)
55 if(data->flags & LCD14_KILO)
57 if(data->flags & LCD14_MEGA)
63 /* Does the packet "measure" more than one type of value ?*/
64 if(data->flags & LCD14_HZ)
66 if(data->flags & LCD14_OHM)
68 if(data->flags & LCD14_FARAD)
70 if(data->flags & LCD14_AMP)
72 if(data->flags & LCD14_VOLT)
74 if(data->flags & LCD14_DUTY)
76 if(data->flags & LCD14_CELSIUS)
78 /* Do not test for hFE. hFE is not implemented and always '1' */
82 /* Both AC and DC ? */
83 if( (data->flags & LCD14_AC) && (data->flags & LCD14_DC) )
86 /* OK, no duplicates */
90 /* We "cook" a raw lcd14_pcaket into a more pallatable form, lcd14_data */
91 static void lcd14_cook_raw(const lcd14_packet *packet, lcd14_data * data)
95 /* Get the digits out */
96 for(i = 0; i < 4; i++)
98 size_t j = (i << 1) + 1;
99 data->digit[i] = ( (packet->raw[j] & ~LCD14_SYNC_MASK) << 4 ) |
100 ( (packet->raw[j+1] & ~LCD14_SYNC_MASK) );
103 /* Now extract the flags */
104 data->flags = ( (packet->raw[0] & ~LCD14_SYNC_MASK) << 20) |
105 ( (packet->raw[9] & ~LCD14_SYNC_MASK) << 16) |
106 ( (packet->raw[10]& ~LCD14_SYNC_MASK) << 12) |
107 ( (packet->raw[11]& ~LCD14_SYNC_MASK) << 8) |
108 ( (packet->raw[12]& ~LCD14_SYNC_MASK) << 4) |
109 ( (packet->raw[13]& ~LCD14_SYNC_MASK) );
113 /* Since the DMM does not identify itslef in any way shape, or form, we really
114 * don't know for sure who is sending the data. We must use every possible
115 * check to filter out bad packets, especially since detection mechanism depends
116 * on how well we can filter out bad packets packets */
117 SR_PRIV gboolean lcd14_is_packet_valid(const lcd14_packet *packet,
120 /* Callers not interested in the data, pass NULL */
121 lcd14_data placeholder;
124 /* We start with our syncronization nibbles, then move to more advanced
126 if(!lcd14_is_sync_valid(packet))
129 lcd14_cook_raw(packet, data);
131 if(!lcd14_is_selection_good(data))
134 /* Made it here, huh? Then 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 static double lcdraw_to_double(lcd14_data *data)
172 /* *********************************************************************
173 * Get a raw floating point value from the data
174 **********************************************************************/
176 double multiplier = 1;
178 gboolean dp_reached = FALSE;
181 /* We have 4 digits, and we start from the most significant */
182 for(i = 0; i < 4; i++)
184 uint8_t raw_digit = data->digit[i];
185 digit = lcd14_to_digit(raw_digit);
186 if(digit == LCD14_LCD_INVALID) {
190 /* Digit 1 does not have a decimal point. Instead, the decimal
191 * point is used to indicate MAX, so we must avoid testing it */
192 if( (i > 0) && (raw_digit & LCD14_DP_MASK) )
194 if(dp_reached) multiplier /= 10;
195 rawval = rawval * 10 + digit;
197 rawval *= multiplier;
198 if(data->digit[0] & LCD14_D0_NEG)
201 /* See if we need to multiply our raw value by anything */
202 if(data->flags & LCD14_NANO) {
204 } else if(data->flags & LCD14_MICRO) {
206 } else if(data->flags & LCD14_MILLI) {
208 } else if(data->flags & LCD14_KILO) {
210 } else if(data->flags & LCD14_MEGA) {
217 static void lcd14_handle_packet(lcd14_data *data, struct dev_context *devc)
219 double rawval = lcdraw_to_double(data);
220 /* *********************************************************************
221 * Now see what the value means, and pass that on
222 **********************************************************************/
223 struct sr_datafeed_packet packet;
224 struct sr_datafeed_analog *analog;
226 if( !(analog = g_try_malloc0(sizeof(struct sr_datafeed_analog))) ) {
227 sr_err("failed to malloc packet");
230 analog->num_samples = 1;
231 if( !(analog->data = g_try_malloc(sizeof(float))) ) {
232 sr_err("failed to malloc data");
236 *analog->data = (float)rawval;
239 /* What does the data mean ? */
240 if(data->flags & LCD14_VOLT) {
241 analog->mq = SR_MQ_VOLTAGE;
242 analog->unit = SR_UNIT_VOLT;
243 if(data->flags & LCD14_AC)
244 analog->mqflags |= SR_MQFLAG_AC;
246 analog->mqflags |= SR_MQFLAG_DC;
248 else if(data->flags & LCD14_AMP) {
249 analog->mq = SR_MQ_CURRENT;
250 analog->unit = SR_UNIT_AMPERE;
251 if(data->flags & LCD14_AC)
252 analog->mqflags |= SR_MQFLAG_AC;
254 analog->mqflags |= SR_MQFLAG_DC;
256 else if(data->flags & LCD14_OHM) {
257 if(data->flags & LCD14_BEEP)
258 analog->mq = SR_MQ_CONTINUITY;
260 analog->mq = SR_MQ_RESISTANCE;
262 analog->unit = SR_UNIT_OHM;
264 analog->unit = SR_UNIT_BOOLEAN;
265 *analog->data = FALSE;
268 else if(data->flags & LCD14_FARAD) {
269 analog->mq = SR_MQ_CAPACITANCE;
270 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 deive, just Celsius */
275 analog->unit = SR_UNIT_CELSIUS;
277 else if(data->flags & LCD14_HZ) {
278 analog->mq = SR_MQ_FREQUENCY;
279 analog->unit = SR_UNIT_HERTZ;
281 else if(data->flags & LCD14_DUTY) {
282 analog->mq = SR_MQ_DUTY_CYCLE;
283 analog->unit = SR_UNIT_PERCENTAGE;
285 else if(data->flags & LCD14_HFE) {
286 analog->mq = SR_MQ_GAIN;
287 analog->unit = SR_UNIT_UNITLESS;
289 else if(data->flags & LCD14_DIODE) {
290 analog->mq = SR_MQ_VOLTAGE;
291 analog->unit = SR_UNIT_VOLT;
292 analog->mqflags |= SR_MQFLAG_DIODE | SR_MQFLAG_DC;
295 sr_warn("unable to identify measurement mode");
298 /* What other flags are associated with the data? */
299 if(data->flags & LCD14_HOLD) {
300 analog->mqflags |= SR_MQFLAG_HOLD;
302 if(data->flags & LCD14_AUTO) {
303 analog->mqflags |= SR_MQFLAG_AUTORANGE;
305 if(data->flags & LCD14_REL) {
306 analog->mqflags |= SR_MQFLAG_RELATIVE;
309 if (analog->mq != -1) {
310 /* Got a measurement. */
311 sr_spew("val %f", rawval);
312 packet.type = SR_DF_ANALOG;
313 packet.payload = analog;
314 sr_session_send(devc->cb_data, &packet);
317 g_free(analog->data);
321 static void handle_new_data(struct dev_context *devc, int fd)
326 /* Try to get as much data as the buffer can hold */
327 len = DMM_BUFSIZE - devc->buflen;
328 len = serial_read(fd, devc->buf + devc->buflen, len);
330 sr_err("serial port read error!");
335 /* Now look for packets in that data */
336 while((devc->buflen - offset) >= LCD14_PACKET_SIZE)
338 lcd14_packet * packet = (void *)(devc->buf + offset);
340 if( lcd14_is_packet_valid(packet, &data) )
342 lcd14_handle_packet(&data, devc);
343 offset += LCD14_PACKET_SIZE;
349 /* If we have any data left, move it to the beginning of our buffer */
350 for(i = 0; i < devc->buflen - offset; i++)
351 devc->buf[i] = devc->buf[offset + i];
352 devc->buflen -= offset;
355 SR_PRIV int lcd14_receive_data(int fd, int revents, void *cb_data)
357 const struct sr_dev_inst *sdi;
358 struct dev_context *devc;
360 if (!(sdi = cb_data))
363 if (!(devc = sdi->priv))
366 if (revents == G_IO_IN)
368 /* Serial data arrived. */
369 handle_new_data(devc, fd);
372 if (devc->num_samples >= devc->limit_samples) {
373 sdi->driver->dev_acquisition_stop(sdi, cb_data);