Root/tools/atrf-path/gui.c

Source at commit 436c9faed55cac5f224d31dec2bf608efe580674 created 9 years 5 months ago.
By Werner Almesberger, atrf-path: the GUI is now activated with -g; also changed arguments 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 N_CHAN 16
33
34
35#define FG_RGBA 0xffffffff /* measurement color */
36#define OK_RGBA 0x00ff00ff
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 void segment(SDL_Surface *s, int *last_x, int *last_y, int x,
50    const struct sample *res, int have_last)
51{
52    int y = YRES-(res->avg-Y_MIN)/(Y_MAX-Y_MIN)*YRES-1;
53
54    if (have_last) {
55        aalineColor(s, *last_x, *last_y, x, y, FG_RGBA);
56}
57    *last_x = x;
58    *last_y = y;
59}
60
61
62static void draw(SDL_Surface *s, const struct sample *res)
63{
64    int last_x, last_y;
65    int x, i;
66
67    x = CHAN_X_OFFSET;
68    for (i = 0; i != N_CHAN; i++) {
69        segment(s, &last_x, &last_y, x, res++, i);
70        x += 2*SIDE_STEP;
71        segment(s, &last_x, &last_y, x, res++, 1);
72        x += CHAN_STEP-2*SIDE_STEP;
73    }
74}
75
76
77static void clear(SDL_Surface *s)
78{
79    SDL_FillRect(s, NULL, SDL_MapRGB(s->format, 0, 0, 0));
80}
81
82
83/* --- temporarily, for optimizing --- */
84
85#include <sys/time.h>
86
87
88static double t0;
89
90
91static double t(void)
92{
93    struct timeval tv;
94
95    gettimeofday(&tv, NULL);
96    return tv.tv_sec+tv.tv_usec/1000000.0;
97}
98
99
100static void tstart(void)
101{
102    t0 = t();
103}
104
105
106static void tstop(void)
107{
108    fprintf(stderr, "%.3f\n", t()-t0);
109}
110
111
112void gui(const struct sweep *sweep, int sweeps)
113{
114    SDL_Surface *surf;
115    SDL_Event event;
116    int cycle = 0;
117
118    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
119        fprintf(stderr, "SDL_init: %s\n", SDL_GetError());
120        exit(1);
121    }
122    atexit(SDL_Quit);
123
124    surf = SDL_SetVideoMode(XRES, YRES, 0, SDL_SWSURFACE);
125    if (!surf) {
126        fprintf(stderr, "SDL_SetVideoMode: %s\n", SDL_GetError());
127        exit(1);
128    }
129
130    while (cycle != sweeps || !sweeps) {
131        struct sample res[N_CHAN*2];
132
133        while (SDL_PollEvent(&event))
134            if (event.type == SDL_KEYDOWN ||
135                event.type == SDL_QUIT)
136                return;
137        tstart();
138        do_sweep(sweep, res);
139        tstop();
140
141        SDL_LockSurface(surf);
142
143        clear(surf);
144
145        if (cycle++ & 1) {
146            filledCircleColor(surf, STATUS_X, STATUS_Y, STATUS_R,
147                OK_RGBA);
148            aacircleColor(surf, STATUS_X, STATUS_Y, STATUS_R,
149                OK_RGBA);
150        }
151        draw(surf, res);
152
153        SDL_UnlockSurface(surf);
154        SDL_UpdateRect(surf, 0, 0, 0, 0);
155    }
156}
157

Archive Download this file



interactive