home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 15 / 15.iso / s / s300 / 1.ddi / CHAP7 / PDSP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-07-11  |  6.2 KB  |  250 lines

  1. /* Control display functions -- maintain graphic displays of
  2. system variables. 
  3.  
  4. Each  display has two variables,  one displayed by a vertical line
  5. whose length changes in proportion to the variable value, and the 
  6. other variable represented by a short horizontal bar whose position
  7. moves in porportion to its value.
  8.  
  9. Typical uses are for displaying a process output variable and the
  10. associated setpoint, or, when only the vertical bar is used, to display
  11. other process variables, output values, etc.
  12.  
  13. Each display is described by a data structure containing all of the
  14. relevant information.  Functions are provided to fill the structure and
  15. operate on it, so the user programmer need only declare the structure
  16. and assign storaage for it.
  17.  
  18. When defining display units in a user program, all that is needed is 
  19. to define the storage space for each and call the set-up program.
  20. The storage is allocated with one of the following declarations:
  21.  
  22. DISPLAY temp_dsp;
  23.  
  24. or to define a group of displays with,
  25.  
  26. DISPLAY dsp[10];
  27.  
  28. or to use the memory allocator,
  29.  
  30. DISPLAY *pr_dsp;
  31. char *malloc();
  32. ...
  33. pr_dsp = (DISPLAY *)malloc(NDSP * sizeof(DISPLAY));
  34.  
  35. Then, the function dsp_set() is called to put data into the structure.
  36.  
  37. copyright (c) 1987, D. M. Auslander
  38.  
  39. Created: 25 June 87
  40.  
  41. Updates:
  42.         10-July-87, DMA, bugs
  43. */
  44.  
  45. #include "display.h"    /* Contains the description of the data structure */
  46. #include "dparam.h"        /* Contains system-specific parameters */
  47.  
  48. /* Set up a structure for a display -- 
  49. pd is a pointer to a (blank) structure -- space for the structure must
  50. be assigned in the **calling** function */
  51.  
  52. dsp_set(pd,x,y,label,lngth,lcolor,vbot,vtop,fv1,fv2,fmt,dblnk)    /*
  53. --------------------------------------------------------------    */
  54. struct display *pd;
  55. float (*fv1)(),(*fv2)();    /* Functions called to get
  56.                 data */
  57. float x,y;        /* Position of the bottom of the vertical bar */
  58. float vbot,vtop;    /* Variable values at the top and bottom of the
  59.             line */
  60. float lngth;        /* Line length (in cm) */
  61. int lcolor;        /* Data display color */
  62. char *label;        /* Label for this display */
  63. char *fmt;        /* String with format to convert data */
  64. char *dblnk;        /* String ob blanks to erase old data */
  65. {
  66. pd->x = x;
  67. pd->y = y;
  68. pd->label = label;
  69. pd->lngth = lngth;
  70. pd->lcolor = lcolor;
  71. pd->vbot = vbot;
  72. pd->vtop = vtop;
  73. pd->fv1 = fv1;
  74. pd->fv2 = fv2;
  75. pd->fmt = fmt;
  76. pd->dblnk = dblnk;
  77. dsp_flag(pd,1,1,1,1);        /* Default is for all displays to be on */
  78. }
  79.  
  80. dsp_flag(pd,v1l,v1d,v2b,v2d)    /* Set the enabling flags for each of the
  81. -------------------------    itemss */
  82. int v1l,v1d,v2b,v2d;
  83. struct display *pd;
  84. {
  85. pd->v1line = v1l;
  86. pd->v1data = v1d;
  87. pd->v2bar = v2b;
  88. pd->v2data = v2d;
  89. }
  90.  
  91.  
  92. dsp_init(pd)    /* Initialize a display -- put on the labels, blank the spot
  93. ------------    where the line goes, etc. */
  94. struct display *pd;
  95. {
  96. char cc[50];        /* Space for a character buffer */
  97. float x,y,lngth;
  98. int lcolor;
  99.  
  100. x = pd->x;    /* Copy to local variables */
  101. y = pd->y;
  102. lngth = pd->lngth;
  103. lcolor = pd->lcolor;
  104.  
  105. mv_cur(x,y - laboff);        /* Move cursor to label position */
  106. pstr(pd->label);        /* Write the label to the screen */
  107.  
  108. drawl(x,y,x,y + lngth,backc);    /* Erase the old line */
  109.  
  110. /* Draw the top and bottom tick bars */
  111.  
  112. drawl(x + bx,y,x + bx + bw,y,lcolor);    /* Bottom */
  113.  
  114. sprintf(cc,pd->fmt,pd->vbot);        /* Value */
  115. mv_cur(x +  bx + bw + blx,y);        /* Print it */
  116. pstr(cc);
  117.  
  118. drawl(x + bx,y + lngth,x + bx + bw,y +lngth,lcolor);    /* Top */
  119.  
  120. sprintf(cc,pd->fmt,pd->vtop);        /* Value */
  121. mv_cur(x +  bx + bw + blx,y + lngth);        /* Print it */
  122. pstr(cc);
  123.  
  124. pd->val1 = pd->vbot;        /* Initialize values to point to the bottom */
  125. pd->val2 = pd->vbot;
  126.  
  127. pd->l1old = 0.0;            /* Old length values */
  128. pd->l2old = 0.0;
  129. }
  130.  
  131. dsp_data(pd)        /* Display the data
  132. ------------        */
  133. struct display *pd;
  134. {
  135. float x,y,lngth,vbot,vtop;
  136. float val,lnew,l1old,l2old,lim();
  137. char cc[50];
  138. char cbuf[20];
  139.  
  140. int lcolor,color;
  141.  
  142. x = pd->x;    /* Copy to local variables */
  143. y = pd->y;
  144. lngth = pd->lngth;
  145. lcolor = pd->lcolor;
  146. vbot = pd->vbot;
  147. vtop = pd->vtop;
  148. l1old = pd->l1old;
  149. l2old = pd->l2old;
  150.  
  151. if(pd->v1line || pd->v1data)
  152.     {
  153.     val = (*(pd->fv1))();    /* Get the value of the first variable */
  154.  
  155.     if((pd->v1line) && (val != pd->val1))
  156.         {
  157.         /* Only proceed if the value has changed and the line
  158.         is to be drawn */
  159.  
  160.         lnew = (val - vbot) * lngth / (vtop - vbot);
  161.         lnew = lim(lnew,0.0,lngth);
  162.  
  163.         /* Check to see if line goes up or down */
  164.         if(lnew > l1old)color = lcolor;
  165.         else        color = backc;
  166.         drawl(x,y + l1old,x,y + lnew,color);    /* Draw */
  167.         pd->l1old = lnew;    /* Save the new line length */        
  168.         }
  169.  
  170.     if(pd->v1data && (val != pd->val1))
  171.         {
  172.         /* Write the data value */
  173.  
  174.         if(pd->v1line)
  175.             mv_cur(x - v1x,y + l1old);
  176.         else
  177.             mv_cur(x - v1x,y + v1y);
  178.  
  179.         pstr(pd->dblnk);    /* Erase old value */
  180.         if(pd->v1line)
  181.             mv_cur(x - v1x,y + lnew);
  182.         else
  183.             mv_cur(x - v1x,y + v1y);
  184.  
  185.         sprintf(cc,pd->fmt,val);
  186.         pstr(cc);    /* Print value */
  187.         }
  188.     }
  189. pd->val1 = val;    /* Save value */
  190.  
  191. /* Now do second value */
  192.  
  193. if(pd->v2bar || pd->v2data)
  194.     {
  195.     val = (*(pd->fv2))();    /* Get the value of the first variable */
  196.  
  197.     if(pd->v2bar && (pd->val2 != val))
  198.         {
  199.         /* Only draw if bar is enabled and data has changed */
  200.  
  201.         lnew = (val - vbot) * lngth / (vtop - vbot);
  202.         lnew = lim(lnew,0.0,lngth);
  203.  
  204.         /* Erase the old value; Check first for overlap with top
  205.         and bottom fiducial marks */
  206.  
  207.         if((l2old > ltol) && (l2old < (lngth - ltol)))
  208.             {
  209.             drawl(x + bx,y +  l2old,x + bx + bw,y + l2old,backc);
  210.             }
  211.     
  212.         /* Draw the new value's bar */
  213.         if((lnew > ltol) && (lnew < (lngth - ltol)))
  214.             {
  215.             drawl(x + bx,y +  lnew,x + bx + bw,y + lnew,lcolor);
  216.             }
  217.         pd->l2old = lnew;    /* Save the new line length */        
  218.         }
  219.  
  220.     if(pd->v2data && (pd->val2 != val))
  221.         {
  222.         /* Write the data value */
  223.  
  224.         if(pd->v2bar)
  225.             mv_cur(x + v2x,y + l2old);
  226.         else
  227.             mv_cur(x + v2x,y + v2y);
  228.  
  229.         pstr(pd->dblnk);    /* Erase old value */
  230.         if(pd->v2bar)
  231.             mv_cur(x + v2x,y + lnew);
  232.         else
  233.             mv_cur(x + v2x,y + v2y);
  234.  
  235.         sprintf(cc,pd->fmt,val);
  236.         pstr(cc);    /* Print value */
  237.         }
  238.     }
  239. pd->val2 = val;        /* Save the value */
  240. }
  241.  
  242. float lim(x,xmin,xmax)        /* Return x or its limits
  243. ---------------------        */
  244. float x,xmin,xmax;
  245. {
  246. if(x < xmin)return(xmin);
  247. else if (x > xmax)return(xmax);
  248. return(x);
  249. }
  250.