2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
31 static void handle_packet(const uint8_t *rs_packet,
32 struct dev_context *devc)
35 struct sr_datafeed_packet packet;
36 struct sr_datafeed_analog *analog;
38 /* TODO: Check malloc return value. */
39 analog = g_try_malloc0(sizeof(struct sr_datafeed_analog));
40 /* TODO: Check malloc return value. */
42 analog->num_samples = 1;
45 sr_rs9lcd_parse(rs_packet, &rawval, analog, NULL);
46 analog->data = &rawval;
48 if (analog->mq != -1) {
49 /* Got a measurement. */
50 sr_spew("Value: %f.", rawval);
51 packet.type = SR_DF_ANALOG;
52 packet.payload = analog;
53 sr_session_send(devc->cb_data, &packet);
59 static void handle_new_data(struct dev_context *devc)
65 /* Try to get as much data as the buffer can hold. */
66 len = RS_DMM_BUFSIZE - devc->buflen;
67 len = serial_read(devc->serial, devc->buf + devc->buflen, len);
69 sr_err("Serial port read error.");
74 /* Now look for packets in that data. */
75 while ((devc->buflen - offset) >= RS_22_812_PACKET_SIZE) {
76 rs_packet = (void *)(devc->buf + offset);
77 if (sr_rs9lcd_packet_valid(rs_packet)) {
78 handle_packet(rs_packet, devc);
79 offset += RS_22_812_PACKET_SIZE;
85 /* If we have any data left, move it to the beginning of our buffer. */
86 for (i = 0; i < devc->buflen - offset; i++)
87 devc->buf[i] = devc->buf[offset + i];
88 devc->buflen -= offset;
91 SR_PRIV int radioshack_dmm_receive_data(int fd, int revents, void *cb_data)
93 struct sr_dev_inst *sdi;
94 struct dev_context *devc;
101 if (!(devc = sdi->priv))
104 if (revents == G_IO_IN) {
105 /* Serial data arrived. */
106 handle_new_data(devc);
109 if (devc->num_samples >= devc->limit_samples) {
110 sdi->driver->dev_acquisition_stop(sdi, cb_data);