]> sigrok.org Git - libsigrok.git/commitdiff
Add helper functions for software limits
authorLars-Peter Clausen <redacted>
Sun, 1 May 2016 11:50:08 +0000 (13:50 +0200)
committerUwe Hermann <redacted>
Mon, 9 May 2016 10:12:14 +0000 (12:12 +0200)
Signed-off-by: Lars-Peter Clausen <redacted>
Makefile.am
src/libsigrok-internal.h
src/sw_limits.c [new file with mode: 0644]

index 134896270c110eed95751abbd6e74ac5ae1c51c9..7f0cab112a38572fa11e2ff2f9a343f963778437 100644 (file)
@@ -65,7 +65,8 @@ libsigrok_la_SOURCES = \
        src/log.c \
        src/version.c \
        src/error.c \
-       src/std.c
+       src/std.c \
+       src/sw_limits.c
 
 # Input modules
 libsigrok_la_SOURCES += \
index 0af77db6c33e4dc41cd898b996631a058395a72c..0d6e0dab29d144eb35a56bb3e4851d7781a439b0 100644 (file)
@@ -1258,4 +1258,23 @@ SR_PRIV gboolean sr_kern_packet_valid(const uint8_t *buf);
 SR_PRIV int sr_kern_parse(const uint8_t *buf, float *floatval,
                struct sr_datafeed_analog_old *analog, void *info);
 
+/*--- sw_limits.c -----------------------------------------------------------*/
+
+struct sr_sw_limits {
+       uint64_t limit_samples;
+       uint64_t limit_msec;
+       uint64_t samples_read;
+       uint64_t start_time;
+};
+
+SR_PRIV int sr_sw_limits_config_get(struct sr_sw_limits *limits, uint32_t key,
+       GVariant **data);
+SR_PRIV int sr_sw_limits_config_set(struct sr_sw_limits *limits, uint32_t key,
+       GVariant *data);
+SR_PRIV void sr_sw_limits_acquisition_start(struct sr_sw_limits *limits);
+SR_PRIV gboolean sr_sw_limits_check(struct sr_sw_limits *limits);
+SR_PRIV void sr_sw_limits_update_samples_read(struct sr_sw_limits *limits,
+       uint64_t samples_read);
+SR_PRIV void sr_sw_limits_init(struct sr_sw_limits *limits);
+
 #endif
diff --git a/src/sw_limits.c b/src/sw_limits.c
new file mode 100644 (file)
index 0000000..eb1e5c3
--- /dev/null
@@ -0,0 +1,96 @@
+/*
+ * This file is part of the libsigrok project.
+ *
+ * Copyright (C) 2016 Lars-Peter Clausen <lars@metafoo.de>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+#include <ctype.h>
+#include <libsigrok/libsigrok.h>
+#include "libsigrok-internal.h"
+
+SR_PRIV void sr_sw_limits_init(struct sr_sw_limits *limits)
+{
+       limits->limit_samples = 0;
+       limits->limit_msec = 0;
+}
+
+SR_PRIV int sr_sw_limits_config_get(struct sr_sw_limits *limits, uint32_t key,
+       GVariant **data)
+{
+       switch (key) {
+       case SR_CONF_LIMIT_SAMPLES:
+               *data = g_variant_new_uint64(limits->limit_samples);
+               break;
+       case SR_CONF_LIMIT_MSEC:
+               *data = g_variant_new_uint64(limits->limit_msec / 1000);
+               break;
+       default:
+               return SR_ERR_NA;
+       }
+
+       return SR_OK;
+}
+
+SR_PRIV int sr_sw_limits_config_set(struct sr_sw_limits *limits, uint32_t key,
+       GVariant *data)
+{
+       switch (key) {
+       case SR_CONF_LIMIT_SAMPLES:
+               limits->limit_samples = g_variant_get_uint64(data);
+               break;
+       case SR_CONF_LIMIT_MSEC:
+               limits->limit_msec = g_variant_get_uint64(data) * 1000;
+               break;
+       default:
+               return SR_ERR_NA;
+       }
+
+       return SR_OK;
+}
+
+SR_PRIV void sr_sw_limits_acquisition_start(struct sr_sw_limits *limits)
+{
+       limits->samples_read = 0;
+       limits->start_time = g_get_monotonic_time();
+}
+
+SR_PRIV gboolean sr_sw_limits_check(struct sr_sw_limits *limits)
+{
+       if (limits->limit_samples) {
+               if (limits->samples_read >= limits->limit_samples)
+                       return TRUE;
+       }
+
+       if (limits->limit_msec) {
+               guint64 now;
+               now = g_get_monotonic_time();
+               if (now > limits->start_time &&
+                       now - limits->start_time > limits->limit_msec)
+                       return TRUE;
+       }
+
+       return FALSE;
+}
+
+SR_PRIV void sr_sw_limits_update_samples_read(struct sr_sw_limits *limits,
+       uint64_t samples_read)
+{
+       limits->samples_read += samples_read;
+}