home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 309_01 / mcrdrv.h < prev    next >
Text File  |  1990-03-20  |  6KB  |  206 lines

  1. /* MCRDRV.H
  2.  
  3.    This is the drivers for decoding and CARD (MCR) reading techniques
  4.    The program reads three lines from an input port.
  5.  
  6.       The lines are : CARD SENSE         (b0)
  7.                       CLOCK              (b1)
  8.                       DATA               (b2)
  9.  
  10.    These procedures are provided for use by the calling program:
  11.  
  12.       initmcr();
  13.       sensecard();
  14.       readcard();
  15.       decodecard();
  16.  
  17.    The following global declarations are required for this to work:
  18.  
  19. */
  20. #define MaxBitSize      401
  21. #define IOBaseAddress   0x9000
  22. #define MCRPortInit     0x227
  23. #define CardSenseBit    1
  24. #define MCR             0x226
  25. #define ClockBit        2
  26. #define DataBit         4
  27. #define CardSTX         11
  28. #define CardETX         15
  29. #define forwards        1
  30. #define backwards       -1
  31.  
  32. #data
  33. int     MaxIndex;
  34. char    buf[MaxBitSize];
  35.  
  36. #code
  37.  
  38. initmcr() /* This procedure initializes the PPI on Hugh's digital board */
  39. {
  40.         char *port;
  41.         port = IOBaseAddress;
  42.         port[ MCRPortInit ] = 0x83; /* Initialize 8255 PPI */
  43. }
  44.  
  45. sensecard() /* This procedure senses if a card is in the MCR, 1 = card */
  46. {
  47.         char    *portMCR;
  48.         portMCR = IOBaseAddress+MCR;
  49.         return( ((*portMCR) & CardSenseBit ) == 0 );
  50. }
  51.  
  52. readcard()
  53. {
  54.  /* This procedure reads a card connected to the 8255 PPI, placing each
  55.    data pulse bit in the byte array 'buf'.
  56.  
  57.    The procedure sets the global variable 'MAXINDEX' to the end of the
  58.    data in the array 'buf'.
  59.  */
  60.         char *portMCR;
  61.         MaxIndex = 0;
  62.         portMCR = IOBaseAddress+MCR;
  63.  
  64.         do
  65.         {
  66.                 while( (*portMCR & ClockBit) == 0 )
  67.                         ;
  68.                 while( ((*portMCR & ClockBit) != 0) && ((*portMCR & CardSenseBit) ==0) )
  69.                         ; /* Wait for edge */
  70.                 buf[ MaxIndex ] = *portMCR & DataBit; /* Get Card */
  71.                 if( MaxIndex < MaxBitSize )  MaxIndex++;
  72.         } while( (*portMCR & CardSenseBit) == 0);
  73. }
  74.  
  75. Pack( index, dirn, result )
  76. int index, dirn;
  77. char *result;
  78. {
  79. /*
  80.    This function packs the 5 bit code (4 bits+parity) in buf[ index ]
  81.    into the result byte.
  82.  
  83.    The function returns TRUE if the parity is correct.
  84.  
  85.    The pack may be done in either direction (forwards or backwards), with
  86.    dirn having the value 1 or -1
  87. */
  88.         char mask, parity;
  89.         mask = 1;
  90.         parity = 0;
  91.         *result = 0;
  92.         do {
  93.                 if( buf[index] == 0) {
  94.                         *result = *result | mask;
  95.                         if( parity == 0 ) parity = 1; else parity = 0;
  96.                 }
  97.                 index = index + dirn;
  98.                 mask  = mask + mask;
  99.         } while ( mask != 0x20 );
  100.         *result = *result & 15;
  101.         return parity;
  102. }
  103.  
  104. CheckPattern( index,dirn,result,rslt )
  105. int index, dirn;
  106. char result[], *rslt;
  107. {
  108.         char lst, Cont, loc;
  109.   /*
  110.    This function checks the pattern in the array buf, starting at element
  111.    'index'.
  112.  
  113.    If the pattern starts with a card 'STX' and ends with a card 'ETX', the
  114.    function returns TRUE. The decoded ASCII is placed in the string 'result'.
  115.  
  116.    The returned BYTE 'rslt' contains the last code found.
  117.   */
  118.         loc = 0;
  119.         if( Pack( index,dirn,rslt ) )
  120.         {
  121.                 if( *rslt == CardSTX )
  122.                 {
  123.                         index = index + ( dirn*5 );
  124.                         lst   = 0;
  125.                         Cont  = 1;
  126.                         while( Pack( index,dirn,rslt ) && Cont )
  127.                         {
  128.                                 if( *rslt == CardETX )
  129.                                 {
  130.                                         *rslt = 0;
  131.                                         result[ lst ] = 0;
  132.                                         loc = 1;
  133.                                         Cont = 0;
  134.                                 }
  135.                                 else if( ( index<=0 ) || ( index>=MaxBitSize ) )
  136.                                 {
  137.                                         rslt = 0;
  138.                                         result[lst] = 0;
  139.                                         loc = 1;
  140.                                         Cont = 0;
  141.                                 }
  142.                                 else
  143.                                 {
  144.                                         result[lst] = *rslt + 0x30;
  145.                                         lst++;
  146.                                         index = index+( dirn*5 );
  147.                                 }
  148.                         }
  149.                 }
  150.                 else if( *rslt == CardETX ) loc = 0;
  151.                 else
  152.                 {
  153.                         *rslt = 0;
  154.                         loc = 0;
  155.                 }
  156.         }
  157.         return loc;
  158. }
  159.  
  160. Decode( dirn, result )
  161. int dirn;
  162. char result[];
  163. {
  164.         char lastchar, loc, Cont;
  165.         int index;
  166. /*
  167.    This function returns TRUE if the buf array can be coded correctly. The
  168.    result is returned in the string 'result'.
  169.  
  170.    This function only makes a single pass through the buf bit array in one
  171.    direction.
  172. */
  173.         loc = 0;
  174.         if( dirn == forwards ) index = 0;
  175.         else index = MaxIndex;
  176.         Cont = 1;
  177.         while( Cont && ( index >= 0 ) && ( index <= MaxIndex ) )
  178.         {
  179.                 if( CheckPattern( index,dirn,result,&lastchar ) )
  180.                 {
  181.                         Cont = 0;
  182.                         loc = 1;
  183.                 }
  184.                 else if( lastchar == CardETX )
  185.                 {
  186.                         Cont = 0;
  187.                         loc  = 0;
  188.                 }
  189.                 else index = index+dirn;
  190.         }
  191.         return loc;
  192. }
  193.  
  194.  
  195. decodecard( result )
  196. char result[];
  197. {
  198. /*  This function attempts to decode the buf bit array in both the forward and
  199.    backward directions.
  200. */
  201.         if( Decode( forwards,result ) ) return 1;
  202.         else if( Decode( backwards,result ) ) return 1;
  203.         else return 0;
  204. }
  205.  
  206.