]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2014 Matthias Heidbrink <m-sigrok@heidbrink.biz> | |
5 | * Copyright (C) 2014 Bert Vermeulen <bert@biot.com> (code from atten-pps3xxx) | |
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 <config.h> | |
22 | #include <errno.h> | |
23 | #include <string.h> | |
24 | #include "protocol.h" | |
25 | ||
26 | /** Send data packets for current measurements. */ | |
27 | static void send_data(struct sr_dev_inst *sdi) | |
28 | { | |
29 | struct dev_context *devc; | |
30 | struct sr_datafeed_packet packet; | |
31 | struct sr_datafeed_analog analog; | |
32 | struct sr_analog_encoding encoding; | |
33 | struct sr_analog_meaning meaning; | |
34 | struct sr_analog_spec spec; | |
35 | int i; | |
36 | float data[MAX_CHANNELS]; | |
37 | ||
38 | devc = sdi->priv; | |
39 | packet.type = SR_DF_ANALOG; | |
40 | packet.payload = &analog; | |
41 | ||
42 | /* Note: digits/spec_digits will be overridden later. */ | |
43 | sr_analog_init(&analog, &encoding, &meaning, &spec, 0); | |
44 | ||
45 | analog.meaning->channels = sdi->channels; | |
46 | analog.num_samples = 1; | |
47 | analog.meaning->mq = SR_MQ_VOLTAGE; | |
48 | analog.meaning->unit = SR_UNIT_VOLT; | |
49 | analog.meaning->mqflags = SR_MQFLAG_DC; | |
50 | analog.encoding->digits = 3; | |
51 | analog.spec->spec_digits = 2; | |
52 | analog.data = data; | |
53 | ||
54 | for (i = 0; i < devc->model->num_channels; i++) | |
55 | ((float *)analog.data)[i] = devc->channel_status[i].output_voltage_last; /* Value always 3.3 or 5 for channel 3, if present! */ | |
56 | sr_session_send(sdi, &packet); | |
57 | ||
58 | analog.meaning->mq = SR_MQ_CURRENT; | |
59 | analog.meaning->unit = SR_UNIT_AMPERE; | |
60 | analog.meaning->mqflags = 0; | |
61 | analog.encoding->digits = 4; | |
62 | analog.spec->spec_digits = 3; | |
63 | analog.data = data; | |
64 | for (i = 0; i < devc->model->num_channels; i++) | |
65 | ((float *)analog.data)[i] = devc->channel_status[i].output_current_last; /* Value always 0 for channel 3, if present! */ | |
66 | sr_session_send(sdi, &packet); | |
67 | ||
68 | sr_sw_limits_update_samples_read(&devc->limits, 1); | |
69 | } | |
70 | ||
71 | /** Process a complete line (without CR/LF) in buf. */ | |
72 | static void process_line(struct sr_dev_inst *sdi) | |
73 | { | |
74 | struct dev_context *devc; | |
75 | double dbl; | |
76 | int auxint; | |
77 | ||
78 | devc = sdi->priv; | |
79 | ||
80 | switch (devc->acq_req_pending) { | |
81 | case 0: /* Should not happen... */ | |
82 | break; | |
83 | case 1: /* Waiting for data reply to request */ | |
84 | /* Convert numbers */ | |
85 | switch (devc->acq_req) { | |
86 | case AQ_U1: | |
87 | case AQ_U2: | |
88 | case AQ_I1: | |
89 | case AQ_I2: | |
90 | if (sr_atod(devc->buf, &dbl) != SR_OK) { | |
91 | sr_err("Failed to convert '%s' to double, errno=%d %s", | |
92 | devc->buf, errno, g_strerror(errno)); | |
93 | dbl = 0.0; | |
94 | } | |
95 | break; | |
96 | case AQ_STATUS: | |
97 | if (sr_atoi(devc->buf, &auxint) != SR_OK) { | |
98 | sr_err("Failed to convert '%s' to int, errno=%d %s", | |
99 | devc->buf, errno, g_strerror(errno)); | |
100 | auxint = 0; | |
101 | } | |
102 | break; | |
103 | default: | |
104 | break; | |
105 | } | |
106 | ||
107 | switch (devc->acq_req) { | |
108 | case AQ_U1: | |
109 | devc->channel_status[0].output_voltage_last = dbl; | |
110 | break; | |
111 | case AQ_I1: | |
112 | devc->channel_status[0].output_current_last = dbl; | |
113 | break; | |
114 | case AQ_U2: | |
115 | devc->channel_status[1].output_voltage_last = dbl; | |
116 | break; | |
117 | case AQ_I2: | |
118 | devc->channel_status[1].output_current_last = dbl; | |
119 | break; | |
120 | case AQ_STATUS: /* Process status and generate data. */ | |
121 | if (lps_process_status(sdi, auxint) == SR_OK) { | |
122 | send_data(sdi); | |
123 | } | |
124 | break; | |
125 | default: | |
126 | break; | |
127 | } | |
128 | ||
129 | devc->acq_req_pending = 2; | |
130 | break; | |
131 | case 2: /* Waiting for OK after request */ | |
132 | if (strcmp(devc->buf, "OK")) { | |
133 | sr_err("Unexpected reply while waiting for OK: '%s'", devc->buf); | |
134 | } | |
135 | devc->acq_req_pending = 0; | |
136 | break; | |
137 | } | |
138 | ||
139 | devc->buf[0] = '\0'; | |
140 | devc->buflen = 0; | |
141 | } | |
142 | ||
143 | SR_PRIV int motech_lps_30x_receive_data(int fd, int revents, void *cb_data) | |
144 | { | |
145 | struct sr_dev_inst *sdi; | |
146 | struct dev_context *devc; | |
147 | struct sr_serial_dev_inst *serial; | |
148 | int len; | |
149 | ||
150 | (void)fd; | |
151 | ||
152 | if (!(sdi = cb_data)) | |
153 | return TRUE; | |
154 | ||
155 | if (!(devc = sdi->priv)) | |
156 | return TRUE; | |
157 | ||
158 | serial = sdi->conn; | |
159 | ||
160 | if (revents == G_IO_IN) { /* Serial data arrived. */ | |
161 | while (LINELEN_MAX - devc->buflen - 2 > 0) { | |
162 | len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1); | |
163 | if (len < 1) | |
164 | break; | |
165 | ||
166 | /* Eliminate whitespace at beginning of line. */ | |
167 | if (g_ascii_isspace(devc->buf[0])) { | |
168 | devc->buf[0] = '\0'; | |
169 | devc->buflen = 0; | |
170 | continue; | |
171 | } | |
172 | ||
173 | devc->buflen += len; | |
174 | devc->buf[devc->buflen] = '\0'; | |
175 | ||
176 | /* If line complete, process msg. */ | |
177 | if ((devc->buflen > 0) && ((devc->buf[devc->buflen-1] == '\r') || devc->buf[devc->buflen-1] == '\n')) { | |
178 | devc->buflen--; | |
179 | devc->buf[devc->buflen] = '\0'; | |
180 | ||
181 | sr_spew("Line complete: \"%s\"", devc->buf); | |
182 | process_line(sdi); | |
183 | } | |
184 | } | |
185 | } | |
186 | ||
187 | if (sr_sw_limits_check(&devc->limits)) | |
188 | sr_dev_acquisition_stop(sdi); | |
189 | ||
190 | /* Only request the next packet if required. */ | |
191 | if (!((sdi->status == SR_ST_ACTIVE) && (devc->acq_running))) | |
192 | return TRUE; | |
193 | ||
194 | if (devc->acq_req_pending) { | |
195 | int64_t elapsed_us = g_get_monotonic_time() - devc->req_sent_at; | |
196 | if (elapsed_us > (REQ_TIMEOUT_MS * 1000)) { | |
197 | sr_spew("Request timeout: req=%d t=%" PRIi64 "us", | |
198 | (int)devc->acq_req, elapsed_us); | |
199 | devc->acq_req_pending = 0; | |
200 | } | |
201 | } | |
202 | ||
203 | if (devc->acq_req_pending == 0) { | |
204 | switch (devc->acq_req) { | |
205 | case AQ_NONE: /* Fall through */ | |
206 | case AQ_STATUS: | |
207 | devc->acq_req = AQ_U1; | |
208 | lps_send_req(serial, "VOUT1"); | |
209 | break; | |
210 | case AQ_U1: | |
211 | devc->acq_req = AQ_I1; | |
212 | lps_send_req(serial, "IOUT1"); | |
213 | break; | |
214 | case AQ_I1: | |
215 | if (devc->model->num_channels == 1) { | |
216 | devc->acq_req = AQ_STATUS; | |
217 | lps_send_req(serial, "STATUS"); | |
218 | } else { | |
219 | devc->acq_req = AQ_U2; | |
220 | lps_send_req(serial, "VOUT2"); | |
221 | } | |
222 | break; | |
223 | case AQ_U2: | |
224 | devc->acq_req = AQ_I2; | |
225 | lps_send_req(serial, "IOUT2"); | |
226 | break; | |
227 | case AQ_I2: | |
228 | devc->acq_req = AQ_STATUS; | |
229 | lps_send_req(serial, "STATUS"); | |
230 | break; | |
231 | default: | |
232 | sr_err("Illegal devc->acq_req=%d", devc->acq_req); | |
233 | return SR_ERR; | |
234 | } | |
235 | devc->req_sent_at = g_get_real_time(); | |
236 | devc->acq_req_pending = 1; | |
237 | } | |
238 | ||
239 | return TRUE; | |
240 | } |