]> sigrok.org Git - libsigrok.git/blame_incremental - hardware/serial-dmm/protocol.c
serial-dmm: Add PeakTech 3410 support.
[libsigrok.git] / hardware / serial-dmm / protocol.c
... / ...
CommitLineData
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
5 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
21#include <glib.h>
22#include <stdlib.h>
23#include <math.h>
24#include <string.h>
25#include <errno.h>
26#include "libsigrok.h"
27#include "libsigrok-internal.h"
28#include "protocol.h"
29
30static void log_dmm_packet(const uint8_t *buf)
31{
32 sr_dbg("DMM packet: %02x %02x %02x %02x %02x %02x %02x"
33 " %02x %02x %02x %02x %02x %02x %02x",
34 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6],
35 buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13]);
36}
37
38SR_PRIV void dmm_details_dt4000zc(struct sr_datafeed_analog *analog, void *info)
39{
40 dmm_details_tp4000zc(analog, info); /* Same as TP4000ZC. */
41}
42
43SR_PRIV void dmm_details_tp4000zc(struct sr_datafeed_analog *analog, void *info)
44{
45 struct fs9721_info *info_local;
46
47 info_local = (struct fs9721_info *)info;
48
49 /* User-defined FS9721_LP3 flag 'c2c1_10' means temperature. */
50 if (info_local->is_c2c1_10) {
51 analog->mq = SR_MQ_TEMPERATURE;
52 /* No Kelvin or Fahrenheit from the device, just Celsius. */
53 analog->unit = SR_UNIT_CELSIUS;
54 }
55}
56
57static void handle_packet(const uint8_t *buf, struct dev_context *devc,
58 int dmm, void *info)
59{
60 float floatval;
61 struct sr_datafeed_packet packet;
62 struct sr_datafeed_analog *analog;
63
64 log_dmm_packet(buf);
65
66 if (!(analog = g_try_malloc0(sizeof(struct sr_datafeed_analog)))) {
67 sr_err("Analog packet malloc failed.");
68 return;
69 }
70
71 analog->num_samples = 1;
72 analog->mq = -1;
73
74 dmms[dmm].packet_parse(buf, &floatval, analog, info);
75 analog->data = &floatval;
76
77 if (dmms[dmm].dmm_details)
78 dmms[dmm].dmm_details(analog, info);
79
80 if (analog->mq != -1) {
81 /* Got a measurement. */
82 packet.type = SR_DF_ANALOG;
83 packet.payload = analog;
84 sr_session_send(devc->cb_data, &packet);
85 devc->num_samples++;
86 }
87
88 g_free(analog);
89}
90
91static void handle_new_data(struct dev_context *devc, int dmm, void *info)
92{
93 int len, i, offset = 0;
94
95 /* Try to get as much data as the buffer can hold. */
96 len = DMM_BUFSIZE - devc->buflen;
97 len = serial_read(devc->serial, devc->buf + devc->buflen, len);
98 if (len < 1) {
99 sr_err("Serial port read error: %d.", len);
100 return;
101 }
102 devc->buflen += len;
103
104 /* Now look for packets in that data. */
105 while ((devc->buflen - offset) >= dmms[dmm].packet_size) {
106 if (dmms[dmm].packet_valid(devc->buf + offset)) {
107 handle_packet(devc->buf + offset, devc, dmm, info);
108 offset += dmms[dmm].packet_size;
109 } else {
110 offset++;
111 }
112 }
113
114 /* If we have any data left, move it to the beginning of our buffer. */
115 for (i = 0; i < devc->buflen - offset; i++)
116 devc->buf[i] = devc->buf[offset + i];
117 devc->buflen -= offset;
118}
119
120static int receive_data(int fd, int revents, int dmm, void *info, void *cb_data)
121{
122 struct sr_dev_inst *sdi;
123 struct dev_context *devc;
124 int ret;
125
126 (void)fd;
127
128 if (!(sdi = cb_data))
129 return TRUE;
130
131 if (!(devc = sdi->priv))
132 return TRUE;
133
134 if (revents == G_IO_IN) {
135 /* Serial data arrived. */
136 handle_new_data(devc, dmm, info);
137 } else {
138 /* Timeout, send another packet request (if DMM needs it). */
139 if (dmms[dmm].packet_request) {
140 ret = dmms[dmm].packet_request(devc->serial);
141 if (ret < 0) {
142 sr_err("Failed to request packet: %d.", ret);
143 return FALSE;
144 }
145 }
146 }
147
148 if (devc->num_samples >= devc->limit_samples) {
149 sr_info("Requested number of samples reached, stopping.");
150 sdi->driver->dev_acquisition_stop(sdi, cb_data);
151 return TRUE;
152 }
153
154 return TRUE;
155}
156
157SR_PRIV int digitek_dt4000zc_receive_data(int fd, int revents, void *cb_data)
158{
159 struct fs9721_info info;
160
161 return receive_data(fd, revents, DIGITEK_DT4000ZC, &info, cb_data);
162}
163
164SR_PRIV int tekpower_tp4000zc_receive_data(int fd, int revents, void *cb_data)
165{
166 struct fs9721_info info;
167
168 return receive_data(fd, revents, TEKPOWER_TP4000ZC, &info, cb_data);
169}
170
171SR_PRIV int metex_me31_receive_data(int fd, int revents, void *cb_data)
172{
173 struct metex14_info info;
174
175 return receive_data(fd, revents, METEX_ME31, &info, cb_data);
176}
177
178SR_PRIV int peaktech_3410_receive_data(int fd, int revents, void *cb_data)
179{
180 struct metex14_info info;
181
182 return receive_data(fd, revents, PEAKTECH_3410, &info, cb_data);
183}