Root/ubb-la/gui.c

Source at commit f26fd3564a2664fe3deea5b86bd46e8dad8209ac created 6 years 8 months ago.
By Werner Almesberger, ubb-la/gui.c (show_map): keep view rectangle size constant
1/*
2 * gui.c - Graphical output for ubb-la
3 *
4 * Written 2013 by Werner Almesberger
5 * Copyright 2013 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#include <stdarg.h>
14#include <stdint.h>
15#include <stdlib.h>
16#include <alloca.h>
17
18#include "SDL.h"
19#include "SDL_gfxPrimitives.h"
20#include "SDL_gfxPrimitives_font.h"
21
22#include "gui.h"
23
24
25#if 0
26#define DEBUG(fmt, ...) fprintf(stderr, fmt, __VA_ARGS__)
27#else
28#define DEBUG(fmt, ...)
29#endif
30
31
32#define XRES 320 /* canvas width */
33#define YRES 240 /* canvas height */
34
35
36/* ----- Colors ------------------------------------------------------------ */
37
38
39#define TEXT_RGBA 0xffffffff /* general text */
40#define UNIT_RGBA 0xb0e0ffff /* units */
41#define MAP_BUF_RGBA 0x808080ff /* buffer in the map */
42#define MAP_VIEW_RGBA 0xffffffff /* current view in the map */
43#define CENTER_RGBA 0x5080ffff /* center marker */
44#define LEVEL_RGBA 0xffff00ff /* constant level or single change */
45#define BOUNCE_RGBA 0xff8080ff /* bouncing signal */
46#define LABEL_RGBA 0xffffffff /* channel label */
47#define DIV_RGBA 0x808080ff /* divisions */
48
49
50/* ----- Layout ------------------------------------------------------------ */
51
52
53#define XCENTER ((XRES+CH_XOFF)/2)
54#define XWIDTH (XRES-CH_XOFF)
55
56#define MAP_BUF_Y0 2
57#define MAP_BUF_Y1 8
58#define MAP_VIEW_Y0 0
59#define MAP_VIEW_Y1 10
60
61#define CENTER_W 8
62#define CENTER_Y0 20
63#define CENTER_Y1 24
64
65#define CH_XOFF 30
66#define CH_YOFF 30
67#define CH_SKIP 16
68#define CH_HEIGHT 8
69
70#define DIV_X 32
71#define DIV_Y 6
72
73#define MAX_ZOOM 3
74
75#define UNIT_GAP 2 /* space between number and unit */
76
77#define POS_X 168
78#define POS_Y (MEAS_DIV_Y-8-3)
79
80#define MEAS_DIV_Y (FREQ_Y-3-1)
81
82#define FREQ_X 0
83#define FREQ_Y 222
84#define INTERVAL_X 0
85#define INTERVAL_Y (FREQ_Y+8+2)
86#define DIV_SAMP_X (DIV_INT_X-8)
87#define DIV_SAMP_Y FREQ_Y
88#define DIV_INT_X 80
89#define DIV_INT_Y INTERVAL_Y
90
91
92/* ----- Miscellaneous definitions ----------------------------------------- */
93
94
95#define REPEAT_DELAY_MS 300
96#define REPEAT_INTERVAL_MS 30
97
98static SDL_Surface *surf;
99
100
101/* ----- SDL initialization and screen-wide functions ---------------------- */
102
103
104void gui_init(void)
105{
106    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
107        fprintf(stderr, "SDL_init: %s\n", SDL_GetError());
108        exit(1);
109    }
110    atexit(SDL_Quit);
111
112    surf = SDL_SetVideoMode(XRES, YRES, 0, SDL_SWSURFACE);
113    if (!surf) {
114        fprintf(stderr, "SDL_SetVideoMode: %s\n", SDL_GetError());
115        exit(1);
116    }
117
118    SDL_EnableKeyRepeat(REPEAT_DELAY_MS, REPEAT_INTERVAL_MS);
119}
120
121
122static void clear(void)
123{
124    SDL_LockSurface(surf);
125    SDL_FillRect(surf, NULL, SDL_MapRGB(surf->format, 0, 0, 0));
126}
127
128
129static void update(void)
130{
131    SDL_UnlockSurface(surf);
132    SDL_UpdateRect(surf, 0, 0, 0, 0);
133}
134
135
136/* ----- Text output ------------------------------------------------------- */
137
138
139/*
140 * stringColor from SDL_gfx fails for some reason. SDL_ttf is just too much
141 * compatibility trouble. So we do our own.
142 */
143
144static void textf(int x, int y, uint32_t color, const char *fmt, ...)
145{
146    va_list ap;
147    char *tmp, *s;
148    uint8_t *p;
149    int n, ix, iy;
150
151    va_start(ap, fmt);
152    n = vsnprintf(NULL, 0, fmt, ap);
153    va_end(ap);
154    tmp = alloca(n+1);
155    va_start(ap, fmt);
156    vsnprintf(tmp, n+1, fmt, ap);
157    va_end(ap);
158
159    for (s = tmp; *s; s++) {
160        p = gfxPrimitivesFontdata+(*s << 3);
161        for (iy = 0; iy != 8; iy++) {
162            for (ix = 0; ix != 8; ix++)
163                if ((*p << ix) & 0x80)
164                    pixelColor(surf, x+ix, y+iy, color);
165            p++;
166        }
167        x += 8;
168    }
169}
170
171
172/* ----- Map of buffer and view -------------------------------------------- */
173
174
175static void show_map(int skip, int nibbles, int s0, int s1)
176{
177    int w = nibbles-skip;
178    int m = (nibbles+skip) >> 1;
179    int scale = 0;
180    int x0, x1;
181
182    while (w >= XWIDTH/2) {
183        w >>= 1;
184        scale++;
185    }
186    boxColor(surf, XCENTER-(w >> 1), MAP_BUF_Y0,
187        XCENTER+(w >> 1), MAP_BUF_Y1, MAP_BUF_RGBA);
188    x0 = (s0-m+(XCENTER << scale)) >> scale;
189    x1 = x0+((s1-s0) >> scale);
190    rectangleColor(surf, x0, MAP_VIEW_Y0, x1, MAP_VIEW_Y1, MAP_VIEW_RGBA);
191}
192
193
194/* ----- Waveform elements ------------------------------------------------- */
195
196
197static inline int ch_y(int ch, int v)
198{
199    return CH_YOFF+CH_SKIP*ch+(v ? 0 : CH_HEIGHT);
200}
201
202
203static void bounce(int x, int ch)
204{
205    vlineColor(surf, x, ch_y(ch, 0), ch_y(ch, 1), BOUNCE_RGBA);
206}
207
208
209static void change(int x, int ch)
210{
211    vlineColor(surf, x, ch_y(ch, 0), ch_y(ch, 1), LEVEL_RGBA);
212}
213
214
215static void level(int x0, int x1, int ch, int v)
216{
217    if (x0 == x1)
218        pixelColor(surf, x0, ch_y(ch, v), LEVEL_RGBA);
219    else
220        hlineColor(surf, x0, x1, ch_y(ch, v), LEVEL_RGBA);
221}
222
223
224/* ----- Show (part of) a buffer ------------------------------------------- */
225
226
227static void show_buffer_zoom_in(const uint8_t *buf, int skip, int nibbles,
228    int x0, int x1)
229{
230    int f = (x1-x0)/(nibbles-skip);
231    int x, i, j;
232    int last[4] = { -1, -1, -1, -1};
233    uint8_t v, bit;
234
235    DEBUG("in: %d-%d (%d) Sa %d-%d (%d) pix %d f (%d)\n",
236        skip, nibbles, nibbles-skip, x0, x1, x1-x0, f, f*(nibbles-skip));
237    x = x0;
238    for (i = skip; i != nibbles; i++) {
239        v = (buf[i >> 1] >> 4*(~i & 1)) & 0xf;
240        for (j = 0; j != 4; j++) {
241            bit = (v >> j) & 1;
242            if (bit == last[j] || last[j] == -1) {
243                level(x, x+f-1, j, bit);
244            } else {
245                change(x, j);
246                level(x+1, x+f-1, j, bit);
247            }
248            last[j] = bit;
249        }
250        x += f;
251    }
252}
253
254
255static void show_buffer_zoom_out(const uint8_t *buf, int skip, int nibbles,
256    int x0, int x1)
257{
258    int f = (nibbles-skip)/(x1-x0);
259    int x;
260    int i = skip, n, j;
261    int changes[4], last[4] = { -1, -1, -1, -1}, next[4];
262    uint8_t v, bit;
263
264    DEBUG("out: %d-%d (%d) Sa %d-%d (%d) pix %d f (%d)\n",
265        skip, nibbles, nibbles-skip, x0, x1, x1-x0, f, f*(x1-x0));
266    for (x = x0; x != x1; x++) {
267        n = i+f;
268        for (j = 0; j != 4; j++) {
269            next[j] = last[j];
270            changes[j] = 0;
271        }
272        while (i != n) {
273            v = (buf[i >> 1] >> 4*(~i & 1)) & 0xf;
274            for (j = 0; j != 4; j++) {
275                bit = (v >> j) & 1;
276                if (bit != next[j]) {
277                    changes[j]++;
278                    next[j] = bit;
279                }
280            }
281            i++;
282        }
283        for (j = 0; j != 4; j++) {
284            if (changes[j] > 1)
285                bounce(x, j);
286            else if (!changes[j] || last[j] == -1)
287                level(x, x, j, next[j]);
288            else
289                change(x, j);
290            last[j] = next[j];
291        }
292    }
293}
294
295
296static void show_buffer(const uint8_t *buf, int skip, int nibbles,
297    int x0, int x1, int zoom, int pos)
298{
299    int xm, w, s, p0, p1;
300    int d, dp;
301
302    xm = (x0+x1) >> 1;
303    dp = pos-((nibbles+skip) >> 1);
304    if (zoom < 0) {
305        s = (x1-x0) << -zoom;
306        show_map(skip, nibbles, pos-s/2, pos+s/2);
307        w = (nibbles-skip) >> -zoom;
308        p0 = xm-(w >> 1)-(dp >> -zoom);
309        p1 = xm+((w+1) >> 1)-(dp >> -zoom);
310        if (p0 < x0) {
311            skip += (x0-p0) << -zoom;
312            p0 = x0;
313        }
314        if (p1 > x1) {
315            nibbles -= (p1-x1) << -zoom;
316            p1 = x1;
317        }
318        show_buffer_zoom_out(buf, skip, nibbles, p0, p1);
319    } else {
320        s = (x1-x0) >> zoom;
321        show_map(skip, nibbles, pos-s/2, pos+s/2);
322        w = (nibbles-skip) << zoom;
323        p0 = xm-(w >> 1)-(dp << zoom);
324        p1 = xm+((w+1) >> 1)-(dp << zoom);
325        if (p0 < x0) {
326            d = ((x0-p0)+(1 << zoom)-1) >> zoom;
327            skip += d;
328            p0 += d << zoom;
329        }
330        if (p1 > x1) {
331            d = ((p1-x1)+(1 << zoom)-1) >> zoom;
332            nibbles -= d;
333            p1 -= d << zoom;
334        }
335        show_buffer_zoom_in(buf, skip, nibbles, p0, p1);
336    }
337}
338
339
340/* ----- Display the sample frequency -------------------------------------- */
341
342
343static void si_text(int x, int y, double v, const char *unit, int digits)
344{
345    const char *pfx;
346
347    if (v >= 1e6) {
348        v /= 1e6;
349        pfx = "M";
350    } else if (v >= 1e3) {
351        v /= 1e3;
352        pfx = "k";
353    } else if (v >= 1) {
354        pfx = " ";
355    } else if (v >= 1e-3) {
356        v *= 1e3;
357        pfx = "m";
358    } else if (v >= 1e-6) {
359        v *= 1e6;
360        pfx = "u";
361    } else {
362        v *= 1e9;
363        pfx = "n";
364    }
365    if (v >= 10 && digits == 3)
366        textf(x, y, TEXT_RGBA, "%3d", (int) (v+0.5));
367    else if (v >= 100 && digits == 4)
368        textf(x, y, TEXT_RGBA, "%4d", (int) (v+0.5));
369    else if (v >= 100)
370        textf(x, y, TEXT_RGBA, "%*.*f", digits, digits-4, v);
371    else if (v >= 10)
372        textf(x, y, TEXT_RGBA, "%*.*f", digits, digits-3, v);
373    else
374        textf(x, y, TEXT_RGBA, "%*.*f", digits, digits-2, v);
375    textf(x+digits*8+UNIT_GAP, y, UNIT_RGBA, "%s%s", pfx, unit);
376}
377
378
379static void show_freq(double freq, int zoom)
380{
381    int div;
382
383    si_text(FREQ_X, FREQ_Y, freq, "Sa/s", 3);
384    si_text(INTERVAL_X, INTERVAL_Y, 1/freq, "s/Sa", 3);
385    div = (DIV_X >> MAX_ZOOM) << (MAX_ZOOM-zoom);
386    textf(DIV_SAMP_X, DIV_SAMP_Y, TEXT_RGBA, "%4d", div);
387    textf(DIV_SAMP_X+4*8+UNIT_GAP, DIV_SAMP_Y, UNIT_RGBA, "Sa/div", div);
388    si_text(DIV_INT_X, DIV_INT_Y, div/freq, "s/div", 3);
389}
390
391
392/* ----- Show a divisions -------------------------------------------------- */
393
394
395static void show_divisions(void)
396{
397    int n = XWIDTH/2/DIV_X;
398    int i;
399
400    for (i = -n; i <= n; i++)
401        vlineColor(surf, XCENTER+i*DIV_X,
402            ch_y(0, 1)-DIV_Y, ch_y(3, 0)+DIV_Y,
403            i ? DIV_RGBA : CENTER_RGBA);
404}
405
406
407/* ----- Main event loop --------------------------------------------------- */
408
409
410static int pos_step(int zoom)
411{
412    return 1 << (MAX_ZOOM-zoom+1);
413}
414
415
416void gui(const uint8_t *buf, int skip, int nibbles, double freq)
417{
418    SDL_Event event;
419    int pos = (skip+nibbles) >> 1;
420    int zoom; /* < 0: zoom out; 0: 1 pixel = 1 sample; > 1: zoom in */
421    int min_zoom = 0;
422    int i;
423
424    while (XWIDTH < (nibbles-skip) >> -min_zoom)
425        min_zoom--;
426    zoom = min_zoom;
427    while (1) {
428        clear();
429        for (i = 0; i != 4; i++)
430            textf(0, ch_y(i, 1), LABEL_RGBA, "CH%d", i);
431        show_divisions();
432        filledTrigonColor(surf, XCENTER, CENTER_Y1,
433            XCENTER-CENTER_W/2, CENTER_Y0,
434            XCENTER+CENTER_W/2, CENTER_Y0, CENTER_RGBA);
435        show_buffer(buf, skip, nibbles, CH_XOFF, XRES, zoom, pos);
436        show_freq(freq, zoom);
437        si_text(POS_X, POS_Y, pos/freq, "s", 7);
438        hlineColor(surf, 0, XRES-1, MEAS_DIV_Y, DIV_RGBA);
439        update();
440
441        while (1) {
442            SDL_WaitEvent(&event);
443            switch (event.type) {
444            case SDL_KEYDOWN:
445                switch (event.key.keysym.sym) {
446                case SDLK_UP:
447                    if (zoom < MAX_ZOOM)
448                        zoom++;
449                    break;
450                case SDLK_DOWN:
451                    if (zoom > min_zoom)
452                        zoom--;
453                    break;
454                case SDLK_LEFT:
455                    pos -= pos_step(zoom);
456                    if (pos < 0)
457                        pos = 0;
458                    break;
459                case SDLK_RIGHT:
460                    pos += pos_step(zoom);
461                    if (pos > nibbles-1)
462                        pos = nibbles-1;
463                    break;
464                case SDLK_RETURN:
465                case SDLK_q:
466                    return;
467                default:
468                    printf("%x\n", event.key.keysym.sym);
469                    continue;
470                }
471                break;
472            case SDL_QUIT:
473                return;
474            default:
475                continue;
476            }
477            break;
478        }
479    }
480}
481

Archive Download this file

Branches:
master



interactive