From 4eb1065dd7f430f37d0e8cc9b137e7dbde6ab4e3 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Thu, 21 Jun 2012 16:21:56 -0300 Subject: [PATCH] tools/: add sampling mode (-S) with graphical output --- tools/Makefile | 8 ++-- tools/antorcha.c | 68 +++++++++++++++++++++++++++---- tools/plot.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++ tools/plot.h | 20 ++++++++++ 4 files changed, 187 insertions(+), 10 deletions(-) create mode 100644 tools/plot.c create mode 100644 tools/plot.h diff --git a/tools/Makefile b/tools/Makefile index c95f5df..6811779 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -15,10 +15,12 @@ MAIN = antorcha CFLAGS = -g -I../fw -I../../ben-wpan/tools/include \ -I../../ben-wpan/atusb/fw/include \ - -Wall -LDLIBS = -L../../ben-wpan/tools/lib -latrf -lusb + -Wall \ + $(shell sdl-config --cflags) +LDLIBS = -L../../ben-wpan/tools/lib -latrf -lusb \ + $(shell sdl-config --libs) -lSDL_gfx -OBJS = antorcha.o hash.o +OBJS = antorcha.o hash.o plot.o .PHONY: all update ping off diff --git a/tools/antorcha.c b/tools/antorcha.c index d0ca4d8..5cb969c 100644 --- a/tools/antorcha.c +++ b/tools/antorcha.c @@ -22,9 +22,11 @@ #include #include -#include #include +#include "hash.h" +#include "plot.h" + static int verbose = 1; static int debug = 0; @@ -275,6 +277,52 @@ static void image(struct atrf_dsc *dsc, const char *name) } +/* ----- Samples ----------------------------------------------------------- */ + + +static void samples(struct atrf_dsc *dsc) +{ + uint8_t buf[MAX_PSDU] = { 0, }; + int got; + uint8_t *s; + int x, y; + + buf[0] = 1; + packet(dsc, SAMPLE, 0, 0, buf, PAYLOAD); + plot_init(); + while (1) { + got = rf_recv(dsc, buf, sizeof(buf)); + if (got <= 3) + continue; + if (buf[0] != SAMPLES) + continue; + if (debug > 1) { + int i; + + for (i = 0; i != got; i++) + fprintf(stderr, " %02x", buf[i]); + fprintf(stderr, "\n"); + } + if (debug) + fprintf(stderr, "%d:", got); + s = buf+3+2; + while (s < buf+got-2) { + s += 2; + x = *s++; + x |= *s++ << 8; + s += 2; + y = *s++; + y |= *s++ << 8; + if (debug) + fprintf(stderr, "\t%d %d\n", x, y); + plot(x, y); + } + } + buf[0] = 0; + packet(dsc, SAMPLE, 0, 0, buf, PAYLOAD); +} + + /* ----- Command-line processing ------------------------------------------- */ @@ -284,7 +332,8 @@ static void usage(const char *name) "usage: %s [-d] image_file\n" "%6s %s [-d] -F firmware_file\n" "%6s %s [-d] -P\n" - , name, "", name, "", name); + "%6s %s [-d] -S\n" + , name, "", name, "", name, "", name); exit(1); } @@ -292,14 +341,14 @@ static void usage(const char *name) int main(int argc, char **argv) { const char *fw = NULL; - int do_ping = 0; + int do_ping = 0, do_sample = 0; struct atrf_dsc *dsc; int c; - while ((c = getopt(argc, argv, "dF:P")) != EOF) + while ((c = getopt(argc, argv, "dF:PS")) != EOF) switch (c) { case 'd': - debug = 1; + debug++; break; case 'F': fw = optarg; @@ -307,13 +356,16 @@ int main(int argc, char **argv) case 'P': do_ping = 1; break; + case 'S': + do_sample = 1; + break; default: usage(*argv); } - if (do_ping && fw) + if (do_ping+do_sample+!!fw > 1) usage(*argv); - if (do_ping || fw) { + if (do_ping || do_sample || fw) { if (argc != optind) usage(*argv); } else { @@ -328,6 +380,8 @@ int main(int argc, char **argv) rf_init(dsc, 8, 15); if (do_ping) ping(dsc); + else if (do_sample) + samples(dsc); else if (fw) firmware(dsc, fw); else diff --git a/tools/plot.c b/tools/plot.c new file mode 100644 index 0000000..6e61390 --- /dev/null +++ b/tools/plot.c @@ -0,0 +1,101 @@ +/* + * tools/plot.c - Sample plot + * + * Written 2012 by Werner Almesberger + * Copyright 2012 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 "SDL.h" +#include "SDL_gfxPrimitives.h" + + +#define XRES 1024 +#define YRES 1024 + +#define RADIUS 3 + +#define OLD_RGBA 0xffffff20 +#define NEW_RGBA 0xffffffff + + +static SDL_Surface *surf; +static int first = 1; +static int last_x, last_y; + +static SDL_Surface *back; +static SDL_Rect back_rect = { + .x = 0, + .y = 0, + .w = 2*RADIUS+1, + .h = 2*RADIUS+1, +}; + +static SDL_Rect front_rect = { + .w = 2*RADIUS+1, + .h = 2*RADIUS+1, +}; + + +void plot(int x, int y) +{ + if (!first) { + SDL_BlitSurface(back, &back_rect, surf, &front_rect); + SDL_LockSurface(surf); + filledCircleColor(surf, last_x, last_y, RADIUS, OLD_RGBA); + SDL_UnlockSurface(surf); + } + + front_rect.x = x; + front_rect.y = y; + x += RADIUS; + y += RADIUS; + + SDL_BlitSurface(surf, &front_rect, back, &back_rect); + SDL_LockSurface(surf); + filledCircleColor(surf, x, y, RADIUS, NEW_RGBA); + SDL_UnlockSurface(surf); + + if (!first) + SDL_UpdateRect(surf, + last_x-RADIUS, last_y-RADIUS, 2*RADIUS+1, 2*RADIUS+1); + SDL_UpdateRect(surf, x-RADIUS, y-RADIUS, 2*RADIUS+1, 2*RADIUS+1); + + first = 0; + last_x = x; + last_y = y; +} + + +void plot_init(void) +{ + const SDL_PixelFormat *f; + + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + fprintf(stderr, "SDL_init: %s\n", SDL_GetError()); + exit(1); + } + atexit(SDL_Quit); + + surf = SDL_SetVideoMode(XRES+2*RADIUS+1, YRES+2*RADIUS+1, 0, + SDL_SWSURFACE); + if (!surf) { + fprintf(stderr, "SDL_SetVideoMode: %s\n", SDL_GetError()); + exit(1); + } + + f = surf->format; + back = SDL_CreateRGBSurface(SDL_SWSURFACE, 2*RADIUS+1, 2*RADIUS+1, + f->BitsPerPixel, f->Rmask, f->Gmask, f->Bmask, f->Amask); + if (!back) { + fprintf(stderr, "SDL_SetVideoMode: %s\n", SDL_GetError()); + exit(1); + } +} diff --git a/tools/plot.h b/tools/plot.h new file mode 100644 index 0000000..b2aea85 --- /dev/null +++ b/tools/plot.h @@ -0,0 +1,20 @@ +/* + * tools/plot.h - Sample plot + * + * Written 2012 by Werner Almesberger + * Copyright 2012 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 PLOT_H +#define PLOT_H + +void plot(int x, int y); +void plot_init(void); + +#endif /* !PLOT_H */