Root/
| Source at commit 121acf6ffad8f1bcd4b08e50e293e597880b68eb created 10 years 11 months ago. By Werner Almesberger, tornado/cpu/cpu.brd: make connection to the MCU's GND pad straighter | |
|---|---|
| 1 | /* |
| 2 | * tools/ant-gui/ant-gui.c - Antorcha GUI |
| 3 | * |
| 4 | * Written 2012 by Werner Almesberger |
| 5 | * Copyright 2012 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 <stdint.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <stdio.h> |
| 17 | #include <unistd.h> |
| 18 | |
| 19 | #include "SDL.h" |
| 20 | #include "SDL_gfxPrimitives.h" |
| 21 | |
| 22 | #include <libant/libant.h> |
| 23 | |
| 24 | |
| 25 | #define XRES 320 /* canvas width */ |
| 26 | #define YRES 240 /* canvas height */ |
| 27 | |
| 28 | #define W 80 /* image width */ |
| 29 | #define H 16 /* image height */ |
| 30 | #define GAP 6 |
| 31 | |
| 32 | #define NX ((XRES+GAP)/(W+GAP)) |
| 33 | #define NY ((YRES+GAP)/(H+GAP)) |
| 34 | |
| 35 | |
| 36 | #define FG_SEL_RGBA 0x000000ff |
| 37 | #define BG_SEL_RGBA 0xffffffff |
| 38 | #define FG_NORM_RGBA 0xffffffff |
| 39 | #define BG_NORM_RGBA 0x000000ff |
| 40 | #define FG_ACT_RGBA 0xff2020ff |
| 41 | |
| 42 | |
| 43 | static void upload(const uint8_t *img) |
| 44 | { |
| 45 | FILE *file; |
| 46 | |
| 47 | file = popen("ant-cl -", "w"); |
| 48 | if (!file) { |
| 49 | perror("ant-cl"); |
| 50 | exit(1); |
| 51 | } |
| 52 | if (dump_binary(file, img, W, H) < 0) { |
| 53 | perror("ant-cl"); |
| 54 | fclose(file); |
| 55 | exit(1); |
| 56 | } |
| 57 | if (fclose(file) < 0) { |
| 58 | perror("ant-cl"); |
| 59 | exit(1); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | |
| 64 | static void render(SDL_Surface *s, const uint8_t *img, int x, int y, |
| 65 | int sel, int act) |
| 66 | { |
| 67 | int ix, iy; |
| 68 | |
| 69 | uint32_t fg = act ? FG_ACT_RGBA : sel ? FG_SEL_RGBA : FG_NORM_RGBA; |
| 70 | uint32_t bg = sel ? BG_SEL_RGBA : BG_NORM_RGBA; |
| 71 | |
| 72 | for (iy = 0; iy != H; iy++) |
| 73 | for (ix = 0; ix != W; ix++) |
| 74 | if (img[(iy*W+ix) >> 3] & (1 << (ix & 7))) |
| 75 | pixelColor(s, x+ix, y+iy, fg); |
| 76 | else |
| 77 | pixelColor(s, x+ix, y+iy, bg); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | static void gui(uint8_t *const *imgs, int n) |
| 82 | { |
| 83 | SDL_Surface *surf; |
| 84 | SDL_Event event; |
| 85 | int x = 0, y = 0, y_top = 0; |
| 86 | int dx, dy, do_upload; |
| 87 | int ix, iy, i; |
| 88 | int active = -1; |
| 89 | |
| 90 | if (SDL_Init(SDL_INIT_VIDEO) < 0) { |
| 91 | fprintf(stderr, "SDL_init: %s\n", SDL_GetError()); |
| 92 | exit(1); |
| 93 | } |
| 94 | atexit(SDL_Quit); |
| 95 | |
| 96 | surf = SDL_SetVideoMode(XRES, YRES, 0, SDL_SWSURFACE); |
| 97 | if (!surf) { |
| 98 | fprintf(stderr, "SDL_SetVideoMode: %s\n", SDL_GetError()); |
| 99 | exit(1); |
| 100 | } |
| 101 | |
| 102 | while (1) { |
| 103 | dx = dy = do_upload = 0; |
| 104 | SDL_WaitEvent(&event); |
| 105 | switch (event.type) { |
| 106 | case SDL_KEYDOWN: |
| 107 | switch (event.key.keysym.sym) { |
| 108 | case SDLK_UP: |
| 109 | if (y > 0) |
| 110 | dy = -1; |
| 111 | break; |
| 112 | case SDLK_DOWN: |
| 113 | if ((y+1)*NX+x < n) |
| 114 | dy = 1; |
| 115 | break; |
| 116 | case SDLK_RIGHT: |
| 117 | dx = 1; |
| 118 | break; |
| 119 | case SDLK_LEFT: |
| 120 | dx = -1; |
| 121 | break; |
| 122 | case SDLK_RETURN: |
| 123 | do_upload = 1; |
| 124 | break; |
| 125 | case SDLK_q: |
| 126 | return; |
| 127 | default: |
| 128 | break; |
| 129 | } |
| 130 | break; |
| 131 | case SDL_QUIT: |
| 132 | return; |
| 133 | default: |
| 134 | break; |
| 135 | } |
| 136 | |
| 137 | x = (x+NX+dx) % NX; |
| 138 | y += dy; |
| 139 | if (y*NX+x >= n) |
| 140 | x = n-y*NX-1; |
| 141 | if (y < y_top) |
| 142 | y_top = y; |
| 143 | if (y >= y_top+NY) |
| 144 | y_top = y-NY+1; |
| 145 | |
| 146 | if (do_upload) { |
| 147 | i = y*NX+x; |
| 148 | upload(imgs[i]); |
| 149 | active = i; |
| 150 | } |
| 151 | |
| 152 | SDL_LockSurface(surf); |
| 153 | SDL_FillRect(surf, NULL, SDL_MapRGB(surf->format, 0, 0, 0)); |
| 154 | for (iy = 0; iy != NY; iy++) |
| 155 | for (ix = 0; ix != NX; ix++) { |
| 156 | i = (y_top+iy)*NX+ix; |
| 157 | if (i >= n) |
| 158 | break; |
| 159 | render(surf, imgs[i], |
| 160 | ix*(W+GAP), iy*(H+GAP), |
| 161 | ix == x && y_top+iy == y, i == active); |
| 162 | } |
| 163 | SDL_UnlockSurface(surf); |
| 164 | SDL_UpdateRect(surf, 0, 0, 0, 0); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | |
| 169 | static void flush_edits(uint8_t ***imgs, int *n, struct edit *edits) |
| 170 | { |
| 171 | const char *err; |
| 172 | |
| 173 | if (!edits) |
| 174 | return; |
| 175 | *imgs = realloc(*imgs, sizeof(uint8_t **)*(*n+1)); |
| 176 | if (!*imgs) { |
| 177 | perror("realloc"); |
| 178 | exit(1); |
| 179 | } |
| 180 | (*imgs)[*n] = apply_edits(W, H, edits, &err); |
| 181 | if (!(*imgs)[*n]) { |
| 182 | fprintf(stderr, "%s\n", err); |
| 183 | exit(1); |
| 184 | } |
| 185 | (*n)++; |
| 186 | free_edits(edits); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | static void generate(uint8_t ***imgs, int *n, const char *path) |
| 191 | { |
| 192 | FILE *file; |
| 193 | char buf[100]; |
| 194 | struct edit *edits = NULL, **last = &edits; |
| 195 | const char *err; |
| 196 | char *nl; |
| 197 | |
| 198 | file = fopen(path, "r"); |
| 199 | if (!file) { |
| 200 | perror(path); |
| 201 | exit(1); |
| 202 | } |
| 203 | while (fgets(buf, sizeof(buf), file)) { |
| 204 | if (buf[0] == '#') |
| 205 | continue; |
| 206 | nl = strchr(buf, '\n'); |
| 207 | if (nl) |
| 208 | *nl = 0; |
| 209 | if (!buf[0]) { |
| 210 | flush_edits(imgs, n, edits); |
| 211 | edits = NULL; |
| 212 | last = &edits; |
| 213 | continue; |
| 214 | } |
| 215 | if (edits) { |
| 216 | *last = malloc(sizeof(struct edit)); |
| 217 | if (!*last) |
| 218 | abort(); |
| 219 | (*last)->type = edit_nl; |
| 220 | last = &(*last)->next; |
| 221 | } |
| 222 | *last = text2edit(buf, &err); |
| 223 | if (!*last) { |
| 224 | fprintf(stderr, "\"%s\": %s\n", buf, err); |
| 225 | exit(1); |
| 226 | } |
| 227 | while (*last) |
| 228 | last = &(*last)->next; |
| 229 | } |
| 230 | fclose(file); |
| 231 | flush_edits(imgs, n, edits); |
| 232 | } |
| 233 | |
| 234 | |
| 235 | static void usage(const char *name) |
| 236 | { |
| 237 | fprintf(stderr, "usage: %s [-F font_dir ...] file ...\n", name); |
| 238 | exit(1); |
| 239 | } |
| 240 | |
| 241 | |
| 242 | int main(int argc, char **argv) |
| 243 | { |
| 244 | uint8_t **imgs = NULL; |
| 245 | int n = 0; |
| 246 | int i, c; |
| 247 | |
| 248 | while ((c = getopt(argc, argv, "F:")) != EOF) |
| 249 | switch (c) { |
| 250 | case 'F': |
| 251 | add_font_dir(optarg); |
| 252 | break; |
| 253 | default: |
| 254 | usage(*argv); |
| 255 | } |
| 256 | |
| 257 | if (optind == argc) |
| 258 | usage(*argv); |
| 259 | |
| 260 | for (i = optind; i != argc; i++) |
| 261 | generate(&imgs, &n, argv[i]); |
| 262 | gui(imgs, n); |
| 263 | exit(1); |
| 264 | } |
| 265 | |
Branches:
master
tornado-v1
