Root/tools/atspi-rssi/gui.c

Source at commit 256da91e0407b82ad12aa63673c58d164ed2403b created 8 years 11 months ago.
By Werner Almesberger, atspi-rssid: for color and geometry, use #defines instead of literals
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
39#define FG_RGBA 0xffffff00 /* grid foreround color */
40#define BG_RGBA 0x00408080 /* grid background color */
41#define CHAN_RGBA 0xff4040ff /* channel number color */
42#define FREQ_RGBA 0x20ff00ff /* frequency color */
43
44#define X_STEP 17 /* grid x step */
45#define Y_STEP 2 /* grid y step */
46#define Z_STEP 3 /* z multiplier */
47#define X_STEP_Y 1 /* x shift for each y step */
48#define X_OFFSET 7 /* x coordinate of lower left grid corner */
49#define Y_OFFSET 40 /* y coordinate of lower left grid corner */
50
51
52static struct timeval t0;
53
54
55static void shift_grid(int *z, int nx, int ny)
56{
57    int *p1, *p0, *s;
58    int x, y;
59
60    p1 = z+(ny-1)*nx;
61    for (y = 1; y != ny; y++) {
62        p0 = s = p1-nx;
63        for (x = 0; x != nx; x++)
64            *p1++ = *p0++;
65        p1 = s;
66    }
67}
68
69
70static void sweep(struct atspi_dsc *dsc, int *z)
71{
72    int chan;
73
74    for (chan = 11; chan <= 26; chan++) {
75        atspi_reg_write(dsc, REG_PHY_CC_CCA, chan);
76        /* 150 us, according to AVR2001 section 3.5 */
77        wait_for_interrupt(dsc, IRQ_PLL_LOCK, IRQ_PLL_LOCK, 10, 20);
78
79        *z++ = Z_STEP*atspi_reg_read(dsc, REG_PHY_RSSI) & RSSI_MASK;
80#if 0
81        if (chan >= 13 && chan <= 19 )
82            z[-1] = 3*28-(chan-16)*(chan-16)*(chan-16)*(chan-16);
83#endif
84    }
85}
86
87
88static void clear(SDL_Surface *s)
89{
90    SDL_FillRect(s, NULL, SDL_MapRGB(s->format, 0, 0, 0));
91}
92
93
94#define CBIG(pos) \
95    x-5+(pos)*6, x-1+(pos)*6, y+8, y+4, y, CHAN_RGBA
96#define CSMALL(pos) \
97    x-7+(pos)*4, x-5+(pos)*4, y+15, y+13, y+11, FREQ_RGBA
98
99
100static void label_channels(SDL_Surface *s, int sx, int x0, int y0)
101{
102    int x, y, i, c, f;
103
104    x = x0;
105    y = s->h-y0+4;
106    for (i = 0; i != N_CHAN; i++) {
107        c = i+11;
108        digit(s, c/10, CBIG(0));
109        digit(s, c % 10, CBIG(1));
110        f = 2405+5*i;
111        if (i & 1)
112            y++;
113        digit(s, f/1000, CSMALL(0));
114        digit(s, (f/100) % 10, CSMALL(1));
115        digit(s, (f/10) % 10, CSMALL(2));
116        digit(s, f % 10, CSMALL(3));
117        if (i & 1)
118            y--;
119        x += sx;
120    }
121}
122
123
124void gui(struct atspi_dsc *dsc)
125{
126    SDL_Surface *surf;
127    int z[N_CHAN*N_TIME];
128    SDL_Event event;
129
130    memset(z, 0, sizeof(z));
131    gettimeofday(&t0, NULL);
132    
133    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
134        fprintf(stderr, "SDL_init: %s\n", SDL_GetError());
135        exit(1);
136    }
137    atexit(SDL_Quit);
138
139    surf = SDL_SetVideoMode(XRES, YRES, 0, SDL_SWSURFACE);
140    if (!surf) {
141        fprintf(stderr, "SDL_SetVideoMode: %s\n", SDL_GetError());
142        exit(1);
143    }
144
145    while (1) {
146        while (SDL_PollEvent(&event))
147            if (event.type == SDL_KEYDOWN ||
148                event.type == SDL_QUIT)
149                return;
150
151        shift_grid(z, N_CHAN, N_TIME);
152        sweep(dsc, z);
153
154        SDL_LockSurface(surf);
155
156        clear(surf);
157        zgrid_draw(surf, z, N_CHAN, N_TIME,
158            X_STEP, Y_STEP, X_STEP_Y,
159            X_OFFSET, Y_OFFSET,
160            FG_RGBA, BG_RGBA);
161        label_channels(surf, X_STEP, X_OFFSET, Y_OFFSET);
162
163        SDL_UnlockSurface(surf);
164        SDL_UpdateRect(surf, 0, 0, 0, 0);
165    }
166}
167

Archive Download this file



interactive