2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
20 #include "libsigrok.h"
21 #include "libsigrok-internal.h"
24 #define LOG_PREFIX "trigger"
30 * Creating, using, or destroying triggers.
34 * @defgroup grp_trigger Trigger handling
36 * Creating, using, or destroying triggers.
41 SR_API struct sr_trigger *sr_trigger_new(const char *name)
43 struct sr_trigger *trig;
45 trig = g_malloc0(sizeof(struct sr_trigger));
47 trig->name = g_strdup(name);
52 SR_API void sr_trigger_free(struct sr_trigger *trig)
54 struct sr_trigger_stage *stage;
57 for (l = trig->stages; l; l = l->next) {
59 g_slist_free_full(stage->matches, g_free);
61 g_slist_free_full(trig->stages, g_free);
67 SR_API struct sr_trigger_stage *sr_trigger_stage_add(struct sr_trigger *trig)
69 struct sr_trigger_stage *stage;
71 stage = g_malloc0(sizeof(struct sr_trigger_stage));
72 stage->stage = g_slist_length(trig->stages);
73 trig->stages = g_slist_append(trig->stages, stage);
78 SR_API int sr_trigger_match_add(struct sr_trigger_stage *stage,
79 struct sr_channel *ch, int trigger_match, float value)
81 struct sr_trigger_match *match;
83 if (ch->type == SR_CHANNEL_LOGIC) {
84 if (trigger_match != SR_TRIGGER_ZERO &&
85 trigger_match != SR_TRIGGER_ONE &&
86 trigger_match != SR_TRIGGER_RISING &&
87 trigger_match != SR_TRIGGER_FALLING &&
88 trigger_match != SR_TRIGGER_EDGE) {
89 sr_err("Invalid trigger match for a logic channel.");
94 } else if (ch->type == SR_CHANNEL_ANALOG) {
95 if (trigger_match != SR_TRIGGER_FALLING &&
96 trigger_match != SR_TRIGGER_OVER &&
97 trigger_match != SR_TRIGGER_UNDER) {
98 sr_err("Invalid trigger match for an analog channel.");
103 match = g_malloc0(sizeof(struct sr_trigger_match));
105 match->match = trigger_match;
106 match->value = value;
107 stage->matches = g_slist_append(stage->matches, match);