Commit 03f528e2 authored by Jeroen Vreeken's avatar Jeroen Vreeken
Browse files

extra bool player block

make tracediff capable of triggering commands
parent d9cc90d9
......@@ -28,8 +28,10 @@
#include <trace/trace.h>
struct timespec t_int;
char *tracename;
static struct timespec t_int;
static char *tracename;
static bool do_exec = false;
static char **exec_arg = NULL;
static void handler_interval(struct trace *trace,
struct timespec *interval, enum trace_interval_type type)
......@@ -42,40 +44,57 @@ static void handler_interval(struct trace *trace,
static void handler_value(struct trace *trace, int channel,
struct trace_value *value)
{
bool is_true = false;
switch (trace->type) {
case TRACE_VALUE_TYPE_FLOAT:
printf("%ld.%09ld %e\n",
value->t.tv_sec, value->t.tv_nsec, value->value.f);
is_true = value->value.f == 0.0;
break;
case TRACE_VALUE_TYPE_BOOL:
printf("%ld.%09ld %d\n",
value->t.tv_sec, value->t.tv_nsec, value->value.b);
is_true = value->value.b;
break;
case TRACE_VALUE_TYPE_UINT8:
printf("%ld.%09ld %u\n",
value->t.tv_sec, value->t.tv_nsec, value->value.u8);
is_true = value->value.u8;
break;
case TRACE_VALUE_TYPE_UINT16:
printf("%ld.%09ld %u\n",
value->t.tv_sec, value->t.tv_nsec, value->value.u16);
is_true = value->value.u16;
break;
case TRACE_VALUE_TYPE_UINT32:
printf("%ld.%09ld %u\n",
value->t.tv_sec, value->t.tv_nsec, value->value.u32);
is_true = value->value.u32;
break;
case TRACE_VALUE_TYPE_SINT8:
printf("%ld.%09ld %d\n",
value->t.tv_sec, value->t.tv_nsec, value->value.s8);
is_true = value->value.s8;
break;
case TRACE_VALUE_TYPE_SINT16:
printf("%ld.%09ld %d\n",
value->t.tv_sec, value->t.tv_nsec, value->value.s16);
is_true = value->value.s16;
break;
case TRACE_VALUE_TYPE_SINT32:
printf("%ld.%09ld %d\n",
value->t.tv_sec, value->t.tv_nsec, value->value.s32);
is_true = value->value.s32;
break;
}
if (is_true && do_exec) {
if (!fork()) {
printf("Starting %s\n", exec_arg[0]);
execvp(exec_arg[0], exec_arg);
}
}
}
int main(int argc, char **argv)
......@@ -89,6 +108,11 @@ int main(int argc, char **argv)
return 0;
}
if (argc > 3) {
do_exec = true;
exec_arg = &argv[4];
}
tracename = argv[3];
trace = trace_open(argv[1], atoi(argv[2]));
......
/*
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>
#include <controller/controller_block.h>
#include <controller/controller_time.h>
#include <controller/controller_mem.h>
#include <block/block_boolplayer.h>
#include <log/log.h>
/*
inputs outputs
nr name type nr name type
------------------------------------------
| |
---| 0 start bool 0 value bool |----
| |
---| 1 stop bool 1 playing bool |----
| |
---| 2 select int 2 done bool |----
| |
------------------------------------------
*/
struct controller_block_private {
bool *start;
bool *stop;
uint32_t *select;
bool value;
bool playing;
bool done;
bool reset_on_stop;
int cur;
float playtime;
bool err;
int nr;
struct block_boolplayer_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_boolplayer_param *traject;
if (*priv->stop) {
priv->playing = false;
playtime = 0.0;
priv->done = false;
if (priv->reset_on_stop) {
priv->value = 0.0;
}
} else if (priv->playing) {
traject = priv->trajectories[priv->cur];
playtime += controller_time_period_get(player->time);
if (priv->playtime >= traject->time[traject->len - 1]) {
priv->playing = false;
priv->done = true;
priv->value = traject->value[traject->len - 1];
playtime = 0.0;
} else {
int t1;
for (t1 = 0; t1 < traject->len; t1++) {
if (traject->time[t1] >= priv->playtime) {
t1--;
break;
}
}
priv->value = traject->value[t1];
priv->playing = true;
priv->done = false;
}
} else {
if (*priv->start) {
int cur = findtraject(priv, *priv->select);
if (cur >= priv->nr) {
if (!priv->err)
log_send(LOG_T_ERROR,
"%s: Illegal select, trajectory %"PRId32" not available!",
player->name, *priv->select);
priv->err = true;
} else {
priv->cur = cur;
traject = priv->trajectories[cur];
priv->playing = true;
priv->done = false;
playtime = 0.0;
priv->value = traject->value[0];
priv->err = false;
}
}
}
priv->playtime = playtime;
}
static int param_set_traject(struct controller_block *player, char *param,
int argc, va_list ap)
{
struct controller_block_private *priv = player->private;
int nr;
int traject_nr = va_arg(ap, int);
int traject_len = va_arg(ap, int);
int *traject_value = va_arg(ap, int *);
float *traject_time = va_arg(ap, float *);
nr = findtraject(priv, traject_nr);
if (nr == priv->nr) {
priv->trajectories = controller_mem_realloc(
CONTROLLER_MEM_PERIODIC_READ,
priv->trajectories,
sizeof(struct block_boolplayer_param *) *
priv->nr + 1);
priv->trajectories[priv->nr] = controller_mem_calloc(
CONTROLLER_MEM_PERIODIC_READ, 1,
sizeof(struct block_boolplayer_param));
priv->nr++;
priv->trajectories[nr]->nr = traject_nr;
}
priv->trajectories[nr]->len = traject_len;
priv->trajectories[nr]->value = controller_mem_realloc(CONTROLLER_MEM_PERIODIC_READ,
priv->trajectories[nr]->value, sizeof(bool) * traject_len);
priv->trajectories[nr]->time = controller_mem_realloc(CONTROLLER_MEM_PERIODIC_READ,
priv->trajectories[nr]->time, sizeof(float) * traject_len);
int i;
for (i = 0; i < traject_len; i++) {
priv->trajectories[nr]->value[i] = traject_value[i];
}
memcpy(priv->trajectories[nr]->time, traject_time, sizeof(float) * traject_len);
return 0;
}
static int param_set_reset_on_stop(struct controller_block *player,
char *param, int argc, va_list ap)
{
struct controller_block_private *priv = player->private;
priv->reset_on_stop = va_arg(ap, int);
return 0;
}
static struct controller_block_param_list params[] = {
{ "traject", true, param_set_traject, .args = { "int,int,int*,float*", NULL } },
{ "reset_on_stop", true, param_set_reset_on_stop, .args = { "int", NULL } },
{ NULL },
};
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[] = {
{ "value", CONTROLLER_BLOCK_TERM_BOOL, offsetof(struct controller_block_private, value) },
{ "playing", CONTROLLER_BLOCK_TERM_BOOL, offsetof(struct controller_block_private, playing) },
{ "done", CONTROLLER_BLOCK_TERM_BOOL, offsetof(struct controller_block_private, done) },
{ NULL }
};
struct controller_block * block_boolplayer_create(char *name, int argc,
va_list ap)
{
struct controller_block *player;
if (!(player = controller_block_alloc("boolplayer", name, sizeof(struct controller_block_private))))
return NULL;
player->private->playing = false;
player->private->done = false;
player->private->value = false;
player->private->reset_on_stop = false;
player->private->nr = 0;
player->private->cur = 0;
player->private->err = false;
player->private->trajectories = NULL;
if (controller_block_interm_list_init(player, interms))
goto err_block;
if (controller_block_outterm_list_init(player, outterms))
goto err_block;
player->calculate = calculate;
if (controller_block_param_list_add(player, params))
goto err_block;
if (controller_block_add(player))
goto err_block;
return player;
err_block:
controller_block_free(player);
return NULL;
}
BLOCK_CREATE(boolplayer) = {
.create = block_boolplayer_create,
.args = { NULL },
};
/*
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/>.
*/
#ifndef _INCLUDE_BLOCK_BOOLPLAYER_
#define _INCLUDE_BLOCK_BOOLPLAYER_
#include <controller/controller_block.h>
struct controller_block * block_boolplayer_create(char *name, int argc,
va_list ap);
struct block_boolplayer_param {
int nr;
int len;
bool *value;
float *time;
};
#endif /* _INCLUDE_BLOCK_BOOLPLAYER_ */
......@@ -7,6 +7,7 @@ BLOCKS := \
add \
and \
bool2float \
boolplayer \
bridge_pwm \
command_bool \
command_float \
......
Markdown is supported
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