Root/tools/atspi-rssi/gui.c

Source at commit 0463bbea5d737f4385ed09997cff6f6ee9232640 created 8 years 11 months ago.
By Werner Almesberger, Added graphical display of RSSI scan results.
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}
148

Archive Download this file



interactive