block_command_bool.c 2.3 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
/*
	Copyright Jeroen Vreeken (jeroen@vreken.net), 2014

	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 <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <log/log.h>

#include <controller/controller_block.h>
#include <controller/controller_command.h>


struct controller_block_private {
	bool value;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
31
	uint32_t id;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
32
33
34
35
36
37
38
39
40
41
42

	struct controller_command *command;
};

static void command_calculate(struct controller_block *cmd)
{
	struct controller_block_private *priv = cmd->private;
	struct command_entry c_entry;

	if (!controller_command_queue_read(priv->command, &c_entry)) {
		priv->value = c_entry.value.b;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
43
44
45
		priv->id = c_entry.id;
	} else {
		priv->id = COMMAND_ID_NONE;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
46
47
48
49
50
51
	}
}


static struct controller_block_outterm_list outterms[] = {
	{ "value", CONTROLLER_BLOCK_TERM_BOOL,  offsetof(struct controller_block_private, value) },
Jeroen Vreeken's avatar
Jeroen Vreeken committed
52
	{ "id",    CONTROLLER_BLOCK_TERM_UINT32, offsetof(struct controller_block_private, id) },
Jeroen Vreeken's avatar
Jeroen Vreeken committed
53
54
55
56
	{ NULL },
};


57
static struct controller_block * block_command_bool_create(char *name, int argc, va_list val)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
58
59
60
61
62
63
64
{
	struct controller_block *cmd;
	
	if (!(cmd = controller_block_alloc("command_bool", name, sizeof(struct controller_block_private))))
		return NULL;

	cmd->private->value = false;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
65
	cmd->private->id = COMMAND_ID_NONE;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
66
67
68
69
70
71
72
73

	if (controller_block_outterm_list_init(cmd, outterms))
		goto err_block;

	cmd->calculate = command_calculate;

	controller_block_add(cmd);

Jeroen Vreeken's avatar
Jeroen Vreeken committed
74
	cmd->private->command = controller_command_create(cmd, name, "Boolean");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
75
76
77
78
79
80
81
82
83
	cmd->private->command->value_type = COMMAND_VALUE_TYPE_BOOL;
	cmd->private->command->command_types[0] = COMMAND_PTYPE_SETPOINT;

	return cmd;

err_block:
	controller_block_free(cmd);
	return NULL;
}
84
85
86
87
88

BLOCK_CREATE(command_bool) = {
	.create = block_command_bool_create,
	.args = { NULL },
};