⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 plc.c

📁 单片PLC,AT2581实现梯形图功能,可作为参考
💻 C
📖 第 1 页 / 共 5 页
字号:
   r -= scroll_baserow;
   if (r > MAX_GRAPHICS_ROW - LADDER_ROW_INCREMENT + LADDER_ROW_HEIGHT)
      return;
   if (level == 0)
      {
      for (i = 0; i < ladder_size; i++)
         if (rung_list[i] == rung)
            break;
      sprintf(tempstr,"%2d",i+1);
      strout(r,LADDER_LABEL_COL,tempstr);
      }
#ifdef NEVERDEF
   if (level)  /* not root */
      strout(r-LADDER_ROW_HEIGHT,rungs[rung].info.l_col,or_vertical);
#endif
   for (j = 0; j < rungs[rung].num_elements; j++)
      {
      c = rungs[rung].info.l_col + j * LADDER_ELEMENT_WIDTH;

           /* start link */
      if (rungs[rung].node[j].or_branch == OR_NULL)
         {
         if ((level == 0) || (j))
            strout(r,c,or_null);
         else
            strout(r,c,or_start);
         }
      else
         {
         if ((!level) || (j != 0))
            strout(r,c,or_link);
         else
            strout(r,c,or_start_t);
         i = rungs[rungs[rung].node[j].or_branch].info.u_row - scroll_baserow - LADDER_ROW_HEIGHT;
         for ( ; i > r; i -= LADDER_ROW_HEIGHT)
            strout(i,c,or_vertical);
         show_rung(rungs[rung].node[j].or_branch,level+1);
         }

        /* node element */
      strout(r,c+TEXTWIDTH,rung_element);
      if (rungs[rung].node[j].objindex != NULL_OBJ)
         (*objs[rungs[rung].node[j].objindex].drawfn)(r,c+TEXTWIDTH,&rungs[rung].node[j],TEXTFOREGROUND,TEXTBACKGROUND);
        /* end link */
      c += LADDER_ELEMENT_WIDTH - TEXTWIDTH;
      if (rungs[rung].node[j].linkstatus == ENDLINK)
         {
         if ((level) && (j == rungs[rung].num_elements - 1))  /* end of OR branch */
            strout(r,c,or_end_t);
         else
            strout(r,c,or_link);
         }
      else
         {
         if ((level) && (j == rungs[rung].num_elements - 1))  /* end of OR branch */
            strout(r,c,or_end);
         else
            {
            strout(r,c,or_null);
#ifdef SHOW_NODES
            if (j != rungs[rung].num_elements - 1)
               {
               setpixel(r+3,c,TEXTFOREGROUND);
               setpixel(r+4,c,TEXTFOREGROUND);
               }
#endif
            }
         }
      }
   if (rungs[rung].node[0].or_parent != OR_NULL)
      {
      j = rungs[rungs[rung].node[0].or_parent].info.u_row - scroll_baserow;
      for (r -= LADDER_ROW_HEIGHT ; r > j; r -= LADDER_ROW_HEIGHT)
         strout(r,c,or_vertical);
      }
   }

erase_ladder()
   {
   erase_box(0,LADDER_LEFT_COL - 3 * TEXTWIDTH,MAX_GRAPHICS_ROW,LADDER_RIGHT_COL - LADDER_LEFT_COL + 8 * TEXTWIDTH);
   }

show_ladder()
   {
   WORD j;
   WORD r;
   WORD maxr;

   erase_ladder();
   maxr = get_ladder_maxrow() - scroll_baserow;
   for (j = 0; j < ladder_size; j++)
      {
      show_rung(rung_list[j],0);
      }
   for (j = 0; j <= maxr; j += LADDER_ROW_HEIGHT)
      {
      strout(j,LADDER_LEFT_COL - TEXTWIDTH,ladder_left);
      strout(j,LADDER_RIGHT_COL,ladder_right);
      }
   show_scrollbar();
   }

show_scrollbar()
   {
   WORD rung, r, maxr;

   cstrout(SCROLL_UPARROW_ROW,SCROLL_LEFTCOL,scroll_uparrow,DKGREY,BLACK);
   cstrout(SCROLL_DOWNARROW_ROW,SCROLL_LEFTCOL,scroll_downarrow,DKGREY,BLACK);
   for (rung = 0; rung < ladder_size; rung++)
      if (rungs[rung_list[rung]].info.u_row == (scroll_baserow + LADDER_ROW_HEIGHT))
         break;
   r = TEXTHEIGHT;
   if (rung)  /* if we can scroll up */
      {
      strout(SCROLL_UPARROW_ROW,SCROLL_LEFTCOL,scroll_uparrow);
      if (ladder_size > 1)
         maxr = SCROLLBAR_SIZE * rung / (ladder_size - 1);
      else
         maxr = SCROLLBAR_SIZE;
      for ( ; r < maxr; r += TEXTHEIGHT)
         strout(r,SCROLL_LEFTCOL,scroll_bar);
      }
   cstrout(r,SCROLL_LEFTCOL,scroll_box,GREEN,TEXTBACKGROUND);
   for (r += TEXTHEIGHT; r <= SCROLLBAR_SIZE; r += TEXTHEIGHT)
      strout(r,SCROLL_LEFTCOL,scroll_bar);
   if (rung != ladder_size - 1)
      strout(SCROLL_DOWNARROW_ROW,SCROLL_LEFTCOL,scroll_downarrow);
   }

void scrollladder(menuindex)
   WORD menuindex;
   {
   WORD rung, r;

   for (rung = 0; rung < ladder_size; rung++)
      if (rungs[rung_list[rung]].info.u_row == (scroll_baserow + LADDER_ROW_HEIGHT))
         break;
   if (ladder_size > 1)
      r = SCROLLBAR_SIZE * rung / (ladder_size - 1) + TEXTHEIGHT;
   else
      r = 2 * TEXTHEIGHT;
   if (mouser <= SCROLL_UPARROW_ROW + TEXTHEIGHT)
      rung = rung ? rung - 1 : 0;
   else
      if (mouser >= SCROLL_DOWNARROW_ROW)
         rung = (rung == ladder_size - 1) ? rung : rung + 1;
      else
         {
         if (mouser <= r)
            {
            if (rung)
               scroll_baserow = rungs[rung_list[rung-1]].info.u_row - LADDER_ROW_HEIGHT;
                  /* we want the "previous" rung to be the last rung on the screen */
            for ( ; rung > 0; rung--)  /* scroll up one "page" */
               if (((scroll_baserow + LADDER_ROW_HEIGHT) - rungs[rung_list[rung]].info.u_row < MAX_GRAPHICS_ROW)
                   && ((scroll_baserow + LADDER_ROW_HEIGHT) - rungs[rung_list[rung-1]].info.u_row >= MAX_GRAPHICS_ROW))
                  break;
            }
         else
            if (rung != ladder_size-1)
               {
               for ( ; rung < ladder_size - 1; rung++)  /* scroll down one "page" */
                  if (rungs[rung_list[rung]].info.u_row - (scroll_baserow + LADDER_ROW_HEIGHT) >= MAX_GRAPHICS_ROW)
                     break;
               }
         }
   scroll_baserow = rungs[rung_list[rung]].info.u_row - LADDER_ROW_HEIGHT;
   show_ladder();
   }

erase_box(r,c,numrows,numcols)
   {
#asm
   push es
   cmp  word [bp+8],0   ;get rows
   jz   skip_erase_box
   cmp  word [bp+10],0  ;and cols
   jz   skip_erase_box
   mov  ax,0a000h
   mov  es,ax
erase_box_loop:
   mov  ax,word [bp+4]   ;get row
   mov  bx,1024  ;bytes per line
   mul  bx
   push ax
   cmp  dx,word videopage_
   je   erase_box_havebank
   mov  word videopage_,dx
   mov  bx,0   ;bh = subfn = select memory window, bl = window = A
   mov  ax,4f05h
   int  10h
erase_box_havebank:
   pop  di
   add  di,word [bp+6]  ;plus cols
   mov  cx,word [bp+10] ;get number of cols
   cld
   xor  ax,ax
   clc
   rcr  cx,1
   jcxz erase_box_skipit
rep stosw
erase_box_skipit:
   jnc  erase_box_skipodd
   stosb
erase_box_skipodd:
   inc  word [bp+4]   ;next row
   dec  word [bp+8]   ;count this wor
   jnz  erase_box_loop
skip_erase_box:
   pop  es
#endasm
   }

gchar()
   {
#asm
   xor  al,al             ;set ZR, clear CY
   mov  ah,11h            ;fn = 101-key check for char
   int  16h               ;call BIOS
   jnae g_a_c_2           ;error, check normal way
   jnz  get_the_char      ;have it, get it
g_a_c_2:
   mov  ah,1              ;check for char
   int  16h               ;call BIOS
   jnz  get_the_char      ;no char, we're done
   xor  ax,ax             ;return is 0
   jmp  get_a_char_done   ;and quit
get_the_char:
   mov  ah,0              ;fn = get char
   int  16h               ;call BIOS
   or   al,al             ;char = 0 (special char?)
   jnz  get_a_char_normal ;no, skip this
   mov  al,ah             ;scancode to al
   mov  ah,1              ;set fnkey flag
   jmp  get_a_char_done   ;and done
get_a_char_normal:
   mov  ah,0              ;clear scancode
;   cmp  al,'a'            ;lower case?
;   jnae get_a_char_done   ;no, skip this
;   cmp  al,'z'            ;still lower case?
;   ja   get_a_char_done   ;no, skip this
;   sub  al,20h            ;force upper case
get_a_char_done:
#endasm
   }

getstr(row,col,str,maxstr,foreground,background)
   WORD row, col;
   char *str;
   WORD maxstr,foreground,background;
   {
   int ch, i;
   char onechar[2];

   while (csts())
      ci();  /* clear keyboard queue */
   onechar[1] = '\0';
   i = 0;
   do
      {
      onechar[0] = '_';
      cstrout(row,col,onechar,RED,background);  /* draw cursor */
      do
         {
         ch = gchar();
         } while (!ch);
      onechar[0] = ' ';
      cstrout(row,col,onechar,foreground,background);  /* erase cursor */
      if ((ch > ' ') && (ch < 0x7f) && (i < maxstr))
         {
         str[i++] =
         onechar[0] = toupper(ch);
         cstrout(row,col,onechar,foreground,background);
         col += TEXTWIDTH;
         }
      if ((ch == '\010') && (i))  /* backspace */
         {
         i--;
         onechar[0] = ' ';
         col -= TEXTWIDTH;
         cstrout(row,col,onechar,foreground,background);
         }
      if (ch == ESC)
         i = 0;  /* return blank string */
      } while ((ch != CR) && (ch != ESC) && (ch != '\t'));
   str[i] = '\0';
   while (csts())
      ci();  /* clear keyboard queue */
   }

draw_popbox(label)
   BYTE *label;
   {
   WORD r, c, i, j, pos;
   BYTE boxline[POPBOX_SIZE + 2];

   for (i = 1; i < POPBOX_SIZE - 1; i++)
      boxline[i] = ' ';
   boxline[i+1] = 0;  /* set EOS */
   boxline[POPBOX_SIZE - 1] =
   boxline[0] = BOX_SIDEBAR;
   for (r = POPBOX_ROW + TEXTHEIGHT, i = 1; i < POPBOX_MAXROWS; i++, r += TEXTHEIGHT)
      cstrout(r,POPBOX_COL,boxline,POPBOX_FOREGROUND,POPBOX_BACKGROUND);
   for (i = 1; i < POPBOX_SIZE - 1; i++)
      boxline[i] = BOX_TOPBAR;
   boxline[0] = BOX_TOP_LEFT;
   boxline[POPBOX_SIZE - 1] = BOX_TOP_RIGHT;
   cstrout(POPBOX_ROW,POPBOX_COL,boxline,POPBOX_FOREGROUND,POPBOX_BACKGROUND);
   boxline[0] = BOX_BOTTOM_LEFT;
   boxline[POPBOX_SIZE - 1] = BOX_BOTTOM_RIGHT;
   cstrout(POPBOX_ROW + TEXTHEIGHT * POPBOX_MAXROWS,POPBOX_COL,boxline,POPBOX_FOREGROUND,POPBOX_BACKGROUND);
   if (label)
      cstrout(POPBOX_ROW + 2 * TEXTHEIGHT, POPBOX_COL + 3 * TEXTWIDTH,label,POPBOX_FOREGROUND,POPBOX_BACKGROUND);
   }

erase_popbox()
   {
   erase_box(POPBOX_ROW,POPBOX_COL,(POPBOX_MAXROWS + 1) * TEXTHEIGHT,POPBOX_SIZE * TEXTWIDTH);
   }

get_values(label,q1,q2,q3,a1,a2,a3)
   BYTE *label, *q1, *q2, *q3, *a1, *a2, *a3;
   {
   WORD pos;

   draw_popbox(label);
   pos = 0;
   if (a1)
      *a1 = 0;
   if (a2)
      *a2 = 0;
   if (a3)
      *a3 = 0;
   if (q1)
      {
      cstrout(POPBOX_ROW + 4 * TEXTHEIGHT, POPBOX_COL + 3 * TEXTWIDTH,q1,POPBOX_FOREGROUND,POPBOX_BACKGROUND);
      if (strlen(q1) > pos)
         pos = strlen(q1);
      }
   if (q2)
      {
      cstrout(POPBOX_ROW + 5 * TEXTHEIGHT, POPBOX_COL + 3 * TEXTWIDTH,q2,POPBOX_FOREGROUND,POPBOX_BACKGROUND);
      if (strlen(q2) > pos)
         pos = strlen(q2);
      }
   if (q3)
      {
      cstrout(POPBOX_ROW + 6 * TEXTHEIGHT, POPBOX_COL + 3 * TEXTWIDTH,q3,POPBOX_FOREGROUND,POPBOX_BACKGROUND);
      if (strlen(q3) > pos)
         pos = strlen(q3);
      }
   if ((pos) && (q1))
      {
      getstr(POPBOX_ROW + 4 * TEXTHEIGHT, POPBOX_COL + (3 + pos) * TEXTWIDTH, a1, 10,POPBOX_FOREGROUND,POPBOX_BACKGROUND);
      if (a1[0] == 0)
         pos = 0;  /* don't ask any more questions */
      }
   if ((pos) && (q2))
      {
      getstr(POPBOX_ROW + 5 * TEXTHEIGHT, POPBOX_COL + (3 + pos) * TEXTWIDTH, a2, 10,POPBOX_FOREGROUND,POPBOX_BACKGROUND);
      if (a2[0] == 0)
         pos = 0;  /* don't ask any more questions */
      }
   if ((pos) && (q3))
      getstr(POPBOX_ROW + 6 * TEXTHEIGHT, POPBOX_COL + (3 + pos) * TEXTWIDTH, a3, 10,POPBOX_FOREGROUND,POPBOX_BACKGROUND);
   erase_popbox();
   }

init_mouse()
   {
#asm
   xor  ax,ax              ;get a 0
   mov  es,ax              ;address INT seg
   mov  ax,word es:[0cch]  ;get INT 33 ofs
   or   ax,word es:[0ceh]  ;OR-in INT 33 seg
   jz   init_mouse_done    ;no INT vector, w

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -