]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com> | |
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 <glib.h> | |
21 | #include <stdlib.h> | |
22 | #include <math.h> | |
23 | #include <string.h> | |
24 | #include <errno.h> | |
25 | #include "libsigrok.h" | |
26 | #include "libsigrok-internal.h" | |
27 | #include "protocol.h" | |
28 | ||
29 | /* User-defined FS9721_LP3 flag 'c2c1_10' means temperature on this DMM. */ | |
30 | #define is_temperature info.is_c2c1_10 | |
31 | ||
32 | /* Now see what the value means, and pass that on. */ | |
33 | static void fs9721_serial_handle_packet(const uint8_t *buf, | |
34 | struct dev_context *devc) | |
35 | { | |
36 | float floatval; | |
37 | struct sr_datafeed_packet packet; | |
38 | struct sr_datafeed_analog *analog; | |
39 | struct fs9721_info info; | |
40 | ||
41 | if (!(analog = g_try_malloc0(sizeof(struct sr_datafeed_analog)))) { | |
42 | sr_err("Analog packet malloc failed."); | |
43 | return; | |
44 | } | |
45 | ||
46 | if (!(analog->data = g_try_malloc(sizeof(float)))) { | |
47 | sr_err("Analog value malloc failed."); | |
48 | g_free(analog); | |
49 | return; | |
50 | } | |
51 | ||
52 | analog->num_samples = 1; | |
53 | analog->mq = -1; | |
54 | ||
55 | sr_fs9721_parse(buf, &floatval, analog, &info); | |
56 | *analog->data = floatval; | |
57 | ||
58 | if (is_temperature) { | |
59 | analog->mq = SR_MQ_TEMPERATURE; | |
60 | /* No Kelvin or Fahrenheit from the device, just Celsius. */ | |
61 | analog->unit = SR_UNIT_CELSIUS; | |
62 | } | |
63 | ||
64 | if (analog->mq != -1) { | |
65 | /* Got a measurement. */ | |
66 | packet.type = SR_DF_ANALOG; | |
67 | packet.payload = analog; | |
68 | sr_session_send(devc->cb_data, &packet); | |
69 | devc->num_samples++; | |
70 | } | |
71 | ||
72 | g_free(analog->data); | |
73 | g_free(analog); | |
74 | } | |
75 | ||
76 | static void handle_new_data(struct dev_context *devc, int fd) | |
77 | { | |
78 | int len, i, offset = 0; | |
79 | ||
80 | /* Try to get as much data as the buffer can hold. */ | |
81 | len = DMM_BUFSIZE - devc->buflen; | |
82 | len = serial_read(fd, devc->buf + devc->buflen, len); | |
83 | if (len < 1) { | |
84 | sr_err("Serial port read error: %d.", len); | |
85 | return; | |
86 | } | |
87 | devc->buflen += len; | |
88 | ||
89 | /* Now look for packets in that data. */ | |
90 | while ((devc->buflen - offset) >= FS9721_PACKET_SIZE) { | |
91 | if (sr_fs9721_packet_valid(devc->buf + offset)) { | |
92 | fs9721_serial_handle_packet(devc->buf + offset, devc); | |
93 | offset += FS9721_PACKET_SIZE; | |
94 | } else { | |
95 | offset++; | |
96 | } | |
97 | } | |
98 | ||
99 | /* If we have any data left, move it to the beginning of our buffer. */ | |
100 | for (i = 0; i < devc->buflen - offset; i++) | |
101 | devc->buf[i] = devc->buf[offset + i]; | |
102 | devc->buflen -= offset; | |
103 | } | |
104 | ||
105 | SR_PRIV int tekpower_dmm_receive_data(int fd, int revents, void *cb_data) | |
106 | { | |
107 | const struct sr_dev_inst *sdi; | |
108 | struct dev_context *devc; | |
109 | ||
110 | if (!(sdi = cb_data)) | |
111 | return TRUE; | |
112 | ||
113 | if (!(devc = sdi->priv)) | |
114 | return TRUE; | |
115 | ||
116 | if (revents == G_IO_IN) { | |
117 | /* Serial data arrived. */ | |
118 | handle_new_data(devc, fd); | |
119 | } | |
120 | ||
121 | if (devc->num_samples >= devc->limit_samples) { | |
122 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
123 | return TRUE; | |
124 | } | |
125 | ||
126 | return TRUE; | |
127 | } |