-
Notifications
You must be signed in to change notification settings - Fork 367
/
Copy pathtrigger.c
181 lines (159 loc) · 4.81 KB
/
trigger.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* This file is part of the libsigrok project.
*
* Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <libsigrok/libsigrok.h>
#include "libsigrok-internal.h"
/** @cond PRIVATE */
#define LOG_PREFIX "trigger"
/** @endcond */
/**
* @file
*
* Creating, using, or destroying triggers.
*/
/**
* @defgroup grp_trigger Trigger handling
*
* Creating, using, or destroying triggers.
*
* @{
*/
/**
* Create a new trigger.
*
* The caller is responsible to free the trigger (including all stages and
* matches) using sr_trigger_free() once it is no longer needed.
*
* @param name The trigger name to use. Can be NULL.
*
* @return A newly allocated trigger.
*
* @since 0.4.0
*/
SR_API struct sr_trigger *sr_trigger_new(const char *name)
{
struct sr_trigger *trig;
trig = g_malloc0(sizeof(struct sr_trigger));
if (name)
trig->name = g_strdup(name);
return trig;
}
/**
* Free a previously allocated trigger.
*
* This will also free any trigger stages/matches in this trigger.
*
* @param trig The trigger to free. Must not be NULL.
*
* @since 0.4.0
*/
SR_API void sr_trigger_free(struct sr_trigger *trig)
{
struct sr_trigger_stage *stage;
GSList *l;
if (!trig)
return;
for (l = trig->stages; l; l = l->next) {
stage = l->data;
if (stage->matches)
g_slist_free_full(stage->matches, g_free);
}
g_slist_free_full(trig->stages, g_free);
g_free(trig->name);
g_free(trig);
}
/**
* Allocate a new trigger stage and add it to the specified trigger.
*
* The caller is responsible to free the trigger (including all stages and
* matches) using sr_trigger_free() once it is no longer needed.
*
* @param trig The trigger to add a stage to. Must not be NULL.
*
* @retval NULL An invalid (NULL) trigger was passed into the function.
* @retval other A newly allocated trigger stage (which has also been added
* to the list of stages of the specified trigger).
*
* @since 0.4.0
*/
SR_API struct sr_trigger_stage *sr_trigger_stage_add(struct sr_trigger *trig)
{
struct sr_trigger_stage *stage;
if (!trig)
return NULL;
stage = g_malloc0(sizeof(struct sr_trigger_stage));
stage->stage = g_slist_length(trig->stages);
trig->stages = g_slist_append(trig->stages, stage);
return stage;
}
/**
* Allocate a new trigger match and add it to the specified trigger stage.
*
* The caller is responsible to free the trigger (including all stages and
* matches) using sr_trigger_free() once it is no longer needed.
*
* @param stage The trigger stage to add the match to. Must not be NULL.
* @param ch The channel for this trigger match. Must not be NULL. Must be
* either of type SR_CHANNEL_LOGIC or SR_CHANNEL_ANALOG.
* @param trigger_match The type of trigger match. Must be a valid trigger
* type from enum sr_trigger_matches. The trigger type
* must be valid for the respective channel type as well.
* @param value Trigger value.
*
* @retval SR_OK Success.
* @retval SR_ERR_ARG Invalid argument(s) were passed to this functions.
*
* @since 0.4.0
*/
SR_API int sr_trigger_match_add(struct sr_trigger_stage *stage,
struct sr_channel *ch, int trigger_match, float value)
{
struct sr_trigger_match *match;
if (!stage || !ch)
return SR_ERR_ARG;
if (ch->type == SR_CHANNEL_LOGIC) {
if (trigger_match != SR_TRIGGER_ZERO &&
trigger_match != SR_TRIGGER_ONE &&
trigger_match != SR_TRIGGER_RISING &&
trigger_match != SR_TRIGGER_FALLING &&
trigger_match != SR_TRIGGER_EDGE) {
sr_err("Invalid trigger match for a logic channel.");
return SR_ERR_ARG;
}
} else if (ch->type == SR_CHANNEL_ANALOG) {
if (trigger_match != SR_TRIGGER_RISING &&
trigger_match != SR_TRIGGER_FALLING &&
trigger_match != SR_TRIGGER_EDGE &&
trigger_match != SR_TRIGGER_OVER &&
trigger_match != SR_TRIGGER_UNDER) {
sr_err("Invalid trigger match for an analog channel.");
return SR_ERR_ARG;
}
} else {
sr_err("Unsupported channel type: %d.", ch->type);
return SR_ERR_ARG;
}
match = g_malloc0(sizeof(struct sr_trigger_match));
match->channel = ch;
match->match = trigger_match;
match->value = value;
stage->matches = g_slist_append(stage->matches, match);
return SR_OK;
}
/** @} */