block_multiplexer.c 2.91 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
/*
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2011

	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>

Jeroen Vreeken's avatar
Jeroen Vreeken committed
23
#include <controller/controller_block.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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

/*
       inputs      outputs
       nr name     nr name

     ----------------------
     |                    |
  ---| 0  select   0  out |----
     |	                  |
  ---| 1  in0             |
     |	                  |
     | .. ...	          |
     |	                  |
  ---| n  in(n-1)         |
     |	                  |
     ----------------------
     
 */

struct controller_block_private {
	float    out;
	int      n;
	uint32_t *select;
	float    **in;
};

static void calculate(struct controller_block *mul)
{
	struct controller_block_private *priv = mul->private;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
53
54
	int n = priv->n;
	int select = *priv->select;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
55
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
56
57
58
59
	if (select > n)
		select = n;
	
	priv->out = *priv->in[select];
Jeroen Vreeken's avatar
Jeroen Vreeken committed
60
61
62
63
64
65
66
67
68
69
}


struct controller_block * block_multiplexer_create(char *name, va_list ap)
{
	struct controller_block *mul;
	int n, i;
	
	n = va_arg(ap, int);

Jeroen Vreeken's avatar
Jeroen Vreeken committed
70
71
	if (!(mul = controller_block_alloc("multiplexer", name,
	    sizeof(struct controller_block_private))))
Jeroen Vreeken's avatar
Jeroen Vreeken committed
72
73
		return NULL;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
74
	mul->private->n = n - 1;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
	mul->private->out = 0.0;
	
	mul->private->in = calloc(sizeof(float*), n);
	if (!mul->private->in)
		goto err_in;

	mul->inputs = 1 + n;
	mul->input = malloc(sizeof(struct controller_block_interm)*(n+1));
	if (!mul->input)
		goto err_input;
	mul->input[0].name = "select";
	mul->input[0].type = CONTROLLER_BLOCK_TERM_UINT32;
	mul->input[0].value.u32 = &mul->private->select;
	mul->input[0].ghostof = NULL;
	for (i = 0; i < n; i++) {
		mul->input[1+i].name = malloc(16);
		sprintf(mul->input[1+i].name, "in%d", i);
		mul->input[1+i].type = CONTROLLER_BLOCK_TERM_FLOAT;
		mul->input[1+i].value.f = &mul->private->in[i];
		mul->input[1+i].ghostof = NULL;
	}
	
	mul->outputs = 1;
	mul->output = malloc(sizeof(struct controller_block_outterm) * 1);
	if (!mul->output)
		goto err_output;
	mul->output[0].name = "out";
	mul->output[0].type = CONTROLLER_BLOCK_TERM_FLOAT;
	mul->output[0].value.f = &mul->private->out;
	mul->output[0].source = mul;

	mul->calculate = calculate;

	mul->params = 0;
	mul->param = NULL;
	mul->param_get = NULL;
	mul->param_set = NULL;

	controller_block_add(mul);
	return mul;

err_output:
err_input:
	free(mul->private->in);
err_in:
Jeroen Vreeken's avatar
Jeroen Vreeken committed
120
	controller_block_free(mul);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
121
122
	return NULL;
}