Module: xenomai-abe Branch: analogy Commit: 5727a5aeb07b63d3e76ebc4f0bfe4bd4d3987b7e URL: http://git.xenomai.org/?p=xenomai-abe.git;a=commit;h=5727a5aeb07b63d3e76ebc4f0bfe4bd4d3987b7e
Author: Alexis Berlemont <[email protected]> Date: Sun Oct 31 16:01:24 2010 +0100 analogy: add wf_generate tool --- src/utils/analogy/Makefile.am | 18 +++- src/utils/analogy/wf_facilities.c | 24 ++-- src/utils/analogy/wf_facilities.h | 16 ++-- src/utils/analogy/wf_generate.c | 232 +++++++++++++++++++++++++++++++++++++ 4 files changed, 266 insertions(+), 24 deletions(-) diff --git a/src/utils/analogy/Makefile.am b/src/utils/analogy/Makefile.am index 3d8cb5e..8e7734f 100644 --- a/src/utils/analogy/Makefile.am +++ b/src/utils/analogy/Makefile.am @@ -1,6 +1,13 @@ sbin_PROGRAMS = analogy_config -bin_PROGRAMS = cmd_read cmd_write cmd_bits insn_read insn_write insn_bits +bin_PROGRAMS = \ + cmd_read \ + cmd_write \ + cmd_bits \ + insn_read \ + insn_write \ + insn_bits \ + wf_generate CPPFLAGS = \ @XENO_USER_CFLAGS@ \ @@ -9,11 +16,11 @@ CPPFLAGS = \ LDFLAGS = \ @XENO_USER_LDFLAGS@ -lib_LIBRARIES = libwaveform.a +noinst_HEADERS = wf_facilities.h -libwaveform_a_SOURCES = signal_generation.c +lib_LIBRARIES = libwaveform.a -noinst_HEADERS = signal_generation.h +libwaveform_a_SOURCES = wf_facilities.c analogy_config_SOURCES = analogy_config.c analogy_config_LDADD = \ @@ -58,3 +65,6 @@ insn_bits_LDADD = \ ../../drvlib/analogy/libanalogy.la \ ../../skins/rtdm/librtdm.la \ ../../skins/common/libxenomai.la + +wf_generate_SOURCES = wf_generate.c +wf_generate_LDADD = ./libwaveform.a -lm diff --git a/src/utils/analogy/wf_facilities.c b/src/utils/analogy/wf_facilities.c index ce250e7..08d105e 100644 --- a/src/utils/analogy/wf_facilities.c +++ b/src/utils/analogy/wf_facilities.c @@ -10,7 +10,7 @@ #define PI 3.14159265358979323846 #endif -void a4l_sg_init_sine(struct waveform_config *config, double *values) +void a4l_wf_init_sine(struct waveform_config *config, double *values) { int i; @@ -24,7 +24,7 @@ void a4l_sg_init_sine(struct waveform_config *config, double *values) } } -void a4l_sg_init_sawtooth(struct waveform_config *config, double *values) +void a4l_wf_init_sawtooth(struct waveform_config *config, double *values) { int i; @@ -41,7 +41,7 @@ void a4l_sg_init_sawtooth(struct waveform_config *config, double *values) } } -void a4l_sg_init_triangular(struct waveform_config *config, double *values) +void a4l_wf_init_triangular(struct waveform_config *config, double *values) { int i; @@ -67,7 +67,7 @@ void a4l_sg_init_triangular(struct waveform_config *config, double *values) } } -void a4l_sg_init_steps(struct waveform_config *config, double *values) +void a4l_wf_init_steps(struct waveform_config *config, double *values) { int i; @@ -82,7 +82,7 @@ void a4l_sg_init_steps(struct waveform_config *config, double *values) } } -void a4l_sg_set_sample_count(struct waveform_config *config) +void a4l_wf_set_sample_count(struct waveform_config *config) { int sample_count = MIN_SAMPLE_COUNT; int best_count = MIN_SAMPLE_COUNT; @@ -116,7 +116,7 @@ void a4l_sg_set_sample_count(struct waveform_config *config) config->spl_count = best_count; } -int a4l_sg_check_config(struct waveform_config *config) +int a4l_wf_check_config(struct waveform_config *config) { if (config->wf_amplitude == 0) @@ -134,18 +134,18 @@ int a4l_sg_check_config(struct waveform_config *config) } static void (* init_values[])(struct waveform_config *, double *) = { - a4l_sg_init_sine, - a4l_sg_init_sawtooth, - a4l_sg_init_triangular, - a4l_sg_init_steps, + a4l_wf_init_sine, + a4l_wf_init_sawtooth, + a4l_wf_init_triangular, + a4l_wf_init_steps, }; -void a4l_sg_init_values(struct waveform_config *config, double *values) +void a4l_wf_init_values(struct waveform_config *config, double *values) { init_values[config->wf_kind](config, values); } -void a4l_sg_dump_values(struct waveform_config *config, double *values) +void a4l_wf_dump_values(struct waveform_config *config, double *values) { int i; diff --git a/src/utils/analogy/wf_facilities.h b/src/utils/analogy/wf_facilities.h index 6c99d51..7c9c705 100644 --- a/src/utils/analogy/wf_facilities.h +++ b/src/utils/analogy/wf_facilities.h @@ -24,13 +24,13 @@ struct waveform_config { int spl_count; }; -void a4l_sg_init_sine(struct waveform_config *config, double *values); -void a4l_sg_init_sawtooth(struct waveform_config *config, double *values); -void a4l_sg_init_triangular(struct waveform_config *config, double *values); -void a4l_sg_init_steps(struct waveform_config *config, double *values); -void a4l_sg_set_sample_count(struct waveform_config *config); -int a4l_sg_check_config(struct waveform_config *config); -void a4l_sg_init_values(struct waveform_config *config, double *values); -void a4l_sg_dump_values(struct waveform_config *config, double *values); +void a4l_wf_init_sine(struct waveform_config *config, double *values); +void a4l_wf_init_sawtooth(struct waveform_config *config, double *values); +void a4l_wf_init_triangular(struct waveform_config *config, double *values); +void a4l_wf_init_steps(struct waveform_config *config, double *values); +void a4l_wf_set_sample_count(struct waveform_config *config); +int a4l_wf_check_config(struct waveform_config *config); +void a4l_wf_init_values(struct waveform_config *config, double *values); +void a4l_wf_dump_values(struct waveform_config *config, double *values); #endif /* __SIGNAL_GENERATION_H__ */ diff --git a/src/utils/analogy/wf_generate.c b/src/utils/analogy/wf_generate.c new file mode 100644 index 0000000..40e518b --- /dev/null +++ b/src/utils/analogy/wf_generate.c @@ -0,0 +1,232 @@ +/** + * @file + * Analogy for Linux, test program for waveform generation + * + * @note Copyright (C) 1997-2000 David A. Schleef <[email protected]> + * @note Copyright (C) 2008 Alexis Berlemont <[email protected]> + * + * Xenomai 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 2 of the License, or + * (at your option) any later version. + * + * Xenomai 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 Xenomai; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <errno.h> +#include <getopt.h> +#include <string.h> + +#include "wf_facilities.h" + +void do_print_usage(void) +{ + fprintf(stdout, "usage:\twf_generate [OPTS]\n"); + fprintf(stdout, "\tOPTS:\t -v, --verbose: verbose output\n"); + fprintf(stdout, + "\t\t -t, --type: waveform type " + "(sine, sawtooth, triangular, steps\n"); + fprintf(stdout, "\t\t -f, --frequency: waveform frequency\n"); + fprintf(stdout, "\t\t -a, --amplitude: waveform amplitude\n"); + fprintf(stdout, "\t\t -o, --offset: waveform offet\n"); + fprintf(stdout, "\t\t -s, --sampling-frequency: sampling frequency\n"); + fprintf(stdout, "\t\t -O, --outpout: output file (or stdout)\n"); + fprintf(stdout, "\t\t -h, --help: print this help\n"); +} + +static struct option opts[] = { + {"verbose", no_argument, NULL, 'v'}, + {"type", required_argument, NULL, 't'}, + {"frequency", required_argument, NULL, 'f'}, + {"amplitude", required_argument, NULL, 'a'}, + {"offset", required_argument, NULL, 'o'}, + {"sampling-frequency", required_argument, NULL, 's'}, + {"output", required_argument, NULL, 'O'}, + {"help", no_argument, NULL, 'h'}, + {0}, +}; + +int select_type(struct waveform_config *config, char *arg) +{ + int err = 0; + + if (!strcmp(arg, "sine")) + config->wf_kind = WAVEFORM_SINE; + else if (!strcmp(arg, "sawtooth")) + config->wf_kind = WAVEFORM_SAWTOOTH; + else if (!strcmp(arg, "triangular")) + config->wf_kind = WAVEFORM_TRIANGULAR; + else if (!strcmp(arg, "steps")) + config->wf_kind = WAVEFORM_STEPS; + else { + fprintf(stderr, "Error: type %s is not recognized\n", arg); + err = -EINVAL; + } + + return err; +} + +struct config { + int verbose; + char *filename; + FILE *output; + struct waveform_config wf; +}; + +void cleanup_config(struct config *cfg) +{ + if (cfg->output && strcmp(cfg->filename, "stdout")) { + fclose(cfg->output); + } + +} + +int init_config(struct config *cfg, int argc, char *argv[]) +{ + int err = 0; + + memset(cfg, 0, sizeof(struct config)); + + cfg->wf.wf_kind = WAVEFORM_SINE; + cfg->wf.wf_frequency = 500.0; + cfg->wf.wf_amplitude = 1.0; + cfg->wf.wf_offset = 0.0; + cfg->wf.spl_frequency = 1000.0; + cfg->wf.spl_count = 0; + + while ((err = getopt_long(argc, + argv, "vt:f:a:o:s:O:h", opts, NULL)) >= 0) { + + switch (err) { + + case 'v': + cfg->verbose = 1; + break; + case 't': + err = select_type(&cfg->wf, optarg); + if (err < 0) + goto out; + break; + case 'f': + errno = 0; + cfg->wf.wf_frequency = strtod(optarg, NULL); + if (errno) { + err = -errno; + goto bad_conversion; + } + break; + case 'a': + errno = 0; + cfg->wf.wf_amplitude = strtod(optarg, NULL); + if (errno) { + err = -errno; + goto bad_conversion; + } + break; + case 'o': + errno = 0; + cfg->wf.wf_offset = strtod(optarg, NULL); + if (errno) { + err = -errno; + goto bad_conversion; + } + break; + case 's': + errno = 0; + cfg->wf.spl_frequency = strtod(optarg, NULL); + if (errno) { + err = -errno; + goto bad_conversion; + } + break; + case 'O': + cfg->filename = optarg; + break; + case 'h': + default: + err = -EINVAL; + do_print_usage(); + goto out; + } + } + + err = 0; + + if (cfg->filename != NULL) { + cfg->output = fopen(cfg->filename, "w"); + if (cfg->output == NULL) { + err = -errno; + fprintf(stderr, "%s: %s\n", cfg->filename, strerror(errno)); + goto out; + } + } else { + cfg->output = stdout; + cfg->filename = "stdout"; + } + + if (isatty(fileno(cfg->output))) { + err = -EINVAL; + fprintf(stderr, + "Error: output terminals are not allowed (%s)\n", + cfg->filename); + goto out; + } + +out: + if (err < 0) + cleanup_config(cfg); + + return err; + +bad_conversion: + fprintf(stderr, "Error: bad option(s) value(s)\n"); + do_print_usage(); + return err; +} + +int main(int argc, char *argv[]) +{ + int err = 0; + struct config cfg; + double *values = NULL; + + + err = init_config(&cfg, argc, argv); + + err = a4l_wf_check_config(&cfg.wf); + if (err < 0) + goto out; + + a4l_wf_set_sample_count(&cfg.wf); + + values = malloc(cfg.wf.spl_count * sizeof(double)); + if (!values) { + err = -ENOMEM; + fprintf(stderr, "Error: values allocations failed\n"); + goto out; + } + + a4l_wf_init_values(&cfg.wf, values); + + err = fwrite(values, sizeof(double), cfg.wf.spl_count, cfg.output); + if (err != cfg.wf.spl_count) { + err = -errno; + perror("Error: output file write: )"); + goto out; + } + +out: + cleanup_config(&cfg); + + return err; +} _______________________________________________ Xenomai-git mailing list [email protected] https://mail.gna.org/listinfo/xenomai-git
