Follow us on Facebook
Follow us on Twitter
Signalogic on LinkedIn

Source Code Home

 
 
#define _MAIN_CPP_
#include "xprog.h"
#undef _MAIN_CPP_
#include "globals.h"
#include "dlibt50.h"
#include "Hwlibx.h"
#include "../EngMgr/EngMgr.h"
#include 


int RunXDScope(int UserNumChan, int UserTraceNum, int UserTraces[4], int IOBaseAddress, int SampFreq)
{

   bool Done = false;
   int  MainWidth, MainHeight;
   int  MainPositionX, MainPositionY;
   bool UserDataValid;

   MainWidth = 800;  // it's in pixels... right?   dunno mang...
   MainHeight = 600;
   MainPositionX = 50;
   MainPositionY = 50;

   // create Main window
   if (!CreateAndInitializeWindow(SigDScopeWindow, MainWidth, MainHeight, MainPositionX, MainPositionY, false))  {   
      printf("ERROR In Creating And Initializing Main Window \n");
      return -1;
   }
   
   OnRun(SigDScopeWindow, UserNumChan, UserTraceNum, UserTraces, IOBaseAddress, SampFreq);
   
   printf("Initial run completed.\n");
   
   printf("Done.\n");


   if (!DeinitAndDestroyWindow(SigDScopeWindow)) {
      printf("ERROR In Deinitializing And Destroying Window \n");
      return -1;
   }

    return 0;
}

void DummyFunc(void) {

}

bool WindowMessageHandler(WinParams &wp)
{
   int TestTraces[4] = {0, 0, 0, 0};

   XEvent e;
   XNextEvent(wp.disp, &e);
   
   switch (e.type)   {
      
      case Expose:
         {
            bool bPaint = PaintScreen(wp, e, 0, 0, TestTraces);
            XFlush(wp.disp);
            return bPaint;
         }
         
      case KeyPress:  // with the current code path in main(), any keypress will close the window gracefully
         return HandleKeys(e);
         
      default:
         return false;
   }

   return false;
}


bool CreateAndInitializeWindow(WinParams &wp, int Width, int Height, int PosX, int PosY, bool bStatusWindow)
{

   int screenNum;
   long EventMask;

   /* Create display on X server */
   wp.disp = XOpenDisplay(0);

   /* Make Sure The Display has been opened */
   if (!wp.disp)
      return false;

   /* check the number of the default screen for our X server. */
   screenNum = DefaultScreen(wp.disp);
  
   /* set display size and position attributes */
   wp.screenWidth = Width;
   wp.screenHeight = Height;
   wp.screenTopX = PosX;  // anyone have any idea why these attributes don't affect the position of the window? -DE
   wp.screenTopY = PosY;
   
   //Make sure window isn't bigger than screen
   if (DisplayWidth(wp.disp, screenNum) < wp.screenWidth) {
     //Uh-oh...it is.  Resize window width to 100 pixels smaller than the main display
     wp.screenWidth = DisplayWidth(wp.disp, screenNum) - 100;
   }
   
   if (DisplayHeight(wp.disp, screenNum) < wp.screenHeight) {
     //Uh-oh...it is.  Resize window height to 100 pixels smaller than the main display
     wp.screenHeight = DisplayHeight(wp.disp, screenNum) - 100;
   }
   
   /* Get the root window ID */
   wp.rootWindowID = RootWindow(wp.disp, screenNum);

   /* Get some colors */
   wp.blackColor = BlackPixel(wp.disp, DefaultScreen(wp.disp));
   wp.whiteColor = WhitePixel(wp.disp, DefaultScreen(wp.disp));

   /* Create the window */
   wp.rootWindow = XCreateSimpleWindow(wp.disp, RootWindow(wp.disp, screenNum), 
      wp.screenTopX, /* Top X */
      wp.screenTopY,    /* Top Y */
      wp.screenWidth, /* Width */
      wp.screenHeight,   /* Height */
      wp.borderWidth, /* Border Width */
      wp.blackColor, wp.whiteColor);
      
   if (wp.rootWindow == BadAlloc)  {
      printf("Bad Alloc\n");
      return false;
   }
   else if (wp.rootWindow == BadMatch)   {
      printf("Bad Match\n");
      return false;
   }
   else if (wp.rootWindow == BadPixmap)  {
      printf("Bad PixMap\n");
      return false;
   }
   else if (wp.rootWindow == BadValue)  {
      printf("Bad Value\n");
      return false;
   }
   else if (wp.rootWindow == BadWindow)   {
      printf("Bad Window\n");
      return false;
   }
   else 
      printf("No Errors Creating Simple Window\n");


   /* Set up event handling */
   EventMask = ExposureMask |      /* Paint */
               KeyPressMask |      /* Keyboard key hit */
               ButtonPressMask |   /* Mouse button hit */
               ButtonReleaseMask;  /* Mouse button released */
	  
   XSelectInput(wp.disp, wp.rootWindow, EventMask);
	  
   XMapWindow(wp.disp, wp.rootWindow);

   /* Flushes the pending requests to X and waits for finished */
   XSync(wp.disp, 0);

   /* Create graphics context */
   wp.gc = XCreateGC(wp.disp, wp.rootWindow, 0, 0);
   
   if ((int)wp.gc == BadAlloc)  {
      printf("Bad Alloc\n");
      return false;
   }
   else if ((int)wp.gc == BadDrawable)   {
      printf("Bad Drawable\n");
      return false;
   }
   else if ((int)wp.gc == BadMatch)   {
      printf("Bad Match\n");
      return false;
   }
   else if ((int)wp.gc == BadPixmap)  {
      printf("Bad PixMap\n");
      return false;
   }
   else if ((int)wp.gc == BadValue)  {
      printf("Bad Value\n");
      return false;
   }
   else
      printf("No Errors Creating GC\n");
   

   /* Set window attributes */
   XSetForeground(wp.disp, wp.gc, wp.whiteColor);
   XSetBackground(wp.disp, wp.gc, wp.blackColor);

   /* Set Fill Style to SOLID */
   XSetFillStyle(wp.disp, wp.gc, FillSolid);

   /* Set the Line Attributes */
   XSetLineAttributes(wp.disp, wp.gc, 2, LineSolid, CapRound, JoinRound);

   XStoreName(wp.disp, wp.rootWindow, "XDScope - Signalogic");
      
   return true;
}

bool DeinitAndDestroyWindow(WinParams &wp)
{
   
   /* Free the graphics context */
   if(wp.gc)
      XFreeGC(wp.disp, wp.gc);
   
   /* Close connection to X server */
   if(wp.disp)
      XCloseDisplay(wp.disp);

   return true;
}

void OnRun(WinParams &wp, int NumChannels, int NumTraces, int TraceNums[4], int IOBaseAddress, int SampFreq)
{

   XEvent     e;
   bool       bPaint;
   bool       bEvent;
   long       EventMask;



   DSPHWHandler.SetNumberOfChannels(NumChannels);
   
   DSPHWHandler.SetIOBaseAddr(IOBaseAddress);
   DSPHWHandler.SetSamplingRate(SampFreq);
   DSPHWHandler.StartHardware();
  
  
   DSPHWHandler.SetChannelList(0);
   printf("Hardware started\n");
   sleep(2);
   
   DSPHWHandler.RunHardware();
      
   
   while(1) 
   {
   
     
      DSPHWHandler.WaitForBuffer();
      memset(pDataBuffer, 500, sizeof(short)*MAX_BUFFER_SIZE);  /* clear out the local buffer */

      /* fill the buffer with the values from the current DSP time data buffer */
      
      DSPHWHandler.ReadBuffer( pDataBuffer, MAX_BUFFER_SIZE );  

      /* Formerly WindowMessageHandler */ 
      /* paint the window */
      EventMask = KeyPressMask;

      
      bEvent = XCheckWindowEvent(wp.disp, wp.rootWindow, EventMask, &e);  // if there's a keypress event in the queue, get it, otherwise keep going
      
      /* Clear the window */
      XClearWindow(wp.disp, wp.rootWindow);
      
      bPaint = PaintScreen(wp, e, NumChannels, NumTraces, TraceNums);
      
   
      if(bEvent)	   
         goto cleanup;
      /* end message handling -DE */

   }  /* end dscope refreshing */

cleanup:

   
	DSPHWHandler.StopHardware();
}