Date:2010-11-09 00:06:06 (8 years 10 months ago)
Author:Werner Almesberger
Commit:0463bbea5d737f4385ed09997cff6f6ee9232640
Message:Added graphical display of RSSI scan results.

- tools/Makefile.common: CFLAGS and OBJS can now be target-specific
- tools/atspi-rssi/Makefile: add SDL, SDL_gfx, and the GUI object files
when building on the host
- tools/atspi-rssi/Makefile: set -DHAVE_GFX when building on the host
- tools/atspi-rssi/atspi-rssi.c (usage, main): new option -g to use GUI
mode
- tools/atspi-rssi/atspi-rssi.c (main): rename variable for the number of
sweeps in non-GUI mode from "sweeps" to "arg", since it may have other
uses in the future
- tools/atspi-rssi/digit.h, tools/atspi-rssi/digit.c: draw 7 segment style
digits
- tools/atspi-rssi/zgrid.h, tools/atspi-rssi/zgrid.c: display a surface in
faux 3D with SDL/SDL_gfx
- tools/tools/atspi-rssi/gui.h, tools/atspi-rssi/gui.c: display RSSI scans
as a scrolling 3D profile
Files: tools/Makefile.common (2 diffs)
tools/atspi-rssi/Makefile (1 diff)
tools/atspi-rssi/atspi-rssi.c (5 diffs)
tools/atspi-rssi/digit.c (1 diff)
tools/atspi-rssi/digit.h (1 diff)
tools/atspi-rssi/gui.c (1 diff)
tools/atspi-rssi/gui.h (1 diff)
tools/atspi-rssi/zgrid.c (1 diff)
tools/atspi-rssi/zgrid.h (1 diff)

Change Details

tools/Makefile.common
2222
2323CC_host = gcc
2424CC_ben = mipsel-openwrt-linux-gcc
25CFLAGS_host =
26CFLAGS_ben =
2527LDLIBS_host = -lusb
2628LDLIBS_ben =
2729MACROS_host = -DHAVE_USB
...... 
4042    AR = $(AR_quiet)
4143endif
4244
43CFLAGS += -I../../atusb/fw/include -I../include $(MACROS_$(TARGET))
45CFLAGS += $(CFLAGS_$(TARGET)) -I../../atusb/fw/include -I../include \
46          $(MACROS_$(TARGET))
4447LDLIBS = $(LDLIBS_$(TARGET)) -L../lib -latspi
48OBJS += $(OBJS_$(TARGET))
tools/atspi-rssi/Makefile
1414MAIN = atspi-rssi
1515
1616include ../Makefile.common
17
18CFLAGS_host += $(shell sdl-config --cflags)
19MACROS_host += -DHAVE_GFX
20LDLIBS_host += $(shell sdl-config --libs) -lSDL_gfx
21OBJS_host = gui.o zgrid.o digit.o
22
23$(MAIN): $(OBJS_$(TARGET))
tools/atspi-rssi/atspi-rssi.c
2121#include "atspi.h"
2222#include "misctxrx.h"
2323
24#ifdef HAVE_GFX
25#include "gui.h"
26#else
27#define gui(dsc) abort()
28#endif
29
2430
2531static struct timeval t0;
2632static volatile int run = 1;
...... 
5965{
6066    fprintf(stderr,
6167"usage: %s [-n] sweeps\n", name);
68
69#ifdef HAVE_GFX
70    fprintf(stderr,
71"%6s %s -g\n", "", name);
72#endif
73
6274    exit(1);
6375}
6476
...... 
6678int main(int argc, char **argv)
6779{
6880    struct atspi_dsc *dsc;
69    unsigned long sweeps, i;
81    unsigned long arg = 0, i;
7082    char *end;
7183    int c;
84    int graphical = 0;
7285
73    while ((c = getopt(argc, argv, "n")) != EOF)
86    while ((c = getopt(argc, argv, "gn")) != EOF)
7487        switch (c) {
88#ifdef HAVE_GFX
89        case 'g':
90            graphical = 1;
91
92            break;
93#endif
7594        case 'n':
7695            break;
7796        default:
...... 
7998        }
8099
81100    switch (argc-optind) {
101    case 0:
102        if (!graphical)
103            usage(*argv);
104        break;
82105    case 1:
83        sweeps = strtoul(argv[optind], &end, 0);
106        if (graphical)
107            usage(*argv);
108        arg = strtoul(argv[optind], &end, 0);
84109        if (*end)
85110            usage(*argv);
86111        break;
...... 
100125     * We'll wait for the PLL lock after selecting the channel.
101126     */
102127
103    gettimeofday(&t0, NULL);
104    for (i = 0; run && i != sweeps; i++)
105        sweep(dsc);
128    if (graphical)
129        gui(dsc);
130    else {
131        gettimeofday(&t0, NULL);
132        for (i = 0; run && i != arg; i++)
133            sweep(dsc);
134    }
106135
107136    atspi_reg_write(dsc, REG_TRX_STATE, TRX_CMD_TRX_OFF);
108137
tools/atspi-rssi/digit.c
1/*
2 * atspi-rssi/digit.c - Draw 7 segment style digits
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdint.h>
15
16#include "SDL.h"
17#include "SDL_gfxPrimitives.h"
18
19#include "digit.h"
20
21
22static void hlines_3(SDL_Surface *s, int x0, int x1, int y0, int ym, int y1,
23    uint32_t fg)
24{
25    hlineColor(s, x0, x1, y0, fg);
26    hlineColor(s, x0, x1, ym, fg);
27    hlineColor(s, x0, x1, y1, fg);
28}
29
30
31void digit(SDL_Surface *s, int n, int x0, int x1, int y0, int ym, int y1,
32    uint32_t fg)
33{
34    switch (n) {
35    case 8:
36        hlineColor(s, x0, x1, ym, fg);
37        /* fall through */
38    case 0:
39        hlineColor(s, x0, x1, y0, fg);
40        vlineColor(s, x0, y0, y1, fg);
41        /* fall through */
42    case 7:
43        hlineColor(s, x0, x1, y1, fg);
44        vlineColor(s, x1, y0, y1, fg);
45        break;
46
47    case 1:
48        lineColor(s, x0, ym, x1, y1, fg);
49        vlineColor(s, x1, y0, y1, fg);
50        break;
51
52    case 2:
53        hlines_3(s, x0, x1, y0, ym, y1, fg);
54        vlineColor(s, x0, y0, ym, fg);
55        vlineColor(s, x1, ym, y1, fg);
56        break;
57
58    case 9:
59        vlineColor(s, x0, ym, y1, fg);
60        /* fall through */
61    case 3:
62        hlines_3(s, x0, x1, y0, ym, y1, fg);
63        vlineColor(s, x1, y0, y1, fg);
64        break;
65
66    case 4:
67        hlineColor(s, x0, x1, ym, fg);
68        vlineColor(s, x0, ym, y1, fg);
69        vlineColor(s, x1, y0, y1, fg);
70        break;
71
72    case 6:
73        hlines_3(s, x0, x1, y0, ym, y1, fg);
74        vlineColor(s, x0, y0, y1, fg);
75        vlineColor(s, x1, y0, ym, fg);
76        break;
77
78    case 5:
79        hlines_3(s, x0, x1, y0, ym, y1, fg);
80        vlineColor(s, x0, ym, y1, fg);
81        vlineColor(s, x1, y0, ym, fg);
82        break;
83
84    default:
85        abort();
86    }
87}
tools/atspi-rssi/digit.h
1/*
2 * atspi-rssi/digit.h - Draw 7 segment style digits
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#ifndef DIGIT_H
15#define DIGIT_H
16
17#include <stdint.h>
18
19#include "SDL.h"
20
21
22void digit(SDL_Surface *s, int n, int x0, int x1, int y0, int ym, int y1,
23    uint32_t fg);
24
25#endif /* !DIGIT_H */
tools/atspi-rssi/gui.c
1/*
2 * atspi-rssi/gui.c - Graphical output for atspi-rssi
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdlib.h>
15#include <stdio.h>
16#include <unistd.h>
17#include <signal.h>
18#include <sys/time.h>
19
20#include "SDL.h"
21#include "SDL_gfxPrimitives.h"
22
23#include "at86rf230.h"
24#include "atspi.h"
25#include "misctxrx.h"
26
27#include "zgrid.h"
28#include "digit.h"
29#include "gui.h"
30
31
32#define XRES 320
33#define YRES 240
34
35#define N_CHAN 16
36#define N_TIME 64
37
38
39static struct timeval t0;
40
41
42static void shift_grid(int *z, int nx, int ny)
43{
44    int *p1, *p0, *s;
45    int x, y;
46
47    p1 = z+(ny-1)*nx;
48    for (y = 1; y != ny; y++) {
49        p0 = s = p1-nx;
50        for (x = 0; x != nx; x++)
51            *p1++ = *p0++;
52        p1 = s;
53    }
54}
55
56
57static void sweep(struct atspi_dsc *dsc, int *z)
58{
59    int chan;
60
61    for (chan = 11; chan <= 26; chan++) {
62        atspi_reg_write(dsc, REG_PHY_CC_CCA, chan);
63        /* 150 us, according to AVR2001 section 3.5 */
64        wait_for_interrupt(dsc, IRQ_PLL_LOCK, IRQ_PLL_LOCK, 10, 20);
65
66        *z++ = 3*atspi_reg_read(dsc, REG_PHY_RSSI) & RSSI_MASK;
67#if 0
68        if (chan >= 13 && chan <= 19 )
69            z[-1] = 3*28-(chan-16)*(chan-16)*(chan-16)*(chan-16);
70#endif
71    }
72}
73
74
75static void clear(SDL_Surface *s)
76{
77    SDL_FillRect(s, NULL, SDL_MapRGB(s->format, 0, 0, 0));
78}
79
80
81#define CBIG(pos) \
82    x-5+(pos)*6, x-1+(pos)*6, y+8, y+4, y, 0xff4040ff
83#define CSMALL(pos) \
84    x-7+(pos)*4, x-5+(pos)*4, y+15, y+13, y+11, 0x20ff00ff
85
86
87static void label_channels(SDL_Surface *s, int sx, int x0, int y0)
88{
89    int x, y, i, c, f;
90
91    x = x0;
92    y = s->h-y0+4;
93    for (i = 0; i != N_CHAN; i++) {
94        c = i+11;
95        digit(s, c/10, CBIG(0));
96        digit(s, c % 10, CBIG(1));
97        f = 2405+5*i;
98        if (i & 1)
99            y++;
100        digit(s, f/1000, CSMALL(0));
101        digit(s, (f/100) % 10, CSMALL(1));
102        digit(s, (f/10) % 10, CSMALL(2));
103        digit(s, f % 10, CSMALL(3));
104        if (i & 1)
105            y--;
106        x += sx;
107    }
108}
109
110
111void gui(struct atspi_dsc *dsc)
112{
113    SDL_Surface *surf;
114    int z[N_CHAN*N_TIME];
115
116    memset(z, 0, sizeof(z));
117    gettimeofday(&t0, NULL);
118
119    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
120        fprintf(stderr, "SDL_init: %s\n", SDL_GetError());
121        exit(1);
122    }
123    atexit(SDL_Quit);
124
125    surf = SDL_SetVideoMode(XRES, YRES, 0, SDL_SWSURFACE);
126    if (!surf) {
127        fprintf(stderr, "SDL_SetVideoMode: %s\n", SDL_GetError());
128        exit(1);
129    }
130
131    while (1) {
132        shift_grid(z, N_CHAN, N_TIME);
133        sweep(dsc, z);
134
135        SDL_LockSurface(surf);
136
137        clear(surf);
138        zgrid_draw(surf, z, N_CHAN, N_TIME,
139            17, 2, 1,
140            7, 40,
141            0xffffff00, 0x00408080);
142        label_channels(surf, 17, 7, 40);
143
144        SDL_UnlockSurface(surf);
145        SDL_UpdateRect(surf, 0, 0, 0, 0);
146    }
147}
tools/atspi-rssi/gui.h
1/*
2 * atspi-rssi/gui.h - Graphical output for atspi-rssi
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13#ifndef GUI_H
14#define GUI_H
15
16#include "atspi.h"
17
18
19void gui(struct atspi_dsc *dsc);
20
21#endif /* !GUI_H */
tools/atspi-rssi/zgrid.c
1/*
2 * atspi-rssi/zgrid.c - Display a surface in faux 3D with SDL/SDL_gfx
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdint.h>
15#include <stdlib.h>
16#include <stdio.h>
17
18#include "SDL.h"
19#include "SDL_gfxPrimitives.h"
20
21#include "zgrid.h"
22
23
24#define SWAP(x, y) \
25    do { typeof(x) swap_tmp = (x); (x) = (y); (y) = swap_tmp; } while (0)
26
27
28static int *alloc_row(int nx)
29{
30    int *p;
31
32    p = malloc(nx*sizeof(*p));
33    if (p)
34        return p;
35    perror("malloc");
36    exit(1);
37}
38
39
40static void draw_band(SDL_Surface *s, const int *ax, const int *ay,
41    const int *bx, const int *by, int n, uint32_t fg, uint32_t bg)
42{
43    Sint16 px[4], py[4];
44    int i;
45
46    for (i = n-2; i >= 0; i--) {
47        px[0] = ax[i];
48        px[1] = ax[i+1];
49        px[2] = bx[i+1];
50        px[3] = bx[i];
51        py[0] = ay[i];
52        py[1] = ay[i+1];
53        py[2] = by[i+1];
54        py[3] = by[i];
55        filledPolygonColor(s, px, py, 4, bg);
56        aalineColor(s, ax[i+1], ay[i+1], bx[i+1], by[i+1], fg);
57        aalineColor(s, bx[i], by[i], bx[i+1], by[i+1], fg);
58    }
59    aalineColor(s, ax[0], ay[0], bx[0], by[0], fg);
60}
61
62
63static void draw_polyline(SDL_Surface *s, const int *px, const int *py,
64    int n, uint32_t rgba)
65{
66    int i;
67
68    for (i = 0; i != n-1; i++)
69        aalineColor(s, px[i], py[i], px[i+1], py[i+1], rgba);
70}
71
72
73void zgrid_draw(SDL_Surface *s, const int *z, int nx, int ny,
74    int sx, int sy, int sxy, int x0, int y0, uint32_t fg, uint32_t bg)
75{
76    int *lx = alloc_row(nx);
77    int *ly = alloc_row(nx);
78    int *px = alloc_row(nx);
79    int *py = alloc_row(nx);
80    int x, y, yz0;
81    const int *zp;
82    uint8_t a;
83
84    for (y = ny-1; y >= 0; y--) {
85        a = (ny-1-y)*0xe0/(ny-1)+0x1f;
86        yz0 = s->h-y0-y*sy-1;
87        zp = z+y*nx;
88        for (x = 0; x != nx; x++) {
89            px[x] = x0+x*sx+y*sxy;
90            py[x] = yz0-zp[x];
91        }
92        if (y != ny-1) {
93            draw_band(s, px, py, lx, ly, nx, fg | a, bg);
94        }
95        SWAP(px, lx);
96        SWAP(py, ly);
97    }
98    draw_polyline(s, lx, ly, nx, fg | 0xff);
99}
tools/atspi-rssi/zgrid.h
1/*
2 * atspi-rssi/zgrid.h - Display a surface in faux 3D with SDL/SDL_gfx
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13#ifndef ZGRID_H
14#define ZGRID_H
15
16#include <stdint.h>
17
18#include "SDL.h"
19
20
21void zgrid_draw(SDL_Surface *s, const int *z, int nx, int ny,
22    int sx, int sy, int sxy, int x0, int y0, uint32_t fg, uint32_t bg);
23
24#endif /* !ZGRID_H */

Archive Download the corresponding diff file



interactive