ucmd_colors.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* colors: animated rainbow in the terminal window's canvas. ESC returns.
  2. * Target 30 fps, matching the WM flush rate. */
  3. #include "ucmd_lib.h"
  4. #define RAINBOW_SEG 256
  5. #define RAINBOW_FULL (6 * RAINBOW_SEG)
  6. #define FRAME_MS 33
  7. #define PHASE_STEP 32
  8. /* Hue LUT decimated by 4 (see the old kernel colors.c): stores RGB565. */
  9. #define LUT_STEP 4
  10. #define LUT_SIZE (RAINBOW_FULL / LUT_STEP)
  11. /* FPS overlay: 7 chars x 8 px, top-left; the rainbow skips this rect. */
  12. #define FPS_RECT_W (7 * 8)
  13. #define FPS_RECT_H 8
  14. static unsigned short hue_lut[LUT_SIZE];
  15. static unsigned short rgb565(unsigned int r, unsigned int g, unsigned int b)
  16. {
  17. return (unsigned short)(((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3));
  18. }
  19. static void build_hue_lut(void)
  20. {
  21. unsigned int i;
  22. for (i = 0; i < LUT_SIZE; i++) {
  23. unsigned int hue = i * LUT_STEP;
  24. unsigned int seg = hue / RAINBOW_SEG;
  25. unsigned int f = hue % RAINBOW_SEG;
  26. unsigned int r1, g1, b1, r2, g2, b2, r, g, b;
  27. if (seg >= 6)
  28. seg = 0;
  29. switch (seg) {
  30. case 0: r1 = 255; g1 = 0; b1 = 0; r2 = 255; g2 = 255; b2 = 0; break;
  31. case 1: r1 = 255; g1 = 255; b1 = 0; r2 = 0; g2 = 255; b2 = 0; break;
  32. case 2: r1 = 0; g1 = 255; b1 = 0; r2 = 0; g2 = 255; b2 = 255; break;
  33. case 3: r1 = 0; g1 = 255; b1 = 255; r2 = 0; g2 = 0; b2 = 255; break;
  34. case 4: r1 = 0; g1 = 0; b1 = 255; r2 = 255; g2 = 0; b2 = 255; break;
  35. default: r1 = 255; g1 = 0; b1 = 255; r2 = 255; g2 = 0; b2 = 0; break;
  36. }
  37. r = (r1 * (RAINBOW_SEG - f) + r2 * f) / RAINBOW_SEG;
  38. g = (g1 * (RAINBOW_SEG - f) + g2 * f) / RAINBOW_SEG;
  39. b = (b1 * (RAINBOW_SEG - f) + b2 * f) / RAINBOW_SEG;
  40. hue_lut[i] = rgb565(r, g, b);
  41. }
  42. }
  43. static void draw_frame(unsigned short *px, unsigned int w, unsigned int h,
  44. unsigned int phase)
  45. {
  46. unsigned int scale_x = RAINBOW_FULL / (w ? w : 1);
  47. unsigned int scale_y = RAINBOW_FULL / (h ? h : 1);
  48. unsigned int y, x;
  49. for (y = 0; y < h; y++) {
  50. unsigned int row_hue = (phase + y * scale_y) % RAINBOW_FULL;
  51. unsigned short *row = px + y * w;
  52. for (x = 0; x < w; x++) {
  53. if (!(y < FPS_RECT_H && x < FPS_RECT_W))
  54. row[x] = hue_lut[row_hue / LUT_STEP];
  55. row_hue += scale_x;
  56. if (row_hue >= RAINBOW_FULL)
  57. row_hue -= RAINBOW_FULL;
  58. }
  59. }
  60. }
  61. /* Minimal 8x8 glyphs for the FPS overlay: "0123456789 fps" (subset of
  62. * font_8x8.c, to keep the ELF small). */
  63. static const unsigned char glyphs[13][8] = {
  64. { 0x3C,0x66,0x6E,0x76,0x66,0x66,0x3C,0x00 }, /* 0 */
  65. { 0x18,0x38,0x18,0x18,0x18,0x18,0x7E,0x00 }, /* 1 */
  66. { 0x3C,0x66,0x06,0x0C,0x18,0x30,0x7E,0x00 }, /* 2 */
  67. { 0x3C,0x66,0x06,0x1C,0x06,0x66,0x3C,0x00 }, /* 3 */
  68. { 0x06,0x0E,0x1E,0x36,0x66,0x7F,0x06,0x00 }, /* 4 */
  69. { 0x7E,0x60,0x7C,0x06,0x06,0x66,0x3C,0x00 }, /* 5 */
  70. { 0x3C,0x66,0x60,0x7C,0x66,0x66,0x3C,0x00 }, /* 6 */
  71. { 0x7E,0x06,0x0C,0x18,0x30,0x30,0x30,0x00 }, /* 7 */
  72. { 0x3C,0x66,0x66,0x3C,0x66,0x66,0x3C,0x00 }, /* 8 */
  73. { 0x3C,0x66,0x66,0x3E,0x06,0x66,0x3C,0x00 }, /* 9 */
  74. { 0x1C,0x30,0x30,0x7C,0x30,0x30,0x30,0x00 }, /* f */
  75. { 0x00,0x00,0x7C,0x66,0x66,0x7C,0x60,0x60 }, /* p */
  76. { 0x00,0x00,0x3E,0x60,0x3C,0x06,0x7C,0x00 }, /* s */
  77. };
  78. static int glyph_index(char c)
  79. {
  80. if (c >= '0' && c <= '9')
  81. return c - '0';
  82. if (c == 'f')
  83. return 10;
  84. if (c == 'p')
  85. return 11;
  86. if (c == 's')
  87. return 12;
  88. return -1;
  89. }
  90. static void draw_string(unsigned short *px, unsigned int w, unsigned int h,
  91. unsigned int x0, unsigned int y0, const char *s)
  92. {
  93. for (; *s; s++, x0 += 8) {
  94. int gi = glyph_index(*s);
  95. unsigned int x, y;
  96. if (gi < 0)
  97. continue; /* spaces etc. */
  98. for (y = 0; y < 8; y++) {
  99. if (y0 + y >= h)
  100. break;
  101. for (x = 0; x < 8; x++)
  102. if ((glyphs[gi][y] & (0x80u >> x)) && x0 + x < w)
  103. px[(y0 + y) * w + x0 + x] = rgb565(255, 255, 255);
  104. }
  105. }
  106. }
  107. static void format_fps(unsigned int fps, char *buf)
  108. {
  109. if (fps > 999)
  110. fps = 999;
  111. buf[0] = (char)('0' + fps / 100);
  112. buf[1] = (char)('0' + (fps / 10) % 10);
  113. buf[2] = (char)('0' + fps % 10);
  114. buf[3] = ' ';
  115. buf[4] = 'f';
  116. buf[5] = 'p';
  117. buf[6] = 's';
  118. buf[7] = '\0';
  119. }
  120. void _start(const char *arg)
  121. {
  122. struct win_info info;
  123. unsigned short *px;
  124. unsigned int w, h, phase = 0, frames = 0, last_fps, displayed = 0;
  125. char fps_buf[8];
  126. (void)arg;
  127. if (u_win_create(512, 320, "colors", &info) != 0) {
  128. u_puts("colors: no graphics window\n");
  129. u_exit(1);
  130. }
  131. px = (unsigned short *)info.fb;
  132. w = info.width;
  133. h = info.height;
  134. build_hue_lut();
  135. last_fps = u_ticks_ms();
  136. for (;;) {
  137. unsigned int frame_start, elapsed;
  138. {
  139. int k = u_key_poll();
  140. if (k == 0x1B || k == 0x03)
  141. break;
  142. }
  143. frame_start = u_ticks_ms();
  144. draw_frame(px, w, h, phase);
  145. phase = (phase + PHASE_STEP) % RAINBOW_FULL;
  146. frames++;
  147. if (frame_start - last_fps >= 1000) {
  148. displayed = frames;
  149. frames = 0;
  150. last_fps = frame_start;
  151. }
  152. format_fps(displayed, fps_buf);
  153. draw_string(px, w, h, 0, 0, fps_buf);
  154. u_win_present();
  155. elapsed = u_ticks_ms() - frame_start;
  156. if (elapsed < FRAME_MS)
  157. u_sleep_ms(FRAME_MS - elapsed);
  158. }
  159. u_win_close();
  160. u_exit(0);
  161. }