Root/tools/atrf-path/gui.c

Source at commit f32f48ae2673b799878a831ba9e216b26177505a created 8 years 6 months ago.
By Werner Almesberger, atrf-path: visualize the limits in GUI mode
1/*
2 * atrf-path/gui.c - Graphical output for atrf-path
3 *
4 * Written 2011 by Werner Almesberger
5 * Copyright 2011 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
18#include "SDL.h"
19#include "SDL_gfxPrimitives.h"
20
21#include "at86rf230.h"
22#include "atrf.h"
23#include "misctxrx.h"
24
25#include "sweep.h"
26#include "gui.h"
27
28
29#define XRES 320
30#define YRES 240
31
32#define FG_RGBA 0xffffffff /* measurement color */
33#define OK_RGBA 0x00ff00ff
34#define OVER_RGBA 0xffff00ff
35#define UNDER_RGBA 0xff0000ff
36#define LIMIT_RGBA 0xff0000ff
37
38#define CHAN_STEP 20 /* 4 pixels/MHz */
39#define SIDE_STEP 2
40#define CHAN_X_OFFSET 10
41#define Y_MIN -94
42#define Y_MAX -10
43
44#define STATUS_X (XRES-15)
45#define STATUS_Y 15
46#define STATUS_R 8
47
48
49static int avg2y(double avg)
50{
51    return YRES-(avg-Y_MIN)/(Y_MAX-Y_MIN)*YRES-1;
52}
53
54
55static void segment(SDL_Surface *s, int *last_x, int *last_y, int x,
56    const struct sample *res, int first)
57{
58    int y = avg2y(res->avg);
59
60    if (!first) {
61        aalineColor(s, *last_x, *last_y, x, y, FG_RGBA);
62}
63    *last_x = x;
64    *last_y = y;
65}
66
67
68static void draw(SDL_Surface *s, const struct sample *res, int cont_tx)
69{
70    int last_x, last_y;
71    int first, x, i;
72
73    x = CHAN_X_OFFSET;
74    first = 1;
75    for (i = 0; i != N_CHAN; i++) {
76        if (cont_tx != CONT_TX_P500K) {
77            segment(s, &last_x, &last_y, x, res, first);
78            first = 0;
79        }
80        res++;
81        x += 2*SIDE_STEP;
82
83        if (cont_tx != CONT_TX_M500K) {
84            segment(s, &last_x, &last_y, x, res, first);
85            first = 0;
86        }
87        res++;
88        x += CHAN_STEP-2*SIDE_STEP;
89    }
90}
91
92
93static void draw_limit(SDL_Surface *s, const double *v)
94{
95    int x, y, i, last = 0;
96
97    x = CHAN_X_OFFSET;
98    for (i = 0; i != N_CHAN; i++) {
99        y = avg2y(*v);
100        if (i)
101            vlineColor(s, x-CHAN_STEP/2, last, y, LIMIT_RGBA);
102        hlineColor(s, x-CHAN_STEP/2, x+CHAN_STEP/2, y, LIMIT_RGBA);
103        last = y;
104        x += CHAN_STEP;
105        v++;
106    }
107}
108
109
110static void indicate(SDL_Surface *s, int fail)
111{
112    static uint32_t last = 0;
113    uint32_t color;
114
115    switch (fail) {
116    case 0:
117        color = OK_RGBA;
118        break;
119    case 1:
120        color = OVER_RGBA;
121        break;
122    case -1:
123        color = UNDER_RGBA;
124        break;
125    default:
126        abort();
127    }
128    if (color == last)
129        color = 0;
130    last = color;
131
132    filledCircleColor(s, STATUS_X, STATUS_Y, STATUS_R, color);
133    aacircleColor(s, STATUS_X, STATUS_Y, STATUS_R, color);
134}
135
136
137static void clear(SDL_Surface *s)
138{
139    SDL_FillRect(s, NULL, SDL_MapRGB(s->format, 0, 0, 0));
140}
141
142
143/* --- temporarily, for optimizing --- */
144
145#include <sys/time.h>
146
147
148static double t0;
149
150
151static double t(void)
152{
153    struct timeval tv;
154
155    gettimeofday(&tv, NULL);
156    return tv.tv_sec+tv.tv_usec/1000000.0;
157}
158
159
160static void tstart(void)
161{
162    t0 = t();
163}
164
165
166static void tstop(void)
167{
168    fprintf(stderr, "%.3f\n", t()-t0);
169}
170
171
172void gui(const struct sweep *sweep, int sweeps)
173{
174    SDL_Surface *surf;
175    SDL_Event event;
176    int cycle = 0;
177    int fail;
178
179    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
180        fprintf(stderr, "SDL_init: %s\n", SDL_GetError());
181        exit(1);
182    }
183    atexit(SDL_Quit);
184
185    surf = SDL_SetVideoMode(XRES, YRES, 0, SDL_SWSURFACE);
186    if (!surf) {
187        fprintf(stderr, "SDL_SetVideoMode: %s\n", SDL_GetError());
188        exit(1);
189    }
190
191    while (cycle != sweeps || !sweeps) {
192        struct sample res[N_CHAN*2];
193
194        while (SDL_PollEvent(&event))
195            if (event.type == SDL_KEYDOWN ||
196                event.type == SDL_QUIT)
197                return;
198        tstart();
199        fail = do_sweep(sweep, res);
200        tstop();
201
202        SDL_LockSurface(surf);
203
204        clear(surf);
205
206        draw_limit(surf, sweep->min);
207        draw_limit(surf, sweep->max);
208        indicate(surf, fail);
209        draw(surf, res, sweep->cont_tx);
210
211        SDL_UnlockSurface(surf);
212        SDL_UpdateRect(surf, 0, 0, 0, 0);
213    }
214}
215

Archive Download this file



interactive