block_trajectplayer.c 6.8 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
/*
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2011
	Copyright Stichting C.A. Muller Radioastronomiestation, 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
24
25
26
27
#include <controller/controller_block.h>
#include <controller/controller_sample.h>
#include <block/block_trajectplayer.h>
#include <log/log.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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

/*
       inputs               outputs
       nr name   type       nr name     type

     ------------------------------------------
     |                                        |
  ---| 0  start  bool       0  position float |----
     |	                                      |
  ---| 1  stop   bool       1  playing  bool  |----
     |                                        |
  ---| 2  select int        2  done     bool  |----
     |	                                      |
     ------------------------------------------
     
 */

struct controller_block_private {
	bool *start;
	bool *stop;
	uint32_t *select;
	
	float position;
	bool playing;
	bool done;
	
	bool reset_on_stop;
	
	int cur;
	float playtime;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
58
	bool err;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
	
	int nr;
	struct block_trajectplayer_param **trajectories;
};

static int findtraject(struct controller_block_private *priv, int select)
{
	int i;
	
	for (i = 0; i < priv->nr; i++) {
		if (priv->trajectories[i]->nr == select)
			break;
	}
	return i;
}

static void calculate(struct controller_block *player)
{     
	struct controller_block_private *priv = player->private;
	double playtime = priv->playtime;
	struct block_trajectplayer_param *traject;


	if (*priv->stop) {
		priv->playing = false;
		playtime = 0.0;
		priv->done = false;
		if (priv->reset_on_stop) {
			priv->position = 0.0;
		}
	} else if (priv->playing) {
		traject = priv->trajectories[priv->cur];
		playtime += controller_sample_period();
		if (priv->playtime >= traject->time[traject->len - 1]) {
			priv->playing = false;
			priv->done = true;
			priv->position = traject->position[traject->len - 1];
			playtime = 0.0;
		} else {
			int t1, t2;
			float posdiff;
			float tdiff;
			float tinter;
			
			for (t1 = 0; t1 < traject->len; t1++) {
				if (traject->time[t1] >= priv->playtime) {
					t1--;
					break;
				}
			}
			if (t1 < 0)
				t1 = 0;
			t2 = t1 + 1;
			if (t2 >= traject->len)
				t2 = traject->len;
			tdiff = traject->time[t2] - traject->time[t1];
			tinter = playtime - traject->time[t1];
			posdiff = traject->position[t2] - traject->position[t1];
			priv->position =
			    traject->position[t1] + posdiff * tinter / tdiff;
			priv->playing = true;
			priv->done = false;
		}
	} else {
		if (*priv->start) {
			int cur = findtraject(priv, *priv->select);
			if (cur >= priv->nr) {
Jeroen Vreeken's avatar
Jeroen Vreeken committed
126
127
128
129
130
				if (!priv->err)
					log_send(LOG_T_ERROR,
					     "%s: Illegal select, trajectory %d not available!", 
					     player->name, *priv->select);
				priv->err = true;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
131
132
133
134
135
136
137
			} else {
				priv->cur = cur;
				traject = priv->trajectories[cur];
				priv->playing = true;
				priv->done = false;
				playtime = 0.0;
				priv->position = traject->position[0];
Jeroen Vreeken's avatar
Jeroen Vreeken committed
138
				priv->err = false;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
			}
		}
	}
	
	priv->playtime = playtime;
}

static struct controller_block_param_list params[] = {
	{ "traject",       true },
	{ "reset_on_stop", true },
	{ NULL },
};

static void param_get(struct controller_block *player, int param, void *val)
{
	//TODO
}

static void param_set(struct controller_block *player, int param, va_list ap)
{
	struct controller_block_private *priv = player->private;
	struct block_trajectplayer_param traject;
	int traject_nr;
	
	switch (param) {
		case 0: {
			traject.nr = va_arg(ap, int);
			traject.len = va_arg(ap, int);
			traject.position = va_arg(ap, float *);
			traject.time = va_arg(ap, float *);
			
			traject_nr = findtraject(priv, traject.nr);
			
			if (traject_nr == priv->nr) {
				priv->trajectories = realloc(priv->trajectories,
				    sizeof(struct block_trajectplayer_param *) *
Jeroen Vreeken's avatar
Jeroen Vreeken committed
175
				    priv->nr + 1);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
				priv->trajectories[priv->nr] = calloc(
				    sizeof(struct block_trajectplayer_param), 1);
				priv->nr++;
				priv->trajectories[traject_nr]->nr = traject.nr;
			}
			priv->trajectories[traject_nr]->len = traject.len;
			priv->trajectories[traject_nr]->position = realloc(
			    priv->trajectories[traject_nr]->position,
			    sizeof(float) * traject.len);
			priv->trajectories[traject_nr]->time = realloc(
			    priv->trajectories[traject_nr]->time,
			    sizeof(float) * traject.len);
			memcpy(priv->trajectories[traject_nr]->position,
			    traject.position, sizeof(float) * traject.len);
			memcpy(priv->trajectories[traject_nr]->time,
			    traject.time, sizeof(float) * traject.len);
			
			
			break;
		}
		case 1: {
			priv->reset_on_stop = va_arg(ap, int);
			break;
		}
		default:
			break;
	}
}
Jeroen Vreeken's avatar
Jeroen Vreeken committed
204
205
206
207
208
209
210
211
212
213
214
215
216
217
static struct controller_block_interm_list interms[] = {
	{ "start",  CONTROLLER_BLOCK_TERM_BOOL,   offsetof(struct controller_block_private, start)  },
	{ "stop",   CONTROLLER_BLOCK_TERM_BOOL,   offsetof(struct controller_block_private, stop)   },
	{ "select", CONTROLLER_BLOCK_TERM_UINT32, offsetof(struct controller_block_private, select) },
	{ NULL }
};

static struct controller_block_outterm_list outterms[] = {
	{ "position", CONTROLLER_BLOCK_TERM_FLOAT, offsetof(struct controller_block_private, position) },
	{ "playing",  CONTROLLER_BLOCK_TERM_BOOL,  offsetof(struct controller_block_private, playing)  },
	{ "done",     CONTROLLER_BLOCK_TERM_BOOL,  offsetof(struct controller_block_private, done)     },
	{ NULL }
};

Jeroen Vreeken's avatar
Jeroen Vreeken committed
218
219
220
221
222

struct controller_block * block_trajectplayer_create(char *name)
{
	struct controller_block *player;
	
Jeroen Vreeken's avatar
Jeroen Vreeken committed
223
	if (!(player = controller_block_alloc("trajectplayer", name, sizeof(struct controller_block_private))))
Jeroen Vreeken's avatar
Jeroen Vreeken committed
224
225
226
227
228
229
230
231
232
		return NULL;

	player->private->playing = false;
	player->private->done = false;
	player->private->position = 0.0;

	player->private->reset_on_stop = false;
	player->private->nr = 0;
	player->private->cur = 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
233
	player->private->err = false;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
234
235
	player->private->trajectories = NULL;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
236
237
	if (controller_block_interm_list_init(player, interms))
		goto err_block;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
238

Jeroen Vreeken's avatar
Jeroen Vreeken committed
239
240
	if (controller_block_outterm_list_init(player, outterms))
		goto err_block;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
241
242
243
244

	player->calculate = calculate;

	if (controller_block_param_list_init(player, params))
Jeroen Vreeken's avatar
Jeroen Vreeken committed
245
		goto err_block;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
246
247
248
249
250
251
252

	player->param_get = param_get;
	player->param_set = param_set;

	controller_block_add(player);
	return player;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
253
254
err_block:
	controller_block_free(player);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
255
256
	return NULL;
}