]>
Commit | Line | Data |
---|---|---|
d375b3c3 AG |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Bert Vermeulen <bert@biot.com> | |
5 | * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com> | |
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 | ||
d375b3c3 AG |
21 | #include <stdlib.h> |
22 | #include <math.h> | |
23 | #include <string.h> | |
24 | #include <errno.h> | |
ba6383f8 UH |
25 | #include <glib.h> |
26 | #include "libsigrok.h" | |
27 | #include "libsigrok-internal.h" | |
936e27f1 | 28 | #include "protocol.h" |
ba6383f8 | 29 | |
4e172b8f | 30 | |
05f134ab | 31 | static void handle_packet(const uint8_t *rs_packet, |
ba6383f8 | 32 | struct dev_context *devc) |
d375b3c3 | 33 | { |
05f134ab | 34 | float rawval; |
d375b3c3 AG |
35 | struct sr_datafeed_packet packet; |
36 | struct sr_datafeed_analog *analog; | |
37 | ||
886a52b6 | 38 | /* TODO: Check malloc return value. */ |
d375b3c3 | 39 | analog = g_try_malloc0(sizeof(struct sr_datafeed_analog)); |
886a52b6 | 40 | /* TODO: Check malloc return value. */ |
d375b3c3 | 41 | |
05f134ab AG |
42 | analog->num_samples = 1; |
43 | analog->mq = -1; | |
d375b3c3 | 44 | |
05f134ab AG |
45 | sr_rs9lcd_parse(rs_packet, &rawval, analog, NULL); |
46 | analog->data = &rawval; | |
d375b3c3 AG |
47 | |
48 | if (analog->mq != -1) { | |
49 | /* Got a measurement. */ | |
dccbd0ed | 50 | sr_spew("Value: %f.", rawval); |
d375b3c3 AG |
51 | packet.type = SR_DF_ANALOG; |
52 | packet.payload = analog; | |
53 | sr_session_send(devc->cb_data, &packet); | |
54 | devc->num_samples++; | |
55 | } | |
d375b3c3 AG |
56 | g_free(analog); |
57 | } | |
58 | ||
401476da | 59 | static void handle_new_data(struct dev_context *devc) |
d375b3c3 AG |
60 | { |
61 | int len; | |
ba6383f8 | 62 | size_t i, offset = 0; |
05f134ab | 63 | uint8_t *rs_packet; |
ba6383f8 UH |
64 | |
65 | /* Try to get as much data as the buffer can hold. */ | |
d375b3c3 | 66 | len = RS_DMM_BUFSIZE - devc->buflen; |
401476da | 67 | len = serial_read(devc->serial, devc->buf + devc->buflen, len); |
d375b3c3 | 68 | if (len < 1) { |
ba6383f8 | 69 | sr_err("Serial port read error."); |
d375b3c3 AG |
70 | return; |
71 | } | |
72 | devc->buflen += len; | |
73 | ||
ba6383f8 UH |
74 | /* Now look for packets in that data. */ |
75 | while ((devc->buflen - offset) >= RS_22_812_PACKET_SIZE) { | |
76 | rs_packet = (void *)(devc->buf + offset); | |
05f134ab | 77 | if (sr_rs9lcd_packet_valid(rs_packet)) { |
ba6383f8 | 78 | handle_packet(rs_packet, devc); |
d375b3c3 AG |
79 | offset += RS_22_812_PACKET_SIZE; |
80 | } else { | |
81 | offset++; | |
82 | } | |
83 | } | |
84 | ||
ba6383f8 UH |
85 | /* If we have any data left, move it to the beginning of our buffer. */ |
86 | for (i = 0; i < devc->buflen - offset; i++) | |
d375b3c3 AG |
87 | devc->buf[i] = devc->buf[offset + i]; |
88 | devc->buflen -= offset; | |
89 | } | |
90 | ||
ba6383f8 | 91 | SR_PRIV int radioshack_dmm_receive_data(int fd, int revents, void *cb_data) |
d375b3c3 | 92 | { |
642e9d62 | 93 | struct sr_dev_inst *sdi; |
d375b3c3 AG |
94 | struct dev_context *devc; |
95 | ||
401476da BV |
96 | (void)fd; |
97 | ||
d375b3c3 AG |
98 | if (!(sdi = cb_data)) |
99 | return TRUE; | |
100 | ||
101 | if (!(devc = sdi->priv)) | |
102 | return TRUE; | |
103 | ||
ba6383f8 | 104 | if (revents == G_IO_IN) { |
d375b3c3 | 105 | /* Serial data arrived. */ |
401476da | 106 | handle_new_data(devc); |
d375b3c3 AG |
107 | } |
108 | ||
109 | if (devc->num_samples >= devc->limit_samples) { | |
110 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
111 | return TRUE; | |
112 | } | |
113 | ||
114 | return TRUE; | |
115 | } |