Skip to content

Commit

Permalink
模拟时钟
Browse files Browse the repository at this point in the history
  • Loading branch information
xwwwb committed Jun 8, 2023
1 parent fd8cfdc commit 8c2c73b
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/25_day/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ clock.bim : clock.obj a_nask.obj Makefile
clock.obj a_nask.obj

clock.hrb : clock.bim Makefile
$(BIM2HRB) clock.bim clock.hrb 56k
$(BIM2HRB) clock.bim clock.hrb 100k



Expand Down
20 changes: 20 additions & 0 deletions src/25_day/a_nask.nas
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
GLOBAL _api_getyear
GLOBAL _api_getmonth
GLOBAL _api_getday
GLOBAL _api_drawcircle

[SECTION .text]

Expand Down Expand Up @@ -275,4 +276,23 @@ _api_getday: ; int api_getday(void);
MOV EDX,35
INT 0x40
POP EBX
RET

_api_drawcircle: ; void api_drawcircle(int win, int x, int y, int r, int nouse, int col);
PUSH EDI
PUSH ESI
PUSH EBP
PUSH EBX
MOV EDX,40
MOV EBX,[ESP+20] ; win
MOV EAX,[ESP+24] ; x
MOV ECX,[ESP+28] ; y
MOV ESI,[ESP+32] ; r
MOV EDI,[ESP+36] ; nouse
MOV EBP,[ESP+40] ; col
INT 0x40
POP EBX
POP EBP
POP ESI
POP EDI
RET
1 change: 1 addition & 0 deletions src/25_day/bootpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ void putblock8_8(char *vram, int vxsize, int pxsize,
int pysize, int px0, int py0, char *buf, int bxsize);
void putfont16(char *vram, int xsize, int x, int y, char c, char font[16][16]);
char *getzhfont(int c);
void drawcircle(char *vram, int x0, int y0, int r0, unsigned char c, int xsize);
#define COL8_000000 0
#define COL8_FF0000 1
#define COL8_00FF00 2
Expand Down
157 changes: 140 additions & 17 deletions src/25_day/clock.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <math.h>
#include <stdio.h>
int api_openwin(char *buf, int xsiz, int ysiz, int col_inv, char *title);
void api_initmalloc(void);
Expand All @@ -14,31 +15,153 @@ int api_gethour(void);
int ap_getyear(void);
int api_getmonth(void);
int api_getday(void);
void api_drawcircle(int win, int x, int y, int r, int nouse, int col);
int api_alloctimer(void);
void api_inittimer(int timer, int data);
void api_settimer(int timer, int time);
void api_boxfilwin(int win, int x0, int y0, int x1, int y1, int col);

double M_PI = 3.14;
HariMain(void) {
char *buf;

int win, i;
api_initmalloc();
buf = api_malloc(160 * 160);
win = api_openwin(buf, 160, 160, -1, "CLOCK");
int sec = api_getsecond();
int min = api_getminute();
int hour = api_gethour();
int year = api_getyear();
int mon = api_getmonth();
int day = api_getday();
char text[10] = {0};
sprintf(text, "%02d:%02d:%02d\0", hour, min, sec);
char text2[12] = {0};
sprintf(text2, "%04d/%02d/%02d\0", year, mon, day);
api_putstrwin(win, 28, 27, 0, 11, text);
api_putstrwin(win, 28, 45, 0, 10, text2);
api_refreshwin(win, 6, 27, 154, 27 + 40);
buf = api_malloc(160 * 180);
win = api_openwin(buf, 160, 180, -1, "CLOCK");


static int label_m[60][2] = {
{80, 33},
{86, 33},
{92, 34},
{98, 35},
{104, 38},
{110, 41},
{115, 44},
{120, 48},
{124, 52},
{128, 57},
{131, 62},
{134, 68},
{137, 74},
{138, 80},
{139, 86},
{140, 93},
{139, 99},
{138, 105},
{137, 111},
{134, 117},
{131, 122},
{128, 128},
{124, 133},
{120, 137},
{115, 141},
{110, 144},
{104, 147},
{98, 150},
{92, 151},
{86, 152},
{80, 153},
{73, 152},
{67, 151},
{61, 150},
{55, 147},
{50, 144},
{44, 141},
{39, 137},
{35, 133},
{31, 128},
{28, 123},
{25, 117},
{22, 111},
{21, 105},
{20, 99},
{20, 93},
{20, 86},
{21, 80},
{22, 74},
{25, 68},
{28, 62},
{31, 57},
{35, 52},
{39, 48},
{44, 44},
{50, 41},
{55, 38},
{61, 35},
{67, 34},
{73, 33}};
static int label_h[12][2] = {
{80, 63},
{95, 67},
{105, 78},
{110, 93},
{105, 108},
{95, 118},
{79, 123},
{65, 118},
{54, 108},
{50, 93},
{54, 78},
{64, 67}};
static int label_text[12][2] = {
{80, 38},
{107, 45},
{127, 65},
{135, 93},
{127, 120},
{107, 140},
{79, 148},
{52, 140},
{32, 120},
{25, 93},
{32, 65},
{52, 45}};


int timer;
timer = api_alloctimer();
api_inittimer(timer, 128);
char *s;
int sec = 0, min = 0, hou = 0;

sec = api_getsecond();
min = api_getminute();
hou = api_gethour();
api_boxfilwin(win + 1, 80 - 65, 93 - 65, 80 + 65, 93 + 65, 8);
// 圆心是80,93,半径是65 表盘
api_drawcircle(win, 80, 93, 65, 0, 15);
api_linewin(win + 1, 80, 93, label_m[min][0], label_m[min][1], 0);
api_linewin(win + 1, 80, 93, label_h[hou % 12][0], label_h[hou % 12][1], 1);
api_linewin(win + 1, 80, 93, label_m[sec][0], label_m[sec][1], 3);
int dx = -5, dy = -6;
for (;;) {
if (api_getkey(1) == 0x0a) {
break; /*按下回车键则break; */
// 清除原本
api_boxfilwin(win + 1, 80 - 65, 93 - 65, 80 + 65, 93 + 65, 8);
// 圆心是80,93,半径是65 表盘
api_drawcircle(win, 80, 93, 65, 0, 15);
// int i;
// for (i = 0; i < 12; i++) {
// char s[2] = {0};
// int j = (i + 12) % 12;
// if (j == 0) j = 12;
// sprintf(s, "%d", j);
// api_putstrwin(win + 1, label_text[i][0] + dx, label_text[i][1] + dy, 0, 2, s);
// }
// api_linewin(win + 1, 80, 93, label_m[min][0], label_m[min][1], 0);
// api_linewin(win + 1, 80, 93, label_h[hou % 12][0], label_h[hou % 12][1], 1);
// api_linewin(win + 1, 80, 93, label_m[sec][0], label_m[sec][1], 3);

api_refreshwin(win, 0, 0, 160, 180);

api_settimer(timer, 100); /* 1秒 */
if (api_getkey(1) != 128) {
break;
}
sec = api_getsecond();
min = api_getminute();
hou = api_gethour();
}
api_closewin(win);
api_end();
Expand Down
9 changes: 9 additions & 0 deletions src/25_day/clock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import math

r = 55
for i in range(0, 12):
# 60个刻度的坐标
x = 80 + r * math.sin(math.pi / 180 * i * 30)
y = 93 - r * math.cos(math.pi / 180 * i * 30)
# 输出为 {x,y}, 的形式
print("{%d,%d}," % (x, y))
9 changes: 9 additions & 0 deletions src/25_day/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,15 @@ int *hrb_api(int edi, int esi, int ebp, int esp, int ebx, int edx, int ecx, int
else if (edx == 35) {
reg[7] = get_day_of_month();
}
else if (edx == 40) {
sht = (struct SHEET *) (ebx & 0xfffffffe);
int x = eax, y = ecx, r = esi;
int color = ebp;
drawcircle(sht->buf, x, y, r, color, sht->bxsize);
if ((ebx & 1) == 0) {
sheet_refresh(sht, x + r, y + r, x - r, y - r);
}
}

return 0;
}
Expand Down
31 changes: 31 additions & 0 deletions src/25_day/graphic.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,37 @@ void boxfill8(unsigned char *vram, int xsize, unsigned char c, int x0, int y0, i
return;
}

void drawFullCircle(int x0, int y0, int x, int y, int color, int xsize, char *vram) {
int circle_x = x0;
int circle_y = y0;
vram[(circle_y + y) * xsize + (circle_x + x)] = color;
vram[(circle_y + y) * xsize + (circle_x - x)] = color;
vram[(circle_y - y) * xsize + (circle_x + x)] = color;
vram[(circle_y - y) * xsize + (circle_x - x)] = color;
vram[(circle_y + x) * xsize + (circle_x + y)] = color;
vram[(circle_y + x) * xsize + (circle_x - y)] = color;
vram[(circle_y - x) * xsize + (circle_x + y)] = color;
vram[(circle_y - x) * xsize + (circle_x - y)] = color;
}
void drawcircle(char *vram, int x0, int y0, int r0, unsigned char c, int xsize) {
int x = 0;
int y = r0;
int d = 1 - y;
int color = c;
while (x < y) {
drawFullCircle(x0, y0, x, y, color, xsize, vram);
if (d < 0) {
d = d + 2 * x + 3;
}
else {
d = d + 2 * (x - y) + 5;
y = y - 1;
}
x = x + 1;
}
}


void init_screen8(char *vram, int x, int y) {
boxfill8(vram, x, COL8_008484, 0, 0, x - 1, y - 29);
boxfill8(vram, x, COL8_C6C6C6, 0, y - 28, x - 1, y - 28);
Expand Down
Binary file modified src/z_tools/qemu/fdimage0.bin
Binary file not shown.

0 comments on commit 8c2c73b

Please sign in to comment.