clock.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* clock command: analog clock in VESA LFB. Press ESC to return. */
  2. #include "io.h"
  3. #include "rtc.h"
  4. #include "timer.h"
  5. #include "vga.h"
  6. /* Palette index to RGB. 0=black, 7=gray, 12=light red, 15=white */
  7. static const unsigned int pal_r[16] = { 0, 0, 0, 0, 170, 170, 170, 170, 85, 85, 85, 85, 255, 255, 255, 255 };
  8. static const unsigned int pal_g[16] = { 0, 0, 170, 170, 0, 0, 85, 170, 85, 85, 255, 255, 85, 85, 255, 255 };
  9. static const unsigned int pal_b[16] = { 0, 170, 0, 170, 0, 170, 0, 170, 85, 255, 85, 255, 85, 255, 85, 255 };
  10. #define COL_BG 0
  11. #define COL_FACE 15
  12. #define COL_TICK 7
  13. #define COL_HOUR 15
  14. #define COL_MIN 15
  15. #define COL_SEC 12
  16. #define COL_CENTER 15
  17. /*
  18. * Precomputed unit-vector table at radius 64 for 60 clock positions.
  19. */
  20. static const signed char tab_x[60] = {
  21. 0, 7, 13, 20, 26, 32, 37, 42, 47, 51,
  22. 55, 59, 62, 63, 64, 63, 62, 59, 55, 51,
  23. 47, 42, 37, 32, 26, 20, 13, 7, 0, -7,
  24. -13,-20,-26,-32,-37,-42,-47,-51,-55,-59,
  25. -62,-63,-64,-63,-62,-59,-55,-51,-47,-42,
  26. -37,-32,-26,-20,-13, -7
  27. };
  28. static const signed char tab_y[60] = {
  29. -64,-63,-62,-59,-55,-51,-47,-42,-37,-32,
  30. -26,-20,-13, -7, 0, 7, 13, 20, 26, 32,
  31. 37, 42, 47, 51, 55, 59, 62, 63, 64, 63,
  32. 62, 59, 55, 51, 47, 42, 37, 32, 26, 20,
  33. 13, 7, 0, -7,-13,-20,-26,-32,-37,-42,
  34. -47,-51,-55,-59,-62,-63
  35. };
  36. /*
  37. * Hour tick positions at radius 256 for exact alignment on the circle.
  38. * For hour h (0=12 o'clock): x = 256*sin(h*30°), y = -256*cos(h*30°).
  39. * 256*√3/2 ≈ 222.
  40. */
  41. static const short hour_tab_x[12] = {
  42. 0, 128, 222, 256, 222, 128, 0, -128, -222, -256, -222, -128
  43. };
  44. static const short hour_tab_y[12] = {
  45. -256, -222, -128, 0, 128, 222, 256, 222, 128, 0, -128, -222
  46. };
  47. static unsigned int clock_cx, clock_cy, clock_radius, clock_rad_hour, clock_rad_min, clock_rad_sec, clock_tick_len;
  48. static void put_pixel(int x, int y, unsigned char color)
  49. {
  50. vga_put_pixel_rgb((unsigned int)x, (unsigned int)y,
  51. pal_r[color], pal_g[color], pal_b[color]);
  52. }
  53. static void clear_screen(unsigned char color)
  54. {
  55. vga_clear_rgb(pal_r[color], pal_g[color], pal_b[color]);
  56. }
  57. static void draw_line(int x0, int y0, int x1, int y1, unsigned char color)
  58. {
  59. int dx = x1 - x0;
  60. int dy = y1 - y0;
  61. int ax = (dx < 0) ? -dx : dx;
  62. int ay = (dy < 0) ? -dy : dy;
  63. int steps = (ax > ay) ? ax : ay;
  64. int i;
  65. if (steps == 0) {
  66. put_pixel(x0, y0, color);
  67. return;
  68. }
  69. for (i = 0; i <= steps; i++) {
  70. int x = x0 + dx * i / steps;
  71. int y = y0 + dy * i / steps;
  72. put_pixel(x, y, color);
  73. }
  74. }
  75. static void draw_circle(int cx, int cy, int r, unsigned char color)
  76. {
  77. int x = r, y = 0;
  78. int d = 1 - r;
  79. while (y <= x) {
  80. put_pixel(cx + x, cy + y, color);
  81. put_pixel(cx - x, cy + y, color);
  82. put_pixel(cx + x, cy - y, color);
  83. put_pixel(cx - x, cy - y, color);
  84. put_pixel(cx + y, cy + x, color);
  85. put_pixel(cx - y, cy + x, color);
  86. put_pixel(cx + y, cy - x, color);
  87. put_pixel(cx - y, cy - x, color);
  88. if (d <= 0) {
  89. d += 2 * y + 3;
  90. } else {
  91. d += 2 * (y - x) + 5;
  92. x--;
  93. }
  94. y++;
  95. }
  96. }
  97. static void draw_thick_line(int x0, int y0, int x1, int y1, unsigned char color)
  98. {
  99. draw_line(x0, y0, x1, y1, color);
  100. draw_line(x0 + 1, y0, x1 + 1, y1, color);
  101. draw_line(x0, y0 + 1, x1, y1 + 1, color);
  102. }
  103. static void draw_face(void)
  104. {
  105. int i;
  106. int inner = clock_radius - clock_tick_len;
  107. draw_circle(clock_cx, clock_cy, clock_radius, COL_FACE);
  108. draw_circle(clock_cx, clock_cy, clock_radius - 1, COL_FACE);
  109. for (i = 0; i < 12; i++) {
  110. /* Use high-precision hour table and rounded scaling so ticks sit on the circle */
  111. int ox = clock_cx + (hour_tab_x[i] * (int)clock_radius + 128) / 256;
  112. int oy = clock_cy + (hour_tab_y[i] * (int)clock_radius + 128) / 256;
  113. int ix = clock_cx + (hour_tab_x[i] * (int)inner + 128) / 256;
  114. int iy = clock_cy + (hour_tab_y[i] * (int)inner + 128) / 256;
  115. draw_line(ix, iy, ox, oy, COL_TICK);
  116. }
  117. }
  118. static void draw_hand(int idx60, int length, unsigned char color, int thick)
  119. {
  120. int ex = clock_cx + tab_x[idx60] * (int)length / 64;
  121. int ey = clock_cy + tab_y[idx60] * (int)length / 64;
  122. if (thick)
  123. draw_thick_line(clock_cx, clock_cy, ex, ey, color);
  124. else
  125. draw_line(clock_cx, clock_cy, ex, ey, color);
  126. }
  127. static void draw_hands(unsigned int h, unsigned int m, unsigned int s)
  128. {
  129. int hour_idx;
  130. hour_idx = (int)((h % 12) * 5 + m / 12);
  131. if (hour_idx >= 60)
  132. hour_idx = 59;
  133. draw_hand(hour_idx, (int)clock_rad_hour, COL_HOUR, 1);
  134. draw_hand((int)m, (int)clock_rad_min, COL_MIN, 1);
  135. draw_hand((int)s, (int)clock_rad_sec, COL_SEC, 0);
  136. put_pixel(clock_cx, clock_cy, COL_CENTER);
  137. put_pixel(clock_cx + 1, clock_cy, COL_CENTER);
  138. put_pixel(clock_cx, clock_cy + 1, COL_CENTER);
  139. put_pixel(clock_cx + 1, clock_cy + 1, COL_CENTER);
  140. }
  141. void program_clock_main(void)
  142. {
  143. unsigned int last_sec = 99;
  144. unsigned int h, m, s;
  145. unsigned int tick;
  146. unsigned int w, h_;
  147. w = vga_fb_width();
  148. h_ = vga_fb_height();
  149. clock_cx = w / 2;
  150. clock_cy = h_ / 2;
  151. clock_radius = (w < h_) ? (w / 2) : (h_ / 2);
  152. if (clock_radius > 200)
  153. clock_radius = 200;
  154. clock_rad_hour = clock_radius / 2;
  155. clock_rad_min = clock_radius * 7 / 10;
  156. clock_rad_sec = clock_radius * 9 / 10;
  157. clock_tick_len = clock_radius / 13;
  158. if (clock_tick_len < 2)
  159. clock_tick_len = 2;
  160. clear_screen(COL_BG);
  161. io_drain_keyboard();
  162. for (;;) {
  163. int k = io_poll_key();
  164. if (k == 0x1B)
  165. break;
  166. rtc_read_hms(&h, &m, &s);
  167. if (s != last_sec) {
  168. clear_screen(COL_BG);
  169. draw_face();
  170. draw_hands(h, m, s);
  171. last_sec = s;
  172. }
  173. tick = get_ticks_ms();
  174. while (get_ticks_ms() - tick < 50)
  175. __asm__ volatile ("hlt");
  176. }
  177. }