/*   SEETHIS - Send Messages to the SLI


  "Can You See This" - Message displayed on an LCD at Various Speeds
  and for Various Lengths of Data.

  myke predko - 96.12.15

                                                                    */


/*  Defines                                                         */
#include <stdio.h>
#include <dos.h>

#define KEYINT  0x0016          /*  BIOS Keyboard Interrupt         */
#define GETKEY  0x010           /*  Return the Entered Key          */


/*  Global Variables                                                */
int Port = 1;                   /*  COM Port Used and Address       */
int PortAddr = 0x03F8;
long int Speed = 2400;          /*  Selected/Default Data Speed     */
int SpeedDiv = 0x030;
int EndFlag = 0;                /*  Flag Set to End the Program     */



/*  Routines                                                        */
int GetSel( int Bot, int Top )  /*  Handle KeyStrokes for Menu      */
{

union REGS regs;                /*  Do Interrupt Calls for Keys     */

int  EndFlag = 0;               /*  Wait Until End Key Hit          */
int  RetKey = 0;                /*  Key to be Returned              */


  while ( EndFlag == 0 )        /*  Loop Until Valid Key Returned   */
    {
      regs.h.ah = GETKEY;       /*  Get a KeyStroke                 */
      int86( KEYINT, &regs, &regs );
      if (( regs.h.al != 0x000 ) && ( regs.h.al != 0x0E0 ))
        switch ( regs.h.al )    /*  Handle ONLY Valid KeyStrokes    */
          {
            case 0x0D:          /*  Enter - Can we Return?          */
              if ( RetKey != 0 )
                EndFlag++;      /*  Yes, Something to Return        */
              break;
            case 0x08:          /*  BackSpace, Anything to Clear?   */
              if ( RetKey != 0 )
                {
                  RetKey = 0;   /*  Nothing to Handle               */
                  printf( "%c", 0x08 );  /*  Delete the Character   */
                  printf( " " );
                  printf( "%c", 0x08 );
                }  /*  endif  */
              break;
            default:            /*  Handle Everything Else          */
              if (( RetKey == 0 ) &&
                       (( regs.h.al >= Bot ) && ( regs.h.al <= Top )))
                printf( "%c", RetKey = regs.h.al );
          } /*  endswitch  */
    }  /*  endwhile  */

  return( RetKey & 0x0F );      /*  Just Return the Number          */

}  /*  End GetSel  */


PortSel()                       /*  Select the Port to Use          */
{


  printf( "Port Select - 1 - COM1\n" );
  printf( "              2 - COM2\n" );
  printf( "              3 - COM3\n" );
  printf( "              4 - COM4\n" );
  printf( "              5 - Return without Entering Anything\n\n" );
  printf( " Enter " );

  switch ( GetSel( '1', '5' ))  /*  Handle the Data Selection       */
    {
      case 1:                   /*  Handle the Port Selection       */
        Port = 1;
        PortAddr = 0x03F8;
        break;
      case 2:
        Port = 2;
        PortAddr = 0x02F8;
        break;
      case 3:
        Port = 3;
        PortAddr = 0x03E8;
        break;
      case 4:
        Port = 4;
        PortAddr = 0x02E8;
        break;
      default:;                 /*  Else, End the Program           */
    } /*  endswitch  */

  printf( "\n\n" );             /*  Port Selected - Return          */

}  /*  End PortSel  */


SpeedSel()                      /*  Select the Speed to Use         */
{


  printf( "Speed Select - 1 - 110 bps\n" );
  printf( "               2 - 300 bps\n" );
  printf( "               3 - 1200 bps\n" );
  printf( "               4 - 2400 bps\n" );
  printf( "               5 - 9600 bps\n" );
  printf( "               6 - 19200 bps\n" );
  printf( "               7 - 115200 bps\n" );
  printf( "               8 - Return without Entering Anything\n\n" );
  printf( " Enter " );

  switch ( GetSel( '1', '9' ))  /*  Handle the Data Selection       */
    {
      case 1:                   /*  110 bps                         */
        Speed = 110;
        SpeedDiv = 1047;
        break;
      case 2:                   /*  300 bps                         */
        Speed = 300;
        SpeedDiv = 768;
        break;
      case 3:                   /*  1200 bps                        */
        Speed = 1200;
        SpeedDiv = 192;
        break;
      case 4:                   /*  2400 bps                        */
        Speed = 2400;
        SpeedDiv = 96;
        break;
      case 5:                   /*  9600 bps                        */
        Speed = 9600;
        SpeedDiv = 12;
        break;
      case 6:                   /*  19200 bps                       */
        Speed = 19200;
        SpeedDiv = 6;
        break;
      case 7:                   /*  115200 bps                      */
        Speed = 115200;
        SpeedDiv = 1;
        break;
      default:;                 /*  Else, End the Program           */
    } /*  endswitch  */

  printf( "\n\n" );             /*  Port Selected - Return          */

}  /*  End SpeedSel  */


PortInit()                      /*  Initialize Port to Correct Speed*/
{


  outp( PortAddr + 3, inp( PortAddr + 3 ) | 0x080 );  /*  Set Port  */
  outp( PortAddr, SpeedDiv & 0x0FF );  /*   Divisor                 */
  outp( PortAddr + 1, SpeedDiv >> 8 );
  outp( PortAddr + 3, inp( PortAddr + 3 ) & 0x07F );

  outp( PortAddr + 4, inp( PortAddr + 4 ) | 0x003 );

}  /*  End PortInit  */

SendMsg( int i )                /*  Send the Data the Spec'd # of   */
{                               /*   Times                          */

int  j;                         /*  Character Count                 */
char far * Message = "Can You See This";  /*  The Message to send   */


  while ( i != 0 )              /*  Count Down How Many Times to    */
    {                           /*   Send the Message               */
      for ( j = 0; j < 16; j++ )
        {
          outp( PortAddr, Message[ j ]);
          while(( inp( PortAddr + 5 ) & 0x020 ) == 0 );
        }  /*  endfor  */
      i--;
    }  /*  endwhile  */

}  /*  End SendMsg  */



SendData()                      /*  Find Out How Many Times to Send */
{                               /*   The Message                    */

union REGS regs;

int  i;


  PortInit();                   /*  Initialize Port to correct Speed*/

  printf( "Device Select - 1 - SLI\n" );
  printf( "                2 - Another Serial Interface\n\n" );
  printf( " Enter " );

  switch ( GetSel( '1', '2' ))  /*  Handle the Data Selection       */
    {
      case 1:                   /*  SLI - Have to Ask to Hit Reset  */
        printf( "\n\nPress RESET on SLI and Press Any Key when Ready\n\n" );
        regs.h.ah = GETKEY;     /*  Get a KeyStroke                 */
        int86( KEYINT, &regs, &regs );
        for ( i = 0; i < 1000; i++ )
          {
            inp( PortAddr );    /*  Make Sure Delay is Put in       */
          }  /*  endfor  */
        outp( PortAddr, 0x00D ); /*  Send a Carriage Return         */
      default:                  /*  Else, Skip the Above            */
        printf( "\n\n" );
    } /*  endswitch  */

  printf( "Select Number of Lines to Send - 1 - 1 Line\n" );
  printf( "                                 2 - 2 Lines\n" );
  printf( "                                 3 - 10 Lines\n" );
  printf( "                                 4 - 100 Lines\n" );
  printf( "                                 5 - 1000 Lines\n" );
  printf( "                                 6 - Return without Sending Anything\n\n" );
  printf( " Enter " );

  switch ( GetSel( '1', '6' ))  /*  Handle the Data Selection       */
    {
      case 1:                   /*  1 Line to Send                  */
        SendMsg( 1 );
        break;
      case 2:                   /*  2 Lines to Send                 */
        SendMsg( 2 );
        break;
      case 3:                   /*  10 Lines to Send                */
        SendMsg( 10 );
        break;
      case 4:                   /*  100 Lines to Send               */
        SendMsg( 100 );
        break;
      case 5:                   /*  1000 Lines to Send              */
        SendMsg( 1000 );
        break;
      default:;                 /*  Else, Return without Sending    */
    } /*  endswitch  */

  printf( "\n\n" );             /*  Port Selected - Return          */

}  /*  End SendData  */


/*  MainLine                                                        */
main(argc, argv, envp)
  int argc;
  char *argv[];
  char *envp;
{


  printf( "\"Can You See This\" - myke predko - December 15, 1996\n\n" );

  while ( EndFlag == 0 )        /*  Wait for Program Stop Selected  */
    {

      printf( "Current  - Port COM%i  Speed ", Port );
      if ( Speed == 115200 )
        printf( "115200 bps\n\n" );
      else
        printf( "%i bps\n\n", Speed );

      printf( "Menu     - 1 - Select Port\n" );
      printf( "           2 - Select Data Speed\n" );
      printf( "           3 - Send Data\n" );
      printf( "           4 - End Program\n\n" );
      printf( " Enter " );

      switch ( GetSel( '1', '4' ))  /*  Handle the Data Selection   */
        {
          case 1:               /*  Select the Port                 */
            printf( "\n\n" );
            PortSel();
            break;
          case 2:               /*  Select the Data Speed           */
            printf( "\n\n" );
            SpeedSel();
            break;
          case 3:               /*  Send the Data                   */
            printf( "\n\n" );
            SendData();
            break;
          default:              /*  Else, End the Program           */
            printf( "\n" );
            EndFlag++;
        } /*  endswitch  */
    }  /*  endwhile  */

}  /*  End Mainline  */
