Category : Display Utilities
Archive   : HCLOCK.ZIP
Filename : HCLOCK.C

 
Output of file : HCLOCK.C contained in archive : HCLOCK.ZIP

/*
HCLOCK.C
By Ray Genovese
Originally written in TC 1.0
compile with unsigned character default
use hclock.prj with pixeladd.asm and plot.asm
*/

#include
#include
#include

void hgraphics_on(),hgraphics_off(),hclear(),hselect_page();
void hdraw_line(),hdraw_box(),hwrite_string(),pulse_colon();
void hplot_string(),ndelay(),tone();
void hplot2_string(),hplot3_string();
void update(),print_hourl(),print_minl(),print_hourh(),print_minh();
void cls(),goto_xy(),curs_on(),curs_off(),exit2dos();
int c_break(),print_time();
extern void plot_h();

struct date today;
struct time now;

/* global variables */
int page,active_page;
unsigned char oldminl,oldminh,oldhourl,oldhourh,oldsec;
unsigned char ch,pulse=1,circ[2]={7,0},colon[2]={58,0};
unsigned char hourl[2]={48,0};
unsigned char hourh[2]={48,0};
unsigned char minl[2]={48,0};
unsigned char minh[2]={48,0};

/* option variables */
char mil=0;
char beep15=1;
char beep30=1;
char beep60=1;

main()
{
ctrlbrk(c_break);
(void)setcbrk(1);
cls();
curs_off();
hclear(0);
hgraphics_on();
hselect_page(0);
/* outside border */
hdraw_box(0,0,719,347);
hdraw_box(1,1,718,346);
hdraw_box(3,3,716,344);
hdraw_box(5,5,714,342);
hdraw_box(6,6,713,341);
/* 4-corners */
hdraw_box(21,13,43,35);
hdraw_box(676,13,698,35);
hdraw_box(21,312,43,334);
hdraw_box(676,312,698,334);
hdraw_box(23,15,41,33);
hdraw_box(678,15,696,33);
hdraw_box(23,314,41,332);
hdraw_box(678,314,696,332);
gettime(&now);
update();
oldhourh=hourh[0];
oldhourl=hourl[0];
oldminh=minh[0];
oldminl=minl[0];
print_time(0);
print_time(1);
hplot2_string(colon,296,95,16);
print_time(2);
print_time(3);
if(kbhit())
(void)getch();
while(1==1){
if(kbhit())
exit2dos();
gettime(&now);
if(oldsec!=now.ti_sec){
oldsec=now.ti_sec;
update();
pulse_colon();
}
if(hourh[0]!=oldhourh){
oldhourh=hourh[0];
print_time(0);
}
if(hourl[0]!=oldhourl){
oldhourl=hourl[0];
print_time(1);
}
if(minh[0]!=oldminh){
oldminh=minh[0];
print_time(2);
}
if(minl[0]!=oldminl){
oldminl=minl[0];
print_time(3);
}
}
}


void update()
{

/* convert digits */
if(now.ti_min>9){
minh[0]=48+(now.ti_min/10);
minl[0]=48+(now.ti_min-(minh[0]-48)*10);
}
else{
minh[0]=48;
minl[0]=48+now.ti_min;
}
if(!mil)
if(now.ti_hour>12)
now.ti_hour-=12;
if(now.ti_hour>9){
hourh[0]=48+(now.ti_hour/10);
hourl[0]=48+(now.ti_hour-(hourh[0]-48)*10);
}
else{
hourh[0]=48;
hourl[0]=48+now.ti_hour;
}
}

int print_time(char digit)
{
if(digit==3)
hplot2_string(minl,552,105,16);
else if(digit==2)
hplot2_string(minh,424,105,16);
else if(digit==1)
hplot2_string(hourl,168,105,16);
else if(digit==0)
hplot2_string(hourh,40,105,16);
}


void pulse_colon()
{

if(pulse){
hplot2_string(" ",24,16,2);
hplot2_string(" ",679,16,2);
hplot2_string(" ",24,315,2);
hplot2_string(" ",679,315,2);
pulse=0;
}
else{
hplot2_string(circ,24,16,2);
hplot2_string(circ,679,16,2);
hplot2_string(circ,24,315,2);
hplot2_string(circ,679,315,2);
pulse=1;
}
}

int c_break(void)
{
tone();
exit2dos();
return(1);
}

void exit2dos()
{
char ch;
int x;
if(kbhit())
(void)getch();
tone();
hplot2_string("Exit program? [Y/N]",284,325,1);
ch=toupper(getch());
if(ch=='Y'){
hclear(0);
hgraphics_off();
curs_on();
cls();
exit(1);
}
else
hplot2_string(" ",284,325,1);
}

void ndelay(int x)
{
int y,z;
for(y=0;y for(z=0;z<30000;z++);
}


void hgraphics_on()
{
/* set the graphic mode to page 0 & enable page flipping */
int regdata[16]={0x35,0x2d,0x2e,0x07,0x5b,0x02,0x57,0x57,
0x02,0x03,0x00,0x00,0x00,0x00,0x00,0x00};
int x=0;

outportb(0x3bf,0x03);
outportb(0x3b8,0x02);
for(x=0;x<16;x++){
outportb(0x3b4,x);
outportb(0x3b5,regdata[x]);
outportb(0x3b8,0x0a);
}
active_page=0;
page=0xb000;
}

void hgraphics_off()
{
/* reset to text mode and display page 0 */
int regdata[16]={0x61,0x50,0x52,0x0f,0x19,0x06,0x19,0x19,
0x02,0x0d,0x0b,0x0c,0x00,0x00,0x00,0x00};
int x=0;

outportb(0x3b8,0x00);
for(x=0;x<16;x++){
outportb(0x3b4,x);
outportb(0x3b5,regdata[x]);
outport(0x3b8,0x08);
}
}

void hclear(int page_n)
{
/* clear page 0 or 1 */
long x;
register y=0;

if(page_n==0)
for(x=0;x<0x8000;x+=2)
poke(0xb000,x,y);
else if(page_n==1)
for(x=0;x<0x8000;x+=2)
poke(0xb800,x,y);
}

void hselect_page(int page_n)
{
/* display page 0 or 1 */
if(page_n==0)
outportb(0x3b8,0x0a);
else if(page_n==1)
outportb(0x3b8,0x8a);
}

void hdraw_line(int x1,int y1,int x2,int y2)
{
/* draws line with plot_h()
uses Bresenham's algorithm */
int change_x,change_y,add_x,add_y,length,x;
int err_x=0;
int err_y=0;

change_x=x2-x1;
change_y=y2-y1;
if(change_x>0)
add_x=1;
else if(change_x<0)
add_x=-1;
else
add_x=0;
if(change_y>0)
add_y=1;
else if(change_y<0)
add_y=-1;
else
add_y=0;
change_x=abs(change_x);
change_y=abs(change_y);
if(change_x>change_y)
length=change_x;
else
length=change_y;
/* draw it-finally! */
for(x=0;x<=length;x++){
plot_h(x1,y1,1);
err_x+=change_x;
err_y+=change_y;
if(err_x>length){
err_x-=length;
x1+=add_x;
}
if(err_y>length){
err_y-=length;
y1+=add_y;
}
}
}

void hdraw_box(int x1,int y1,int x2,int y2)
/* draw a box using upper left and lower right x & y's */
{
hdraw_line(x1,y1,x1,y2);
hdraw_line(x2,y1,x2,y2);
hdraw_line(x1,y1,x2,y1);
hdraw_line(x1,y2,x2,y2);
}

void hwrite_string(unsigned char *string,int xp, int yp)
{
unsigned long romchar,romchars,romcharo,ppos,pos;
unsigned int counter1,counter2=0;

if(yp>42)
return;
yp=yp*8;
while(string[counter2]){
if(xp>712)
break;
/* get the character set address */
/* note: only values 1-127 are valid */
romcharo=0xfa6e;
romchars=0xf000;
romchar=romcharo+(string[counter2++]*8);
ppos=(0x2000*(yp%4))+(90*(yp/4))+(xp/8);
pos=ppos;
for(counter1=0;counter1<4;pos+=8192,romchar++,counter1++)
pokeb(page,pos,peekb(romchars,romchar));
pos=ppos+90;
for(counter1=0;counter1<4;pos+=8192,romchar++,counter1++)
pokeb(page,pos,peekb(romchars,romchar));
xp+=8;
}
}
void hplot_string(unsigned char *string,int xp, int yp,int size)
/* draws a string of variable size */
{
unsigned long romchar,romchars,romcharo;
unsigned int counter1,counter2=0,char_byte,x,xx,y,mask;

while(string[counter2]){
/* get the character set address */
/* note: only values 1-127 are valid */
romcharo=0xfa6e;
romchars=0xf000;
romchar=romcharo+(string[counter2++]*8);
/* 8 bytes in a character */
for(counter1=0;counter1<8;romchar++,counter1++){
char_byte=peekb(romchars,romchar);
/* do 8 bits size times down */
for(y=0;y /* plot 8 bits size times across */
mask=128;
for(xx=0;xx<8;xx++){
/* do a bit size times */
if(char_byte&mask)
for(x=0;x plot_h(xp,yp,1);
xp++;
}
else
xp+=size;
mask>>=1;
}
yp++;
xp=xp-(8*size);
}
}
yp=yp-(8*size);
xp=xp+(8*size);
}
}
void hplot2_string(unsigned char *string,int xp, int yp,int size)
/* draws a string of variable size */
/* entire matrix is filled [destructive version] */
{
unsigned long romchar,romchars,romcharo;
unsigned int counter1,counter2=0,char_byte,x,xx,y,mask;

while(string[counter2]){
/* get the character set address */
/* note: only values 1-127 are valid */
romcharo=0xfa6e;
romchars=0xf000;
romchar=romcharo+(string[counter2++]*8);
/* 8 bytes in a character */
for(counter1=0;counter1<8;romchar++,counter1++){
char_byte=peekb(romchars,romchar);
/* do 8 bits size times down */
for(y=0;y /* plot 8 bits size times across */
mask=128;
for(xx=0;xx<8;xx++){
/* do a bit size times */
if(char_byte&mask)
for(x=0;x plot_h(xp,yp,1);
xp++;
}
else
for(x=0;x plot_h(xp,yp,0);
xp++;
}

mask>>=1;
}
yp++;
xp=xp-(8*size);
}
}
yp=yp-(8*size);
xp=xp+(8*size);
}
}
void hplot3_string(unsigned char *string,int xp, int yp,int size)
/* draws a reversed video string in of variable size */
/* entire matrix is filled [destructive version] */
{
unsigned long romchar,romchars,romcharo;
unsigned int counter1,counter2=0,char_byte,x,xx,y,mask;

while(string[counter2]){
/* get the character set address */
/* note: only values 1-127 are valid */
romcharo=0xfa6e;
romchars=0xf000;
romchar=romcharo+(string[counter2++]*8);
/* 8 bytes in a character */
for(counter1=0;counter1<8;romchar++,counter1++){
char_byte=peekb(romchars,romchar);
/* do 8 bits size times down */
for(y=0;y /* plot 8 bits size times across */
mask=128;
for(xx=0;xx<8;xx++){
/* do a bit size times */
if(char_byte&mask)
for(x=0;x plot_h(xp,yp,0);
xp++;
}
else
for(x=0;x plot_h(xp,yp,1);
xp++;
}

mask>>=1;
}
yp++;
xp=xp-(8*size);
}
}
yp=yp-(8*size);
xp=xp+(8*size);
}
}


/* clear screen */
void cls()
{
union REGS r;
r.h.ah=6;
r.h.al=0;
r.h.ch=0;
r.h.cl=0;
r.h.dh=24;
r.h.dl=79;
r.h.bh=7;
int86(0x10,&r,&r);
}

/* position cursor to x,y */
void goto_xy(int x,int y)
{
union REGS r;
r.h.ah=2;
r.h.dl=x;
r.h.dh=y;
r.h.bh=0;
int86(0x10,&r,&r);
}

/* turn cursor off */
void curs_off()
{
int x;
union REGS r;
r.h.ah=3;
r.h.bh=0;
int86(0x10,&r,&r);
x=_CX|0x2000;
r.h.ah=1;
r.h.cl=x%256;
r.h.ch=x/256;
int86(0x10,&r,&r);
}

/* turn cursor on */
void curs_on()
{
union REGS r;
int x;
r.h.ah=3;
r.h.bh=0;
int86(0x10,&r,&r);
x=_CX&0xdfff;
r.h.cl=x%256;
r.h.ch=x/256;
r.h.ah=1;
int86(0x10,&r,&r);
}

void tone()
/* sounds a simple tone */
{
int store;
int count=0;

store=inport(97);
outport(97,79);
while(count++<25000);
outport(97,store);
}