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/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.
42 * Create a new trigger.
44 * The caller is responsible to free the trigger (including all stages and
45 * matches) using sr_trigger_free() once it is no longer needed.
47 * @param name The trigger name to use. Can be NULL.
49 * @return A newly allocated trigger.
53 SR_API struct sr_trigger *sr_trigger_new(const char *name)
55 struct sr_trigger *trig;
57 trig = g_malloc0(sizeof(struct sr_trigger));
59 trig->name = g_strdup(name);
65 * Free a previously allocated trigger.
67 * This will also free any trigger stages/matches in this trigger.
69 * @param trig The trigger to free. Must not be NULL.
73 SR_API void sr_trigger_free(struct sr_trigger *trig)
75 struct sr_trigger_stage *stage;
81 for (l = trig->stages; l; l = l->next) {
85 g_slist_free_full(stage->matches, g_free);
87 g_slist_free_full(trig->stages, g_free);
94 * Allocate a new trigger stage and add it to the specified trigger.
96 * The caller is responsible to free the trigger (including all stages and
97 * matches) using sr_trigger_free() once it is no longer needed.
99 * @param trig The trigger to add a stage to. Must not be NULL.
101 * @retval NULL An invalid (NULL) trigger was passed into the function.
102 * @retval other A newly allocated trigger stage (which has also been added
103 * to the list of stages of the specified trigger).
107 SR_API struct sr_trigger_stage *sr_trigger_stage_add(struct sr_trigger *trig)
109 struct sr_trigger_stage *stage;
114 stage = g_malloc0(sizeof(struct sr_trigger_stage));
115 stage->stage = g_slist_length(trig->stages);
116 trig->stages = g_slist_append(trig->stages, stage);
122 * Allocate a new trigger match and add it to the specified trigger stage.
124 * The caller is responsible to free the trigger (including all stages and
125 * matches) using sr_trigger_free() once it is no longer needed.
127 * @param stage The trigger stage to add the match to. Must not be NULL.
128 * @param ch The channel for this trigger match. Must not be NULL. Must be
129 * either of type SR_CHANNEL_LOGIC or SR_CHANNEL_ANALOG.
130 * @param trigger_match The type of trigger match. Must be a valid trigger
131 * type from enum sr_trigger_matches. The trigger type
132 * must be valid for the respective channel type as well.
133 * @param value Trigger value.
135 * @retval SR_OK Success.
136 * @retval SR_ERR_ARG Invalid argument(s) were passed to this functions.
140 SR_API int sr_trigger_match_add(struct sr_trigger_stage *stage,
141 struct sr_channel *ch, int trigger_match, float value)
143 struct sr_trigger_match *match;
148 if (ch->type == SR_CHANNEL_LOGIC) {
149 if (trigger_match != SR_TRIGGER_ZERO &&
150 trigger_match != SR_TRIGGER_ONE &&
151 trigger_match != SR_TRIGGER_RISING &&
152 trigger_match != SR_TRIGGER_FALLING &&
153 trigger_match != SR_TRIGGER_EDGE) {
154 sr_err("Invalid trigger match for a logic channel.");
157 } else if (ch->type == SR_CHANNEL_ANALOG) {
158 if (trigger_match != SR_TRIGGER_RISING &&
159 trigger_match != SR_TRIGGER_FALLING &&
160 trigger_match != SR_TRIGGER_EDGE &&
161 trigger_match != SR_TRIGGER_OVER &&
162 trigger_match != SR_TRIGGER_UNDER) {
163 sr_err("Invalid trigger match for an analog channel.");
167 sr_err("Unsupported channel type: %d.", ch->type);
171 match = g_malloc0(sizeof(struct sr_trigger_match));
173 match->match = trigger_match;
174 match->value = value;
175 stage->matches = g_slist_append(stage->matches, match);