From: Lars-Peter Clausen Date: Sun, 1 May 2016 11:50:08 +0000 (+0200) Subject: Add helper functions for software limits X-Git-Tag: libsigrok-0.5.0~459 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=aea4e458485559ffb7999ecbda0ed349c7d8edb7;p=libsigrok.git Add helper functions for software limits Signed-off-by: Lars-Peter Clausen --- diff --git a/Makefile.am b/Makefile.am index 13489627..7f0cab11 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 += \ diff --git a/src/libsigrok-internal.h b/src/libsigrok-internal.h index 0af77db6..0d6e0dab 100644 --- a/src/libsigrok-internal.h +++ b/src/libsigrok-internal.h @@ -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 index 00000000..eb1e5c39 --- /dev/null +++ b/src/sw_limits.c @@ -0,0 +1,96 @@ +/* + * This file is part of the libsigrok project. + * + * Copyright (C) 2016 Lars-Peter Clausen + * + * 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 . + */ + +#include +#include +#include +#include +#include +#include +#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; +}