]> sigrok.org Git - libsigrok.git/blob - trigger.c
build: Portability fixes.
[libsigrok.git] / 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 SR_API struct sr_trigger *sr_trigger_new(const char *name)
28 {
29         struct sr_trigger *trig;
30
31         trig = g_malloc0(sizeof(struct sr_trigger));
32         if (name)
33                 trig->name = g_strdup(name);
34
35         return trig;
36 }
37
38 SR_API void sr_trigger_free(struct sr_trigger *trig)
39 {
40         struct sr_trigger_stage *stage;
41         GSList *l;
42
43         for (l = trig->stages; l; l = l->next) {
44                 stage = l->data;
45                 g_slist_free_full(stage->matches, g_free);
46         }
47         g_slist_free_full(trig->stages, g_free);
48
49         g_free(trig->name);
50         g_free(trig);
51 }
52
53 SR_API struct sr_trigger_stage *sr_trigger_stage_add(struct sr_trigger *trig)
54 {
55         struct sr_trigger_stage *stage;
56
57         stage = g_malloc0(sizeof(struct sr_trigger_stage));
58         stage->stage = g_slist_length(trig->stages);
59         trig->stages = g_slist_append(trig->stages, stage);
60
61         return stage;
62 }
63
64 SR_API int sr_trigger_match_add(struct sr_trigger_stage *stage,
65                 struct sr_channel *ch, int trigger_match, float value)
66 {
67         struct sr_trigger_match *match;
68
69         if (ch->type == SR_CHANNEL_LOGIC) {
70                 if (trigger_match != SR_TRIGGER_ZERO &&
71                                 trigger_match != SR_TRIGGER_ONE &&
72                                 trigger_match != SR_TRIGGER_RISING &&
73                                 trigger_match != SR_TRIGGER_FALLING &&
74                                 trigger_match != SR_TRIGGER_EDGE) {
75                         sr_err("Invalid trigger match for a logic channel.");
76                         return SR_ERR_ARG;
77                 }
78
79
80         } else if (ch->type == SR_CHANNEL_ANALOG) {
81                 if (trigger_match != SR_TRIGGER_FALLING &&
82                                 trigger_match != SR_TRIGGER_OVER &&
83                                 trigger_match != SR_TRIGGER_UNDER) {
84                         sr_err("Invalid trigger match for an analog channel.");
85                         return SR_ERR_ARG;
86                 }
87         }
88
89         match = g_malloc0(sizeof(struct sr_trigger_match));
90         match->channel = ch;
91         match->match = trigger_match;
92         match->value = value;
93         stage->matches = g_slist_append(stage->matches, match);
94
95         return SR_OK;
96 }