]>
Commit | Line | Data |
---|---|---|
7dc55d93 AG |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com> | |
729b01f9 | 5 | * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de> |
7dc55d93 AG |
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 | ||
7dc55d93 AG |
21 | #include <stdlib.h> |
22 | #include <math.h> | |
23 | #include <string.h> | |
24 | #include <errno.h> | |
22f54192 | 25 | #include <glib.h> |
bbabddbd UH |
26 | #include "libsigrok.h" |
27 | #include "libsigrok-internal.h" | |
28 | #include "protocol.h" | |
7dc55d93 | 29 | |
6bef68a7 UH |
30 | static 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 | ||
f086b830 UH |
38 | SR_PRIV void dmm_details_dt4000zc(struct sr_datafeed_analog *analog, void *info) |
39 | { | |
40 | dmm_details_tp4000zc(analog, info); /* Same as TP4000ZC. */ | |
41 | } | |
42 | ||
729b01f9 UH |
43 | SR_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; | |
6dca2f16 UH |
52 | analog->unit = SR_UNIT_CELSIUS; |
53 | } | |
54 | } | |
55 | ||
56 | SR_PRIV void dmm_details_va18b(struct sr_datafeed_analog *analog, void *info) | |
57 | { | |
58 | struct fs9721_info *info_local; | |
59 | ||
60 | info_local = (struct fs9721_info *)info; | |
61 | ||
62 | /* User-defined FS9721_LP3 flag 'c2c1_01' means temperature. */ | |
63 | if (info_local->is_c2c1_01) { | |
64 | analog->mq = SR_MQ_TEMPERATURE; | |
729b01f9 UH |
65 | analog->unit = SR_UNIT_CELSIUS; |
66 | } | |
67 | } | |
68 | ||
d4bd66a0 UH |
69 | SR_PRIV void dmm_details_pce_dm32(struct sr_datafeed_analog *analog, void *info) |
70 | { | |
71 | struct fs9721_info *info_local; | |
72 | ||
73 | info_local = (struct fs9721_info *)info; | |
74 | ||
75 | /* User-defined FS9721_LP3 flag 'c2c1_01' means temperature (F). */ | |
76 | if (info_local->is_c2c1_01) { | |
77 | analog->mq = SR_MQ_TEMPERATURE; | |
78 | analog->unit = SR_UNIT_FAHRENHEIT; | |
79 | } | |
80 | ||
81 | /* User-defined FS9721_LP3 flag 'c2c1_10' means temperature (C). */ | |
82 | if (info_local->is_c2c1_10) { | |
83 | analog->mq = SR_MQ_TEMPERATURE; | |
84 | analog->unit = SR_UNIT_CELSIUS; | |
85 | } | |
86 | } | |
87 | ||
69e19dd7 | 88 | static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi, |
729b01f9 | 89 | int dmm, void *info) |
7dc55d93 | 90 | { |
8c1adf37 | 91 | float floatval; |
7dc55d93 AG |
92 | struct sr_datafeed_packet packet; |
93 | struct sr_datafeed_analog *analog; | |
69e19dd7 | 94 | struct dev_context *devc; |
7dc55d93 | 95 | |
6bef68a7 | 96 | log_dmm_packet(buf); |
69e19dd7 | 97 | devc = sdi->priv; |
6bef68a7 | 98 | |
bbabddbd | 99 | if (!(analog = g_try_malloc0(sizeof(struct sr_datafeed_analog)))) { |
8c1adf37 | 100 | sr_err("Analog packet malloc failed."); |
7dc55d93 AG |
101 | return; |
102 | } | |
bbabddbd | 103 | |
69e19dd7 | 104 | analog->probes = sdi->probes; |
bbabddbd | 105 | analog->num_samples = 1; |
7dc55d93 AG |
106 | analog->mq = -1; |
107 | ||
729b01f9 | 108 | dmms[dmm].packet_parse(buf, &floatval, analog, info); |
d84fc9cb | 109 | analog->data = &floatval; |
be5c1d3b | 110 | |
22f54192 | 111 | /* If this DMM needs additional handling, call the resp. function. */ |
ce3777ad UH |
112 | if (dmms[dmm].dmm_details) |
113 | dmms[dmm].dmm_details(analog, info); | |
7dc55d93 | 114 | |
7dc55d93 AG |
115 | if (analog->mq != -1) { |
116 | /* Got a measurement. */ | |
7dc55d93 AG |
117 | packet.type = SR_DF_ANALOG; |
118 | packet.payload = analog; | |
119 | sr_session_send(devc->cb_data, &packet); | |
120 | devc->num_samples++; | |
121 | } | |
bbabddbd | 122 | |
7dc55d93 AG |
123 | g_free(analog); |
124 | } | |
125 | ||
69e19dd7 | 126 | static void handle_new_data(struct sr_dev_inst *sdi, int dmm, void *info) |
7dc55d93 | 127 | { |
69e19dd7 | 128 | struct dev_context *devc; |
bbabddbd | 129 | int len, i, offset = 0; |
bbabddbd | 130 | |
69e19dd7 BV |
131 | devc = sdi->priv; |
132 | ||
bbabddbd | 133 | /* Try to get as much data as the buffer can hold. */ |
7dc55d93 | 134 | len = DMM_BUFSIZE - devc->buflen; |
d35afa87 | 135 | len = serial_read(devc->serial, devc->buf + devc->buflen, len); |
7dc55d93 | 136 | if (len < 1) { |
bbabddbd | 137 | sr_err("Serial port read error: %d.", len); |
7dc55d93 AG |
138 | return; |
139 | } | |
140 | devc->buflen += len; | |
141 | ||
bbabddbd | 142 | /* Now look for packets in that data. */ |
729b01f9 UH |
143 | while ((devc->buflen - offset) >= dmms[dmm].packet_size) { |
144 | if (dmms[dmm].packet_valid(devc->buf + offset)) { | |
69e19dd7 | 145 | handle_packet(devc->buf + offset, sdi, dmm, info); |
729b01f9 | 146 | offset += dmms[dmm].packet_size; |
7dc55d93 AG |
147 | } else { |
148 | offset++; | |
149 | } | |
150 | } | |
151 | ||
bbabddbd UH |
152 | /* If we have any data left, move it to the beginning of our buffer. */ |
153 | for (i = 0; i < devc->buflen - offset; i++) | |
7dc55d93 AG |
154 | devc->buf[i] = devc->buf[offset + i]; |
155 | devc->buflen -= offset; | |
156 | } | |
157 | ||
729b01f9 | 158 | static int receive_data(int fd, int revents, int dmm, void *info, void *cb_data) |
7dc55d93 | 159 | { |
642e9d62 | 160 | struct sr_dev_inst *sdi; |
7dc55d93 | 161 | struct dev_context *devc; |
f9b9bd63 | 162 | int64_t time; |
ce3777ad | 163 | int ret; |
7dc55d93 | 164 | |
d35afa87 BV |
165 | (void)fd; |
166 | ||
7dc55d93 AG |
167 | if (!(sdi = cb_data)) |
168 | return TRUE; | |
169 | ||
170 | if (!(devc = sdi->priv)) | |
171 | return TRUE; | |
172 | ||
bbabddbd | 173 | if (revents == G_IO_IN) { |
7dc55d93 | 174 | /* Serial data arrived. */ |
69e19dd7 | 175 | handle_new_data(sdi, dmm, info); |
ce3777ad UH |
176 | } else { |
177 | /* Timeout, send another packet request (if DMM needs it). */ | |
178 | if (dmms[dmm].packet_request) { | |
179 | ret = dmms[dmm].packet_request(devc->serial); | |
180 | if (ret < 0) { | |
181 | sr_err("Failed to request packet: %d.", ret); | |
182 | return FALSE; | |
183 | } | |
184 | } | |
7dc55d93 AG |
185 | } |
186 | ||
d55c89f5 | 187 | if (devc->limit_samples && devc->num_samples >= devc->limit_samples) { |
35e199da | 188 | sr_info("Requested number of samples reached."); |
7dc55d93 AG |
189 | sdi->driver->dev_acquisition_stop(sdi, cb_data); |
190 | return TRUE; | |
191 | } | |
192 | ||
f9b9bd63 AG |
193 | if (devc->limit_msec) { |
194 | time = (g_get_monotonic_time() - devc->starttime) / 1000; | |
195 | if (time > (int64_t)devc->limit_msec) { | |
35e199da | 196 | sr_info("Requested time limit reached."); |
f9b9bd63 AG |
197 | sdi->driver->dev_acquisition_stop(sdi, cb_data); |
198 | return TRUE; | |
199 | } | |
200 | } | |
201 | ||
7dc55d93 AG |
202 | return TRUE; |
203 | } | |
729b01f9 | 204 | |
22f54192 | 205 | #define RECEIVE_DATA(ID_UPPER, DMM_DRIVER) \ |
dccfe015 UH |
206 | SR_PRIV int receive_data_##ID_UPPER(int fd, int revents, void *cb_data) { \ |
207 | struct DMM_DRIVER##_info info; \ | |
208 | return receive_data(fd, revents, ID_UPPER, &info, cb_data); } | |
209 | ||
210 | /* Driver-specific receive_data() wrappers */ | |
22f54192 UH |
211 | RECEIVE_DATA(DIGITEK_DT4000ZC, fs9721) |
212 | RECEIVE_DATA(TEKPOWER_TP4000ZC, fs9721) | |
213 | RECEIVE_DATA(METEX_ME31, metex14) | |
214 | RECEIVE_DATA(PEAKTECH_3410, metex14) | |
215 | RECEIVE_DATA(MASTECH_MAS345, metex14) | |
216 | RECEIVE_DATA(VA_VA18B, fs9721) | |
217 | RECEIVE_DATA(METEX_M3640D, metex14) | |
218 | RECEIVE_DATA(PEAKTECH_4370, metex14) | |
219 | RECEIVE_DATA(PCE_PCE_DM32, fs9721) | |
220 | RECEIVE_DATA(RADIOSHACK_22_168, metex14) | |
221 | RECEIVE_DATA(RADIOSHACK_22_812, rs9lcd) | |
14766619 UH |
222 | RECEIVE_DATA(VOLTCRAFT_VC820_SER, fs9721) |
223 | RECEIVE_DATA(VOLTCRAFT_VC840_SER, fs9721) | |
986fde75 | 224 | RECEIVE_DATA(UNI_T_UT61E_SER, es51922) |