]> sigrok.org Git - libsigrok.git/blame - src/sw_limits.c
gmc-mh-1x-2x: Use software limits helpers
[libsigrok.git] / src / sw_limits.c
CommitLineData
aea4e458
LPC
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2016 Lars-Peter Clausen <lars@metafoo.de>
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 <config.h>
21#include <stdio.h>
22#include <stdint.h>
23#include <string.h>
24#include <ctype.h>
25#include <libsigrok/libsigrok.h>
26#include "libsigrok-internal.h"
27
28SR_PRIV void sr_sw_limits_init(struct sr_sw_limits *limits)
29{
30 limits->limit_samples = 0;
31 limits->limit_msec = 0;
32}
33
34SR_PRIV int sr_sw_limits_config_get(struct sr_sw_limits *limits, uint32_t key,
35 GVariant **data)
36{
37 switch (key) {
38 case SR_CONF_LIMIT_SAMPLES:
39 *data = g_variant_new_uint64(limits->limit_samples);
40 break;
41 case SR_CONF_LIMIT_MSEC:
42 *data = g_variant_new_uint64(limits->limit_msec / 1000);
43 break;
44 default:
45 return SR_ERR_NA;
46 }
47
48 return SR_OK;
49}
50
51SR_PRIV int sr_sw_limits_config_set(struct sr_sw_limits *limits, uint32_t key,
52 GVariant *data)
53{
54 switch (key) {
55 case SR_CONF_LIMIT_SAMPLES:
56 limits->limit_samples = g_variant_get_uint64(data);
57 break;
58 case SR_CONF_LIMIT_MSEC:
59 limits->limit_msec = g_variant_get_uint64(data) * 1000;
60 break;
61 default:
62 return SR_ERR_NA;
63 }
64
65 return SR_OK;
66}
67
68SR_PRIV void sr_sw_limits_acquisition_start(struct sr_sw_limits *limits)
69{
70 limits->samples_read = 0;
71 limits->start_time = g_get_monotonic_time();
72}
73
74SR_PRIV gboolean sr_sw_limits_check(struct sr_sw_limits *limits)
75{
76 if (limits->limit_samples) {
77 if (limits->samples_read >= limits->limit_samples)
78 return TRUE;
79 }
80
81 if (limits->limit_msec) {
82 guint64 now;
83 now = g_get_monotonic_time();
84 if (now > limits->start_time &&
85 now - limits->start_time > limits->limit_msec)
86 return TRUE;
87 }
88
89 return FALSE;
90}
91
92SR_PRIV void sr_sw_limits_update_samples_read(struct sr_sw_limits *limits,
93 uint64_t samples_read)
94{
95 limits->samples_read += samples_read;
96}