home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / e / e065 / 2.ddi / PATTERN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-28  |  28.3 KB  |  930 lines

  1. /*09-Feb-89  (pattern.c)  Pattern I/O routine */
  2. /* 13Apr90 -emb -chgs for Zortech v2.0 compatibility */
  3. /************************************************************************
  4.  * Copyright(C) 1988-1990 NeuralWare Inc
  5.  * Penn Center West, IV-227, Pittsburgh, PA 15276
  6.  *
  7.  * All rights reserved.  No part of this program may be reproduced,
  8.  * stored in a retrieval system, or transmitted, in any form or by any
  9.  * means, electronic, mechanical, photocopying, recording or otherwise
  10.  * without the prior written permission of the copyright owner,
  11.  * NeuralWare, Inc.
  12.  ************************************************************************
  13.  */
  14. #include "userutl.h"
  15. #include <string.h>
  16. #ifndef SUN
  17. #ifndef DLC
  18. #include <stdlib.h>
  19. #endif
  20. #endif
  21.  
  22. #ifdef MAC
  23. #include  "macuio.redef"
  24. #endif
  25.  
  26. /************************************************************************
  27.  *
  28.  *  This is a general purpose pattern I/O routine for use with
  29.  *  any network type.
  30.  *
  31.  *  Options:
  32.  *
  33.  *  ATTENTION I/O:  This allows the user to create a file of
  34.  *      character patterns and their associated
  35.  *      ASCII code. This file is written in .nna
  36.  *      format and can be used either for file I/O
  37.  *      or for userio learning. The user
  38.  *      is prompted for file name (.nna extension
  39.  *      assumed), and is given the option of writing or
  40.  *      appending the file. The user is provided with
  41.  *      a grid in which to draw patterns. On entering
  42.  *      a pattern, the user is prompted to enter, from
  43.  *      the keyboard, the character that the pattern
  44.  *      depicts. The file is closed on quitting.
  45.  *
  46.  *  REWIND:   If a file is open it is rewound.
  47.  *
  48.  ************************************************************************
  49.  */
  50.  
  51.  
  52. /* grid definitions */
  53. static long n_across[2], n_down[2], n_inputs[2], tot_n_inputs;
  54.  
  55. #define N_ACR_MIN 1
  56. #define N_ACR_MAX 32
  57. #define N_ACC_DEF 6
  58. #define N_DWN_MIN 1
  59. #define N_DWN_MAX 32
  60. #define N_DWN_DEF 8
  61.  
  62. /* window coordinates - initialized in usrIO( ) */
  63. static int  wxstrt[4], wystrt[4], wxend[4], wyend[4];
  64.  
  65. typedef struct _grid {
  66.   unsigned char gr_key;   /* window key for grid */
  67.   unsigned char gr_flag;  /* grid flag */
  68.   int  gr_xoff, gr_yoff;  /* x,y offset from window origin */
  69.   int  gr_nx, gr_ny;    /* # pixels in x, y directions */
  70.   int  gr_pxx, gr_pxy;  /* size of a grid pixel */
  71.   int  gr_color[3];   /* color of lines and pixels */
  72. #define    GR_OFF   0 /* off color index */
  73. #define    GR_ON    1 /* on color index */
  74. #define    GR_LINE  2 /* line color index */
  75.   float *gr_data;   /* data for grid */
  76. } GRID;
  77.  
  78. static  GRID  ip_grid[2] = { 0 }; /* Input Grids */
  79.  
  80. /* Local menu definitions */
  81.  
  82. #define CR_SCROLL 0
  83. #define CR_SEEK   1
  84. #define CR_ENTER  2
  85. #define CR_QUIT   3
  86. #define CR_AUTO   4
  87.  
  88. #define NULL_STR    0
  89.  
  90. static GMENU_ITEM  PMenuList[] = {  /* Menu list */
  91.     { CR_SCROLL, NULL_STR, NULL_STR },
  92.     { CR_SEEK,   NULL_STR, NULL_STR },
  93.     { CR_ENTER,  NULL_STR, NULL_STR },
  94.     { CR_QUIT,   NULL_STR, NULL_STR },
  95.     { CR_AUTO,   NULL_STR, NULL_STR }
  96. };
  97. static GMENU  AIOMenu  = {  /* Attention I/O menu */
  98.   0,
  99.   4,        /* # items */
  100.   3,        /* key */
  101.   0x0000
  102. };
  103. static GMENU  LrnMenu  = {  /* Attention I/O menu */
  104.   0,
  105.   5,        /* # items */
  106.   3,        /* key */
  107.   0x0000
  108. };
  109.  
  110. /* File definitions */
  111. static  char  filename[13];
  112. static  char  filenroot[9];
  113. static  int curr_pattern, n_patterns;   /* pattern index, # patterns */
  114. #define MAX_N_PATTERNS  200
  115. static  long  *filepos; /* Array of file positions */
  116. static  int   *shuffle_array;/* Index into shuffle array */
  117. static  int shufflex =  0;    /* Shuffle Index */
  118. #define MAX_STR 78        /* For reading from file */
  119. static  char  fbuf[MAX_STR + 1];    /* buffer for file i/o */
  120. static  FILE  *fp = 0;      /* File pointer */
  121. static  float *fdata;     /* file data */
  122. /* Network parameters */
  123. static  int   nlayp, ninp, noutp, ltype;  /* Network parameters */
  124. static  char  *csp, *netnp;     /* Network pointers*/
  125. static  int   BamFlag = 0;      /* Flags BAM control strat */
  126. /* Graphics parameters */
  127. static  int   xsize, ysize, ncolor, chrx, chry; /* graphics parameters */
  128. /* Miscellaneous */
  129. static  int abortio;
  130. static  int autoip;
  131. #if UNIX || MAC || NEXT
  132. strlwr( dp )
  133. char *dp;
  134. {
  135.     int   c;
  136.  
  137.     for( ; (c = *dp) != 0; dp++ ) {
  138.   if ( 'A' <= c && c <= 'Z' ) *dp = c - 'A' + 'a';
  139.     }
  140. }
  141. #endif
  142.  
  143.  
  144. #ifdef DLC
  145. /* --- prototypes --- */
  146.   SetUpGrids( );
  147.   DispGrid( GRID * );
  148.   DispPattern( GRID * );
  149.   Sketch( GRID * );
  150.   DrawPixel( GRID *, int, int, int );
  151.   FillPosArr(  );
  152.   ReadFData( );
  153.   WriteFData( );
  154.   Shuffle( int );
  155. #endif
  156.  
  157. SetUpGrids( ) /* Set grids and grid window positions */
  158. {
  159.   int gridwidth[2];
  160.   int inbetween;
  161.   int wx;
  162.   
  163.   if ( BamFlag ) inbetween = 20;
  164.   else inbetween = 0;
  165.   
  166.   gridwidth[0] = gridwidth[1] = 0;
  167.   /* Set up grid parameters */
  168.   for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ ) {
  169.     ip_grid[wx].gr_data = wx ? &fdata[n_inputs[0]] : fdata;
  170.     ip_grid[wx].gr_key = wx+1;
  171.     ip_grid[wx].gr_flag = 0;
  172.     ip_grid[wx].gr_xoff = ip_grid[wx].gr_yoff = 0;
  173.     ip_grid[wx].gr_nx = n_across[wx];
  174.     ip_grid[wx].gr_ny = n_down[wx];
  175.     /* Bigger grid => smaller pixels: */
  176.     ip_grid[wx].gr_pxx = 20 - ( n_across[wx] + n_down[wx] ) / 4;
  177.     if ( ip_grid[wx].gr_pxx < 1 ) ip_grid[wx].gr_pxx = 1;
  178.     ip_grid[wx].gr_pxy = ip_grid[wx].gr_pxx;
  179.     gridwidth[wx] =
  180.       ip_grid[wx].gr_nx * (ip_grid[wx].gr_pxx+1) + 1;
  181.   }
  182.   /* Now calculate positions of grid windows */
  183.   wxstrt[0] =
  184.     ( xsize - gridwidth[0] - gridwidth[1] - inbetween ) / 2 ;
  185.   wystrt[0] = ( ysize - (ip_grid[0].gr_ny * (ip_grid[0].gr_pxy+1) + 1) ) / 2 ;
  186.   if (BamFlag) {
  187.     wxstrt[1] = wxstrt[0] + gridwidth[0] + inbetween;
  188.     wystrt[1] = wystrt[0];
  189.   }
  190.   for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ ) {
  191.     wxend[wx] = wxstrt[wx] + gridwidth[wx] - 1;
  192.     wyend[wx] = wystrt[wx] + ip_grid[wx].gr_ny * (ip_grid[wx].gr_pxy+1);
  193.     ug_window( ip_grid[wx].gr_key, ip_grid[wx].gr_color[GR_OFF],
  194.          wxstrt[wx], wystrt[wx], wxend[wx], wyend[wx] );
  195.   }
  196. }
  197.  
  198. DispGrid( gridp )
  199. GRID  *gridp;   /* pointer to grid structure */
  200. {
  201.   int wx;   /* work index */
  202.   int x0,y0,x1,y1;  /* endpoints of gridlines */
  203.   
  204.   /* Draw vertical gridlines */
  205.   x1 = x0 = gridp->gr_xoff;
  206.   y0 = gridp->gr_yoff;
  207.   y1 = y0 + gridp->gr_ny * ( gridp->gr_pxy + 1 );
  208.   
  209.   for (wx = 0; wx <= gridp->gr_nx; wx++, x1 = x0 += gridp->gr_pxx + 1 )
  210.     ug_line( gridp->gr_key, gridp->gr_color[GR_LINE], 0, x0, y0, x1, y1, 0 );
  211.  
  212.   /* Draw horizontal gridlines */
  213.   x0 = gridp->gr_xoff;
  214.   x1 = x0 + gridp->gr_nx * ( gridp->gr_pxx + 1 );
  215.   y1 = y0 = gridp->gr_yoff;
  216.   
  217.   for (wx = 0; wx <= gridp->gr_ny; wx++, y1 = y0 += gridp->gr_pxy + 1 )
  218.     ug_line( gridp->gr_key, gridp->gr_color[GR_LINE], 0, x0, y0, x1, y1, 0 );
  219. }
  220.  
  221. DispPattern( gridp )
  222. GRID  *gridp;   /* Pointer to grid structure */
  223. {
  224.   int wx, wy, wz; /* work indices */
  225.   float *dp;    /* pointer to data */
  226.  
  227.   dp = &gridp->gr_data[0];
  228.   if ( dp == ( float * )0 ) return;
  229.   
  230.   for ( wy = gridp->gr_ny, wz = 0; --wy >= 0 ; )
  231.     for ( wx = 0; wx < gridp->gr_nx; wx++, wz++ )
  232.       DrawPixel( gridp, wx, wy,
  233.            gridp->gr_color[ dp[wz]>0.0001 ? GR_ON : GR_OFF ] );
  234. }
  235.  
  236.  
  237. Sketch( gridp )
  238. GRID  *gridp;   /* Pointer to grid structure */
  239. {
  240.   int x0, y0, x1, y1;   /* Grid boundary */
  241.   int wx, wy, wz;   /* work indices */
  242.   int key, xp, yp, button;  /* mouse parameters */
  243.   float *dp;      /* pointer to data */
  244.   
  245.   dp = &gridp->gr_data[0];
  246.   if ( dp == ( float * )0 ) return;
  247.   x0 = gridp->gr_xoff;
  248.   y0 = gridp->gr_yoff;
  249.   x1 = x0 + gridp->gr_nx * ( gridp->gr_pxx + 1 );
  250.   y1 = y0 + gridp->gr_ny * ( gridp->gr_pxy + 1 );
  251.  
  252.   for (;;) {
  253.     ug_mouse( &key, &xp, &yp, &button );
  254.     if ( key != gridp->gr_key || xp < x0 || xp >= x1 || yp < y0 || yp >= y1 )
  255.       return;
  256.     if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
  257.       /* Calculate grid coordinate */
  258.       wx = ( xp - x0 ) / ( gridp->gr_pxx + 1 );
  259.       wy = ( yp - y0 ) / ( gridp->gr_pxy + 1 );
  260.       DrawPixel( gridp, wx, wy,
  261.            gridp->gr_color[ button == MBUT_LEFT ? GR_ON : GR_OFF ] );
  262.       /* Modify data array */
  263.       wz = (gridp->gr_ny - wy - 1) * gridp->gr_nx + wx;
  264.       dp[wz] = button == MBUT_LEFT ? 1.0 : -1.0;
  265.     }
  266.   }
  267. }
  268.  
  269. DrawPixel( gridp, x, y, color )
  270. GRID  *gridp;   /* pointer to grid structure */
  271. int  x, y;    /* grid coordinate */
  272. int  color;   /* pixel color */
  273. {
  274.       int xstrt, ystrt;
  275.  
  276.       xstrt = gridp->gr_xoff + 1 + ( gridp->gr_pxx + 1 ) * x;
  277.       ystrt = gridp->gr_yoff + 1 + ( gridp->gr_pxy + 1 ) * y;
  278.       ug_boxf( gridp->gr_key, color, 0, xstrt, ystrt,
  279.          xstrt + gridp->gr_pxx - 1, ystrt + gridp->gr_pxy - 1 );
  280. }
  281.  
  282. /* NOTE: following routine will not cope with
  283.    general format ".nna" files. In particular, it
  284.    is assumed that any new line which does not
  285.    start with a continuation character ('&') or
  286.    a comment character ('!') is the start of a
  287.    non-empty record; it is also assumed that there
  288.    are no comments in the middle of records */
  289.  
  290. FillPosArr( )
  291. {
  292.   int wx;   /* work index */
  293.   int ch;   /* Character */
  294.   long  fpos;   /* File position */
  295.   int ftt;    /* First time through */
  296.  
  297.   fseek( fp, 0l, 0 );     /* beginning of file */
  298.   for ( wx = 0; wx < MAX_N_PATTERNS; ) {
  299.     if ( (ch=fgetc( fp )) != '!' && ch != '&' ) {
  300.       ungetc( ch, fp );
  301.       fpos = ftell( fp );
  302.       ftt = 1;
  303.     } else ftt = 0;
  304.     /* Make sure there is something on the line;
  305.        scan to end of line */
  306.     while ( (ch=fgetc( fp)) != NEW_LINE ) {
  307.       if ( ftt && ch >= (int)'0' ) {
  308.   filepos[wx++] = fpos;
  309.   ftt = 0;
  310.       }
  311.       if ( ch == EOF ) return( wx );
  312.     }
  313.   }
  314. }
  315.  
  316. ReadFData( flag )
  317. int flag;   /* Whether or not desired output should be read */
  318. {
  319.   int wx, wy;   /* work index */
  320.   int ch;
  321.   float *dp;    /* data pointer */
  322.  
  323.   if ( curr_pattern < 0 ) curr_pattern = 0;
  324.  
  325.   if ( curr_pattern >= n_patterns ) { /* Don't read from file */
  326.     for ( wx = 0; wx < tot_n_inputs; wx++ )
  327.       fdata[wx] = -1.0;
  328.     curr_pattern = n_patterns;
  329.   } else {
  330.     fseek( fp, filepos[curr_pattern], 0 );
  331.     /* NOTE: Following code assumes data has been written using
  332.        format of WriteFData( ) routine */
  333.     for (wx = 0, wy = 18, dp = fdata; wx < tot_n_inputs;
  334.    wx++, wy--, dp++ ) {
  335.       if ( wy == 0 ) {  /* Look for continuation */
  336.   while ( (ch=fgetc( fp)) != NEW_LINE )
  337.     if ( ch == EOF ) goto bad_return;
  338.   if ( fgetc( fp ) != '&' ) goto bad_return;
  339.   wy = 18;
  340.       }
  341.       if ( fscanf( fp, "%f", dp ) <= 0 ) goto bad_return;
  342.     }
  343.   }
  344.   return( 0 );
  345.  
  346. bad_return:
  347.   PutStr( "Bad file format\n" );
  348.   return( -1 );
  349. }
  350.  
  351. WriteFData( flag )
  352. int flag;   /* Whether or not desired output should be written */
  353. {
  354.   int wx, wy;   /* work indices */
  355.  
  356.   if ( curr_pattern < 0 ) curr_pattern = 0;
  357.  
  358.   if ( curr_pattern >= n_patterns ) {
  359.     curr_pattern = n_patterns;
  360.     n_patterns++;
  361.     fseek( fp, 0l, 2 );     /* Seek End of File */
  362.     filepos[curr_pattern] = ftell( fp );
  363.   }
  364.  
  365.   fseek( fp, filepos[curr_pattern], 0 );
  366.  
  367.   fputc( ' ', fp );
  368.   for ( wx = 0, wy = 18; wx < tot_n_inputs; wx++, wy-- ) {
  369.     if ( wy == 0 ) {
  370.       fputc( NEW_LINE, fp );
  371.       fputc( '&' , fp );    /* Continuation line */
  372.       wy = 18;
  373.     }
  374.     fprintf( fp, " %2.0f.", fdata[wx] );
  375.   }
  376.   fputc( NEW_LINE, fp );
  377.   curr_pattern++;
  378. }
  379.  
  380. Shuffle( size )
  381. int size;
  382. {
  383.   int wx, wy, wz; 
  384.   int swap;
  385.  
  386.   for ( wx = 0, wy = size; wx < size; wx++, wy-- ) {
  387.     wz = rand( ) % wy;
  388.     swap = shuffle_array[wx];
  389.     shuffle_array[wx] = shuffle_array[wx + wz];
  390.     shuffle_array[wx + wz] = swap;
  391.   }
  392. }
  393.  
  394. /************************************************************************
  395.  *
  396.  *  UsrIO - user I/O routine to handle requests from NWORKS
  397.  *
  398.  ************************************************************************
  399.  */
  400. static int InitFlag = 0;    /* initialize things flag */
  401. int UsrIO()     /* handle NWORKS requests */
  402. {
  403.     int   key, xp, yp, button;      /* mouse interface */
  404.     int   newfile;
  405.     int   wx;       /* work index */
  406.     GMENU_ITEM  *gmip;
  407.     char  sbuf[60], *sp;      /* work string and pointer */
  408.     if ( InitFlag == 0 ) {
  409.     
  410.     AIOMenu.item = PMenuList;
  411.     LrnMenu.item = PMenuList;
  412.     PMenuList[0].text = "Scroll";
  413.     PMenuList[1].text = "Rewind";
  414.     PMenuList[2].text = "Enter";
  415.     PMenuList[3].text = "Quit";
  416.     PMenuList[4].text = "Auto";
  417.  
  418.  
  419.     if ((filepos=(long *)malloc(MAX_N_PATTERNS*sizeof(long)))
  420.           == (long *)0 ||
  421.       (shuffle_array=(int *)malloc(MAX_N_PATTERNS*sizeof(int)))
  422.           == (int *)0 ||
  423.       (fdata=(float *)malloc(2*N_ACR_MAX*N_DWN_MAX*sizeof(float)))
  424.           == (float *)0 ) {
  425.       PutStr("Not Enough memory to run pattern\n");
  426.       IORTNCDE = -1;
  427.       return;
  428.     }
  429.     
  430.   /* Get screen parameters */
  431.   ug_gparms( &xsize, &ysize, &ncolor, &chrx, &chry);
  432.   
  433.   if ( ncolor < 8 ) {
  434.     gm_intcolor = 1;
  435.     gm_txtcolor = 0;
  436.     gm_outcolor = 0;
  437.   } else {
  438.     gm_intcolor = 7;
  439.     gm_txtcolor = 4;
  440.     gm_outcolor = 0;
  441.   }
  442.     
  443.   for ( wx=0; wx<2; wx++ ) {
  444.     ip_grid[wx].gr_color[GR_OFF]  = gm_intcolor;
  445.     ip_grid[wx].gr_color[GR_ON]   = gm_txtcolor;
  446.     ip_grid[wx].gr_color[GR_LINE] = gm_txtcolor;
  447.   }
  448.  
  449.   InitGMenu( &AIOMenu, chrx, chry );
  450.   AIOMenu.x0 = 0; /* Menu position relative to window */
  451.   AIOMenu.y0 = 0;
  452.  
  453.   InitGMenu( &LrnMenu, chrx, chry );
  454.   LrnMenu.x0 = 0; /* Menu position relative to window */
  455.   LrnMenu.y0 = 0;
  456.  
  457.   ug_rdnetinf( &nlayp, &ninp, &noutp, <ype, &csp, &netnp );
  458.   strncpy( filenroot, netnp, 8 ); /* Read network name */
  459.   filenroot[8] = '\0';
  460.   strncpy( sbuf, csp, 3 ); /* Get first 3 letters of control strategy */
  461.   sbuf[3] = '\0';
  462.   strlwr( sbuf );   /* convert to lower case */
  463.   /* See if BAM control strategy - this will require 2 input grids */
  464.   if ( strcmp( sbuf, "bam" ) == 0 ) BamFlag = 1;
  465.   else BamFlag = 0;
  466.   InitFlag = 1;
  467.     }
  468.  
  469.     IORTNCDE = 0;       /* good return for data */
  470.     switch( IOREQCDE ) {
  471.  
  472.     case RQ_ATTENTION:
  473.       if ( fp != (FILE *) 0 ) fclose( fp );
  474.  
  475.       sprintf( sbuf,
  476.          "Enter name of pattern file ( default: %s ):", filenroot );
  477.       PutStr( sbuf );
  478.       sp = GetStr( );
  479.       PutStr("\n");
  480.       if ( strcmp( sp, "" ) != 0 ) strncpy( filenroot, sp, 9 );
  481.       strcpy( filename, filenroot );
  482.       strcat( filename, ".nna" );
  483.       if ( (fp = fopen( filename, "r" )) == (FILE *)0 )
  484.   {
  485.     newfile = 1;
  486.   }
  487.       else
  488.   {
  489.     newfile = 0;
  490.     fclose(fp);
  491.   }
  492.       
  493.       if ( ( fp = fopen( filename, "a+" ) ) == (FILE *)0 ) {
  494.   sprintf( sbuf, "Cannot open file <%s>\n", filename );
  495.   PutStr( sbuf );
  496.   goto aio_return;
  497.       }
  498.  
  499.       fseek( fp, 0l, 0 );
  500.       if ( newfile ) {      /* New file */
  501.   for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ ) {
  502.     strcpy( sbuf, "Enter number of pixels across" );
  503.     if ( wx == 1 ) strcat( sbuf, " for second grid:" );
  504.     else strcat( sbuf, ":" );
  505.     PutStr( sbuf );
  506.     sp = GetStr( );
  507.     PutStr("\n");
  508.     sscanf( sp, "%ld", &n_across[wx] );
  509.     if ( n_across[wx] < N_ACR_MIN ) n_across[wx] = N_ACR_MIN;
  510.     if ( n_across[wx] > N_ACR_MAX ) n_across[wx] = N_ACR_MAX;
  511.     PutStr( "Enter number of pixels down:" );
  512.     sp = GetStr( );
  513.     PutStr("\n");
  514.     sscanf( sp, "%ld", &n_down[wx] );
  515.     if ( n_down[wx] < N_DWN_MIN ) n_down[wx] = N_DWN_MIN;
  516.     if ( n_down[wx] > N_DWN_MAX ) n_down[wx] = N_DWN_MAX;
  517.     fprintf( fp,
  518.       "! number of pixels across = %2ld, number of pixels down = %2ld%s",
  519.       n_across[wx], n_down[wx], NEW_LINE_STR );
  520.   }
  521.       } else {
  522.   for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ ) {
  523.     if ( fgets( fbuf, MAX_STR, fp ) == 0 ) goto aio_read_error;
  524.     sscanf( fbuf,
  525.       "! number of pixels across = %ld, number of pixels down = %ld",
  526.       &n_across[wx], &n_down[wx] );
  527.     if ( n_across[wx] < N_ACR_MIN || n_across[wx] > N_ACR_MAX ||
  528.          n_down[wx]   < N_DWN_MIN || n_down[wx]   > N_DWN_MAX ) {
  529.       PutStr( "Unexpected values in file\n" );
  530.       goto aio_return;
  531.           }
  532.   }
  533.       }
  534.       tot_n_inputs = 0;
  535.       for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ ) {
  536.   n_inputs[wx] = n_across[wx] * n_down[wx];
  537.   tot_n_inputs += n_inputs[wx];
  538.       }
  539.      
  540.       SetUpGrids( );  /* Set up grid parameters and grid window positions */
  541.  
  542.       /* Set up menu and menu window */
  543.       AIOMenu.key = 3;
  544.       wxstrt[2] = ( xsize - AIOMenu.x1 ) / 2;
  545.       wxend[2] = wxstrt[2] + AIOMenu.x1 + 2;
  546.       wystrt[2] = ( wystrt[0] - AIOMenu.y1 ) / 2;
  547.       wyend[2] = wystrt[2] + AIOMenu.y1 + 2;
  548.       ug_window( AIOMenu.key, ip_grid[wx].gr_color[GR_OFF],
  549.            wxstrt[2], wystrt[2], wxend[2], wyend[2] );
  550.  
  551.       n_patterns = FillPosArr(  );  /* Fill Position array */
  552.       curr_pattern = n_patterns;  /* End of file */
  553.       ReadFData( 0 );     /* Special eof case */
  554.  
  555.       /* Display everything */
  556.       for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ ) {
  557.   DispGrid( &ip_grid[wx] );
  558.         DispPattern( &ip_grid[wx] );
  559.       }
  560.       for (gmip = AIOMenu.item, wx = 0; wx < AIOMenu.num_items;
  561.           gmip++, wx++ )
  562.   gmip->flag &= ~GM_LOCKED;
  563.       DispGMenu( &AIOMenu );
  564.  
  565.       for ( ; ; ) {
  566.         while ( ( gmip=LookGMenu(&AIOMenu,&button) ) != (GMENU_ITEM *) 0 ) {
  567.     switch( gmip->code ) {
  568.       case CR_SCROLL:
  569.         if ( button == MBUT_LEFT )  {   /* Scroll down */
  570.     curr_pattern++;
  571.     if ( ReadFData( 0 ) < 0 ) goto aio_return;
  572.     for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ )
  573.             DispPattern( &ip_grid[wx] );
  574.         }
  575.         if ( button == MBUT_RIGHT ) {   /* Scroll up */
  576.     curr_pattern--;
  577.     if ( ReadFData( 0 ) < 0 ) goto aio_return;
  578.     for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ )
  579.             DispPattern( &ip_grid[wx] );
  580.         }
  581.         break;
  582.       case CR_SEEK:
  583.         if ( button == MBUT_LEFT ) {  /* Start of file data */
  584.     curr_pattern = 0;
  585.     if ( ReadFData( 0 ) < 0 ) goto aio_return;
  586.     for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ )
  587.       DispPattern( &ip_grid[wx] );
  588.         }
  589.         if ( button == MBUT_RIGHT ) {   /* End of file data */
  590.     curr_pattern = n_patterns;
  591.     if ( ReadFData( 0 ) < 0 ) goto aio_return;
  592.     for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ )
  593.       DispPattern( &ip_grid[wx] );
  594.         }
  595.               break;
  596.       case CR_ENTER:
  597.         if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
  598.           WriteFData( 0 );
  599.     ReadFData( 0 );
  600.     for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ )
  601.       DispPattern( &ip_grid[wx] );
  602.         }
  603.         break;
  604.       case CR_QUIT:
  605.         if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
  606.           IORTNCDE = -1;
  607.     goto aio_return;
  608.         }
  609.         break;
  610.     }
  611.     /* If button was pressed, wait for it to be released */
  612.     while ( button != 0 )
  613.       ug_mouse( &key, &xp, &yp, &button );
  614.         }
  615.   for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ )
  616.     Sketch( &ip_grid[wx] );
  617.       }
  618.  
  619. aio_read_error:
  620.       sprintf( sbuf, "Error reading file <%s>\n", filename );
  621.       PutStr( sbuf );
  622.  
  623. aio_return:
  624.       if ( fp != (FILE *)0 ) fclose( fp );
  625.       fp = (FILE *)0;
  626.       for ( wx = 1; wx < 4; wx++ ) ug_windel( wx );
  627.       IORTNCDE = 1;
  628.       break;
  629.  
  630.       
  631.     case RQ_REWIND:       /* rewind the input file */
  632.  
  633.       if ( fp != (FILE *)0 ) fseek( fp, 0l, 0 );
  634.       break;
  635.  
  636.       
  637.     case RQ_LSTART:       /* starting learn */
  638.     case RQ_RSTART:       /* starting recall */
  639.      abortio = 0;       /* abort flag */
  640.       autoip = 0;       /* Assume hand input */
  641.       if ( fp == (FILE *)0 ) {      /* Not yet opened */
  642.         sprintf( sbuf,
  643.            "Enter name of pattern file ( default: %s ):", filenroot );
  644.         PutStr( sbuf );
  645.   sp = GetStr( );
  646.   PutStr("\n");
  647.   if ( strcmp( sp, "" ) != 0 ) strncpy( filenroot, sp, 9 );
  648.   strcpy( filename, filenroot );
  649.   strcat( filename, ".nna" );
  650.         if ( ( fp = fopen( filename, "r" ) ) == (FILE *)0 ) {
  651.     sprintf( sbuf, "Cannot open file <%s>; learn aborted\n", filename );
  652.     PutStr( sbuf );
  653.     abortio = 1;
  654.     goto lrns_return;
  655.         }
  656.   shufflex = 0;
  657.   /* Initialize shuffle array */
  658.         for ( wx = 0; wx < MAX_N_PATTERNS; wx++ )
  659.           shuffle_array[ wx ] = wx;
  660.  
  661.   n_patterns = FillPosArr(  );    /* Fill Position array */
  662.   curr_pattern = 0;     /* Start of of file */
  663.       }
  664.  
  665.       fseek( fp, 0l, 0 );     /* rewind */
  666.  
  667.       for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ ) {
  668.   if ( fgets( fbuf, MAX_STR, fp ) == 0 ) {
  669.           PutStr( "Read error; learn aborted\n" );
  670.           abortio = 1;
  671.           goto lrns_return;
  672.         }
  673.   sscanf( fbuf,
  674.           "! number of pixels across = %ld, number of pixels down = %ld",
  675.           &n_across[wx], &n_down[wx] );
  676.   if ( n_across[wx] < N_ACR_MIN || n_across[wx] > N_ACR_MAX ||
  677.        n_down[wx]   < N_DWN_MIN || n_down[wx]   > N_DWN_MAX ) {
  678.     PutStr( "Unexpected values in file; learn aborted\n" );
  679.     abortio = 1;      /* Abort */
  680.     goto lrns_return;
  681.         }
  682.       }
  683.       tot_n_inputs = 0;
  684.       for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ ) {
  685.   n_inputs[wx] = n_across[wx] * n_down[wx];
  686.   tot_n_inputs += n_inputs[wx];
  687.       }
  688.       if ( tot_n_inputs != ninp ) {
  689.   PutStr( "Data incompatible with network; learn aborted\n" );
  690.         abortio = 1;
  691.         goto lrns_return;
  692.       }
  693.       
  694.       SetUpGrids( );  /* Set up grid parameters and grid window positions */
  695.  
  696.       /* Set up menu and menu window */
  697.       LrnMenu.key = 3;
  698.       wxstrt[2] = ( xsize - LrnMenu.x1 ) / 2;
  699.       wxend[2] = wxstrt[2] + LrnMenu.x1 + 2;
  700.       wystrt[2] = ( wystrt[0] - LrnMenu.y1 ) / 2;
  701.       wyend[2] = wystrt[2] + LrnMenu.y1 + 2;
  702.       ug_window( LrnMenu.key, gm_intcolor,
  703.            wxstrt[2], wystrt[2], wxend[2], wyend[2] );
  704.  
  705. lrns_return:
  706.       if ( abortio ) {
  707.   if ( fp != (FILE *)0 ) fclose( fp );
  708.   fp = (FILE *)0;
  709.   for ( wx = 1; wx < 4; wx++ ) ug_windel( wx );
  710.       }
  711.       break;
  712.       
  713.     case RQ_LEARNIN:        /* read training input */
  714.     case RQ_READ:       /* read test data */
  715.  
  716.       if ( abortio ) goto lrnin_return;
  717.       
  718.       if ( IOLAYER != 0 ) {
  719.   PutStr( "This UserIO program expects data only at the input buffer.\n" );
  720.   PutStr( "You may be using an out-dated control strategy\n" );
  721.   abortio = 1;
  722.   goto lrnin_return;
  723.       }
  724.  
  725.       if ( !autoip ) {
  726.   if ( ReadFData( 0 ) < 0 ) goto lrnin_return;
  727.   for (gmip = LrnMenu.item, wx = 0; wx < LrnMenu.num_items;
  728.           gmip++, wx++ )
  729.     gmip->flag &= ~GM_LOCKED;
  730.   ug_winclr( LrnMenu.key );
  731.   DispGMenu( &LrnMenu );
  732.   for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ ) {
  733.     DispGrid( &ip_grid[wx] );
  734.     DispPattern( &ip_grid[wx] );
  735.   }
  736.         for ( ; ; ) {
  737.           while ( (gmip=LookGMenu(&LrnMenu,&button)) != (GMENU_ITEM *) 0 ) {
  738.       switch( gmip->code ) {
  739.           case CR_SCROLL:
  740.             if ( button == MBUT_LEFT )  { /* Scroll down */
  741.         curr_pattern++;
  742.         if ( ReadFData( 0 ) < 0 ) goto lrnin_return;
  743.         for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ )
  744.           DispPattern( &ip_grid[wx] );
  745.             }
  746.             if ( button == MBUT_RIGHT ) {   /* Scroll up */
  747.         curr_pattern--;
  748.         if ( ReadFData( 0 ) < 0 ) goto lrnin_return;
  749.         for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ )
  750.           DispPattern( &ip_grid[wx] );
  751.       }
  752.       break;
  753.           case CR_SEEK:
  754.             if ( button == MBUT_LEFT ) {  /* Start of file data */
  755.         curr_pattern = 0;
  756.         if ( ReadFData( 0 ) < 0 ) goto lrnin_return;
  757.         for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ )
  758.           DispPattern( &ip_grid[wx] );
  759.             }
  760.             if ( button == MBUT_RIGHT ) {   /* End of file data */
  761.         curr_pattern = n_patterns;
  762.         if ( ReadFData( 0 ) < 0 ) goto lrnin_return;
  763.         for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ )
  764.           DispPattern( &ip_grid[wx] );
  765.             }
  766.                   break;
  767.     case CR_ENTER:
  768.             if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
  769.         memcpy( IODATA, fdata, IOCOUNT*sizeof( float ) );
  770.         curr_pattern++;
  771.         goto lrnin_return;
  772.             }
  773.             break;
  774.           case CR_QUIT:
  775.             if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
  776.               IORTNCDE = -1;
  777.         goto lrnin_return;
  778.             }
  779.             break;
  780.           case CR_AUTO:
  781.             if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
  782.         autoip = 1;
  783.         goto auto_lrn;
  784.       }
  785.       break;
  786.       }
  787.       /* If button was pressed, wait for it to be released */
  788.       while ( button != 0 )
  789.         ug_mouse( &key, &xp, &yp, &button );
  790.     }
  791.     for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ )
  792.       Sketch( &ip_grid[wx] );
  793.         }
  794.       } else {      /* Auto input */
  795. auto_lrn:
  796.   for (gmip = LrnMenu.item, wx = 0; wx < LrnMenu.num_items;
  797.           gmip++, wx++ )
  798.     gmip->flag &= ~GM_LOCKED;
  799.   ug_winclr( LrnMenu.key );
  800.   DispGMenu( &LrnMenu );
  801.   for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ )
  802.     DispGrid( &ip_grid[wx] );
  803.         if ( shufflex == 0 ) Shuffle( n_patterns );
  804.   curr_pattern = shuffle_array[ shufflex++ ];
  805.   if ( ReadFData( 0 ) < 0 ) {
  806.     PutStr( "Error reading file; learn aborted\n" );
  807.     abortio = 1;
  808.     goto lrnin_return;
  809.   }
  810.   if ( shufflex >= n_patterns ) shufflex = 0;
  811.   memcpy( IODATA, fdata, IOCOUNT * sizeof( float ) );
  812.   for ( wx = 0; wx < ( BamFlag ? 2 : 1 ); wx++ )
  813.     DispPattern( &ip_grid[wx] );
  814.       }
  815.  
  816. lrnin_return:
  817.       if ( abortio ) {
  818.   if ( fp != (FILE *)0 ) fclose( fp );
  819.   fp = (FILE *)0;
  820.   for ( wx = 1; wx < 4; wx++ ) ug_windel( wx );
  821.   IORTNCDE = -1;
  822.       }
  823.       break;
  824.  
  825.     case RQ_WRSTEP:       /* write interim results */
  826.       if ( abortio ) goto wrstep_return;
  827.  
  828.       if ( BamFlag ) wx = ( IOLAYER == 1 ? 0 : 1 );
  829.       else wx = 0;
  830.       DispGrid( &ip_grid[wx] );
  831.       memcpy( ip_grid[wx].gr_data, IODATA, n_inputs[wx]*sizeof( float ) );
  832.       DispPattern( &ip_grid[wx] );
  833.  
  834.       if ( !autoip ) {
  835.   for ( ; ; ) {
  836.     PMenuList[CR_SCROLL].flag |= GM_LOCKED;
  837.     PMenuList[CR_SEEK].flag |= GM_LOCKED;
  838.     PMenuList[CR_AUTO].flag |= GM_LOCKED;
  839.     ug_winclr( LrnMenu.key );
  840.     DispGMenu( &LrnMenu );
  841.  
  842.     PutStr(
  843.       "ENTER for next iteration, QUIT to end current recall \n");
  844.     for ( ; ; ) {
  845.       while ( (gmip=LookGMenu(&LrnMenu,&button))==(GMENU_ITEM *) 0) ;
  846.       switch( gmip->code ) {
  847.         case CR_ENTER:
  848.             if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
  849.       goto wrstep_return;
  850.     }
  851.     break;
  852.         case CR_QUIT:
  853.             if ( button == MBUT_LEFT || button == MBUT_RIGHT ) {
  854.       IORTNCDE = -1;
  855.       goto wrstep_return;
  856.     }
  857.     break;
  858.         }
  859.       }
  860.     }
  861.   }
  862. wrstep_return:
  863.       if ( abortio ) {
  864.   if ( fp != (FILE *)0 ) fclose( fp );
  865.   fp = (FILE *)0;
  866.   for ( wx = 1; wx < 4; wx++ ) ug_windel( wx );
  867.   IORTNCDE = -1;
  868.       }
  869.       break;
  870.  
  871.  
  872.     case RQ_LEARNOUT:       /* read desired output */
  873.   /* IODATA points to an empty array of IOCOUNT values.  The
  874.      elements of the array will become the desired outputs for
  875.      training purposes.  These desired outputs correspond to
  876.      the most recent "RQ_LEARNIN" request.
  877.   */
  878.     case RQ_RCLTST:   /* read desired output during recall test */
  879.   /* IODATA points to an empty array of IOCOUNT values.  The
  880.      elements of the array will become the desired outputs for
  881.      recall purposes.  This request is only made during a
  882.      Execute Network/Recall Test.
  883.   */
  884.   break;
  885.  
  886.  
  887.     case RQ_LEARNRSLT:
  888.     case RQ_WRITE:        /* write out results */
  889.   if ( abortio ) goto write_return;
  890.   if ( !autoip ) {
  891.     PutStr(
  892.     "Cycle complete; press left or right mouse button to continue.\n");
  893.     for ( ; ; ) {
  894.       ug_mouse( &key, &xp, &yp, &button ); /* wait for mouse */
  895.       if (button == MBUT_LEFT || button == MBUT_RIGHT) break;
  896.     }
  897.   }
  898.  
  899. write_return:
  900.       if ( abortio ) {
  901.   if ( fp != (FILE *)0 ) fclose( fp );
  902.   fp = (FILE *)0;
  903.   for ( wx = 1; wx < 4; wx++ ) ug_windel( wx );
  904.   IORTNCDE = -1;
  905.       }
  906.   break;
  907.  
  908.     case RQ_TERM:       /* terminate interface */
  909.   /* close any files which may be open.  Deallocate any memory
  910.      which was allocated.  (This is VERY VERY important.  If
  911.      allocated memory is NOT released, dos memory will become
  912.      fragmented and it will become necessary to reboot.
  913.   */
  914. #if 1 
  915.   free((void *)filepos);
  916.   free((void *)shuffle_array);
  917.   free((void *)fdata);
  918. #else
  919.   free(filepos);
  920.   free(shuffle_array);
  921.   free(fdata);
  922. #endif
  923.   if ( fp != (FILE *)0 ) fclose( fp );
  924.   fp = (FILE *)0;
  925.   break;
  926.     }
  927.  
  928.     return;
  929. }
  930.