controller_block.h 7.8 KB
Newer Older
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2007, 2009, 2013
	Copyright Stichting C.A. Muller Radioastronomiestation, 2007, 2009, 2013

	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/>.
 */

#ifndef _INCLUDE_CONTROLLER_BLOCK_H_
#define _INCLUDE_CONTROLLER_BLOCK_H_

#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdarg.h>

enum controller_block_term_type {
	CONTROLLER_BLOCK_TERM_VOID, /* Special case for checks... do NOT use! */
	CONTROLLER_BLOCK_TERM_FLOAT,
	CONTROLLER_BLOCK_TERM_BOOL,
	CONTROLLER_BLOCK_TERM_UINT8,
	CONTROLLER_BLOCK_TERM_UINT16,
	CONTROLLER_BLOCK_TERM_UINT32,
	CONTROLLER_BLOCK_TERM_SINT8,
	CONTROLLER_BLOCK_TERM_SINT16,
	CONTROLLER_BLOCK_TERM_SINT32,
};


/* Each off the inputs and outputs of the block is described with this struct.
   For inputs: the value pointer pointer has to be filled in when building the
               network.
   For outputs: the value pointer is filled at block creation.
 */
struct controller_block_outterm {
	char *name;
	enum controller_block_term_type type;
	union {
		void     *v;
		float    *f;
		bool     *b;
		uint8_t  *u8;
		uint16_t *u16;
		uint32_t *u32;
		int8_t   *s8;
		int16_t  *s16;
		int32_t  *s32;
	} value;
	struct controller_block *source;
	
	/* terminal of source (if not this one) */
	struct controller_block_outterm *sourceterm;
};

struct controller_block_interm {
	char *name;
	enum controller_block_term_type type;
	union {
		void     **v;
		float    **f;
		bool     **b;
		uint8_t  **u8;
		uint16_t **u16;
		uint32_t **u32;
		int8_t   **s8;
		int16_t  **s16;
		int32_t  **s32;
	} value;
	struct controller_block *otherside;
	struct controller_block_interm *ghostof;

	/* link back to owning block */
	struct controller_block *block;
};

/* List of interms which must be created in the block's input pointer.
   This structure and its helper functions can only be used for normal
   inputs which have no 'ghosts' and which are located in the private
   structure.
   
   name:        name of the input terminal
   type:        type of the input terminal
   priv_offset: offset within the private structure where the target
                pointer is located.
 */
struct controller_block_interm_list {
	char *name;
	enum controller_block_term_type type;
	size_t priv_offset;
};

/* List of outterms which must be created in the block's output pointer.
   This structure and its helper functions can only be used for normal
   outputs which have no 'ghosts' and which are located in the private
   structure.

   name:        name of the output terminal
   type:        type of the output terminal
   priv_offset: offset within the private structure where the source
                pointer is located.
 */
struct controller_block_outterm_list {
	char *name;
	enum controller_block_term_type type;
	size_t priv_offset;
};

/* Starting point for a block.
   Additional data a block might need can be pointed to by the private pointer.
 */
struct controller_block {
	char *name;	/* instance name this block */
	char *type;	/* generic type of the block */
	char *context;	/* Which context 'owns' this block */

	/* List of inputs */
	int inputs;
	struct controller_block_interm *input;

	/* List of outputs */
	int outputs;
	struct controller_block_outterm *output;

	/* List of params */
	int params;
	struct controller_block_param_list *param;

	void (*calculate)(struct controller_block *);

	void (*param_get)(struct controller_block *, int, void *);
	void (*param_set)(struct controller_block *, int, va_list);
	
	/* Incomplete type for private data */
	struct controller_block_private *private;
};

struct controller_trace {
	void *ptr;
	enum controller_block_term_type type;
	float *buffer;
	size_t len;
	size_t wr_pos;
	size_t rd_pos;
};

struct controller_block_link {
	char *outblock;
	char *outterm;
	char *inblock;
	char *interm;
	int chain;
	char *type;
};

/* 64bit continuous counter */
extern uint64_t controller_time_nseconds;

extern uint32_t controller_time_seconds;
extern uint32_t controller_time_samplenr;
extern uint32_t controller_samplenr;

int controller_block_create(char *type, char *name, va_list ap);
void controller_block_add(struct controller_block *newblock);
struct controller_block *controller_block_find(char *name);
int controller_block_nr(void);
struct controller_block *controller_block_get(int nr);
struct controller_block_outterm *controller_block_find_outterm(
    struct controller_block *block, char *output);

int controller_block_add_float_input(struct controller_block *blk,
    char *name, float **ptr);
int controller_block_add_bool_input(struct controller_block *blk,
    char *name, bool **ptr);
int controller_block_add_sint16_input(struct controller_block *blk,
    char *name, int16_t **ptr);
int controller_block_add_sint32_input(struct controller_block *blk,
    char *name, int32_t **ptr);

int controller_block_add_float_output(struct controller_block *blk,
    char *name, float *ptr);
int controller_block_add_bool_output(struct controller_block *blk, char *name,
    bool *ptr);
int controller_block_add_uint16_output(struct controller_block *blk,
    char *name, uint16_t *ptr);
int controller_block_add_sint32_output(struct controller_block *blk,
    char *name, int32_t *ptr);
int controller_block_add_sint16_output(struct controller_block *blk,
    char *name, int16_t *ptr);

int controller_block_interm_list_init(struct controller_block *block,
    struct controller_block_interm_list *list);
int controller_block_outterm_list_init(struct controller_block *block,
    struct controller_block_outterm_list *list);

int controller_block_output_get_float(char *block, char *param, float *value);

int controller_block_connect(char *outblock, char *outterm,
    char *inblock, char *interm, int chain);
int controller_block_link(void);

struct controller_block_link *controller_block_link_get(int nr);
int controller_block_link_nr(void);

int controller_block_sample_init(void);

void controller_block_calculate(void);

char *controller_block_context_get(void);
void controller_block_context_set(char *new_context);

/* Helper function to create a new block */
struct controller_block *controller_block_alloc(char *type, char *name,
    size_t private_size);
void controller_block_free(struct controller_block *blk);


#define RPM2RADS(val) ((val)*2.0*M_PI/60.0)
#define RADS2RPM(val) ((val)*60.0/(2.0*M_PI))
#define DEG2RAD(val)  ((val)*2.0*M_PI/360.0)
#define RAD2DEG(val)  ((val)*360.0/(2.0*M_PI))


/*
	controller_block_param
 */

/* List of params which must be initialized in the block's param pointer */
struct controller_block_param_list {
	char *name;
	bool sample;	/* True: all access to params must be 'sample safe' */
};

void controller_block_param_init(void);
int controller_block_param_list_init(struct controller_block *block,
    struct controller_block_param_list *list);
int controller_block_param_set(char *block, char *param, va_list);
int controller_block_param_get(char *block, char *param, void *value);
void controller_block_param_handle(void);

/*
	controller_block_trace
 */

void controller_block_trace_init(int max);
int controller_block_trace_add(char *block, char *outterm,
    struct controller_trace *trace);
void controller_block_trace_del(struct controller_trace *trace);
void controller_block_trace_wait(void);

void controller_block_trace(void);

#endif