clock.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* clock command: analog clock in VGA mode 13h. Press ESC to return. */
  2. #include "io.h"
  3. #include "rtc.h"
  4. #include "timer.h"
  5. #include "vga.h"
  6. #define CX (VGA_GFX_WIDTH / 2)
  7. #define CY (VGA_GFX_HEIGHT / 2)
  8. #define RADIUS 80
  9. #define RAD_HOUR 40
  10. #define RAD_MIN 56
  11. #define RAD_SEC 72
  12. #define TICK_LEN 6
  13. #define COL_BG 0
  14. #define COL_FACE 15 /* white */
  15. #define COL_TICK 7 /* gray */
  16. #define COL_HOUR 15
  17. #define COL_MIN 15
  18. #define COL_SEC 12 /* light red */
  19. #define COL_CENTER 15
  20. /*
  21. * Precomputed unit-vector table at radius 64 for 60 clock positions.
  22. * Index 0 = 12 o'clock (up), 15 = 3 o'clock, 30 = 6 o'clock, etc.
  23. * x = 64*sin(2*pi*i/60), y = -64*cos(2*pi*i/60)
  24. */
  25. static const signed char tab_x[60] = {
  26. 0, 7, 13, 20, 26, 32, 37, 42, 47, 51,
  27. 55, 59, 62, 63, 64, 63, 62, 59, 55, 51,
  28. 47, 42, 37, 32, 26, 20, 13, 7, 0, -7,
  29. -13,-20,-26,-32,-37,-42,-47,-51,-55,-59,
  30. -62,-63,-64,-63,-62,-59,-55,-51,-47,-42,
  31. -37,-32,-26,-20,-13, -7
  32. };
  33. static const signed char tab_y[60] = {
  34. -64,-63,-62,-59,-55,-51,-47,-42,-37,-32,
  35. -26,-20,-13, -7, 0, 7, 13, 20, 26, 32,
  36. 37, 42, 47, 51, 55, 59, 62, 63, 64, 63,
  37. 62, 59, 55, 51, 47, 42, 37, 32, 26, 20,
  38. 13, 7, 0, -7,-13,-20,-26,-32,-37,-42,
  39. -47,-51,-55,-59,-62,-63
  40. };
  41. /* ---- drawing primitives ---- */
  42. static void draw_line(int x0, int y0, int x1, int y1, unsigned char color)
  43. {
  44. int dx = x1 - x0;
  45. int dy = y1 - y0;
  46. int ax = (dx < 0) ? -dx : dx;
  47. int ay = (dy < 0) ? -dy : dy;
  48. int steps = (ax > ay) ? ax : ay;
  49. int i;
  50. if (steps == 0) {
  51. vga_put_pixel(x0, y0, color);
  52. return;
  53. }
  54. for (i = 0; i <= steps; i++) {
  55. int x = x0 + dx * i / steps;
  56. int y = y0 + dy * i / steps;
  57. vga_put_pixel(x, y, color);
  58. }
  59. }
  60. static void draw_circle(int cx, int cy, int r, unsigned char color)
  61. {
  62. int x = r, y = 0;
  63. int d = 1 - r;
  64. while (y <= x) {
  65. vga_put_pixel(cx + x, cy + y, color);
  66. vga_put_pixel(cx - x, cy + y, color);
  67. vga_put_pixel(cx + x, cy - y, color);
  68. vga_put_pixel(cx - x, cy - y, color);
  69. vga_put_pixel(cx + y, cy + x, color);
  70. vga_put_pixel(cx - y, cy + x, color);
  71. vga_put_pixel(cx + y, cy - x, color);
  72. vga_put_pixel(cx - y, cy - x, color);
  73. if (d <= 0) {
  74. d += 2 * y + 3;
  75. } else {
  76. d += 2 * (y - x) + 5;
  77. x--;
  78. }
  79. y++;
  80. }
  81. }
  82. static void draw_thick_line(int x0, int y0, int x1, int y1, unsigned char color)
  83. {
  84. draw_line(x0, y0, x1, y1, color);
  85. draw_line(x0 + 1, y0, x1 + 1, y1, color);
  86. draw_line(x0, y0 + 1, x1, y1 + 1, color);
  87. }
  88. /* ---- clock rendering ---- */
  89. static void draw_face(void)
  90. {
  91. int i;
  92. draw_circle(CX, CY, RADIUS, COL_FACE);
  93. draw_circle(CX, CY, RADIUS - 1, COL_FACE);
  94. /* Hour tick marks */
  95. for (i = 0; i < 60; i += 5) {
  96. int inner = RADIUS - TICK_LEN;
  97. int ox = CX + tab_x[i] * RADIUS / 64;
  98. int oy = CY + tab_y[i] * RADIUS / 64;
  99. int ix = CX + tab_x[i] * inner / 64;
  100. int iy = CY + tab_y[i] * inner / 64;
  101. draw_line(ix, iy, ox, oy, COL_TICK);
  102. }
  103. }
  104. static void draw_hand(int idx60, int length, unsigned char color, int thick)
  105. {
  106. int ex = CX + tab_x[idx60] * length / 64;
  107. int ey = CY + tab_y[idx60] * length / 64;
  108. if (thick)
  109. draw_thick_line(CX, CY, ex, ey, color);
  110. else
  111. draw_line(CX, CY, ex, ey, color);
  112. }
  113. static void draw_hands(unsigned int h, unsigned int m, unsigned int s)
  114. {
  115. int hour_idx;
  116. /* Hour hand: 5 positions per hour + minute offset */
  117. hour_idx = (int)((h % 12) * 5 + m / 12);
  118. if (hour_idx >= 60)
  119. hour_idx = 59;
  120. draw_hand(hour_idx, RAD_HOUR, COL_HOUR, 1);
  121. draw_hand((int)m, RAD_MIN, COL_MIN, 1);
  122. draw_hand((int)s, RAD_SEC, COL_SEC, 0);
  123. /* Center dot */
  124. vga_put_pixel(CX, CY, COL_CENTER);
  125. vga_put_pixel(CX + 1, CY, COL_CENTER);
  126. vga_put_pixel(CX, CY + 1, COL_CENTER);
  127. vga_put_pixel(CX + 1, CY + 1, COL_CENTER);
  128. }
  129. /* ---- main entry point ---- */
  130. void program_clock_main(void)
  131. {
  132. unsigned int last_sec = 99;
  133. unsigned int h, m, s;
  134. unsigned int tick;
  135. vga_set_mode_13();
  136. vga_clear(COL_BG);
  137. io_drain_keyboard();
  138. for (;;) {
  139. int k = io_poll_key();
  140. if (k == 0x1B)
  141. break;
  142. rtc_read_hms(&h, &m, &s);
  143. if (s != last_sec) {
  144. vga_clear(COL_BG);
  145. draw_face();
  146. draw_hands(h, m, s);
  147. last_sec = s;
  148. }
  149. tick = get_ticks_ms();
  150. while (get_ticks_ms() - tick < 50)
  151. __asm__ volatile ("hlt");
  152. }
  153. vga_set_mode_text();
  154. io_clear_screen();
  155. }