ucmd_clock.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 void clock_geometry(void)
  44. {
  45. cx = cw / 2;
  46. cy = ch / 2;
  47. radius = (cw < ch) ? cw / 2 : ch / 2;
  48. if (radius > 200)
  49. radius = 200;
  50. rad_hour = radius / 2;
  51. rad_min = radius * 7 / 10;
  52. rad_sec = radius * 9 / 10;
  53. tick_len = radius / 13;
  54. if (tick_len < 2)
  55. tick_len = 2;
  56. }
  57. static unsigned short rgb565(unsigned int r, unsigned int g, unsigned int b)
  58. {
  59. return (unsigned short)(((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3));
  60. }
  61. static void put_pixel(int x, int y, unsigned char color)
  62. {
  63. if (x < 0 || y < 0 || (unsigned int)x >= cw || (unsigned int)y >= ch)
  64. return;
  65. fb[(unsigned int)y * cw + (unsigned int)x] =
  66. rgb565(pal_r[color], pal_g[color], pal_b[color]);
  67. }
  68. static void clear_screen(unsigned char color)
  69. {
  70. unsigned int i, n = cw * ch;
  71. unsigned short v = rgb565(pal_r[color], pal_g[color], pal_b[color]);
  72. for (i = 0; i < n; i++)
  73. fb[i] = v;
  74. }
  75. static void draw_line(int x0, int y0, int x1, int y1, unsigned char color)
  76. {
  77. int dx = x1 - x0, dy = y1 - y0;
  78. int ax = (dx < 0) ? -dx : dx, ay = (dy < 0) ? -dy : dy;
  79. int steps = (ax > ay) ? ax : ay;
  80. int i;
  81. if (steps == 0) {
  82. put_pixel(x0, y0, color);
  83. return;
  84. }
  85. for (i = 0; i <= steps; i++)
  86. put_pixel(x0 + dx * i / steps, y0 + dy * i / steps, color);
  87. }
  88. static void draw_circle(int ccx, int ccy, int r, unsigned char color)
  89. {
  90. int x = r, y = 0, d = 1 - r;
  91. while (y <= x) {
  92. put_pixel(ccx + x, ccy + y, color);
  93. put_pixel(ccx - x, ccy + y, color);
  94. put_pixel(ccx + x, ccy - y, color);
  95. put_pixel(ccx - x, ccy - y, color);
  96. put_pixel(ccx + y, ccy + x, color);
  97. put_pixel(ccx - y, ccy + x, color);
  98. put_pixel(ccx + y, ccy - x, color);
  99. put_pixel(ccx - y, ccy - x, color);
  100. if (d <= 0)
  101. d += 2 * y + 3;
  102. else {
  103. d += 2 * (y - x) + 5;
  104. x--;
  105. }
  106. y++;
  107. }
  108. }
  109. static void draw_thick_line(int x0, int y0, int x1, int y1, unsigned char color)
  110. {
  111. draw_line(x0, y0, x1, y1, color);
  112. draw_line(x0 + 1, y0, x1 + 1, y1, color);
  113. draw_line(x0, y0 + 1, x1, y1 + 1, color);
  114. }
  115. static void draw_face(void)
  116. {
  117. int i, inner = (int)radius - (int)tick_len;
  118. draw_circle((int)cx, (int)cy, (int)radius, COL_FACE);
  119. draw_circle((int)cx, (int)cy, (int)radius - 1, COL_FACE);
  120. for (i = 0; i < 12; i++) {
  121. int ox = (int)cx + (hour_tab_x[i] * (int)radius + 128) / 256;
  122. int oy = (int)cy + (hour_tab_y[i] * (int)radius + 128) / 256;
  123. int ix = (int)cx + (hour_tab_x[i] * inner + 128) / 256;
  124. int iy = (int)cy + (hour_tab_y[i] * inner + 128) / 256;
  125. draw_line(ix, iy, ox, oy, COL_TICK);
  126. }
  127. }
  128. static void draw_hand(int idx60, int length, unsigned char color, int thick)
  129. {
  130. int ex = (int)cx + tab_x[idx60] * length / 64;
  131. int ey = (int)cy + tab_y[idx60] * length / 64;
  132. if (thick)
  133. draw_thick_line((int)cx, (int)cy, ex, ey, color);
  134. else
  135. draw_line((int)cx, (int)cy, ex, ey, color);
  136. }
  137. static void draw_hands(unsigned int h, unsigned int m, unsigned int s)
  138. {
  139. int hour_idx = (int)((h % 12) * 5 + m / 12);
  140. if (hour_idx >= 60)
  141. hour_idx = 59;
  142. draw_hand(hour_idx, (int)rad_hour, COL_HOUR, 1);
  143. draw_hand((int)m, (int)rad_min, COL_MIN, 1);
  144. draw_hand((int)s, (int)rad_sec, COL_SEC, 0);
  145. put_pixel((int)cx, (int)cy, COL_CENTER);
  146. put_pixel((int)cx + 1, (int)cy, COL_CENTER);
  147. put_pixel((int)cx, (int)cy + 1, COL_CENTER);
  148. put_pixel((int)cx + 1, (int)cy + 1, COL_CENTER);
  149. }
  150. void _start(const char *arg)
  151. {
  152. struct win_info info;
  153. struct sys_time tm;
  154. unsigned int last_sec = 99;
  155. (void)arg;
  156. if (u_win_create(512, 320, "clock", &info) != 0) {
  157. u_puts("clock: no graphics window\n");
  158. u_exit(1);
  159. }
  160. fb = (unsigned short *)info.fb;
  161. cw = info.width;
  162. ch = info.height;
  163. clock_geometry();
  164. for (;;) {
  165. struct win_info cur;
  166. int k = u_key_poll();
  167. if (u_win_info(&cur) == 0 &&
  168. (cur.width != cw || cur.height != ch)) {
  169. if (u_win_ack_resize() == 0) {
  170. fb = (unsigned short *)cur.fb;
  171. cw = cur.width;
  172. ch = cur.height;
  173. clock_geometry();
  174. last_sec = 99;
  175. }
  176. }
  177. if (k == 0x1B || k == 0x03)
  178. break;
  179. u_rtc_hms(&tm);
  180. if (tm.hour > 23 || tm.minute > 59 || tm.second > 59) {
  181. /* bogus read (should not happen): never index with it */
  182. u_sleep_ms(50);
  183. continue;
  184. }
  185. if (tm.second != last_sec) {
  186. clear_screen(COL_BG);
  187. draw_face();
  188. draw_hands(tm.hour, tm.minute, tm.second);
  189. last_sec = tm.second;
  190. u_win_present();
  191. }
  192. u_sleep_ms(50);
  193. }
  194. u_win_close();
  195. u_exit(0);
  196. }