IEEE 802.15.4 subsystem
Sign in or create your account | Project List | Help
IEEE 802.15.4 subsystem Git Source Tree
Root/
Source at commit 6ca63fc6e7d0bac9166945da4e2baa3c73b2a7cc created 12 years 9 months ago. By Werner Almesberger, atrf-path: accept keyboard input both from stdin and SDL | |
---|---|
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 | #include <string.h> |
18 | #include <termios.h> |
19 | #include <fcntl.h> |
20 | #include <errno.h> |
21 | |
22 | #include "SDL.h" |
23 | #include "SDL_gfxPrimitives.h" |
24 | |
25 | #include "at86rf230.h" |
26 | #include "atrf.h" |
27 | #include "misctxrx.h" |
28 | |
29 | #include "sweep.h" |
30 | #include "gui.h" |
31 | |
32 | |
33 | #define XRES 320 |
34 | #define YRES 240 |
35 | |
36 | #define FG_RGBA 0xffffffff /* measurement color */ |
37 | #define OK_RGBA 0x00ff00ff |
38 | #define OVER_RGBA 0xffff00ff |
39 | #define UNDER_RGBA 0xff0000ff |
40 | #define LIMIT_RGBA 0xff0000ff |
41 | #define DUMP_RGBA 0x4040ffff |
42 | |
43 | #define CHAN_STEP 20 /* 4 pixels/MHz */ |
44 | #define SIDE_STEP 2 |
45 | #define CHAN_X_OFFSET 10 |
46 | #define Y_MIN -94 |
47 | #define Y_MAX -10 |
48 | |
49 | #define STATUS_X (XRES-15) |
50 | #define STATUS_Y 15 |
51 | #define STATUS_R 8 |
52 | |
53 | |
54 | static struct sample **dumps = NULL; |
55 | static int n_dumps = 0; |
56 | |
57 | |
58 | static int avg2y(double avg) |
59 | { |
60 | return YRES-(avg-Y_MIN)/(Y_MAX-Y_MIN)*YRES-1; |
61 | } |
62 | |
63 | |
64 | static void segment(SDL_Surface *s, int *last_x, int *last_y, int x, |
65 | const struct sample *res, int first, uint32_t color) |
66 | { |
67 | int y = avg2y(res->avg); |
68 | |
69 | if (!first) { |
70 | aalineColor(s, *last_x, *last_y, x, y, color); |
71 | } |
72 | *last_x = x; |
73 | *last_y = y; |
74 | } |
75 | |
76 | |
77 | static void draw(SDL_Surface *s, const struct sample *res, int cont_tx, |
78 | uint32_t color) |
79 | { |
80 | int last_x, last_y; |
81 | int first, x, i; |
82 | |
83 | x = CHAN_X_OFFSET; |
84 | first = 1; |
85 | for (i = 0; i != N_CHAN; i++) { |
86 | if (cont_tx != CONT_TX_P500K) { |
87 | segment(s, &last_x, &last_y, x, res, first, color); |
88 | first = 0; |
89 | } |
90 | res++; |
91 | x += 2*SIDE_STEP; |
92 | |
93 | if (cont_tx != CONT_TX_M500K) { |
94 | segment(s, &last_x, &last_y, x, res, first, color); |
95 | first = 0; |
96 | } |
97 | res++; |
98 | x += CHAN_STEP-2*SIDE_STEP; |
99 | } |
100 | } |
101 | |
102 | |
103 | static void draw_limit(SDL_Surface *s, const double *v) |
104 | { |
105 | int x, y, i, last = 0; |
106 | |
107 | x = CHAN_X_OFFSET; |
108 | for (i = 0; i != N_CHAN; i++) { |
109 | y = avg2y(*v); |
110 | if (i) |
111 | vlineColor(s, x-CHAN_STEP/2, last, y, LIMIT_RGBA); |
112 | hlineColor(s, x-CHAN_STEP/2, x+CHAN_STEP/2, y, LIMIT_RGBA); |
113 | last = y; |
114 | x += CHAN_STEP; |
115 | v++; |
116 | } |
117 | } |
118 | |
119 | |
120 | static void disc(SDL_Surface *s, uint32_t color) |
121 | { |
122 | filledCircleColor(s, STATUS_X, STATUS_Y, STATUS_R, color); |
123 | aacircleColor(s, STATUS_X, STATUS_Y, STATUS_R, color); |
124 | } |
125 | |
126 | |
127 | static void triangle(SDL_Surface *s, int cx, int cy, int r, uint32_t color) |
128 | { |
129 | filledTrigonColor(s, cx, cy-r, cx+r*1.2, cy+r, cx-r*1.2, cy+r, color); |
130 | // aatrigonColor(s, cx, cy-r, cx+r*1.2, cy+r, cx-r*1.2, cy+r, color); |
131 | } |
132 | |
133 | |
134 | static void up(SDL_Surface *s, uint32_t color) |
135 | { |
136 | triangle(s, STATUS_X, STATUS_Y, STATUS_R, color); |
137 | } |
138 | |
139 | |
140 | static void down(SDL_Surface *s, uint32_t color) |
141 | { |
142 | triangle(s, STATUS_X, STATUS_Y, -STATUS_R, color); |
143 | } |
144 | |
145 | |
146 | static void indicate(SDL_Surface *s, int fail) |
147 | { |
148 | static uint32_t last = 0; |
149 | uint32_t color; |
150 | |
151 | switch (fail) { |
152 | case 0: |
153 | color = OK_RGBA; |
154 | break; |
155 | case 1: |
156 | color = OVER_RGBA; |
157 | break; |
158 | case -1: |
159 | color = UNDER_RGBA; |
160 | break; |
161 | default: |
162 | abort(); |
163 | } |
164 | if (color == last) |
165 | color = 0; |
166 | last = color; |
167 | switch (color) { |
168 | case OK_RGBA: |
169 | disc(s, color); |
170 | break; |
171 | case OVER_RGBA: |
172 | up(s, color); |
173 | break; |
174 | case UNDER_RGBA: |
175 | down(s, color); |
176 | break; |
177 | default: |
178 | break; |
179 | } |
180 | } |
181 | |
182 | |
183 | static void clear(SDL_Surface *s) |
184 | { |
185 | SDL_FillRect(s, NULL, SDL_MapRGB(s->format, 0, 0, 0)); |
186 | } |
187 | |
188 | |
189 | static void dump(const struct sweep *sweep, const struct sample *res) |
190 | { |
191 | const size_t size = sizeof(struct sample)*N_CHAN*2; |
192 | |
193 | print_sweep(sweep, res); |
194 | |
195 | dumps = realloc(dumps, (n_dumps+1)*sizeof(struct sample *)); |
196 | if (!dumps) { |
197 | perror("realloc"); |
198 | exit(1); |
199 | } |
200 | dumps[n_dumps] = malloc(size); |
201 | if (!dumps[n_dumps]) { |
202 | perror("malloc"); |
203 | exit(1); |
204 | } |
205 | memcpy(dumps[n_dumps], res, size); |
206 | n_dumps++; |
207 | } |
208 | |
209 | |
210 | static void draw_dumps(SDL_Surface *s, int cont_tx) |
211 | { |
212 | int i; |
213 | |
214 | for (i = 0; i != n_dumps; i++) |
215 | draw(s, dumps[i], cont_tx, DUMP_RGBA); |
216 | } |
217 | |
218 | |
219 | /* ----- Console input ----------------------------------------------------- */ |
220 | |
221 | |
222 | static struct termios old_term; |
223 | |
224 | |
225 | static void restore_term(void) |
226 | { |
227 | if (tcsetattr(0, TCSAFLUSH, &old_term) < 0) |
228 | perror("tcsetattr"); |
229 | } |
230 | |
231 | |
232 | static void raw(void) |
233 | { |
234 | struct termios term; |
235 | |
236 | if (tcgetattr(0, &old_term) < 0) { |
237 | perror("tcgetattr"); |
238 | exit(1); |
239 | } |
240 | term = old_term; |
241 | cfmakeraw(&term); |
242 | if (tcsetattr(0, TCSAFLUSH, &term) < 0) { |
243 | perror("tcsetattr"); |
244 | exit(1); |
245 | } |
246 | atexit(restore_term); |
247 | if (fcntl(0, F_SETFL, O_NONBLOCK) < 0) { |
248 | perror("fcntl"); |
249 | exit(1); |
250 | } |
251 | } |
252 | |
253 | |
254 | static char get_key(void) |
255 | { |
256 | ssize_t got; |
257 | char ch; |
258 | |
259 | got = read(0, &ch, 1); |
260 | if (got == 1) |
261 | return ch; |
262 | if (got >= 0) { |
263 | fprintf(stderr, "unexpected read() return value %d\n", |
264 | (int) got); |
265 | exit(1); |
266 | } |
267 | if (errno == EAGAIN) |
268 | return 0; |
269 | perror("read"); |
270 | exit(1); |
271 | } |
272 | |
273 | |
274 | /* --- temporarily, for optimizing --- */ |
275 | |
276 | #if 0 |
277 | |
278 | #include <sys/time.h> |
279 | |
280 | |
281 | static double t0; |
282 | |
283 | |
284 | static double t(void) |
285 | { |
286 | struct timeval tv; |
287 | |
288 | gettimeofday(&tv, NULL); |
289 | return tv.tv_sec+tv.tv_usec/1000000.0; |
290 | } |
291 | |
292 | |
293 | static void tstart(void) |
294 | { |
295 | t0 = t(); |
296 | } |
297 | |
298 | |
299 | static void tstop(void) |
300 | { |
301 | fprintf(stderr, "%.3f\n", t()-t0); |
302 | } |
303 | |
304 | #else |
305 | |
306 | static void tstart(void) {} |
307 | static void tstop(void) {} |
308 | |
309 | #endif |
310 | |
311 | |
312 | int gui(const struct sweep *sweep, int sweeps) |
313 | { |
314 | SDL_Surface *surf; |
315 | SDL_Event event; |
316 | int cycle; |
317 | int fail = 0; |
318 | |
319 | if (SDL_Init(SDL_INIT_VIDEO) < 0) { |
320 | fprintf(stderr, "SDL_init: %s\n", SDL_GetError()); |
321 | exit(1); |
322 | } |
323 | atexit(SDL_Quit); |
324 | |
325 | raw(); |
326 | |
327 | surf = SDL_SetVideoMode(XRES, YRES, 0, SDL_SWSURFACE); |
328 | if (!surf) { |
329 | fprintf(stderr, "SDL_SetVideoMode: %s\n", SDL_GetError()); |
330 | exit(1); |
331 | } |
332 | |
333 | for (cycle = 0; cycle != sweeps || !sweeps; cycle++) { |
334 | struct sample res[N_CHAN*2]; |
335 | |
336 | /* |
337 | * Pass/fail logic: |
338 | * |
339 | * Quit exit at any time, without making a pass/fail decision |
340 | * Pass exit if the current result is "pass" |
341 | * ignored if the current result is "over"/"under" |
342 | * Fail exit if the current result is "under" |
343 | * ignored if the current result is "pass" |
344 | * ignored if the current result is "over", because this |
345 | * indicates an invalid measurement, not a defective |
346 | * device |
347 | */ |
348 | |
349 | while (SDL_PollEvent(&event)) |
350 | switch (event.type) { |
351 | case SDL_KEYDOWN: |
352 | switch (event.key.keysym.sym) { |
353 | case SDLK_f: |
354 | if (cycle && fail < 0) |
355 | return -1; |
356 | break; |
357 | case SDLK_p: |
358 | if (cycle && !fail) |
359 | return 1; |
360 | break; |
361 | case SDLK_d: |
362 | if (cycle) |
363 | dump(sweep, res); |
364 | break; |
365 | case SDLK_q: |
366 | return 0; |
367 | default: |
368 | break; |
369 | } |
370 | break; |
371 | case SDL_QUIT: |
372 | return 0; |
373 | default: |
374 | break; |
375 | } |
376 | |
377 | switch (get_key()) { |
378 | case 'F': |
379 | case 'f': |
380 | if (cycle && fail < 0) |
381 | return -1; |
382 | break; |
383 | case 'P': |
384 | case 'p': |
385 | if (cycle && !fail) |
386 | return 1; |
387 | break; |
388 | case 'D': |
389 | case 'd': |
390 | if (cycle) |
391 | dump(sweep, res); |
392 | break; |
393 | case 'Q': |
394 | case 'q': |
395 | case 3: /* Ctrl-C */ |
396 | return 0; |
397 | default: |
398 | break; |
399 | } |
400 | |
401 | tstart(); |
402 | fail = do_sweep(sweep, res); |
403 | tstop(); |
404 | |
405 | SDL_LockSurface(surf); |
406 | |
407 | clear(surf); |
408 | |
409 | draw_dumps(surf, sweep->cont_tx); |
410 | draw_limit(surf, sweep->min); |
411 | draw_limit(surf, sweep->max); |
412 | indicate(surf, fail); |
413 | draw(surf, res, sweep->cont_tx, FG_RGBA); |
414 | |
415 | SDL_UnlockSurface(surf); |
416 | SDL_UpdateRect(surf, 0, 0, 0, 0); |
417 | } |
418 | |
419 | return 0; |
420 | } |
421 |