ucmd_clock.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* clock: analog clock drawn into the terminal window's canvas. ESC (or
  2. * Ctrl+C, which the terminal turns into a kill) returns to the shell. */
  3. #include "ucmd_lib.h"
  4. /* Palette index to RGB. 0=black, 7=gray, 12=light red, 15=white */
  5. static const unsigned char pal_r[16] = { 0, 0, 0, 0, 170, 170, 170, 170, 85, 85, 85, 85, 255, 255, 255, 255 };
  6. static const unsigned char pal_g[16] = { 0, 0, 170, 170, 0, 0, 85, 170, 85, 85, 255, 255, 85, 85, 255, 255 };
  7. static const unsigned char pal_b[16] = { 0, 170, 0, 170, 0, 170, 0, 170, 85, 255, 85, 255, 85, 255, 85, 255 };
  8. #define COL_BG 0
  9. #define COL_FACE 15
  10. #define COL_TICK 7
  11. #define COL_HOUR 15
  12. #define COL_MIN 15
  13. #define COL_SEC 12
  14. #define COL_CENTER 15
  15. /* Unit-vector table at radius 64 for 60 clock positions (from the old kernel
  16. * clock.c). */
  17. static const signed char tab_x[60] = {
  18. 0, 7, 13, 20, 26, 32, 37, 42, 47, 51,
  19. 55, 59, 62, 63, 64, 63, 62, 59, 55, 51,
  20. 47, 42, 37, 32, 26, 20, 13, 7, 0, -7,
  21. -13,-20,-26,-32,-37,-42,-47,-51,-55,-59,
  22. -62,-63,-64,-63,-62,-59,-55,-51,-47,-42,
  23. -37,-32,-26,-20,-13, -7
  24. };
  25. static const signed char tab_y[60] = {
  26. -64,-63,-62,-59,-55,-51,-47,-42,-37,-32,
  27. -26,-20,-13, -7, 0, 7, 13, 20, 26, 32,
  28. 37, 42, 47, 51, 55, 59, 62, 63, 64, 63,
  29. 62, 59, 55, 51, 47, 42, 37, 32, 26, 20,
  30. 13, 7, 0, -7,-13,-20,-26,-32,-37,-42,
  31. -47,-51,-55,-59,-62,-63
  32. };
  33. /* Hour tick positions at radius 256; 256*sin(h*30), -256*cos(h*30). */
  34. static const short hour_tab_x[12] = {
  35. 0, 128, 222, 256, 222, 128, 0, -128, -222, -256, -222, -128
  36. };
  37. static const short hour_tab_y[12] = {
  38. -256, -222, -128, 0, 128, 222, 256, 222, 128, 0, -128, -222
  39. };
  40. static unsigned short *fb;
  41. static unsigned int cw, ch;
  42. static unsigned int cx, cy, radius, rad_hour, rad_min, rad_sec, tick_len;
  43. static unsigned short rgb565(unsigned int r, unsigned int g, unsigned int b)
  44. {
  45. return (unsigned short)(((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3));
  46. }
  47. static void put_pixel(int x, int y, unsigned char color)
  48. {
  49. if (x < 0 || y < 0 || (unsigned int)x >= cw || (unsigned int)y >= ch)
  50. return;
  51. fb[(unsigned int)y * cw + (unsigned int)x] =
  52. rgb565(pal_r[color], pal_g[color], pal_b[color]);
  53. }
  54. static void clear_screen(unsigned char color)
  55. {
  56. unsigned int i, n = cw * ch;
  57. unsigned short v = rgb565(pal_r[color], pal_g[color], pal_b[color]);
  58. for (i = 0; i < n; i++)
  59. fb[i] = v;
  60. }
  61. static void draw_line(int x0, int y0, int x1, int y1, unsigned char color)
  62. {
  63. int dx = x1 - x0, dy = y1 - y0;
  64. int ax = (dx < 0) ? -dx : dx, ay = (dy < 0) ? -dy : dy;
  65. int steps = (ax > ay) ? ax : ay;
  66. int i;
  67. if (steps == 0) {
  68. put_pixel(x0, y0, color);
  69. return;
  70. }
  71. for (i = 0; i <= steps; i++)
  72. put_pixel(x0 + dx * i / steps, y0 + dy * i / steps, color);
  73. }
  74. static void draw_circle(int ccx, int ccy, int r, unsigned char color)
  75. {
  76. int x = r, y = 0, d = 1 - r;
  77. while (y <= x) {
  78. put_pixel(ccx + x, ccy + y, color);
  79. put_pixel(ccx - x, ccy + y, color);
  80. put_pixel(ccx + x, ccy - y, color);
  81. put_pixel(ccx - x, ccy - y, color);
  82. put_pixel(ccx + y, ccy + x, color);
  83. put_pixel(ccx - y, ccy + x, color);
  84. put_pixel(ccx + y, ccy - x, color);
  85. put_pixel(ccx - y, ccy - x, color);
  86. if (d <= 0)
  87. d += 2 * y + 3;
  88. else {
  89. d += 2 * (y - x) + 5;
  90. x--;
  91. }
  92. y++;
  93. }
  94. }
  95. static void draw_thick_line(int x0, int y0, int x1, int y1, unsigned char color)
  96. {
  97. draw_line(x0, y0, x1, y1, color);
  98. draw_line(x0 + 1, y0, x1 + 1, y1, color);
  99. draw_line(x0, y0 + 1, x1, y1 + 1, color);
  100. }
  101. static void draw_face(void)
  102. {
  103. int i, inner = (int)radius - (int)tick_len;
  104. draw_circle((int)cx, (int)cy, (int)radius, COL_FACE);
  105. draw_circle((int)cx, (int)cy, (int)radius - 1, COL_FACE);
  106. for (i = 0; i < 12; i++) {
  107. int ox = (int)cx + (hour_tab_x[i] * (int)radius + 128) / 256;
  108. int oy = (int)cy + (hour_tab_y[i] * (int)radius + 128) / 256;
  109. int ix = (int)cx + (hour_tab_x[i] * inner + 128) / 256;
  110. int iy = (int)cy + (hour_tab_y[i] * inner + 128) / 256;
  111. draw_line(ix, iy, ox, oy, COL_TICK);
  112. }
  113. }
  114. static void draw_hand(int idx60, int length, unsigned char color, int thick)
  115. {
  116. int ex = (int)cx + tab_x[idx60] * length / 64;
  117. int ey = (int)cy + tab_y[idx60] * length / 64;
  118. if (thick)
  119. draw_thick_line((int)cx, (int)cy, ex, ey, color);
  120. else
  121. draw_line((int)cx, (int)cy, ex, ey, color);
  122. }
  123. static void draw_hands(unsigned int h, unsigned int m, unsigned int s)
  124. {
  125. int hour_idx = (int)((h % 12) * 5 + m / 12);
  126. if (hour_idx >= 60)
  127. hour_idx = 59;
  128. draw_hand(hour_idx, (int)rad_hour, COL_HOUR, 1);
  129. draw_hand((int)m, (int)rad_min, COL_MIN, 1);
  130. draw_hand((int)s, (int)rad_sec, COL_SEC, 0);
  131. put_pixel((int)cx, (int)cy, COL_CENTER);
  132. put_pixel((int)cx + 1, (int)cy, COL_CENTER);
  133. put_pixel((int)cx, (int)cy + 1, COL_CENTER);
  134. put_pixel((int)cx + 1, (int)cy + 1, COL_CENTER);
  135. }
  136. void _start(const char *arg)
  137. {
  138. struct win_info info;
  139. struct sys_time tm;
  140. unsigned int last_sec = 99;
  141. (void)arg;
  142. if (u_win_create(512, 320, "clock", &info) != 0) {
  143. u_puts("clock: no graphics window\n");
  144. u_exit(1);
  145. }
  146. fb = (unsigned short *)info.fb;
  147. cw = info.width;
  148. ch = info.height;
  149. cx = cw / 2;
  150. cy = ch / 2;
  151. radius = (cw < ch) ? cw / 2 : ch / 2;
  152. if (radius > 200)
  153. radius = 200;
  154. rad_hour = radius / 2;
  155. rad_min = radius * 7 / 10;
  156. rad_sec = radius * 9 / 10;
  157. tick_len = radius / 13;
  158. if (tick_len < 2)
  159. tick_len = 2;
  160. for (;;) {
  161. int k = u_key_poll();
  162. if (k == 0x1B || k == 0x03)
  163. break;
  164. u_rtc_hms(&tm);
  165. if (tm.hour > 23 || tm.minute > 59 || tm.second > 59) {
  166. /* bogus read (should not happen): never index with it */
  167. u_sleep_ms(50);
  168. continue;
  169. }
  170. if (tm.second != last_sec) {
  171. clear_screen(COL_BG);
  172. draw_face();
  173. draw_hands(tm.hour, tm.minute, tm.second);
  174. last_sec = tm.second;
  175. u_win_present();
  176. }
  177. u_sleep_ms(50);
  178. }
  179. u_win_close();
  180. u_exit(0);
  181. }