]>
Commit | Line | Data |
---|---|---|
23669c3d DB |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2019 Dave Buechi <db@pflutsch.ch> | |
5 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
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/>. | |
18 | */ | |
19 | ||
20 | #include <config.h> | |
21 | #include <math.h> | |
22 | #include "protocol.h" | |
23 | ||
24 | static const uint8_t channel_assignment[16][2] = { | |
25 | /* MAIN AUX */ | |
26 | {0, 1}, /* T1 T2 */ | |
27 | {1, 0}, /* T2 T1 */ | |
28 | {2, 0}, /* T1-T2 T1 */ | |
29 | {2, 1}, /* T1-T2 T2 */ | |
30 | {0, 0}, /* T1 T1 MAX */ | |
31 | {1, 1}, /* T2 T2 MAX */ | |
32 | {2, 2}, /* T1-T2 T1-T2 MAX */ | |
33 | {2, 2}, /* T1-T2 T1-T2 MAX */ | |
34 | {0, 0}, /* T1 T1 MIN */ | |
35 | {1, 1}, /* T2 T2 MIN */ | |
36 | {2, 2}, /* T1-T2 T1-T2 MIN */ | |
37 | {2, 2}, /* T1-T2 T1-T2 MIN */ | |
38 | {0, 0}, /* T1 T1 AVG */ | |
39 | {1, 1}, /* T2 T2 AVG */ | |
40 | {2, 2}, /* T1-T2 T1-T2 AVG */ | |
41 | {2, 2}, /* T1-T2 T1-T2 AVG */ | |
42 | }; | |
43 | ||
44 | SR_PRIV gboolean mastech_ms6514_packet_valid(const uint8_t *buf) | |
45 | { | |
46 | if ((buf[0] == 0x65) && (buf[1] == 0x14) && | |
47 | (buf[16] == 0x0D) && (buf[17] == 0x0A)) | |
48 | return TRUE; | |
49 | ||
50 | return FALSE; | |
51 | } | |
52 | ||
53 | static uint64_t mastech_ms6514_flags(const uint8_t *buf, const uint8_t channel_index) | |
54 | { | |
55 | uint64_t flags; | |
56 | ||
57 | flags = 0; | |
58 | if ((buf[10] & 0x40) == 0x40) | |
59 | flags |= SR_MQFLAG_HOLD; | |
60 | ||
61 | if (channel_index == 0) { | |
62 | if ((buf[11] & 0x03) > 0x01) | |
63 | flags |= SR_MQFLAG_RELATIVE; | |
64 | } | |
65 | ||
66 | if (channel_index == 1) { | |
67 | switch (buf[12] & 0x03) { | |
68 | case 0x01: | |
69 | flags |= SR_MQFLAG_MAX; | |
70 | break; | |
71 | case 0x02: | |
72 | flags |= SR_MQFLAG_MIN; | |
73 | break; | |
74 | case 0x03: | |
75 | flags |= SR_MQFLAG_AVG; | |
76 | break; | |
77 | } | |
78 | } | |
79 | ||
80 | return flags; | |
81 | } | |
82 | ||
83 | static enum sr_unit mastech_ms6514_unit(const uint8_t *buf) | |
84 | { | |
85 | enum sr_unit unit; | |
86 | ||
87 | switch (buf[10] & 0x03) { | |
88 | case 0x01: | |
89 | unit = SR_UNIT_CELSIUS; | |
90 | break; | |
91 | case 0x02: | |
92 | unit = SR_UNIT_FAHRENHEIT; | |
93 | break; | |
94 | case 0x03: | |
95 | unit = SR_UNIT_KELVIN; | |
96 | break; | |
97 | default: | |
98 | unit = SR_UNIT_UNITLESS; | |
99 | break; | |
100 | } | |
101 | ||
102 | return unit; | |
103 | } | |
104 | ||
105 | static uint8_t mastech_ms6514_channel_assignment(const uint8_t *buf, const uint8_t index) | |
106 | { | |
107 | return channel_assignment[((buf[12] & 0x03) << 2) + (buf[11] & 0x03)][index]; | |
108 | } | |
109 | ||
110 | static uint8_t mastech_ms6514_data_source(const uint8_t *buf) | |
111 | { | |
112 | if ((buf[2] & 0x01) == 0x01) | |
113 | return DATA_SOURCE_MEMORY; | |
114 | else | |
115 | return DATA_SOURCE_LIVE; | |
116 | } | |
117 | ||
118 | static float mastech_ms6514_temperature(const uint8_t *buf, const uint8_t channel_index, int *digits) | |
119 | { | |
120 | float value; | |
121 | uint8_t modifiers; | |
122 | ||
123 | *digits = 0; | |
124 | value = (buf[5 + channel_index * 2] << 8) + buf[6 + channel_index * 2]; | |
125 | modifiers = buf[11 + channel_index]; | |
126 | ||
127 | if ((modifiers & 0x80) == 0x80) | |
128 | value = -value; | |
129 | ||
130 | if ((modifiers & 0x08) == 0x08) { | |
131 | value /= 10.0; | |
132 | *digits = 1; | |
133 | } | |
134 | ||
135 | if ((modifiers & 0x40) == 0x40) | |
136 | value = INFINITY; | |
137 | ||
138 | return value; | |
139 | } | |
140 | ||
141 | static void mastech_ms6514_data(struct sr_dev_inst *sdi, const uint8_t *buf) | |
142 | { | |
143 | struct dev_context *devc; | |
144 | struct sr_datafeed_packet packet; | |
145 | struct sr_datafeed_analog analog; | |
146 | struct sr_analog_encoding encoding; | |
147 | struct sr_analog_meaning meaning; | |
148 | struct sr_analog_spec spec; | |
149 | struct sr_channel *ch; | |
150 | float value; | |
151 | int i, digits; | |
152 | ||
153 | devc = sdi->priv; | |
154 | ||
155 | if ((devc->data_source == DATA_SOURCE_MEMORY) && \ | |
156 | (mastech_ms6514_data_source(buf) == DATA_SOURCE_LIVE)) { | |
157 | sr_dev_acquisition_stop(sdi); | |
158 | return; | |
159 | } | |
160 | ||
161 | for (i = 0; i < MASTECH_MS6514_NUM_CHANNELS; i++) { | |
162 | ch = g_slist_nth_data(sdi->channels, i); | |
163 | if (!ch->enabled) | |
164 | continue; | |
165 | ||
166 | value = mastech_ms6514_temperature(buf, i, &digits); | |
167 | sr_analog_init(&analog, &encoding, &meaning, &spec, digits); | |
168 | analog.num_samples = 1; | |
169 | analog.data = &value; | |
170 | analog.meaning->mq = SR_MQ_TEMPERATURE; | |
171 | analog.meaning->unit = mastech_ms6514_unit(buf); | |
172 | analog.meaning->mqflags = mastech_ms6514_flags(buf, i); | |
173 | ||
174 | analog.meaning->channels = g_slist_append(NULL, | |
175 | g_slist_nth_data(sdi->channels, | |
176 | mastech_ms6514_channel_assignment(buf, i))); | |
177 | ||
178 | packet.type = SR_DF_ANALOG; | |
179 | packet.payload = &analog; | |
180 | sr_session_send(sdi, &packet); | |
181 | g_slist_free(analog.meaning->channels); | |
182 | } | |
183 | ||
184 | sr_sw_limits_update_samples_read(&devc->limits, 1); | |
185 | } | |
186 | ||
187 | static const uint8_t *mastech_ms6514_parse_data(struct sr_dev_inst *sdi, | |
188 | const uint8_t *buf, int len) | |
189 | { | |
190 | if (len < MASTECH_MS6514_FRAME_SIZE) | |
191 | return NULL; /* Not enough data for a full packet. */ | |
192 | ||
193 | if (buf[0] != 0x65 || buf[1] != 0x14) | |
194 | return buf + 1; /* Try to re-synchronize on a packet start. */ | |
195 | ||
196 | if (buf[16] != 0x0D || buf[17] != 0x0A) | |
197 | return buf + MASTECH_MS6514_FRAME_SIZE; /* Valid start but no valid end -> skip. */ | |
198 | ||
199 | mastech_ms6514_data(sdi, buf); | |
200 | ||
201 | return buf + MASTECH_MS6514_FRAME_SIZE; | |
202 | } | |
203 | ||
204 | SR_PRIV int mastech_ms6514_receive_data(int fd, int revents, void *cb_data) | |
205 | { | |
206 | struct sr_dev_inst *sdi; | |
207 | struct dev_context *devc; | |
208 | struct sr_serial_dev_inst *serial; | |
209 | const uint8_t *ptr, *next_ptr, *end_ptr; | |
210 | int len; | |
211 | ||
212 | (void)fd; | |
213 | ||
214 | if (!(sdi = cb_data) || !(devc = sdi->priv) || revents != G_IO_IN) | |
215 | return TRUE; | |
216 | serial = sdi->conn; | |
217 | ||
218 | /* Try to get as much data as the buffer can hold. */ | |
219 | len = sizeof(devc->buf) - devc->buf_len; | |
220 | len = serial_read_nonblocking(serial, devc->buf + devc->buf_len, len); | |
221 | if (len < 1) { | |
222 | sr_err("Serial port read error: %d.", len); | |
223 | return FALSE; | |
224 | } | |
225 | devc->buf_len += len; | |
226 | ||
227 | /* Now look for packets in that data. */ | |
228 | ptr = devc->buf; | |
229 | end_ptr = ptr + devc->buf_len; | |
230 | while ((next_ptr = mastech_ms6514_parse_data(sdi, ptr, end_ptr - ptr))) | |
231 | ptr = next_ptr; | |
232 | ||
233 | /* If we have any data left, move it to the beginning of our buffer. */ | |
234 | memmove(devc->buf, ptr, end_ptr - ptr); | |
235 | devc->buf_len -= ptr - devc->buf; | |
236 | ||
237 | /* If buffer is full and no valid packet was found, wipe buffer. */ | |
238 | if (devc->buf_len >= sizeof(devc->buf)) { | |
239 | devc->buf_len = 0; | |
240 | return FALSE; | |
241 | } | |
242 | ||
243 | if (sr_sw_limits_check(&devc->limits)) { | |
244 | sr_dev_acquisition_stop(sdi); | |
245 | return TRUE; | |
246 | } | |
247 | ||
248 | return TRUE; | |
249 | } |