一个TC2.0 编写的热力站监视的小程序

来源:互联网 发布:保定知感公司怎么样 编辑:程序博客网 时间:2024/05/01 01:42

8年前编写的,无意又发现它,看了看,还是觉得很有意思。帖到这里给感兴趣的人看看,记得当时用的是研华pcl812的板子,希望也能和我重温当年的那些感情。

主要功能有,8通道,32点,保留历史纪录,实时曲线,历史曲线,线颜色可配,有屏幕保护。

下面的是源代码,你也可以在http://edsoncy.512j.com/tar/HPS.rar 下载。本来想贴两张图的,就是截不到!对了,ALT+X连续两次退出,或是鼠标点击坐上角方框两次。



#include <graphics.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <time.h>

/*=================== mouse start =======================*/
int MOUSE_THERE;
int MOUSE_VISIBLE;
union REGS mregs;

void reset_mouse(void)
{
 MOUSE_THERE = 0;
 MOUSE_VISIBLE = 0;

 if (getvect(0x33) != 0L)
 {
  mregs.x.ax = 0;
  int86(0x33, &mregs, &mregs);
  if (mregs.x.ax !=0)
   MOUSE_THERE = 1;
 }
}

void pascal set_mouse_position(int x, int y)
{
 if (MOUSE_THERE)
 {
  mregs.x.ax = 4;
  mregs.x.cx = x;
  mregs.x.dx = y;
  int86(0x33, &mregs, &mregs);
 }
}

void pascal set_mouse_limits(int x1, int y1, int x2, int y2)
{
 if (MOUSE_THERE)
 {
  mregs.x.ax = 7;
  mregs.x.cx = x1;
  mregs.x.dx = x2;
  int86(0x33, &mregs, &mregs);
  mregs.x.ax = 8;
  mregs.x.cx = y1;
  mregs.x.dx = y2;
  int86(0x33, &mregs, &mregs);
 }
}

void pascal get_mouse_button(unsigned char *lbutton,
 unsigned char *rbutton, int *x, int *y)
{
 if (MOUSE_THERE)
 {
  mregs.x.ax = 3;
  int86(0x33, &mregs, &mregs);

  *lbutton = (mregs.x.bx == 1) ? 1 : 0;
  *rbutton = (mregs.x.bx == 2) ? 1 : 0;
  if (mregs.x.bx == 3)
   *lbutton = *rbutton = 1;

  *x = mregs.x.cx;
  *y = mregs.x.dx;
 }
}
/*=================== mouse over =======================*/

#define _DEBUG

/* define data.dat point num */
#ifndef _DEBUG
 #define FILEMAX 0x3FFFFF
#else
 #include "math.h"
 #define PI 3.14159265758
 #define FILEMAX 0xFFF
#endif

#define SCRMAX 600
/* define keyboard */
#define ALT_U 150
#define ALT_D 160
#define ALT_N 177
#define ALT_X 173
#define ALT_T 148
#define ALT_E 146
#define ALT_C 174
#define ALT_M 178
#define ALT_1 248
#define ALT_2 249
#define ALT_3 250
#define ALT_4 251
#define ALT_5 252
#define ALT_6 253
#define UP 200
#define DOWN 208
#define LEFT 203
#define RIGHT 205
#define ENTER 13

/* ================================ */
static int INTRATE,freescreen,mousespeed;
static int int_ok,run_program=2,scr_num=10,axis=1;
static int tim,intrate,nokeytime,ticker;
static int color[16],color_t[16];
static int colume,mousespeed_t,freescreen_t;
static long unsigned fpt,fpt_t;
static unsigned int drawpt;
static unsigned char draw_data[SCRMAX][16];
unsigned char c[SCRMAX+1][16];
char *unit[3]={"deg.C","Mpa","kmph"};
char *port[6]={"entra_T","entra_P","entra_F",
   "exit_T","exit_P","exit_F"};

time_t ti,cur_ti;
FILE *fdata,*fpoint;

struct datum {
   float hi;     /* high alarm value */
   float lo;     /* low alarm value */
   float span_lo; /* instrument  disp lower limit */
   float span_hi; /* instrument  disp upper limit   */
   int val;     /* 12 bit true value   */
   float fval;   /* after culation */
   char strval[10];   /* str value */
} data[8][6];

#ifndef _DEBUG
extern pcl812(int, unsigned int *);
unsigned int param[60];
unsigned int pcldata[800];
unsigned int far * pcldat;
#else
unsigned int TimeNum;
#endif
/* ================================= */
void cursor(int status);
void draw_3(int startx,int starty,int x,int y);

void interrupt (*oldtimer)(void) = NULL;
void interrupt newtimer(void);
void install(void interrupt (*int_add)(),int int_num);

int get_char(void);
void keybroad(void);

void headscr(void);
void head_bar(void);
void table_backscr(void);
void eachline_backscr(void);
void curve_backscr(void);
void color_backscr(void);
void frontscr(void);

void data_pro(void);
void f_init(void);
int f_fun(int key);
void f_write(int offset);
void driver_init(void);
void span_init(void);
/* ==========  move cursor from _old to _cur ======== */
static int curx,cury,oldx,oldy,line_old;
static char curbuf[210];
unsigned char mouse_lbutt_press_in_this_time_int;

void cursor(int status)
{ int shape[10],*s=shape;
  if (status!=0)
  putimage(oldx,oldy,curbuf,COPY_PUT);
  getimage(curx,cury,curx+16,cury+16,curbuf);
  *s=curx; s++;
  *s=cury; s++;
  *s=curx+16;   s++;
  *s=cury+8; s++;
  *s=curx+10; s++;
  *s=cury+10; s++;
  *s=curx+8; s++;
  *s=cury+16; s++;
  *s=curx; s++;
  *s=cury;
  setcolor(LIGHTBLUE);
  setfillstyle(SOLID_FILL,LIGHTBLUE);
  fillpoly(4,shape);
  setfillstyle(SOLID_FILL,LIGHTGRAY);
  oldx=curx;
  oldy=cury;
  return;
}

void draw_3(int startx,int starty,int x,int y)
{ int shape[8],*s=shape;
  *s=startx;s++;      *s=starty;s++;
  *s=startx+x;s++;    *s=starty+y;s++;
  *s=startx+x;s++;   *s=starty-y;s++;
  *s=startx;s++;   *s=starty;
  fillpoly(3,shape);
}

/* ========== interrupt function ================ */

void install(void interrupt (*int_add)(),int int_num)
{ disable();
  setvect(int_num,int_add);
  enable();
  return;
}

/* ------- interrupt ------- */
void interrupt newtimer (void)

{ (*oldtimer)();
  if (ticker==0)
  /* -----------------------------*/
 { ticker=(((tim%5)==0) ? 19:18);
   if (++tim==5) tim=0;
  run_program=2;
  mouse_lbutt_press_in_this_time_int = 0;
   if (nokeytime <= freescreen) nokeytime++;
   intrate--;
   if (intrate==0)
  { int_ok++;
    intrate=INTRATE;
  }
  /* ---------------------------------*/
   }
 --ticker;

 return;
}


/* ============== keybroad funtion ==================== */
int get_char(void)
{ union REGS rg;
  int c;
  while (1)
   { rg.h.ah=1;
  int86(0x16,&rg,&rg);
  if (rg.x.flags & 0x40)
 { int86(0x28,&rg,&rg);
   break;
 }
  rg.h.ah=0;
  int86(0x16,&rg,&rg);
  if (rg.h.al==0)
 c=rg.h.ah|128;
  else
 c=rg.h.al;
  break;
   }
  return c;
}
/* ------------------------------------------- */

void keybroad(void)
{
  int key;
 unsigned char rbutton, lbutton = 0;
 if (MOUSE_THERE)
  {
  oldx = curx;
  oldy = cury;
  get_mouse_button(&lbutton, &rbutton, &curx, &cury);
  if (lbutton)
   if (mouse_lbutt_press_in_this_time_int)
    lbutton = 0;
   else
    mouse_lbutt_press_in_this_time_int = 1;
  }

  key=get_char();
  if (scr_num<10)
  { if (key==ENTER || lbutton)
   { if (curx>0&&curx<24&&cury>0&&cury<24)
   key=ALT_X;
  else if (curx>26&&curx<134&&cury>0&&cury<24)
   key=ALT_T;
  else if (curx>136&&curx<244&&cury>0&&cury<24)
   key=ALT_E;
  else if (curx>246&&curx<354&&cury>0&&cury<24)
   key=ALT_C;
  else if (curx>366 && cury>0 && curx<639 && cury<24)
   key=ALT_M;
  else if (scr_num<8 && curx>540&&cury>28&&curx<639&&cury<55)
   key=ALT_N;
  else if (scr_num==9 && cury>29 &&cury<63 && curx>407)
   key=ALT_1+(curx-407)/39;
  else if (scr_num==9 && curx>365 && cury>34 && curx<390 && cury<46)
   key=ALT_U;
  else if (scr_num==9 && curx>365 && cury>49 && curx<390 &&cury<61)
   key=ALT_D;
  else if (scr_num==9 && cury>454 && cury<479 && curx>32 && curx<634)
   key= -1;
  else;
    }
  switch (key)
   { case ALT_N :
  if (scr_num<8)
   { char s[2]="1";
  scr_num++;
  if (scr_num>7) scr_num=0;
  (*s)+=scr_num;
  settextstyle(TRIPLEX_FONT,HORIZ_DIR,8);
  setcolor(WHITE);
  bar(570,115,610,180);
  outtextxy(570,100,s);
  setcolor(BLUE);
  settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
   }
  break;

 case ALT_X :
  run_program--;
  break;

 case ALT_T :
  table_backscr();
  break;

 case ALT_E :
  eachline_backscr();
  break;

 case ALT_C :
  curve_backscr();
  break;

 case ALT_M :
  color_backscr();
  break;

 case UP :
  cury-=mousespeed;
  if (cury<0) cury=0;
  set_mouse_position(curx, cury);
  cursor(1);
  break;

 case DOWN :
  cury+=mousespeed;
  if (cury>479) cury=479;
  set_mouse_position(curx, cury);
  cursor(1);
  break;

 case LEFT :
   curx-=mousespeed;
   if (curx<0) curx=4;
  set_mouse_position(curx, cury);
  cursor(1);
   break;

 case RIGHT :
   curx+=mousespeed;
  if (curx>623) curx=623;
  set_mouse_position(curx, cury);
   cursor(1);
   break;

 case ALT_1 :
  if (scr_num==9)
   { fpt_t=fpt+SCRMAX*axis;
  cur_ti=ti-(FILEMAX-SCRMAX*axis)*INTRATE;
  f_fun(key);
   }
  break;

 case ALT_2 :
  if (scr_num==9)
   { fpt_t-=(SCRMAX*axis/2);
  cur_ti-=(SCRMAX*axis*INTRATE/2);
  f_fun(key);
   }
  break;

 case ALT_3 :
  if (scr_num==9)
   { fpt_t-=axis;
  cur_ti-=axis*INTRATE;
  f_fun(key);
   }
  break;

 case ALT_4 :
  if (scr_num==9)
   { fpt_t+=axis;
  cur_ti+=axis*INTRATE;
  f_fun(key);
   }
  break;

 case ALT_5 :
  if (scr_num==9)
   { fpt_t+=(SCRMAX*axis/2);
  cur_ti+=(SCRMAX*axis*INTRATE/2);
  f_fun(key);
   }
  break;

 case ALT_6 :
  if (scr_num==9)
   { fpt_t=fpt;
  cur_ti=ti;
  f_fun(key);
   }
  break;

 case ALT_U :
  if (scr_num == 9) {
   char *s = NULL;
   int i;
   axis<<=1;
   if (axis > 16) axis = 1;
   setcolor(RED);
   bar(330,43,355,51);
   outtextxy(330,43,itoa(axis,s,10));
   bar(1,455,633,478);
   setcolor(WHITE);
   setfillstyle(SOLID_FILL,BLUE);
   for (i=1;i<11;i++) {
   outtextxy(626-i*60,465,itoa(i*INTRATE*axis,s,10));
   outtextxy(642-i*60,465," m");
   bar(633-i*60,455,635-i*60,462);
    }
   f_fun(ALT_2);
  }
  break;

 case ALT_D :
  if (scr_num == 9) {
   char *s = NULL;
   int i;
   axis>>=1;
   if (axis == 0) axis = 16;
   bar(330,43,355,51);
   setcolor(RED);
   outtextxy(330,43,itoa(axis,s,10));
   bar(1,455,633,478);
   setcolor(WHITE);
   setfillstyle(SOLID_FILL,BLUE);
   for (i=1;i<11;i++) {
   outtextxy(626-i*60,465,itoa(i*INTRATE*axis,s,10));
   outtextxy(642-i*60,465," m");
   bar(633-i*60,455,635-i*60,462);
    }

   f_fun(ALT_2);
  }
  break;

 case -1 :
  f_write(634-curx);
  break;

 default :
  key = 0;
  if (oldx != curx || oldy != cury)
  cursor(1);
  break;
  }
   }

  if (scr_num==10)
   { char str[2];
  str[0]=16;
  str[1]='/x0';
  switch (key)
   { case ENTER :
  if (colume<16)
   { ++color_t[colume];
  color_t[colume]&=0x0f;
  setfillstyle(SOLID_FILL,color_t[colume]);
  bar(360,123+colume*15,400,125+colume*15);
  setfillstyle(SOLID_FILL,LIGHTGRAY);
   }
  else if (colume==16) {
   char str[4];
   mousespeed_t++;
   if (mousespeed_t>20) mousespeed_t=1;
   bar(360,360,382,368);
   itoa(mousespeed_t,str,10);
   setcolor(WHITE);
   outtextxy(360,360,str);
  }
   else if (colume == 17) {
  char str[4];
   freescreen_t*=2;
   if (freescreen_t>480) freescreen_t = 60;
   bar(360,375,382,383);
   itoa(freescreen_t,str,10);
   setcolor(WHITE);
   outtextxy(360,375,str);
    }
   else if (colume==18)
  { int i;
    for(i=0;i<16;i++)
    color[i]=color_t[i];
    mousespeed=mousespeed_t;
    freescreen=freescreen_t;
    curve_backscr();
  }
    else
  curve_backscr();

    break;

   case UP :
  bar(245,120+colume*15,252,128+colume*15);
  colume--;
  if (colume<0) colume=19;
  setcolor(WHITE);
  outtextxy(245,120+colume*15,str);
    break;

   case DOWN :
   bar(245,120+colume*15,252,128+colume*15);
   colume++;
   if (colume>19) colume=0;
   setcolor(WHITE);
   outtextxy(245,120+colume*15,str);
   break;

   default :
  key = 0;
    break;
   }
   }

  if (key) {
 if (nokeytime > freescreen) {
  scr_num = 0;
  eachline_backscr();
   }
 nokeytime = 0;
   }
  return;
}
/* ==================================================== */

/* ----- draw head bar ------ */
void head_bar(void)
{ setfillstyle(SOLID_FILL,BLACK);
  bar(0,0,639,479);

  settextstyle(TRIPLEX_FONT,HORIZ_DIR,1);
  setfillstyle(SOLID_FILL,LIGHTGRAY);
  setcolor(WHITE);

  bar3d(0,0,24,24,0,0);
  bar3d(7,11,17,13,0,0);
  bar3d(26,0,134,24,0,0);
  outtextxy(35,3,"total Table");
  bar3d(136,0,244,24,0,0);
  outtextxy(150,3,"Each line");
  bar3d(246,0,364,24,0,0);
  outtextxy(255,3,"total Curve");
  bar3d(366,0,639,24,0,0);
  outtextxy(415,3,"the  Mis  for  H.P.S");

  setcolor(RED);
  if(scr_num<8)
   outtextxy(150,3,"Each line");
  else if(scr_num==8)
   outtextxy(35,3,"total Table");
  else if(scr_num==9)
   outtextxy(255,3,"total Curve");
  else if(scr_num==10)
   outtextxy(415,3,"the  Mis  for  H.P.S");
  setcolor(WHITE);

  return;
}

/* -------- head screen function -------- */
void head_scr(void)
{ head_bar();
  bar3d(150,150,489,320,4,4);
  setcolor(BLUE);
  outtextxy(175,180,"The Manage Information System ");
  outtextxy(175,215,"      for  the H.P.S  of      ");
  outtextxy(175,250,"       the PAN factory        ");
  outtextxy(285,280,"1998.05");

  settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
  return;
}

/* ----------- total table back screen function : scr= 8 ------- */
void table_backscr(void)
{ register int i,j;
  char s[5];

  scr_num=8;
  head_bar();

  /* make marks */
  for (i=0;i<6;i++)
   { bar3d(i*100+40,30,i*100+135,50,3,3);
  outtextxy(i*100+55,30,port[i]);
   }

  /* make line number */
  settextstyle(TRIPLEX_FONT,VERT_DIR,1);
  strcpy(s,"No.1");
  for (i=0;i<8;i++)
   { bar3d(0,i*50+55,35,i*50+100,3,3);
  outtextxy(8,i*50+58,s);
  (*(s+3))++;
   }

  /* draw the up_left box */
  bar3d(0,30,35,50,3,3);

  /* make 48 indecate boxes */
  for (i=0;i<8;i++)
 for (j=0;j<6;j++)
  bar3d(j*100+40,i*50+55,j*100+135,i*50+100,3,3);

  /* make units boxes */
  bar3d(0,455,35,479,3,3);
  settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
  for(i=0;i<6;i++)
   { setcolor(WHITE);
  bar3d(i*100+40,455,i*100+135,479,3,3);
  setcolor(RED);
  outtextxy(i*100+70,462,unit[i%3]);
   }

  cursor(0);
  return;
}

/* ------- each line back screen function : scr= 0 - 7 ------ */
void eachline_backscr(void)
{ register int i,j;
  char s[10];

  scr_num=0;
  head_bar();

  /* make 6 indecate boxes */
  for (i=0;i<6;i++)
   { bar3d(i*90,29,i*90+85,184,3,3);
  outtextxy(i*90+10,35,port[i]);
   }

  /* make 'next' cursor table */
  bar3d(540,28,639,55,0,0);
  outtextxy(570,31,"Next");

  /* make a indecate box for time */
  bar3d(540,60,634,85,3,3);

  /* make line number box */
  bar3d(540,90,634,184,3,3);
  outtextxy(545,95,"line_num");
  s[0]='1';
  s[1]='/x0';
  settextstyle(TRIPLEX_FONT,HORIZ_DIR,8);
  outtextxy(570,100,s);

  settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
  /* make rullor and draw window */
  bar3d(0,189,25,447,3,3);
  bar3d(32,189,634,447,3,3);
  bar3d(0,454,634,479,3,3);
  setfillstyle(SOLID_FILL,BLUE);
  for (i=1;i<4;i++)
   { outtextxy(2,186+64*i,itoa(25*(4-i),s,10));
  outtextxy(10,196+64*i,"%");
  bar(20,189+64*i,24,191+64*i);
   }
  for (i=1;i<11;i++)
   { outtextxy(626-i*60,465,itoa(i*INTRATE,s,10));
  outtextxy(642-i*60,465," m");
  bar(633-i*60,455,635-i*60,462);
   }

  setcolor(RED);
  for (i=0;i<6;i++)
   outtextxy(i*90+40,90,unit[i%3]);

  bar(10,60,75,63);
  setfillstyle(SOLID_FILL,RED);
  bar(100,60,165,63);

  cursor(0);
  return;
}

/* -------- curve back screen function : scr= 9 -------- */
void curve_backscr(void)
{ register int i,j;
  char s[5];

  scr_num=9;
  cur_ti=ti;
  fpt_t=fpt;

  head_bar();

  /* draw directory table */
  for(i=0;i<6;i++)
   bar3d(407+i*39,29,442+i*39,63,0,0);
  setfillstyle(SOLID_FILL,BLUE);
  draw_3(410,47,12,12);
  draw_3(423,47,12,12);
  draw_3(451,47,12,12);
  bar3d(468,35,473,59,0,0);
  draw_3(490,47,24,12);
  draw_3(554,47,-24,12);
  bar3d(570,35,575,59,0,0);
  draw_3(593,47,-12,12);
  draw_3(621,47,-12,12);
  draw_3(634,47,-12,12);

  setfillstyle(SOLID_FILL,LIGHTGRAY);
  settextstyle(DEFAULT_FONT,HORIZ_DIR,1);

  /* draw time box */
  bar3d(0,31,235,63,3,3);
  /* draw axis box and table*/
  bar3d(242,31,400,63,3,3);
  bar3d(365,34,390,46,0,0);
  bar3d(365,49,390,61,0,0);
  outtextxy(370,37,"Up");
  outtextxy(370,52,"Dn");
  /* write time and axis */
  setcolor(BLUE);
  outtextxy(17,43,ctime(&cur_ti));
  outtextxy(250,43,"time axis:");
  setcolor(RED);
  outtextxy(330,43,itoa(axis,s,10));
  setcolor(WHITE);

  bar3d(0,70,634,182,3,3);
  strcpy(s,"T1:");
  for (i=0;i<8;i++)
   { setcolor(WHITE);
  outtextxy(10+i/4*320,85+i%4*25,s);
  s[1]++;
  setcolor(RED);
  outtextxy(110+i/4*320,85+i%4*25,unit[0]);
   }

  strcpy(s,"P1:");
  for (i=0;i<8;i++)
   { setcolor(WHITE);
  outtextxy(170+i/4*320,85+i%4*25,s);
  s[1]++;
  setcolor(RED);
  outtextxy(270+i/4*320,85+i%4*25,unit[1]);
   }

  for (i=0;i<16;i++) {
 setfillstyle(SOLID_FILL,color[i]);
 bar(10+i%4*160,96+i/4*25,150+i%4*160,97+i/4*25);
   }

  setcolor(WHITE);
  setfillstyle(SOLID_FILL,LIGHTGRAY);
  bar3d(0,189,25,447,3,3);
  bar3d(32,189,634,447,3,3);
  bar3d(0,452,636,479,0,0);
  setfillstyle(SOLID_FILL,BLUE);

  for (i=1;i<4;i++)
   { outtextxy(2,186+64*i,itoa(25*(4-i),s,10));
  outtextxy(10,196+64*i,"%");
  bar(20,189+64*i,24,191+64*i);
   }
  for (i=1;i<11;i++)
   { outtextxy(626-i*60,465,itoa(i*INTRATE*axis,s,10));
  outtextxy(642-i*60,465," m");
  bar(633-i*60,455,635-i*60,462);
   }

  f_fun(ALT_4);
  cursor(0);
  return;
}


/* ----  color options back screen : scr=10 key --- */
void color_backscr(void)
{ int i,y;
  char s[6][15]={"line 1 - T :","line 1 - P :","mousespeed :",
     "free screen:","<<   OK   >>","<< Cancel >>"};
  char str[10];
  scr_num=10;
  colume=0;
  head_bar();

  bar3d(200,70,439,430,3,3);
  outtextxy(250,80, "SYSTEM OPTIONS");
  outtextxy(250,95,"============");
  settextstyle(DEFAULT_FONT,HORIZ_DIR,1);

  for(i=0,y=120;i<16;i++,y+=15)
   { outtextxy(260,y,s[i%2]);
  (*(s[i%2]+5))++;
   }
  outtextxy(260,y,s[2]);
  y+=15;
  outtextxy(260,y,s[3]);
  y+=15;
  outtextxy(260,y,s[4]);
  y+=15;
  outtextxy(260,y,s[5]);

  str[0]=16;
  str[1]='/x0';
  outtextxy(245,120,str);

  for(i=0;i<16;i++)
   { color_t[i]=color[i];
  setfillstyle(SOLID_FILL,color_t[i]);
  bar(360,123+i*15,400,125+i*15);
   }
  setfillstyle(SOLID_FILL,LIGHTGRAY);
  mousespeed_t=mousespeed;
  itoa(mousespeed_t,str,10);
  outtextxy(360,360,str);
  freescreen_t=freescreen;
  itoa(freescreen_t,str,10);
  outtextxy(360,375,str);
  return;
}
/* ========== do front screen =================== */
void frontscr(void)
{ register int i,j;
  int dec,sign;
  char *s="";
  /* -------- screen protect ------ */
  if (nokeytime >= freescreen) {
   setfillstyle(SOLID_FILL,BLACK);
   bar(0,0,639,479);
   settextstyle(random(10)+1,HORIZ_DIR,random(4)+1);
   setcolor(random(15)+1);
   outtextxy(random(480),random(480),"The programe is running.");
   setcolor(random(15)+1);
   outtextxy(random(480),random(480),"Press ARROW key to return!");
   setfillstyle(SOLID_FILL,LIGHTGRAY);
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
 }
  /* -------- draw zmt front ------ */
  else if (scr_num==8)
 { setcolor(BLUE);
   for (j=0;j<6;j++)
    { for (i=0;i<8;i++)
    { bar(j*100+55,i*50+70,j*100+120,i*50+90);
   outtextxy(j*100+70,i*50+70,data[i][j].strval);
    }
  }
 }
  /* ---------- draw zht front -------- */
  else if (scr_num<8)
 { setcolor(BLUE);
   for (i=0;i<6;i++)
  { bar(i*90+10,73,i*90+75,80);
    outtextxy(i*90+10,73,data[scr_num][i].strval);
  }
  bar(555,70,618,77);
  s=ctime(&ti);
  s+=11;
  (*(s+8))='/0';
  outtextxy(555,70,s);

  bar(33,190,633,446);
  j=drawpt+1;
  for(i=33;i<SCRMAX+33;i++,j++)
   { if (j>=SCRMAX) j-=SCRMAX;
  putpixel(i,190+draw_data[j][scr_num*2],BLUE);
  putpixel(i,191+draw_data[j][scr_num*2],BLUE);
  putpixel(i,190+draw_data[j][scr_num*2+1],RED);
  putpixel(i,191+draw_data[j][scr_num*2+1],RED);
   }
 }
  return;
}
/* =============== file funtion ==============*/
/* ------- input and process data -------- */
void data_pro(void)
{
  register int i,j;
  struct datum *data_pt;
  int dec,sign;
  char sdec[10]=".0000",s[10];

/* get data */
#ifndef _DEBUG
  pcl812(5, param);
  if (param[45] != 0) {
 printf(" A/D SOFTWARE DATA TRANSFER FAILED !");
 exit(1);
 }
#endif

  for (i = 0; i < 16; i++) {
 data_pt = &data[i/2][i%2];
#ifndef _DEBUG
 for (j = 0; j < 50; j++) {
  data_pt->val += pcldata[i+16*j];
  data_pt->val >>= 1;
  }
#else
 /*  data are simulated severl sinusoids
     1024 is middle value
     20, and 20 is used to amplifier changing sinusoids
 */
 data_pt->val = sin((TimeNum * PI / 6553.2) + PI * i / 16.0) * 0x400 + 0x3ff;
 TimeNum++;
#endif
 if (data_pt->val > 0xFFF ) data_pt->val = 0xFFF;
 data_pt->val &= 0x7FF;
 data_pt->fval = (float)data_pt->val / 2048;
 data_pt->fval = data_pt->span_lo + ( data_pt->span_hi-data_pt->span_lo ) * data_pt->fval;
 strcpy(data_pt->strval,fcvt (data_pt->fval, 2, &dec, &sign));
 strcpy(sdec+1,data_pt->strval+dec);
 strcpy(data_pt->strval+dec,sdec);
   }

  /* ----- get time ----- */
  time(&ti);
  /* ---- save for draw  and ----- */
  drawpt++;
  if (drawpt>=SCRMAX) drawpt=0;
  for (i=0;i<16;i++)
 draw_data[drawpt][i]=~(data[i/2][i%2].val>>3);

  /* ------ save for file ------ */
  fseek(fdata,fpt*16,SEEK_SET);
  fwrite(draw_data[drawpt],16,1,fdata);
  fpt++;fpt&=FILEMAX;
  rewind(fpoint);
  fprintf(fpoint,"%ld, %ld, %s",fpt,ti,ctime(&ti));
  return;
}

/* -------- file inti ------ */
void f_inti(void)
{ char c[16];
  for (fpt_t=0;fpt_t<16;fpt_t++)    c[fpt_t]=0xFF;

  fpoint = fopen("point.dat","r");
  fdata = fopen("data.dat","r");

  if (fpoint != NULL && fdata != NULL) {
 fscanf(fpoint,"%ld, %ld",&fpt_t,&cur_ti);
 time(&ti);
 if (ti - cur_ti < FILEMAX) {
  fclose(fpoint);
  fclose(fdata);
  fpoint=fopen("point.dat","r+");
  fdata=fopen("data.dat","rb+");
  cur_ti+=INTRATE;
  fpt_t=++fpt_t&FILEMAX;
  while (ti - cur_ti > INTRATE) {
   for(; cur_ti<=ti; cur_ti+=INTRATE,fpt_t=++fpt_t&FILEMAX) {
    fseek(fdata,fpt_t*16,SEEK_SET);
    fwrite(c,16,1,fdata);
    }
   time(&ti);
    }
  fpt = ftell(fdata)/16;
  fpt_t = fpt;
  while(cur_ti-ti>1)
   time(&ti);
   }
 else {
  fpoint = NULL;
  fdata = NULL;
   }
 }

  if (fpoint == NULL || fdata == NULL) {
 fclose(fpoint);
 fclose(fdata);
 fpoint=fopen("point.dat","wt");
 fdata=fopen("data.dat","wb+");
 for(fpt_t=0;fpt_t<=FILEMAX;fpt_t++)
  fwrite(c,16,1,fdata);
 rewind(fdata);
   }
  return;
}
/* ------- file write data ----- */
void f_write(int offset)
{ int dec,sign,i;
  unsigned char *ch;
  struct datum *da;
  float f;
  char str[10]="0.0",strdec[10]=".000";
  time_t temp;
  ch=&c[599-offset][0];
  setcolor(BLUE);
  setfillstyle(SOLID_FILL,LIGHTGRAY);
  settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
  for (i=0,da=&data[i/2][i%2];i<16;i++,ch++,da++) {
 f = 1.0 - (float)*ch / 256;
 f = da->span_lo + (da->span_hi-da->span_lo) * f;
 strcpy(str,fcvt (f, 2, &dec, &sign));
 strcpy(strdec+1,str+dec);
 strcpy(str+dec,strdec);
 bar(50+i%4*160,85+i/4*25,100+i%4*160,93+i/4*25);
 outtextxy(50+i%4*160,85+i/4*25,str);
   }

  bar(17,43,220,51);
  temp=cur_ti-axis*offset*INTRATE;
  outtextxy(17,43,ctime(&temp));

  setcolor(LIGHTGRAY);
  line(line_old,190,line_old,446);
  ch=&c[line_old-33][0];
  for(i=0;i<16;i++,ch++) {
 putpixel(line_old,190+*ch,color[i]);
 putpixel(line_old,191+*ch,color[i]);
   }
  line_old=curx;
  setcolor(GREEN);
  line(curx,190,curx,446);

  return;
}

/* -------- file input data ------ */
int f_fun(int key)
{ register int i,j;
  int color_t;
  unsigned long int fi;
  unsigned char *ch;

  fpt_t&=FILEMAX;
  fi=(fpt+SCRMAX*axis)&FILEMAX;
  if ( (fpt<fi && (fpt<fpt_t && fpt_t<fi))
 || (fpt>fi && (fpt<fpt_t || fpt_t<fi)) )
 { if (key<ALT_4)
 { fpt_t=fi;
   cur_ti=ti-(FILEMAX-SCRMAX*axis)*INTRATE;
 }
   else
 { fpt_t=fpt;
   cur_ti=ti;
 }
   putch('/7');
 }

  for(i=SCRMAX,fi=fpt_t;i>=0;i--,fi=(fi-axis)&FILEMAX)
   { fseek(fdata,fi*16,SEEK_SET);
  fread(c[i],16,1,fdata);
   }
  f_write(0);

  bar(33,190,633,446);
  for(i=0;i<16;i++)
   if (color[i]!=LIGHTGRAY)
 { ch=&c[0][i];      j=0;
   color_t=color[i];
   while(j<SCRMAX)
   { putpixel(34+j,190+*ch,color_t);
  putpixel(34+j,191+*ch,color_t);
  ch+=16;    j++;
   }
 }
  return;
}

void driver_init(void)
{
#ifndef _DEBUG
  int i;
  pcldat = pcldata;
  param[0]  = 0;            /* Board number                           */
  param[1]  = 0x300;        /* Base I/O address                       */
  param[5]  = 10;           /* Pacer rate = 2M / (10 * 10) = 20 KHz  */
  param[6]  = 10;
  param[7]  = 0;            /* Trigger mode, 0 : pacer trigger        */
  param[10] = FP_OFF(pcldat);  /* Offset of A/D data buffer A            */
  param[11] = FP_SEG(pcldat);  /* Segment of A/D data buffer A           */
  param[12] = 0;            /* Data buffer B address, if not used,    */
  param[13] = 0;            /* must set to 0.                         */
  param[14] = 800;           /* A/D conversion number                  */
  param[15] = 0;            /* A/D conversion start channel           */
  param[16] = 15;            /* A/D conversion stop channel            */
  param[17] = 1;            /* Overall gain code, 0 : +/- 2.5V          */

  pcl812(3, param);
  if (param[45] != 0) {
  printf(" DRIVER INITIALIZATION FAILED !");
  exit(1);
  }

   pcl812(4, param);
   if (param[45] != 0) {
  printf(" A/D INITIALIZATION FAILED !");
  exit(1);
  }
  #endif
}

/* ------------------------------------*/
void span_init(void)
{ int i;
  struct datum *da;
  INTRATE = 2;
  for(i = 0; i < 16; i++)
 color[i] = i;
  freescreen = 60;
  mousespeed = 10;

  data[0][0].span_lo=0;  data[0][1].span_hi=0;
  data[1][0].span_lo=0;  data[1][1].span_hi=0;
  data[2][0].span_lo=0;  data[2][1].span_hi=0;
  data[3][0].span_lo=0;  data[3][1].span_hi=0;
  data[4][0].span_lo=0;  data[4][1].span_hi=0;
  data[5][0].span_lo=0;  data[5][1].span_hi=0;
  data[6][0].span_lo=0;  data[6][1].span_hi=0;
  data[7][0].span_lo=0;  data[7][1].span_hi=0;

  for(i = 0, da = &data[i/2][i%2]; i < 16; i++, da++) {
 da->span_lo = 0.0;
 da->span_hi = 250.0;
   }
  /* Now, channel 0 is T1 and channel 1 is P1;
  the same, channel 2 is T2 and channel 3 is P2;
  ... ...

  the gain is -2.5v to 2.5V equ the intval is 0 to 4095,
  and -2.5 to 0 is omitted.
  If you want to change the gain, you must to modify
  param[17].Do your best, you will be right !
  */
  return;
}

/* ============= registerbgi =================== */
void error_registerbgi(int errorcode)
{
 if (errorcode < 0)
 {
  printf("Graphics error: %s/n", grapherrormsg(errorcode));
  printf("Press any key to halt:");
  exit(1);
 }
}

void installbgi(void)
{
 int gdriver = VGA, gmode =VGAHI, i;
 error_registerbgi(registerbgifont(triplex_font));
 error_registerbgi(registerbgifont(small_font));
 /*
 error_registerbgi(registerbgifont(sansserif_font));
 error_registerbgi(registerbgifont(gothic_font));
 */
 /* initialize graphics and local variables */
 error_registerbgi(registerbgidriver(EGAVGA_driver));
 initgraph(&gdriver, &gmode, "");
 error_registerbgi(graphresult());
}
/* ============== main funtion ================ */
void main(void)
{
  int keynum;
  driver_init();
  installbgi();
  head_scr();
  span_init();
  f_inti();
 eachline_backscr();
  reset_mouse();
  set_mouse_limits(4, 0, 623, 479);

  intrate=INTRATE;
  oldtimer=getvect(0x1c);
  install(newtimer,0x1c);

  while(run_program)
   {
  keybroad();
  if (int_ok)
   { int_ok--;
  data_pro();
  frontscr();
   }
   }

  install(oldtimer,0x1c);
  fclose(fdata);
  fclose(fpoint);
  closegraph();
  return;
}