]> sigrok.org Git - libsigrok.git/blob - src/trigger.c
rigol-ds: replace some magic numbers by appropriate constant or variable
[libsigrok.git] / src / trigger.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
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 "libsigrok.h"
21 #include "libsigrok-internal.h"
22
23 /* * @cond PRIVATE */
24 #define LOG_PREFIX "trigger"
25 /* * @endcond */
26    
27 /**
28  * @file
29  *
30  * Creating, using, or destroying triggers.
31  */
32
33 /**
34  * @defgroup grp_trigger Trigger handling
35  *
36  * Creating, using, or destroying triggers.
37  *
38  * @{
39  */
40
41 SR_API struct sr_trigger *sr_trigger_new(const char *name)
42 {
43         struct sr_trigger *trig;
44
45         trig = g_malloc0(sizeof(struct sr_trigger));
46         if (name)
47                 trig->name = g_strdup(name);
48
49         return trig;
50 }
51
52 SR_API void sr_trigger_free(struct sr_trigger *trig)
53 {
54         struct sr_trigger_stage *stage;
55         GSList *l;
56
57         for (l = trig->stages; l; l = l->next) {
58                 stage = l->data;
59                 g_slist_free_full(stage->matches, g_free);
60         }
61         g_slist_free_full(trig->stages, g_free);
62
63         g_free(trig->name);
64         g_free(trig);
65 }
66
67 SR_API struct sr_trigger_stage *sr_trigger_stage_add(struct sr_trigger *trig)
68 {
69         struct sr_trigger_stage *stage;
70
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);
74
75         return stage;
76 }
77
78 SR_API int sr_trigger_match_add(struct sr_trigger_stage *stage,
79                 struct sr_channel *ch, int trigger_match, float value)
80 {
81         struct sr_trigger_match *match;
82
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.");
90                         return SR_ERR_ARG;
91                 }
92
93
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.");
99                         return SR_ERR_ARG;
100                 }
101         }
102
103         match = g_malloc0(sizeof(struct sr_trigger_match));
104         match->channel = ch;
105         match->match = trigger_match;
106         match->value = value;
107         stage->matches = g_slist_append(stage->matches, match);
108
109         return SR_OK;
110 }
111
112 /** @} */