From 0a409103eb9a9003d6142cf5a780c3c9b09dd132 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Tue, 12 Apr 2011 22:52:39 -0300 Subject: [PATCH] atrf-path: added basic graphical output - gui.h, gui.c: plot sweep results with SDL_gfx - Makefile: added target-specific variables for SDL and SDL_gfx - atrf-path.c (main): invoke the GUI if the number of sweeps is zero --- tools/atrf-path/Makefile | 12 ++++ tools/atrf-path/atrf-path.c | 6 +- tools/atrf-path/gui.c | 112 ++++++++++++++++++++++++++++++++++++ tools/atrf-path/gui.h | 27 +++++++++ 4 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 tools/atrf-path/gui.c create mode 100644 tools/atrf-path/gui.h diff --git a/tools/atrf-path/Makefile b/tools/atrf-path/Makefile index 3c91715..c1d3f74 100644 --- a/tools/atrf-path/Makefile +++ b/tools/atrf-path/Makefile @@ -14,3 +14,15 @@ MAIN = atrf-path include ../Makefile.common + +CFLAGS_host += $(shell sdl-config --cflags) +MACROS_host += -DHAVE_GFX +LDLIBS_host += $(shell sdl-config --libs) -lSDL_gfx +OBJS_host = gui.o + +CFLAGS_ben_jlime += $(shell sdl-config --cflags) +MACROS_ben_jlime += -DHAVE_GFX +LDLIBS_ben_jlime += -lSDL -lSDL_gfx +OBJS_ben_jlime = gui.o + +$(MAIN): $(OBJS_$(TARGET)) diff --git a/tools/atrf-path/atrf-path.c b/tools/atrf-path/atrf-path.c index dc816ab..5886e79 100644 --- a/tools/atrf-path/atrf-path.c +++ b/tools/atrf-path/atrf-path.c @@ -21,6 +21,7 @@ #include "cwtest.h" #include "atrf.h" +#include "gui.h" #include "sweep.h" @@ -211,7 +212,10 @@ int main(int argc, char **argv) return 1; sweep.power = 15-power; - do_sweeps(&sweep, sweeps); + if (sweeps) /* @@@ hack */ + do_sweeps(&sweep, sweeps); + else + gui(&sweep); atrf_reg_write(sweep.tx, REG_TRX_STATE, TRX_CMD_TRX_OFF); atrf_reg_write(sweep.rx, REG_TRX_STATE, TRX_CMD_TRX_OFF); diff --git a/tools/atrf-path/gui.c b/tools/atrf-path/gui.c new file mode 100644 index 0000000..4b167b8 --- /dev/null +++ b/tools/atrf-path/gui.c @@ -0,0 +1,112 @@ +/* + * atrf-path/gui.c - Graphical output for atrf-path + * + * Written 2011 by Werner Almesberger + * Copyright 2011 Werner Almesberger + * + * 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 2 of the License, or + * (at your option) any later version. + */ + + +#include +#include +#include + +#include "SDL.h" +#include "SDL_gfxPrimitives.h" + +#include "at86rf230.h" +#include "atrf.h" +#include "misctxrx.h" + +#include "sweep.h" +#include "gui.h" + + +#define XRES 320 +#define YRES 240 + +#define N_CHAN 16 + + +#define FG_RGBA 0xffffffff /* measurement color */ + +#define CHAN_STEP 20 /* 4 pixels/MHz */ +#define SIDE_STEP 2 +#define CHAN_X_OFFSET 10 +#define Y_MIN -94 +#define Y_MAX -10 + + +static void segment(SDL_Surface *s, int *last_x, int *last_y, int x, + const struct sample *res, int have_last) +{ + int y = YRES-(res->avg-Y_MIN)/(Y_MAX-Y_MIN)*YRES-1; + + if (have_last) { + aalineColor(s, *last_x, *last_y, x, y, FG_RGBA); +} + *last_x = x; + *last_y = y; +} + + +static void draw(SDL_Surface *s, const struct sample *res) +{ + int last_x, last_y; + int x, i; + + x = CHAN_X_OFFSET; + for (i = 0; i != N_CHAN; i++) { + segment(s, &last_x, &last_y, x, res++, i); + x += 2*SIDE_STEP; + segment(s, &last_x, &last_y, x, res++, 1); + x += CHAN_STEP-2*SIDE_STEP; + } +} + + +static void clear(SDL_Surface *s) +{ + SDL_FillRect(s, NULL, SDL_MapRGB(s->format, 0, 0, 0)); +} + + +void gui(const struct sweep *sweep) +{ + SDL_Surface *surf; + SDL_Event event; + + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + fprintf(stderr, "SDL_init: %s\n", SDL_GetError()); + exit(1); + } + atexit(SDL_Quit); + + surf = SDL_SetVideoMode(XRES, YRES, 0, SDL_SWSURFACE); + if (!surf) { + fprintf(stderr, "SDL_SetVideoMode: %s\n", SDL_GetError()); + exit(1); + } + + while (1) { + struct sample res[N_CHAN*2]; + + while (SDL_PollEvent(&event)) + if (event.type == SDL_KEYDOWN || + event.type == SDL_QUIT) + return; + do_sweep(sweep, res); + + SDL_LockSurface(surf); + + clear(surf); + draw(surf, res); + + SDL_UnlockSurface(surf); + SDL_UpdateRect(surf, 0, 0, 0, 0); + } +} diff --git a/tools/atrf-path/gui.h b/tools/atrf-path/gui.h new file mode 100644 index 0000000..2dc2288 --- /dev/null +++ b/tools/atrf-path/gui.h @@ -0,0 +1,27 @@ +/* + * atrf-path/gui.h - Graphical output for atrf-path + * + * Written 2011 by Werner Almesberger + * Copyright 2011 Werner Almesberger + * + * 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 2 of the License, or + * (at your option) any later version. + */ + +#ifndef GUI_H +#define GUI_H + +#include + +#include "sweep.h" + + +#ifdef HAVE_GFX +void gui(const struct sweep *sweep); +#else +#define gui(sweep) abort() +#endif + +#endif /* !GUI_H */