clock.c 5.4 KB

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