/* * Development window. Frames take the real path: conversion, planner, TCA9539 * byte stream, then the simulated expander and controller. The window shows * the simulator's display memory, so it is what the panel would receive. */ #include #include #include #include #include "app.h" #include "s1d13700_core.h" #include "s1d13700_sim.h" #define SCL_HZ 400000 #define TX_CAP 1025 /* the driver's message limit */ struct gui { struct app *app; struct s1d_config cfg; struct s1d_panel panel; struct s1d_sim sim; uint8_t shadow[S1D_VRAM_SIZE]; uint8_t next[S1D_VRAM_SIZE]; uint8_t tx[TX_CAP]; uint8_t *levels; cairo_surface_t *image; GtkWidget *window, *lcd, *stats; GtkWidget *buttons[RK_COUNT]; bool backlight; unsigned long total_bytes; guint frames; int scale; gchar **press; int press_next; const char *snapshot; }; static const struct key_def { enum radio_key key; const char *label; guint keyval, keyval2; int col, row; /* button grid */ } key_defs[] = { { RK_PREV, "⏮ Prev (Z)", GDK_KEY_z, GDK_KEY_Z, 0, 0 }, { RK_PLAY, "▶ Play (X)", GDK_KEY_x, GDK_KEY_X, 1, 0 }, { RK_PAUSE, "⏯ Pause (C)", GDK_KEY_c, GDK_KEY_space, 2, 0 }, { RK_STOP, "⏹ Stop (V)", GDK_KEY_v, GDK_KEY_V, 3, 0 }, { RK_NEXT, "⏭ Next (B)", GDK_KEY_b, GDK_KEY_B, 4, 0 }, { RK_ESC, "Esc", GDK_KEY_Escape, 0, 0, 1 }, { RK_UP, "▲ Vol+ (↑)", GDK_KEY_Up, 0, 2, 1 }, { RK_MENU, "Menu (M)", GDK_KEY_m, GDK_KEY_M, 4, 1 }, { RK_LEFT, "◀ (←)", GDK_KEY_Left, 0, 1, 2 }, { RK_OK, "OK (Enter)", GDK_KEY_Return, GDK_KEY_KP_Enter, 2, 2 }, { RK_RIGHT, "▶ (→)", GDK_KEY_Right, 0, 3, 2 }, { RK_DOWN, "▼ Vol− (↓)", GDK_KEY_Down, 0, 2, 3 }, { RK_LIST, "List (L)", GDK_KEY_l, GDK_KEY_L, 4, 3 }, }; static int sim_xfer(void *ctx, const uint8_t *buf, size_t len) { s1d_sim_i2c_write(ctx, buf, len); return 0; } static void sim_delay(void *ctx, unsigned int ms) { (void)ctx; (void)ms; } static const struct s1d_ops sim_ops = { sim_xfer, sim_delay }; static void update_image(struct gui *g) { unsigned int w = g->cfg.width, h = g->cfg.height, x, y; /* LCD look, from background to darkest level */ static const uint32_t lit[] = { 0xc4d890, 0x8ca060, 0x506438, 0x142010 }; static const uint32_t dark[] = { 0x4a5238, 0x363e2a, 0x242a1e, 0x101410 }; const uint32_t *pal = g->backlight ? lit : dark; uint8_t *data; int stride; bool shown; shown = s1d_sim_render(&g->sim, w, h, g->levels); cairo_surface_flush(g->image); data = cairo_image_surface_get_data(g->image); stride = cairo_image_surface_get_stride(g->image); for (y = 0; y < h; y++) { uint32_t *row = (uint32_t *)(data + y * stride); for (x = 0; x < w; x++) row[x] = shown ? pal[g->levels[y * w + x] & 3] : pal[0]; } cairo_surface_mark_dirty(g->image); gtk_widget_queue_draw(g->lcd); } static void present(void *ctx, const uint32_t *pixels, int stride, int x, int y, int w, int h) { struct gui *g = ctx; char text[256]; int err, faults; s1d_convert_xrgb8888(&g->cfg, pixels, stride, g->next, x, y, x + w, y + h); memset(&g->panel.stats, 0, sizeof(g->panel.stats)); err = s1d_panel_update(&g->panel, g->next, y, y + h); g->total_bytes += g->panel.stats.i2c_bytes; g->frames++; faults = s1d_sim_fault_count(&g->sim); snprintf(text, sizeof(text), "last update: %lu LCD writes, %lu seeks, %lu I²C bytes, %.1f ms at 400 kHz | " "total %lu bytes in %u updates%s", g->panel.stats.writes, g->panel.stats.seeks, g->panel.stats.i2c_bytes, s1d_stats_bus_us(&g->panel.stats, SCL_HZ) / 1000.0, g->total_bytes, g->frames, err || faults ? " | BUS FAULTS, see stderr" : ""); gtk_label_set_text(GTK_LABEL(g->stats), text); if (faults) g_printerr("simulator faults: %d\n", faults); update_image(g); } static void backlight(void *ctx, bool on) { struct gui *g = ctx; g->backlight = on; update_image(g); } static gboolean lcd_draw(GtkWidget *w, cairo_t *cr, gpointer data) { struct gui *g = data; (void)w; cairo_scale(cr, g->scale, g->scale); cairo_set_source_surface(cr, g->image, 0, 0); cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_NEAREST); cairo_paint(cr); return TRUE; } static void key_clicked(GtkButton *b, gpointer data) { struct gui *g = data; app_key(g->app, GPOINTER_TO_INT(g_object_get_data(G_OBJECT(b), "key"))); } static gboolean key_press(GtkWidget *w, GdkEventKey *ev, gpointer data) { struct gui *g = data; unsigned int i; (void)w; for (i = 0; i < G_N_ELEMENTS(key_defs); i++) { if (ev->keyval == key_defs[i].keyval || (key_defs[i].keyval2 && ev->keyval == key_defs[i].keyval2)) { app_key(g->app, key_defs[i].key); return TRUE; } } return FALSE; } static void build_window(struct gui *g) { GtkWidget *box, *grid; unsigned int i; g->window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(g->window), "Radio panel simulator"); gtk_window_set_resizable(GTK_WINDOW(g->window), FALSE); g_signal_connect(g->window, "destroy", G_CALLBACK(gtk_main_quit), NULL); g_signal_connect(g->window, "key-press-event", G_CALLBACK(key_press), g); box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 6); gtk_container_set_border_width(GTK_CONTAINER(box), 8); gtk_container_add(GTK_CONTAINER(g->window), box); g->lcd = gtk_drawing_area_new(); gtk_widget_set_size_request(g->lcd, (int)g->cfg.width * g->scale, (int)g->cfg.height * g->scale); g_signal_connect(g->lcd, "draw", G_CALLBACK(lcd_draw), g); gtk_box_pack_start(GTK_BOX(box), g->lcd, FALSE, FALSE, 0); g->stats = gtk_label_new(""); gtk_label_set_xalign(GTK_LABEL(g->stats), 0); gtk_label_set_line_wrap(GTK_LABEL(g->stats), TRUE); gtk_label_set_max_width_chars(GTK_LABEL(g->stats), 60); gtk_box_pack_start(GTK_BOX(box), g->stats, FALSE, FALSE, 0); grid = gtk_grid_new(); gtk_grid_set_row_spacing(GTK_GRID(grid), 4); gtk_grid_set_column_spacing(GTK_GRID(grid), 4); gtk_grid_set_column_homogeneous(GTK_GRID(grid), TRUE); gtk_box_pack_start(GTK_BOX(box), grid, FALSE, FALSE, 0); for (i = 0; i < G_N_ELEMENTS(key_defs); i++) { const struct key_def *d = &key_defs[i]; GtkWidget *b = gtk_button_new_with_label(d->label); /* a focused button would take Space and Enter from the shortcuts */ gtk_widget_set_can_focus(b, FALSE); g_object_set_data(G_OBJECT(b), "key", GINT_TO_POINTER(d->key)); g_signal_connect(b, "clicked", G_CALLBACK(key_clicked), g); gtk_grid_attach(GTK_GRID(grid), b, d->col, d->row, 1, 1); g->buttons[d->key] = b; } } /* ---- scripted runs, for automated checks -------------------------------- */ static const char *const key_names[RK_COUNT] = { [RK_ESC] = "esc", [RK_NEXT] = "next", [RK_STOP] = "stop", [RK_MENU] = "menu", [RK_LIST] = "list", [RK_UP] = "up", [RK_PREV] = "prev", [RK_RIGHT] = "right", [RK_DOWN] = "down", [RK_PAUSE] = "pause", [RK_OK] = "ok", [RK_LEFT] = "left", [RK_PLAY] = "play", }; static gboolean press_next(gpointer data) { struct gui *g = data; const char *name = g->press[g->press_next]; int i; if (!name) return G_SOURCE_REMOVE; g->press_next++; for (i = 0; i < RK_COUNT; i++) if (!strcmp(name, key_names[i])) gtk_button_clicked(GTK_BUTTON(g->buttons[i])); return G_SOURCE_CONTINUE; } static gboolean finish(gpointer data) { struct gui *g = data; if (g->snapshot) { GtkAllocation a; cairo_surface_t *s; cairo_t *cr; gtk_widget_get_allocation(g->window, &a); s = cairo_image_surface_create(CAIRO_FORMAT_RGB24, a.width, a.height); cr = cairo_create(s); gtk_widget_draw(g->window, cr); cairo_destroy(cr); cairo_surface_write_to_png(s, g->snapshot); cairo_surface_destroy(s); } gtk_main_quit(); return G_SOURCE_REMOVE; } int main(int argc, char **argv) { static struct gui g; static char *host, *press, *snapshot; static int port = 6600, width = 240, height = 160, bpp = 2, tcr = 162; static int scale = 3, exit_after; static gboolean kmsg; static GOptionEntry entries[] = { { "host", 0, 0, G_OPTION_ARG_STRING, &host, "MPD host (radio.lo)", "NAME" }, { "port", 0, 0, G_OPTION_ARG_INT, &port, "MPD port (6600)", "N" }, { "width", 0, 0, G_OPTION_ARG_INT, &width, "Panel width (240)", "PX" }, { "height", 0, 0, G_OPTION_ARG_INT, &height, "Panel height (160)", "PX" }, { "bpp", 0, 0, G_OPTION_ARG_INT, &bpp, "Bits per pixel: 1, 2, 4 (2)", "N" }, { "scale", 0, 0, G_OPTION_ARG_INT, &scale, "Window zoom (3)", "N" }, { "kmsg", 0, 0, G_OPTION_ARG_NONE, &kmsg, "Show this machine's kernel log", NULL }, { "press", 0, 0, G_OPTION_ARG_STRING, &press, "Click these keys, comma separated", "LIST" }, { "snapshot", 0, 0, G_OPTION_ARG_FILENAME, &snapshot, "Write the window to a PNG before exiting", "FILE" }, { "exit-after", 0, 0, G_OPTION_ARG_INT, &exit_after, "Quit after this many ms", "MS" }, { NULL, 0, 0, 0, NULL, NULL, NULL }, }; struct app_frontend fe = { &g, present, backlight }; struct app_options opt; GError *err = NULL; guint64 size; if (!gtk_init_with_args(&argc, &argv, NULL, entries, NULL, &err)) { g_printerr("%s\n", err ? err->message : "cannot open display"); return 1; } g.cfg = (struct s1d_config){ width, height, bpp, tcr }; g.scale = scale; g.backlight = true; g.snapshot = snapshot; s1d_sim_init(&g.sim); if (s1d_panel_setup(&g.panel, &g.cfg, &sim_ops, &g.sim, g.shadow, g.tx, sizeof(g.tx)) || s1d_panel_init(&g.panel)) { g_printerr("invalid panel configuration\n"); return 1; } size = (guint64)width * height; g.levels = g_malloc(size); g.image = cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height); build_window(&g); update_image(&g); gtk_widget_show_all(g.window); opt = (struct app_options){ host ? host : "radio.lo", port, width, height, kmsg }; g.app = app_new(&opt, &fe); if (press) { g.press = g_strsplit(press, ",", -1); g_timeout_add(400, press_next, &g); } if (exit_after) g_timeout_add(exit_after, finish, &g); gtk_main(); app_free(g.app); return s1d_sim_fault_count(&g.sim) ? 2 : 0; }