examples/DS401_Slave_Gui/main.cpp
changeset 385 fff25f16c923
parent 382 860e858685d3
child 407 384c3cd7afa2
equal deleted inserted replaced
384:83793fc7ce48 385:fff25f16c923
    43 
    43 
    44 
    44 
    45 wxTextCtrl *textLog;
    45 wxTextCtrl *textLog;
    46 int node_id_ext;
    46 int node_id_ext;
    47 int to_start = 0;
    47 int to_start = 0;
       
    48 int openInput = 0;
       
    49 int openOutput = 0;
       
    50 
    48 MyFrame *frame;
    51 MyFrame *frame;
    49 #define MAXLENGTH_BUSNAME 32
    52 #define MAXLENGTH_BUSNAME 32
    50 #define MAXLENGTH_BAUDRATE 8
    53 #define MAXLENGTH_BAUDRATE 8
    51 char _busname[MAXLENGTH_BUSNAME] = "vcan0";
    54 char _busname[MAXLENGTH_BUSNAME] = "vcan0";
    52 char _baudrate[MAXLENGTH_BAUDRATE] = "500K";
    55 char _baudrate[MAXLENGTH_BAUDRATE] = "500K";
    53 s_BOARD SlaveBoard = { _busname, _baudrate };
    56 s_BOARD SlaveBoard = { _busname, _baudrate };
    54 
    57 
    55 #define MAXLENGTH_LIBPATH 1024
    58 #define MAXLENGTH_LIBPATH 1024
    56 char LibraryPath[MAXLENGTH_LIBPATH] = "libcanfestival_can_virtual.so";
    59 char LibraryPath[MAXLENGTH_LIBPATH] = "libcanfestival_can_virtual.so";
    57 double Gtime = 0;
       
    58 double y[28][45];
       
    59 double hdelta = 0;
       
    60 double old_max = 5;
       
    61 double old_min = -5;
       
    62 
    60 
    63 #if defined(WIN32)
    61 #if defined(WIN32)
    64 #define LIB_EXTENT wxT("*.dll")
    62 #define LIB_EXTENT wxT("*.dll")
    65 #else
    63 #else
    66 #define LIB_EXTENT wxT("*.so")
    64 #define LIB_EXTENT wxT("*.so")
   134 EVT_SPINCTRL (INST7, MyFrame::OnInst7)
   132 EVT_SPINCTRL (INST7, MyFrame::OnInst7)
   135 EVT_SPINCTRL (INST8, MyFrame::OnInst8)
   133 EVT_SPINCTRL (INST8, MyFrame::OnInst8)
   136 EVT_LISTBOX (FREQBOX, MyFrame::OnFreqBoxClick)
   134 EVT_LISTBOX (FREQBOX, MyFrame::OnFreqBoxClick)
   137 EVT_MENU (FILE_QUIT, MyFrame::OnQuit)
   135 EVT_MENU (FILE_QUIT, MyFrame::OnQuit)
   138 EVT_BUTTON (QUIT, MyFrame::OnQuit) EVT_MENU (HELP_ABOUT, MyFrame::OnAbout)
   136 EVT_BUTTON (QUIT, MyFrame::OnQuit) EVT_MENU (HELP_ABOUT, MyFrame::OnAbout)
   139   //EVT_PAINT(MyFrame::OnPaint)
   137 EVT_TIMER (TIMER_ID, MyFrame::OnTimer)
   140   EVT_TIMER (TIMER_ID, MyFrame::OnTimer)
       
   141 END_EVENT_TABLE ()IMPLEMENT_APP_NO_MAIN (MyApp);
   138 END_EVENT_TABLE ()IMPLEMENT_APP_NO_MAIN (MyApp);
   142 IMPLEMENT_WX_THEME_SUPPORT;
   139 IMPLEMENT_WX_THEME_SUPPORT;
   143 
   140 
   144      void
   141      void
   145      help ()
   142      help ()
   171 {
   168 {
   172 
   169 
   173   int c;
   170   int c;
   174   extern char *optarg;
   171   extern char *optarg;
   175   char *snodeid;
   172   char *snodeid;
   176   while ((c = getopt (argc, argv, "-b:B:l:i:s")) != EOF)
   173   while ((c = getopt (argc, argv, "-b:B:l:i:sIO")) != EOF)
   177     {
   174     {
   178       switch (c)
   175       switch (c)
   179 	{
   176 	{
   180 	case 'b':
   177 	case 'b':
   181 	  if (optarg[0] == 0)
   178 	  if (optarg[0] == 0)
   211 	  sscanf (snodeid, "%x", &node_id_ext);
   208 	  sscanf (snodeid, "%x", &node_id_ext);
   212 	  break;
   209 	  break;
   213 	case 's':
   210 	case 's':
   214 	  to_start = 1;
   211 	  to_start = 1;
   215 	  break;
   212 	  break;
       
   213 	case 'I':
       
   214 	  openInput = 1;
       
   215 	  break;
       
   216 	case 'O':
       
   217 	  openOutput = 1;
       
   218 	  break;
   216 	default:
   219 	default:
   217 	  help ();
   220 	  help ();
   218 	  exit (1);
   221 	  exit (1);
   219 	}
   222 	}
   220     }
   223     }
   223 
   226 
   224 
   227 
   225 bool
   228 bool
   226 MyApp::OnInit ()
   229 MyApp::OnInit ()
   227 {
   230 {
   228   frame = new MyFrame (_T ("I-O simulator"));
   231   frame = new MyFrame (wxString::Format(wxT("CanOpen virtual DS-401 - Id : %d"),node_id_ext));
   229   frame->Show (true);
   232   frame->Show (true);
       
   233   if(openInput) frame->book->SetSelection(2);
       
   234   else if(openOutput) frame->book->SetSelection(3);
   230   return true;
   235   return true;
   231 }
   236 }
   232 
   237 
   233 MyFrame::MyFrame (const wxString & title):wxFrame (NULL, wxID_ANY, title, wxDefaultPosition, wxSize (800, 740),
   238 MyFrame::MyFrame (const wxString & title):wxFrame (NULL, wxID_ANY, title, wxDefaultPosition, wxSize (800, 740),
   234 	 wxDEFAULT_FRAME_STYLE, wxT ("frame")), m_timer (this,
   239 	 wxDEFAULT_FRAME_STYLE, wxT ("frame")), m_timer (this,
   310 			      wxConvLocal));
   315 			      wxConvLocal));
   311   myhsizer->Add (busname, 0, wxLEFT, 5);
   316   myhsizer->Add (busname, 0, wxLEFT, 5);
   312   myhsizer =
   317   myhsizer =
   313     new wxStaticBoxSizer (new
   318     new wxStaticBoxSizer (new
   314 			  wxStaticBox (panel, wxID_ANY,
   319 			  wxStaticBox (panel, wxID_ANY,
   315 				       _T ("Node ID (Hexa)")), wxHORIZONTAL);
   320 				       _T ("Node ID (Decimal)")), wxHORIZONTAL);
   316   myentrysizer->Add (myhsizer, 0, wxEXPAND | wxALL, 5);
   321   myentrysizer->Add (myhsizer, 0, wxEXPAND | wxALL, 5);
   317   node_id =
   322   node_id =
   318     new wxSpinCtrl (panel, wxID_ANY, wxEmptyString, wxDefaultPosition,
   323     new wxSpinCtrl (panel, wxID_ANY, wxEmptyString, wxDefaultPosition,
   319 		    wxDefaultSize, wxSP_ARROW_KEYS, 1, 127, node_id_ext,
   324 		    wxDefaultSize, wxSP_ARROW_KEYS, 1, 127, node_id_ext,
   320 		    _T ("wxSpinCtrl"));
   325 		    _T ("wxSpinCtrl"));
   540     _T ("Bool Input 1"), _T ("Bool Input 2"),
   545     _T ("Bool Input 1"), _T ("Bool Input 2"),
   541     _T ("Bool Input 3"), _T ("Bool Input 4"),
   546     _T ("Bool Input 3"), _T ("Bool Input 4"),
   542     _T ("Bool Input 5"), _T ("Bool Input 6"),
   547     _T ("Bool Input 5"), _T ("Bool Input 6"),
   543     _T ("Bool Input 7"), _T ("Bool Input 8"),
   548     _T ("Bool Input 7"), _T ("Bool Input 8"),
   544   };
   549   };
   545   panel = new wxPanel (book);
       
   546   book->AddPage(panel, wxT("Graphic"), true); 
       
   547   mysizer = new wxBoxSizer (wxVERTICAL);
       
   548   panel->SetSizer (mysizer);
       
   549   //panel->Hide ();
       
   550   myhsizer =
       
   551     new wxStaticBoxSizer (new wxStaticBox (panel, wxID_ANY, _T ("Graphic")),
       
   552 			  wxVERTICAL);
       
   553   mysizer->Add (myhsizer, 0, wxEXPAND | wxALL, 10);
       
   554   mygraphpan =
       
   555     new wxPanel (panel, wxID_ANY, wxDefaultPosition, wxSize (0, 350),
       
   556 		 wxTAB_TRAVERSAL, wxT ("Graphic"));
       
   557   myhsizer->Add (mygraphpan, wxEXPAND | wxALL, wxEXPAND | wxALL, 15);
       
   558   echelle =
       
   559     new wxSlider (panel, wxID_ANY, 44, 44, 1000, wxDefaultPosition,
       
   560 		  wxDefaultSize, wxSL_HORIZONTAL, wxDefaultValidator,
       
   561 		  wxT ("slider"));
       
   562   myhsizer->Add (echelle, 0, wxEXPAND | wxALL, 0);
       
   563   mybsizer = new wxBoxSizer (wxHORIZONTAL);
       
   564   mysizer->Add (mybsizer, wxEXPAND | wxALL, wxEXPAND | wxALL, 5);
       
   565   myhsizer =
       
   566     new wxStaticBoxSizer (new
       
   567 			  wxStaticBox (panel, wxID_ANY,
       
   568 				       _T ("Select viewable Inputs")),
       
   569 			  wxHORIZONTAL);
       
   570   mybsizer->Add (myhsizer, wxEXPAND | wxALL, wxEXPAND | wxALL, 10);
       
   571   inlist =
       
   572     new wxListBox (panel, wxID_ANY, wxDefaultPosition, wxDefaultSize, 28,
       
   573 		   choice, wxLB_EXTENDED | wxLB_HSCROLL);
       
   574   myhsizer->Add (inlist, wxEXPAND | wxALL, wxEXPAND | wxALL, 5);
       
   575   myhsizer =
       
   576     new wxStaticBoxSizer (new wxStaticBox (panel, wxID_ANY, _T ("Legende")),
       
   577 			  wxHORIZONTAL);
       
   578   mybsizer->Add (myhsizer, wxEXPAND | wxALL, wxEXPAND | wxALL, 10);
       
   579   mylegpan =
       
   580     new wxPanel (panel, wxID_ANY, wxDefaultPosition, wxDefaultSize,
       
   581 		 wxTAB_TRAVERSAL, wxT ("Legende"));
       
   582   myhsizer->Add (mylegpan, wxEXPAND | wxALL, wxEXPAND | wxALL, 15);
       
   583   quit = new wxButton (panel, QUIT, _T ("QUIT"));
       
   584   mysizer->Add (quit, 0, wxALL, 5);
       
   585 
   550 
   586   panel = new wxPanel (book);
   551   panel = new wxPanel (book);
   587   book->InsertPage (0, panel, wxT ("Control"), true);
   552   book->InsertPage (0, panel, wxT ("Control"), true);
   588   mysizer = new wxBoxSizer (wxVERTICAL);
   553   mysizer = new wxBoxSizer (wxVERTICAL);
   589   panel->SetSizer (mysizer);
   554   panel->SetSizer (mysizer);
   610 		    wxTextCtrlNameStr);
   575 		    wxTextCtrlNameStr);
   611   myhsizer->Add (textLog, wxALL, wxEXPAND | wxALL, 5);
   576   myhsizer->Add (textLog, wxALL, wxEXPAND | wxALL, 5);
   612   quit = new wxButton (panel, QUIT, _T ("QUIT"));
   577   quit = new wxButton (panel, QUIT, _T ("QUIT"));
   613   mysizer->Add (quit, 0, wxALL, 5);
   578   mysizer->Add (quit, 0, wxALL, 5);
   614 
   579 
   615   for (int i = 0; i < 28; i++)
       
   616     for (int j = 0; j < 44; j++)
       
   617       y[i][j] = 0;
       
   618   m_timer.Start (100);
   580   m_timer.Start (100);
   619   if (to_start)
   581   if (to_start)
   620     {
   582     {
   621       Start ();
   583       Start ();
   622     }
   584     }
   651 
   613 
   652 void
   614 void
   653 MyFrame::OnStart (wxCommandEvent & WXUNUSED (event))
   615 MyFrame::OnStart (wxCommandEvent & WXUNUSED (event))
   654 {
   616 {
   655   wxString s;
   617   wxString s;
   656 
   618   
   657   node_id_ext = node_id->GetValue ();
   619   node_id_ext = node_id->GetValue ();
   658   s = busname->GetValue ();
   620   s = busname->GetValue ();
   659   strncpy (SlaveBoard.busname, s.mb_str (), MAXLENGTH_BUSNAME);
   621   strncpy (SlaveBoard.busname, s.mb_str (), MAXLENGTH_BUSNAME);
   660   *textLog << wxT ("-- Bus name: ------> ") << s << wxT ("\n");
   622   *textLog << wxT ("-- Bus name: ------> ") << s << wxT ("\n");
   661   *textLog << wxT ("-- Node ID: -------> ") << node_id->
   623   *textLog << wxT ("-- Node ID: -------> ") << node_id->
   662     GetValue () << wxT ("\n");
   624     GetValue () << wxT ("\n");
       
   625   SetTitle(wxString::Format(wxT("CanOpen virtual DS-401 - Id : %d"),node_id->
       
   626 		    GetValue ()));
   663   Start ();
   627   Start ();
   664 }
   628 }
   665 
   629 
   666 
   630 
   667 void
   631 void
   895 
   859 
   896 static int
   860 static int
   897 get_bit (UNS8 input, int bit)
   861 get_bit (UNS8 input, int bit)
   898 {
   862 {
   899   return input & (((UNS8) 1) << bit - 1) ? 1 : 0;
   863   return input & (((UNS8) 1) << bit - 1) ? 1 : 0;
   900 }
       
   901 
       
   902 void
       
   903 MyFrame::Paint ()
       
   904 {
       
   905   double vmax = old_max;
       
   906   double vmin = old_min;
       
   907   double vdelta = vmax - vmin;
       
   908   double vech = ((float) vdelta) / 10.0;
       
   909 
       
   910   double hmax = frame->echelle->GetValue ();
       
   911   double hech = hdelta / 11;
       
   912 
       
   913   double d = 300 / vdelta;
       
   914   double g = 0;
       
   915   double top = 5;
       
   916   double left = 50;
       
   917 
       
   918   double tmpi = 0;
       
   919   wxString tmps;
       
   920   double p = 0;
       
   921   double q = 0;
       
   922   int i = 0;
       
   923   int j = 0;
       
   924   wxColor col[8] =
       
   925     { wxColor (wxT ("BLUE")), wxColor (wxT ("RED")), wxColor (wxT ("GREEN")),
       
   926 wxColor (wxT ("GREY")),
       
   927     wxColor (wxT ("ORANGE")), wxColor (wxT ("YELLOW")),
       
   928       wxColor (wxT ("PINK")), wxColor (wxT ("BLACK"))
       
   929   };
       
   930   hdelta = hmax - 0;
       
   931   g = (Gtime / hdelta) * 44.00;
       
   932 
       
   933   wxClientDC MonDc (mygraphpan);
       
   934   MonDc.Clear ();
       
   935 
       
   936   MonDc.SetPen (wxPen (wxColour (200, 200, 200), 5, wxSOLID));
       
   937   MonDc.DrawRectangle ((int) left, (int) top, 660, 300);
       
   938 
       
   939   MonDc.SetPen (wxPen (wxColor (150, 200, 150), 2, wxSOLID));
       
   940   MonDc.
       
   941     SetFont (wxFont
       
   942 	     (9, wxSWISS, wxNORMAL, wxNORMAL, false, wxT ("Arial Black")));
       
   943   for (i = 0; i < 11; i++)
       
   944     {
       
   945       tmpi = (vmin + (vech * i));
       
   946       tmps.Printf (wxT ("%d"), (int) tmpi);
       
   947       MonDc.DrawRotatedText (tmps, 0, (int) (293 - (30 * i) + top), 0);
       
   948     }
       
   949   for (i = 0; i < 12; i++)
       
   950     {
       
   951       tmpi = (hech * i * 100);
       
   952       if (tmpi < 1000)
       
   953 	tmps.Printf (wxT ("%ims"), (int) tmpi);
       
   954       if (tmpi > 1000)
       
   955 	tmps.Printf (wxT ("%is"), (int) (tmpi / 1000));
       
   956       MonDc.DrawRotatedText (tmps, (int) ((60 * i) + left) - 10,
       
   957 			     (int) (317 + top), 0);
       
   958     }
       
   959   for (j = 1; j < 10; j++)
       
   960     MonDc.DrawLine ((int) left, (int) (300 - (30 * j) + top),
       
   961 		    (int) (left + 660), (int) (300 - (30 * j) + top));
       
   962   MonDc.SetPen (wxPen (wxColor (150, 200, 150), 2, wxSOLID));
       
   963   for (j = 1; j < 22; j++)
       
   964     MonDc.DrawLine ((int) (30 * j + left), (int) (300 + top),
       
   965 		    (int) (30 * j + left), (int) (top));
       
   966   MonDc.SetPen (wxPen (wxColor (150, 170, 150), 1, wxSOLID));
       
   967   for (j = 1; j < 22; j++)
       
   968     MonDc.DrawLine ((int) (30 * j + left - 15), (int) (300 + top),
       
   969 		    (int) (30 * j + left - 15), (int) (top));
       
   970   MonDc.DrawLine ((int) (30 * 22 + left - 15), (int) (300 + top),
       
   971 		  (int) (30 * 22 + left - 15), (int) (top));
       
   972 
       
   973   double m = (-vmin) * d;
       
   974   MonDc.SetPen (wxPen (wxColor (170, 170, 150), 3, wxSOLID));
       
   975   MonDc.DrawLine ((int) (left), (int) (300 + top - m), (int) (left + 660),
       
   976 		  (int) (300 + top - m));
       
   977 
       
   978   wxClientDC MaLeg (mylegpan);
       
   979   MaLeg.Clear ();
       
   980 
       
   981   MaLeg.SetPen (wxPen (wxColour (150, 200, 100), 2, wxSOLID));
       
   982   MaLeg.
       
   983     SetFont (wxFont
       
   984 	     (7, wxSWISS, wxNORMAL, wxNORMAL, false, wxT ("Arial Black")));
       
   985   for (int k = 1, tmpi = 0; k < 30; k++)
       
   986     {
       
   987       if ((tmpi < 8) && is_set (k, inlist, in))
       
   988 	{
       
   989 	  if (k <= 4)
       
   990 	    tmps.Printf (wxT ("Analogue Output %d"), k);
       
   991 	  if ((k > 4) && (k <= 12))
       
   992 	    tmps.Printf (wxT ("Bool Output %d"), k - 4);
       
   993 	  if ((k > 12) && (k <= 20))
       
   994 	    tmps.Printf (wxT ("Analogue Input %d"), k - 12);
       
   995 	  if ((k > 20) && (k <= 28))
       
   996 	    tmps.Printf (wxT ("Bool Input %d"), k - 20);
       
   997 
       
   998 	  MaLeg.DrawRotatedText (tmps, 0, 10 * tmpi, 0);
       
   999 	  MaLeg.SetPen (wxPen (col[tmpi], 4, wxSOLID));
       
  1000 	  MaLeg.DrawLine (100, 10 * tmpi + 6, 115, 10 * tmpi + 6);
       
  1001 	  //////////////////////////////////////////DAW////////////////////////////////////////
       
  1002 	  MonDc.SetPen (wxPen (col[tmpi], 4, wxSOLID));
       
  1003 	  if (k <= 4)
       
  1004 	    y[k - 1][(int) g] = Write_Analogue_Output_16_Bit[k - 1];
       
  1005 	  if ((k > 4) && (k <= 12))
       
  1006 	    y[k - 1][(int) g] = get_bit (Write_Outputs_8_Bit[0], k - 4);
       
  1007 	  if ((k > 12) && (k <= 20))
       
  1008 	    y[k - 1][(int) g] = Read_Analogue_Input_16_Bit[k - 1 - 12];
       
  1009 	  if ((k > 20) && (k <= 28))
       
  1010 	    y[k - 1][(int) g] = get_bit (Read_Inputs_8_Bit[0], k - 20);
       
  1011 
       
  1012 	  if (y[k - 1][(int) g] > old_max)
       
  1013 	    old_max = y[k - 1][(int) g];
       
  1014 	  if (y[k - 1][(int) g] < old_min)
       
  1015 	    old_min = y[k - 1][(int) g];
       
  1016 	  for (j = 1; j < (g + 1); j++)
       
  1017 	    {
       
  1018 	      p = (y[k - 1][j - 1] - vmin) * d;
       
  1019 	      q = (y[k - 1][j] - vmin) * d;
       
  1020 	      MonDc.DrawLine ((int) (15 * (j - 1) + left),
       
  1021 			      (int) (300 + top - p), (int) (15 * j + left),
       
  1022 			      (int) (300 + top - q));
       
  1023 	    }
       
  1024 	  /////////////////////////////////////////////////////////////////////////////////////
       
  1025 	  tmpi++;
       
  1026 	}
       
  1027     }
       
  1028 }
   864 }
  1029 
   865 
  1030 void
   866 void
  1031 actu_output (void)
   867 actu_output (void)
  1032 {
   868 {
  1057 
   893 
  1058 void
   894 void
  1059 actu (void)
   895 actu (void)
  1060 {
   896 {
  1061   actu_output ();
   897   actu_output ();
  1062 
       
  1063   Gtime++;
       
  1064   if (Gtime > hdelta)
       
  1065     {
       
  1066       old_max = 5;
       
  1067       old_min = -5;
       
  1068       Gtime = 0;
       
  1069     }
       
  1070   frame->Paint ();
       
  1071 }
   898 }
  1072 
   899 
  1073 void
   900 void
  1074 MyFrame::OnTimer (wxTimerEvent & event)
   901 MyFrame::OnTimer (wxTimerEvent & event)
  1075 {
   902 {