home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_11 / feeney / mainprog.c next >
C/C++ Source or Header  |  1993-05-21  |  10KB  |  341 lines

  1.           LISTING 2
  2.         Program Body
  3.  
  4. #include <windows.h>
  5. #include <math.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <float.h>
  9. #include <time.h>
  10. #include "MyStatus.h"
  11. #include "MyHeader.h"
  12.  
  13. /*****************************************************
  14.   Main message loop for processing input and window-
  15.   management messages received from WINDOWS.
  16. *****************************************************/
  17. long FAR PASCAL MainWndProc(HWND hWnd, unsigned msg,
  18.         WORD wParam, LONG lParam)
  19. {
  20.   switch (msg) {
  21.     case  WM_TIMER:      // Decipher Timer Function
  22.       switch( wParam ) {
  23.     case IDM_PERIOD: // Move onto next wavelength
  24.       SampleTimerExpired(hWnd,msg,wParam,lParam);
  25.       break;
  26.     case IDM_SAMPLE: // Read data at this wavelength
  27.       SampleTimerTicked(hWnd,msg,wParam,lParam);
  28.     case IDM_TDRIVE: // Read data at this time
  29.       SampleTimer(hWnd,msg,wParam,lParam);
  30.       break;
  31.     case  IDM_START_TIMER: // Start data acq cycle
  32.       StartScan(hWnd,msg,wParam,lParam);
  33.       break;
  34.     case  IDM_STOP_TIMER: // End data acq cycle
  35.       StopScan(hWnd,msg,wParam,lParam);
  36.       break;
  37.       }
  38.       break;
  39.     case WM_SYSCOMMAND:
  40.       return(DefWindowProc(hWnd,msg,wParam,lParam));
  41.     case WM_SIZE:// Window Resized, resize all child.
  42.       return(WndSizeChanged(hWnd,msg,wParam,lParam));
  43.     case WM_COMMAND:  // Decipher User Action
  44.       switch (wParam) {
  45.     case IDM_ACQ_ON: // Start data acq cycle
  46.       SendMsg(hWnd, WM_TIMER, IDM_START, 0L);
  47.       break;
  48.     case IDM_ACQ_OFF: // Stop data acq cycle
  49.       SendMsg(hWnd, WM_TIMER, IDM_STOP, 0L);
  50.       break;
  51.     default: // Some other menu function requested.
  52.       return(MyCommands(hWnd,msg,wParam,lParam));
  53.       }
  54.       break;                  
  55.     case WM_CREATE:  // Create program objects
  56.       CreateMyObjects(hWnd,msg,wParam,lParam);
  57.       break;
  58.     case WM_PAINT:   // Paint Main Window
  59.       PaintScreen(hWnd,msg,WParam,lParam);
  60.       break;
  61.     case WM_DESTROY: // Destroy main and all childern
  62.       DeleteMyObjects(hWnd,msg,WParam,lParam);
  63.       break;
  64.     default:     // Let WINDOWS handle the message.
  65.       return(DefWindowProc(hWnd,msg,wParam,lParam));
  66.   }
  67.   return (NULL);
  68. }
  69. /*
  70.   Sample time expired.
  71.   Measure the signals and display result.
  72.   Then move onto next wavelength!
  73. */
  74. BOOL SampleTimerExpired (HWND hWnd, unsigned msg, 
  75.      WORD wParam, LONG lParam)  
  76. {
  77.   KillTimer( hWnd, IDM_PERIOD ); 
  78.   KillTimer( hWnd, IDM_SAMPLE );
  79.   Calculate_Point();                                                                                                      // Calculate dB Ratio
  80.   Display_Point();                                                                                                                // Draw data on screen
  81.   if ( ++Dat.iPnt >= Dat.Points ) {                                                       // Scan Finished
  82.     SendMsg(hMainWnd, IDM_STOP_TIMER, 0, 0L);
  83.     return(FALSE);
  84.   }
  85.   Goto_Wavelength(X[Dat.iPnt]);                                                   // Next Wavelength
  86.   ResetPoint();
  87.   if ( !SetPeriodTimer ) {
  88.     MsgBox(hWnd, ErrMsg, TimMsg); 
  89.     return(FALSE); 
  90.   }  
  91.   if ( !SetSampleTimer ) { 
  92.     MsgBox(hWnd, ErrMsg, TimMsg); 
  93.     return(FALSE); 
  94.   }
  95.   return(TRUE);
  96. }
  97. /*
  98.   Timer ticked, time to measure signals.
  99. */
  100. BOOL SampleTimerTicked ( HWND hWnd, unsigned msg,
  101.      WORD wParam, LONG lParam)
  102. {
  103.   int i;
  104.  
  105.   KillTimer( hWnd, IDM_SAMPLE );
  106.   Acquire_Point();
  107.   if ( IsRetry ) { // Lock-in error detected.
  108.     KillTimer( hWnd, IDM_PERIOD );
  109.     KillTimer( hWnd, IDM_SAMPLE );
  110.     ResetRetry;   // Clear Error/Retry Bit.
  111.           // Only retry for so many times.
  112.     if (++Dat.iTRY <= _nDATATRIES ) {
  113.        if ( !SetPeriodTimer ) {
  114.      MsgBox(hWnd, ErrMsg, TimMsg);
  115.      return(FALSE);
  116.        }
  117.        if ( !SetSampleTimer ) {
  118.      MsgBox(hWnd, ErrMsg, TimMsg);
  119.      return(FALSE);
  120.        }
  121.     }
  122.     i = MsgBox(hWnd, BadMsg, TryMsg);
  123.     switch ( i ) {   // Tries Expired, Display Dialog
  124.       case IDABORT:  // ask for users advice.
  125.     SendMsg(hWnd, IDM_STOP_TIMER, 0, 0L);
  126.     ResetPoint();
  127.     break;
  128.       case IDIGNORE: // Kept 1 set of last values
  129.     SendMsg(hWnd, WM_TIMER, IDM_PERIOD, 0L);
  130.     break;
  131.       case IDRETRY:  // Reset TRY and repeat reads
  132.       default:
  133.     ResetPoint();
  134.     if ( !SetPeriodTimer ) {
  135.       MsgBox(hWnd, ErrMsg, TimMsg);
  136.     }
  137.     if ( !SetSampleTimer ) {
  138.       MsgBox(hWnd, ErrMsg, TimMsg);
  139.     }
  140.     break;
  141.     }
  142.   } else if ( !SetSampleTimer ) { 
  143.     MsgBox(hWnd, ErrMsg, TimMsg);
  144.   }
  145.   return(TRUE);
  146. }
  147. /*
  148.   Begin the scanning cycle.
  149. */
  150. BOOL StartScan( HWND hWnd, unsigned msg,
  151.      WORD wParam, LONG lParam) 
  152. {
  153.   HMENU hMenu;
  154.  
  155.   hMenu = GetMenu(hWnd);
  156.   if ( !yes_no("START SCAN") ) return(FALSE);
  157.   if ( ++Dat.iScan > nSETs ) Dat.iScan = 0;
  158.   Dat.iPnt = 0;
  159.   ResetPoint();
  160.   SetDataAcq;
  161.   SetData;
  162.   CheckMenuItem(hMenu, IDM_ACQ_OFF, MF_UNCHECKED);
  163.   CheckMenuItem(hMenu, IDM_ACQ_ON, MF_CHECKED);
  164.   if ( Dat.Mode == _Wavelength ) {
  165.     Goto_Wavelength(Dat.Start);
  166.     if  ( !SetSampleTimer ) {
  167.       MsgBox(hWnd, ErrMsg, TimMsg);
  168.       return(FALSE);
  169.     }
  170.     if ( !SetPeriodTimer ) {
  171.       MsgBox(hWnd, ErrMsg, TimMsg);
  172.       return(FALSE);
  173.     }
  174.   } else if ( Dat.Mode == _TimeDrive ) {
  175.     if ( !SetTimer(hWnd,IDM_TDRIVE,Dat.TDStep,0L)) {
  176.       MsgBox(hWnd, ErrMsg, TimMsg);
  177.       return(FALSE);
  178.     }
  179.   }
  180.   return(TRUE);
  181. }
  182. /*
  183.   Stop the scanning cycle.
  184. */
  185. BOOL StopScan( HWND hWnd, unsigned msg,
  186.      WORD wParam, LONG lParam) 
  187. {
  188.   int   flg;
  189.   HMENU hMenu;
  190.  
  191.   hMenu = GetMenuGetMenu(hWnd);
  192.   KillTimer(hMainWnd, IDM_PERIOD); // Kill all timers.
  193.   KillTimer(hMainWnd, IDM_SAMPLE); 
  194.   KillTimer(hMainWnd, IDM_TDRIVE);
  195.   ResetDataAcq; // Uncheck ACQ-in-Progress Menu
  196.   CheckMenuItem(hMenu, IDM_ACQ_ON, MF_UNCHECKED);
  197.   CheckMenuItem(hMenu, IDM_ACQ_OFF,MF_CHECKED);
  198.   if ( Dat.Mode == _WAVELENGTH_ ) {
  199.     Goto_Wavelength(Dat.Start);
  200.   }
  201.   Dat.pts[Dat.iScan] = Dat.iPnt;
  202.   SendMsg( hMainWnd, WM_COMMAND, IDM_QUERY, 0L );
  203.   do {
  204.     flg = yes_no("SAVE SCAN");
  205.     if ( flg ) {
  206.       SendMsg( hMainWnd,WM_COMMAND,IDM_PUT_DATA,0L);
  207.     }
  208.   } while ( flg && IsWriteFailure );
  209.   if ( yes_no("SCAN AGAIN")) {
  210.     SendMsg(hMainWnd, WM_TIMER, IDM_START, 0, 0L );
  211.   }
  212.   return(TRUE);
  213. }
  214. /*
  215.   Get a yes or no reply to a question.
  216. */
  217. BOOL yes_no( char *question )
  218. {
  219.   return( (MessageBox( hMainWnd, (LPSTR) question,
  220.     (PSTR) "Input Required", MB_YESNO |
  221.     MB_ICONQUESTION == IDYES) ? TRUE : FALSE );
  222. }
  223. /*
  224.   Time ticked, measure signals.
  225.   This function is only for the time-drive scan.
  226. */
  227. BOOL SampleTimer( HWND hWnd,unsigned msg,
  228.      WORD wParam,LONG lParam)
  229. {
  230.   KillTimer( hWnd, IDM_TDRIVE );
  231.   Acquire_Point();
  232.   Dat.X = X[Dat.iPnt];
  233.   Calculate_Point();  // Calculate dB Ratio
  234.   Display_Point();    // Draw data on screen
  235.   ResetPoint();
  236.   if ( ++Dat.iPnt >= Dat.Points )  // Scan Finished
  237.     SendMsg(hMainWnd, WM_TIMER, IDM_STOP, 0, 0L);
  238.   else if (!SetTimer(hWnd,IDM_TDRIVE,Dat.TDStep,0L))
  239.     MsgBox(hWnd, ErrMsg, TimMsg);
  240.   return(TRUE);
  241. }
  242. /*
  243.   Decipher the command the user choose from the menu.
  244. */
  245. long MyCommands(HWND hWnd,unsigned msg,
  246.      WORD wParam,LONG lParam)
  247. {
  248.   HMENU hMenu;
  249.  
  250.   hMenu = GetMenu(hWnd);
  251.   switch( wParam ) {
  252.     case IDM_DEBUG:
  253.       if ( IsDebug ) {
  254.     ResetDebug;
  255.     CheckMenuItem(hMenu,IDM_DEBUG,MF_UNCHECKED);
  256.       } else if (ShowBox(hWnd,Debugger,"DebugBox")) {
  257.     SetDebug;
  258.     CheckMenuItem(hMenu, IDM_DEBUG, MF_CHECKED);
  259.       }
  260.       break;
  261.     case IDM_ABOUT:    // Display About Box
  262.       ShowBox( hWnd, About, "AboutBox" );
  263.       break;
  264.     case IDM_C_CLR:    // Display Color Box
  265.       ShowBox( hWnd, DlgClrFunc, "ColorBox" );
  266.       break;
  267.     case  IDM_CONFIG : // Get GPIB addresses
  268.       ShowBox( hWnd, User_CONFIG, "CONFIG" );
  269.       break;
  270.     case  IDM_GOTO:    // Get wavelength to goto.
  271.       DlgIntCmmd = wParam;
  272.       ShowBox( hWnd, GoToFunc, "GoTo" );
  273.       if  ( IsAbort ) ResetAbort;                                                              // Clear Flag Bit
  274.       else Goto_Wavelength( Dat.Position );
  275.       break;
  276.     case IDM_EXIT:     // Terminate program
  277.       if ( MsgBox(hWnd, 2, 6) != IDOK ) {
  278.     break;         // Should we really exit?
  279.       }
  280.       DestroyWindow (hWnd);
  281.       break;
  282.     default:           // Let WINDOWS handle others.
  283.       return(DefWindowProc(hWnd, msg, wParam, lParam));
  284.   }
  285.   return(0L);
  286. }
  287. /*
  288.   Display a dialog box.
  289. */
  290. BOOL ShowBox(HWND hWnd,FARPROC pFunc,LPSTR lpTemplate)
  291. {
  292.   int     i;
  293.   FARPROC lpProc;
  294.  
  295.   lpProc = MakeProcInstance(pFunc, hInst);
  296.   if ( lpProc == NULL ) return(FALSE);
  297.   i = DialogBox(hInst, lpTemplate, hWnd, lpProc);
  298.   FreeProcInstance(lpProc);
  299.   if ( i == -1 ) return(FALSE);
  300.   return(TRUE);
  301. }
  302. /*
  303.   Display an error message.
  304.   Op indicates the type of message, 
  305.     e.g. Warning or Error
  306.     and dialog buttons displayed.
  307.   msg provides the specific text to display.
  308. */
  309. int MsgBox( HWND hWnd, WORD Op, LPSTR msg )
  310. {
  311.   int   msg, wType = MB_OK | MB_ICONHAND;
  312.   char  szText[24];
  313.  
  314.   switch ( Op ) {
  315.     case InfoMsg :
  316.       lstrcpy( szText, "INFO\0" );
  317.       wType = MB_OK | MB_ICONINFORMATION;
  318.       break;
  319.     case VerMsg:
  320.       lstrcpy( szText, "VERIFY\0" );
  321.       wType = MB_OKCANCEL | MB_ICONQUESTION;
  322.       break;
  323.     case ErrMsg:
  324.       lstrcpy( szText, "ERROR\0" );
  325.       wType = MB_ICONHAND | MB_OKCANCEL;
  326.       break;
  327.     case BadMsg:
  328.       lstrcpy( szText, "SERIOUS ERROR\0" );
  329.       wType = MB_ICONSTOP | MB_ABORTRETRYIGNORE;
  330.       break;
  331.     case WarnMsg:
  332.     default:
  333.       lstrcpy( szText, "WARNING\0" );
  334.       wType = MB_ICONHAND | MB_OKCANCEL;
  335.       break;
  336.   }
  337.   msg=MessageBox(hWnd,(LPSTR) str,(LPSTR) msg,wType);
  338.   return( msg );
  339. }
  340.  
  341.