X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Ftrigger.c;h=c8e13d2f610ef0ec9dadbc97c7853e7832c2bcc3;hb=751ba4c8ed3ec40c85e497fde5bf58185f1d9490;hp=0783bb2af84dcd395f672ab8e738267b09b42dac;hpb=155b680da482cea2381becb73c51cfb838bff31e;p=libsigrok.git
diff --git a/src/trigger.c b/src/trigger.c
index 0783bb2a..c8e13d2f 100644
--- a/src/trigger.c
+++ b/src/trigger.c
@@ -17,13 +17,40 @@
* along with this program. If not, see .
*/
-#include "libsigrok.h"
+#include
+#include
#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;
@@ -35,14 +62,28 @@ SR_API struct sr_trigger *sr_trigger_new(const char *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;
- g_slist_free_full(stage->matches, g_free);
+
+ if (stage->matches)
+ g_slist_free_full(stage->matches, g_free);
}
g_slist_free_full(trig->stages, g_free);
@@ -50,10 +91,27 @@ SR_API void sr_trigger_free(struct sr_trigger *trig)
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);
@@ -61,11 +119,33 @@ SR_API struct sr_trigger_stage *sr_trigger_stage_add(struct sr_trigger *trig)
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 &&
@@ -75,15 +155,18 @@ SR_API int sr_trigger_match_add(struct sr_trigger_stage *stage,
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_FALLING &&
+ 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));
@@ -94,3 +177,5 @@ SR_API int sr_trigger_match_add(struct sr_trigger_stage *stage,
return SR_OK;
}
+
+/** @} */