Commit d64f5e02 authored by Daan Vreeken's avatar Daan Vreeken
Browse files

Add a 'comparator' block.

The block will output 'true' when input positive >= input negative.

	new file:   block/block_comparator.c
	modified:   block/build.mk
parent 5dcefc23
/*
* Copyright (c) 2014,
* Daan Vreeken <Daan @ Vitsch . nl> - Vitsch Electronics
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS `AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <controller/controller_block.h>
#include <log/log.h>
/*
inputs outputs
nr name nr name
-----------------------------
| |
---| positive out |----
---| negative |
| |
-----------------------------
The output will be set to 'true' when (positive >= negative)
*/
struct controller_block_private {
// inputs
float *positive, *negative;
// outputs
bool out;
};
#define OUTPUT(name) (priv->name)
#define INPUT(name) (*priv->name)
static void calculate(struct controller_block *block)
{
struct controller_block_private *priv;
priv = block->private;
OUTPUT(out) = INPUT(positive) >= INPUT(negative);
}
static struct controller_block_interm_list interms[] = {
{ "positive", CONTROLLER_BLOCK_TERM_FLOAT,
offsetof(struct controller_block_private, positive) },
{ "negative", CONTROLLER_BLOCK_TERM_FLOAT,
offsetof(struct controller_block_private, negative) },
{ NULL },
};
static struct controller_block_outterm_list outterms[] = {
{ "out", CONTROLLER_BLOCK_TERM_BOOL,
offsetof(struct controller_block_private, out) },
{ NULL },
};
static struct controller_block * block_comparator_create(char *name, int argc,
va_list val)
{
struct controller_block *block;
struct controller_block_private *priv;
block = controller_block_alloc("rangecheck", name,
sizeof(struct controller_block_private));
if (block == NULL)
return NULL;
priv = block->private;
OUTPUT(out) = false;
if (controller_block_interm_list_init(block, interms))
goto err_block;
if (controller_block_outterm_list_init(block, outterms))
goto err_block;
block->calculate = calculate;
controller_block_add(block);
return block;
err_block:
controller_block_free(block);
return NULL;
}
BLOCK_CREATE(rangecheck) = {
.create = block_comparator_create,
.args = { NULL },
};
......@@ -40,7 +40,8 @@ BLOCKS := \
trigger \
value \
value_bool \
value_uint32
value_uint32 \
comparator
BLOCKS_IL := \
and2 \
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment