X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Fmic-985xx%2Fprotocol.c;h=2218f32b2908d3f3538db02489fa5429a2da83b1;hb=08f3b427b620a7a0308b0ee2e61f443a00f3d49c;hp=eb9b48be8d6d24a324deb0776b81514c3ff265be;hpb=676877f6cee574f2a32805b946e56710db502264;p=libsigrok.git
diff --git a/src/hardware/mic-985xx/protocol.c b/src/hardware/mic-985xx/protocol.c
index eb9b48be..2218f32b 100644
--- a/src/hardware/mic-985xx/protocol.c
+++ b/src/hardware/mic-985xx/protocol.c
@@ -14,17 +14,18 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ * along with this program; if not, see .
*/
+#include
#include "protocol.h"
static int mic_send(struct sr_serial_dev_inst *serial, const char *cmd)
{
int ret;
- if ((ret = serial_write_blocking(serial, cmd, strlen(cmd), 0)) < 0) {
+ if ((ret = serial_write_blocking(serial, cmd, strlen(cmd),
+ serial_timeout(serial, strlen(cmd)))) < 0) {
sr_err("Error sending '%s' command: %d.", cmd, ret);
return SR_ERR;
}
@@ -93,7 +94,7 @@ static int handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi, int idx)
{
float temperature, humidity;
struct sr_datafeed_packet packet;
- struct sr_datafeed_analog2 analog;
+ struct sr_datafeed_analog analog;
struct sr_analog_encoding encoding;
struct sr_analog_meaning meaning;
struct sr_analog_spec spec;
@@ -111,32 +112,12 @@ static int handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi, int idx)
return SR_ERR;
}
- memset(&analog, 0, sizeof(struct sr_datafeed_analog));
- memset(&encoding, 0, sizeof(struct sr_analog_encoding));
- memset(&meaning, 0, sizeof(struct sr_analog_meaning));
- memset(&spec, 0, sizeof(struct sr_analog_spec));
+ sr_analog_init(&analog, &encoding, &meaning, &spec, 1);
/* Common values for both channels. */
- packet.type = SR_DF_ANALOG2;
+ packet.type = SR_DF_ANALOG;
packet.payload = &analog;
- analog.encoding = &encoding;
- analog.meaning = &meaning;
- analog.spec = &spec;
analog.num_samples = 1;
- encoding.unitsize = sizeof(float);
- encoding.is_float = TRUE;
-#ifdef WORDS_BIGENDIAN
- encoding.is_bigendian = TRUE;
-#else
- encoding.is_bigendian = FALSE;
-#endif
- encoding.digits = 3; /* Values are always 3-digit numbers. */
- encoding.is_digits_decimal = TRUE;
- encoding.scale.p = 1;
- encoding.scale.q = 1;
- encoding.offset.p = 0;
- encoding.offset.q = 1;
- spec.spec_digits = encoding.digits;
/* Temperature. */
l = g_slist_copy(sdi->channels);
@@ -145,7 +126,7 @@ static int handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi, int idx)
meaning.mq = SR_MQ_TEMPERATURE;
meaning.unit = SR_UNIT_CELSIUS; /* TODO: Use C/F correctly. */
analog.data = &temperature;
- sr_session_send(devc->cb_data, &packet);
+ sr_session_send(sdi, &packet);
g_slist_free(l);
/* Humidity. */
@@ -156,11 +137,11 @@ static int handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi, int idx)
meaning.mq = SR_MQ_RELATIVE_HUMIDITY;
meaning.unit = SR_UNIT_PERCENTAGE;
analog.data = &humidity;
- sr_session_send(devc->cb_data, &packet);
+ sr_session_send(sdi, &packet);
g_slist_free(l);
}
- devc->num_samples++;
+ sr_sw_limits_update_samples_read(&devc->limits, 1);
return SR_OK;
}
@@ -169,7 +150,7 @@ static void handle_new_data(struct sr_dev_inst *sdi, int idx)
{
struct dev_context *devc;
struct sr_serial_dev_inst *serial;
- int len, i, offset = 0;
+ int len, offset;
devc = sdi->priv;
serial = sdi->conn;
@@ -185,6 +166,7 @@ static void handle_new_data(struct sr_dev_inst *sdi, int idx)
devc->buflen += len;
/* Now look for packets in that data. */
+ offset = 0;
while ((devc->buflen - offset) >= mic_devs[idx].packet_size) {
if (mic_devs[idx].packet_valid(devc->buf + offset)) {
handle_packet(devc->buf + offset, sdi, idx);
@@ -195,8 +177,8 @@ static void handle_new_data(struct sr_dev_inst *sdi, int idx)
}
/* If we have any data left, move it to the beginning of our buffer. */
- for (i = 0; i < devc->buflen - offset; i++)
- devc->buf[i] = devc->buf[offset + i];
+ if (offset < devc->buflen)
+ memmove(devc->buf, devc->buf + offset, devc->buflen - offset);
devc->buflen -= offset;
}
@@ -204,7 +186,6 @@ static int receive_data(int fd, int revents, int idx, void *cb_data)
{
struct sr_dev_inst *sdi;
struct dev_context *devc;
- int64_t t;
static gboolean first_time = TRUE;
struct sr_serial_dev_inst *serial;
@@ -229,20 +210,8 @@ static int receive_data(int fd, int revents, int idx, void *cb_data)
}
}
- if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
- sr_info("Requested number of samples reached.");
- sdi->driver->dev_acquisition_stop(sdi, cb_data);
- return TRUE;
- }
-
- if (devc->limit_msec) {
- t = (g_get_monotonic_time() - devc->starttime) / 1000;
- if (t > (int64_t)devc->limit_msec) {
- sr_info("Requested time limit reached.");
- sdi->driver->dev_acquisition_stop(sdi, cb_data);
- return TRUE;
- }
- }
+ if (sr_sw_limits_check(&devc->limits))
+ sr_dev_acquisition_stop(sdi);
return TRUE;
}