clock.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. static unsigned int clock_cx, clock_cy, clock_radius, clock_rad_hour, clock_rad_min, clock_rad_sec, clock_tick_len;
  37. static void put_pixel(int x, int y, unsigned char color)
  38. {
  39. vga_put_pixel_rgb((unsigned int)x, (unsigned int)y,
  40. pal_r[color], pal_g[color], pal_b[color]);
  41. }
  42. static void clear_screen(unsigned char color)
  43. {
  44. vga_clear_rgb(pal_r[color], pal_g[color], pal_b[color]);
  45. }
  46. static void draw_line(int x0, int y0, int x1, int y1, unsigned char color)
  47. {
  48. int dx = x1 - x0;
  49. int dy = y1 - y0;
  50. int ax = (dx < 0) ? -dx : dx;
  51. int ay = (dy < 0) ? -dy : dy;
  52. int steps = (ax > ay) ? ax : ay;
  53. int i;
  54. if (steps == 0) {
  55. put_pixel(x0, y0, color);
  56. return;
  57. }
  58. for (i = 0; i <= steps; i++) {
  59. int x = x0 + dx * i / steps;
  60. int y = y0 + dy * i / steps;
  61. put_pixel(x, y, color);
  62. }
  63. }
  64. static void draw_circle(int cx, int cy, int r, unsigned char color)
  65. {
  66. int x = r, y = 0;
  67. int d = 1 - r;
  68. while (y <= x) {
  69. put_pixel(cx + x, cy + y, color);
  70. put_pixel(cx - x, cy + y, color);
  71. put_pixel(cx + x, cy - y, color);
  72. put_pixel(cx - x, cy - y, color);
  73. put_pixel(cx + y, cy + x, color);
  74. put_pixel(cx - y, cy + x, color);
  75. put_pixel(cx + y, cy - x, color);
  76. put_pixel(cx - y, cy - x, color);
  77. if (d <= 0) {
  78. d += 2 * y + 3;
  79. } else {
  80. d += 2 * (y - x) + 5;
  81. x--;
  82. }
  83. y++;
  84. }
  85. }
  86. static void draw_thick_line(int x0, int y0, int x1, int y1, unsigned char color)
  87. {
  88. draw_line(x0, y0, x1, y1, color);
  89. draw_line(x0 + 1, y0, x1 + 1, y1, color);
  90. draw_line(x0, y0 + 1, x1, y1 + 1, color);
  91. }
  92. static void draw_face(void)
  93. {
  94. int i;
  95. int inner = clock_radius - clock_tick_len;
  96. draw_circle(clock_cx, clock_cy, clock_radius, COL_FACE);
  97. draw_circle(clock_cx, clock_cy, clock_radius - 1, COL_FACE);
  98. for (i = 0; i < 60; i += 5) {
  99. int ox = clock_cx + tab_x[i] * (int)clock_radius / 64;
  100. int oy = clock_cy + tab_y[i] * (int)clock_radius / 64;
  101. int ix = clock_cx + tab_x[i] * (int)inner / 64;
  102. int iy = clock_cy + tab_y[i] * (int)inner / 64;
  103. draw_line(ix, iy, ox, oy, COL_TICK);
  104. }
  105. }
  106. static void draw_hand(int idx60, int length, unsigned char color, int thick)
  107. {
  108. int ex = clock_cx + tab_x[idx60] * (int)length / 64;
  109. int ey = clock_cy + tab_y[idx60] * (int)length / 64;
  110. if (thick)
  111. draw_thick_line(clock_cx, clock_cy, ex, ey, color);
  112. else
  113. draw_line(clock_cx, clock_cy, ex, ey, color);
  114. }
  115. static void draw_hands(unsigned int h, unsigned int m, unsigned int s)
  116. {
  117. int hour_idx;
  118. hour_idx = (int)((h % 12) * 5 + m / 12);
  119. if (hour_idx >= 60)
  120. hour_idx = 59;
  121. draw_hand(hour_idx, (int)clock_rad_hour, COL_HOUR, 1);
  122. draw_hand((int)m, (int)clock_rad_min, COL_MIN, 1);
  123. draw_hand((int)s, (int)clock_rad_sec, COL_SEC, 0);
  124. put_pixel(clock_cx, clock_cy, COL_CENTER);
  125. put_pixel(clock_cx + 1, clock_cy, COL_CENTER);
  126. put_pixel(clock_cx, clock_cy + 1, COL_CENTER);
  127. put_pixel(clock_cx + 1, clock_cy + 1, COL_CENTER);
  128. }
  129. void program_clock_main(void)
  130. {
  131. unsigned int last_sec = 99;
  132. unsigned int h, m, s;
  133. unsigned int tick;
  134. unsigned int w, h_;
  135. w = vga_fb_width();
  136. h_ = vga_fb_height();
  137. clock_cx = w / 2;
  138. clock_cy = h_ / 2;
  139. clock_radius = (w < h_) ? (w / 2) : (h_ / 2);
  140. if (clock_radius > 200)
  141. clock_radius = 200;
  142. clock_rad_hour = clock_radius / 2;
  143. clock_rad_min = clock_radius * 7 / 10;
  144. clock_rad_sec = clock_radius * 9 / 10;
  145. clock_tick_len = clock_radius / 13;
  146. if (clock_tick_len < 2)
  147. clock_tick_len = 2;
  148. clear_screen(COL_BG);
  149. io_drain_keyboard();
  150. for (;;) {
  151. int k = io_poll_key();
  152. if (k == 0x1B)
  153. break;
  154. rtc_read_hms(&h, &m, &s);
  155. if (s != last_sec) {
  156. clear_screen(COL_BG);
  157. draw_face();
  158. draw_hands(h, m, s);
  159. last_sec = s;
  160. }
  161. tick = get_ticks_ms();
  162. while (get_ticks_ms() - tick < 50)
  163. __asm__ volatile ("hlt");
  164. }
  165. }