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 /* Now see what the value means, and pass that on. */
30 static void fs9721_serial_handle_packet(const struct fs9721_data *data,
31 struct dev_context *devc)
34 struct sr_datafeed_packet packet;
35 struct sr_datafeed_analog *analog;
37 if (!(analog = g_try_malloc0(sizeof(struct sr_datafeed_analog)))) {
38 sr_err("Failed to malloc packet.");
42 if (!(analog->data = g_try_malloc(sizeof(float)))) {
43 sr_err("Failed to malloc data.");
48 analog->num_samples = 1;
51 sr_dmm_smart_parse_fs9721(data, &rawval, analog);
52 *analog->data = rawval;
54 if (data->flags & FLAG_TEMP_CELSIUS) {
55 analog->mq = SR_MQ_TEMPERATURE;
56 /* No Kelvin or Fahrenheit from the device, just Celsius. */
57 analog->unit = SR_UNIT_CELSIUS;
61 if (analog->mq != -1) {
62 /* Got a measurement. */
63 packet.type = SR_DF_ANALOG;
64 packet.payload = analog;
65 sr_session_send(devc->cb_data, &packet);
73 static void handle_new_data(struct dev_context *devc, int fd)
75 int len, i, offset = 0;
76 struct fs9721_packet *packet;
77 struct fs9721_data data;
79 /* Try to get as much data as the buffer can hold. */
80 len = DMM_BUFSIZE - devc->buflen;
81 len = serial_read(fd, devc->buf + devc->buflen, len);
83 sr_err("Serial port read error: %d.", len);
88 /* Now look for packets in that data. */
89 while ((devc->buflen - offset) >= FS9721_PACKET_SIZE) {
90 packet = (void *)(devc->buf + offset);
91 if (fs9721_is_packet_valid(packet, &data)) {
92 fs9721_serial_handle_packet(&data, devc);
93 offset += FS9721_PACKET_SIZE;
99 /* If we have any data left, move it to the beginning of our buffer. */
100 for (i = 0; i < devc->buflen - offset; i++)
101 devc->buf[i] = devc->buf[offset + i];
102 devc->buflen -= offset;
105 SR_PRIV int tekpower_dmm_receive_data(int fd, int revents, void *cb_data)
107 const struct sr_dev_inst *sdi;
108 struct dev_context *devc;
110 if (!(sdi = cb_data))
113 if (!(devc = sdi->priv))
116 if (revents == G_IO_IN) {
117 /* Serial data arrived. */
118 handle_new_data(devc, fd);
121 if (devc->num_samples >= devc->limit_samples) {
122 sdi->driver->dev_acquisition_stop(sdi, cb_data);