examples/DS401_Slave_Gui/main.cpp
changeset 295 25ff2fadff89
parent 246 d635cfc520ee
child 302 7ba4302a9999
equal deleted inserted replaced
294:d5493fe07e79 295:25ff2fadff89
     5 #include <wx/tglbtn.h>
     5 #include <wx/tglbtn.h>
     6 #include <wx/slider.h>
     6 #include <wx/slider.h>
     7 #include <iostream>
     7 #include <iostream>
     8 #include <wx/brush.h>
     8 #include <wx/brush.h>
     9 #include <sstream>
     9 #include <sstream>
       
    10 #include <string.h>
    10 #include <wx/listbox.h>
    11 #include <wx/listbox.h>
    11 #include <wx/dynarray.h>
    12 #include <wx/dynarray.h>
    12 #include <wx/dcclient.h>
    13 #include <wx/dcclient.h>
    13 #include <wx/gauge.h>
    14 #include <wx/gauge.h>
    14 #include <wx/spinctrl.h>
    15 #include <wx/spinctrl.h>
    15 
    16 
    16 #include "monicone.xpm"
    17 //#include "monicone.xpm"
    17 
    18 
    18 #include "main.h"
    19 #include "main.h"
    19 #include "tools.h"
       
    20 #include "TestSlaveGui.h"
    20 #include "TestSlaveGui.h"
    21 extern "C" {
    21 extern "C" {
    22 	#include "canfestival.h"
    22 	#include "canfestival.h"
    23 	#include "ObjDict.h"
    23 	#include "ObjDict.h"
    24 }
    24 }
    33 #endif
    33 #endif
    34 
    34 
    35 
    35 
    36 wxTextCtrl		*textLog;
    36 wxTextCtrl		*textLog;
    37 int				node_id_ext;
    37 int				node_id_ext;
       
    38 int to_start = 0;
    38 MyFrame 		*frame; 
    39 MyFrame 		*frame; 
    39 s_BOARD			SlaveBoard = {NULL, NULL};
    40 #define MAXLENGTH_BUSNAME 32
    40 char* 			LibraryPath = NULL;
    41 #define MAXLENGTH_BAUDRATE 8
       
    42 char            _busname[MAXLENGTH_BUSNAME] = "vcan0";
       
    43 char            _baudrate[MAXLENGTH_BAUDRATE] = "500K";
       
    44 s_BOARD			SlaveBoard = {_busname, _baudrate};
       
    45 #define MAXLENGTH_LIBPATH 1024
       
    46 char			LibraryPath[MAXLENGTH_LIBPATH]  = "libcanfestival_can_virtual.so";
    41 double			Gtime = 0;
    47 double			Gtime = 0;
    42 double			y[28][45];
    48 double			y[28][45];
    43 double			hdelta = 0;
    49 double			hdelta = 0;
    44 double			old_max = 5;
    50 double			old_max = 5;
    45 double			old_min = -5;
    51 double			old_min = -5;
   119  EVT_PAINT(MyFrame::OnPaint)
   125  EVT_PAINT(MyFrame::OnPaint)
   120  
   126  
   121  EVT_TIMER(TIMER_ID, MyFrame::OnTimer)
   127  EVT_TIMER(TIMER_ID, MyFrame::OnTimer)
   122 END_EVENT_TABLE()
   128 END_EVENT_TABLE()
   123 
   129 
   124 IMPLEMENT_APP(MyApp)
   130 IMPLEMENT_APP_NO_MAIN(MyApp);
       
   131 IMPLEMENT_WX_THEME_SUPPORT;
       
   132 
       
   133 void help()
       
   134 {
       
   135   printf("**************************************************************\n");
       
   136   printf("*  DS-401 Slave GUI                                          *\n");
       
   137   printf("*                                                            *\n");
       
   138   printf("*   Usage:                                                   *\n");
       
   139   printf("*   ./DS401_Slave_Gui [OPTIONS]                              *\n");
       
   140   printf("*                                                            *\n");
       
   141   printf("*   OPTIONS:                                                 *\n");
       
   142   printf("*     -l : Can library [\"libcanfestival_can_virtual.so\"]     *\n");
       
   143   printf("*                                                            *\n");
       
   144   printf("*    Slave:                                                  *\n");
       
   145   printf("*     -i : Node id format [0x01 , 0x7F]                      *\n");
       
   146   printf("*                                                            *\n");
       
   147   printf("*    CAN bus:                                                *\n");
       
   148   printf("*     -b : bus name [\"1\"]                                    *\n");
       
   149   printf("*     -B : 1M,500K,250K,125K,100K,50K,20K,10K                *\n");
       
   150   printf("*                                                            *\n");
       
   151   printf("**************************************************************\n");
       
   152 }
       
   153 
       
   154 
       
   155 int main(int argc,char **argv)
       
   156 {
       
   157 
       
   158   int c;
       
   159   extern char *optarg;
       
   160   char *snodeid;
       
   161   while ((c = getopt(argc, argv, "-b:B:l:i:s")) != EOF)
       
   162   {
       
   163     switch(c)
       
   164     {
       
   165       case 'b' :
       
   166         if (optarg[0] == 0)
       
   167         {
       
   168           help();
       
   169           exit(1);
       
   170         }
       
   171         SlaveBoard.busname = optarg;
       
   172         break;
       
   173       case 'B' :
       
   174         if (optarg[0] == 0)
       
   175         {
       
   176           help();
       
   177           exit(1);
       
   178         }
       
   179         SlaveBoard.baudrate = optarg;
       
   180         break;
       
   181       case 'l' :
       
   182         if (optarg[0] == 0)
       
   183         {
       
   184           help();
       
   185           exit(1);
       
   186         }
       
   187         strncpy(LibraryPath, optarg, MAXLENGTH_LIBPATH);
       
   188         break;
       
   189       case 'i' :
       
   190         if (optarg[0] == 0)
       
   191         {
       
   192           help();
       
   193           exit(1);
       
   194         }
       
   195         snodeid = optarg;
       
   196         sscanf(snodeid,"%x",&node_id_ext);
       
   197         break;
       
   198       case 's' :
       
   199         to_start = 1;
       
   200         break;
       
   201       default:
       
   202         help();
       
   203         exit(1);
       
   204     }
       
   205   }
       
   206   wxEntry(argc,argv);
       
   207 }
       
   208 
   125 
   209 
   126 bool MyApp::OnInit()
   210 bool MyApp::OnInit()
   127 {
   211 {
   128  	frame = new MyFrame( _T("I-O simulator"));
   212  	frame = new MyFrame( _T("I-O simulator"));
   129  	frame->Show(true);
   213  	frame->Show(true);
   146  wxBoxSizer		*myentrysizer;
   230  wxBoxSizer		*myentrysizer;
   147  wxListBox		*freqlist;
   231  wxListBox		*freqlist;
   148  wxStaticBox	*namebox;
   232  wxStaticBox	*namebox;
   149  wxStaticBox	*freqbox;
   233  wxStaticBox	*freqbox;
   150 
   234 
   151  SetIcon(wxICON(monicone));
   235 // SetIcon(wxICON(monicone));
   152  SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
   236  SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
   153  helpMenu->Append(HELP_ABOUT, _T("&About...\tF1"),
   237  helpMenu->Append(HELP_ABOUT, _T("&About...\tF1"),
   154  _T("Show about dialog"));
   238  _T("Show about dialog"));
   155  fileMenu->Append(FILE_QUIT, _T("E&xit\tAlt-X"),
   239  fileMenu->Append(FILE_QUIT, _T("E&xit\tAlt-X"),
   156  _T("Quit this program"));
   240  _T("Quit this program"));
   176  
   260  
   177  myhsizer = new wxStaticBoxSizer( new wxStaticBox(panel, wxID_ANY, _T("Bus driver")), wxHORIZONTAL );
   261  myhsizer = new wxStaticBoxSizer( new wxStaticBox(panel, wxID_ANY, _T("Bus driver")), wxHORIZONTAL );
   178  mysizer->Add(myhsizer, 0, wxEXPAND | wxALL, 5);
   262  mysizer->Add(myhsizer, 0, wxEXPAND | wxALL, 5);
   179  load = new wxButton( panel, LOAD, _T("Load can driver"));
   263  load = new wxButton( panel, LOAD, _T("Load can driver"));
   180  myhsizer->Add(load, 0, wxLEFT, 5);
   264  myhsizer->Add(load, 0, wxLEFT, 5);
   181  drivername = new wxTextCtrl(panel, wxID_ANY, _T("No driver loaded!!"));
   265  drivername = new wxTextCtrl(panel, wxID_ANY, wxString((const char*)LibraryPath,wxConvLocal));
   182  myhsizer->Add(drivername, 1, wxEXPAND | wxALL, 5);
   266  myhsizer->Add(drivername, 1, wxEXPAND | wxALL, 5);
   183  myentrysizer = new wxBoxSizer( wxHORIZONTAL );
   267  myentrysizer = new wxBoxSizer( wxHORIZONTAL );
   184  mysizer->Add(myentrysizer, 0, wxEXPAND | wxALL, 5);
   268  mysizer->Add(myentrysizer, 0, wxEXPAND | wxALL, 5);
   185  myhsizer = new wxStaticBoxSizer( new wxStaticBox(panel, wxID_ANY, _T("Bus name")), wxHORIZONTAL );
   269  myhsizer = new wxStaticBoxSizer( new wxStaticBox(panel, wxID_ANY, _T("Bus name")), wxHORIZONTAL );
   186  myentrysizer->Add(myhsizer, 0, wxEXPAND | wxALL, 5);
   270  myentrysizer->Add(myhsizer, 0, wxEXPAND | wxALL, 5);
   187  busname = new wxTextCtrl(panel, wxID_ANY, _T("0"));
   271  busname = new wxTextCtrl(panel, wxID_ANY, wxString((const char*)SlaveBoard.busname, wxConvLocal));
   188  myhsizer->Add(busname, 0, wxLEFT, 5);
   272  myhsizer->Add(busname, 0, wxLEFT, 5);
   189  myhsizer = new wxStaticBoxSizer( new wxStaticBox(panel, wxID_ANY, _T("Node ID (Hexa)")), wxHORIZONTAL );
   273  myhsizer = new wxStaticBoxSizer( new wxStaticBox(panel, wxID_ANY, _T("Node ID (Hexa)")), wxHORIZONTAL );
   190  myentrysizer->Add(myhsizer, 0, wxEXPAND | wxALL, 5);
   274  myentrysizer->Add(myhsizer, 0, wxEXPAND | wxALL, 5);
   191  node_id = new wxSpinCtrl(panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, 1, 127, 1, _T("wxSpinCtrl"));
   275  node_id = new wxSpinCtrl(panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, 1, 127, node_id_ext, _T("wxSpinCtrl"));
   192  myhsizer->Add(node_id, 0, wxRIGHT, 5);
   276  myhsizer->Add(node_id, 0, wxRIGHT, 5);
   193  myhsizer = new wxStaticBoxSizer( new wxStaticBox(panel, wxID_ANY, _T("Baudrate")), wxHORIZONTAL );
   277  myhsizer = new wxStaticBoxSizer( new wxStaticBox(panel, wxID_ANY, _T("Baudrate")), wxHORIZONTAL );
   194  mysizer->Add(myhsizer, wxEXPAND | wxALL, wxEXPAND | wxALL, 5);
   278  mysizer->Add(myhsizer, wxEXPAND | wxALL, wxEXPAND | wxALL, 5);
   195  freqlist = new wxListBox( panel, FREQBOX, wxDefaultPosition, wxDefaultSize, 8, choices, wxLB_SINGLE | wxLB_HSCROLL);
   279  freqlist = new wxListBox( panel, FREQBOX, wxDefaultPosition, wxDefaultSize, 8, choices, wxLB_SINGLE | wxLB_HSCROLL);
   196  myhsizer->Add(freqlist, wxEXPAND | wxALL, wxEXPAND | wxALL, 5);
   280  myhsizer->Add(freqlist, wxEXPAND | wxALL, wxEXPAND | wxALL, 5);
   368  
   452  
   369  for (int i= 0; i < 28; i++)
   453  for (int i= 0; i < 28; i++)
   370 	for (int j = 0; j < 44; j++)					
   454 	for (int j = 0; j < 44; j++)					
   371 		y[i][j] = 0;
   455 		y[i][j] = 0;
   372  m_timer.Start(100);
   456  m_timer.Start(100);
       
   457  if(to_start) 
       
   458  {
       
   459     Start();
       
   460  }
   373 }
   461 }
   374 
   462 
   375 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
   463 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
   376 {
   464 {
   377  Close(true);
   465  Close(true);
       
   466 }
       
   467 
       
   468 void MyFrame::Start()
       
   469 {   
       
   470  if (main_can(SlaveBoard, LibraryPath))
       
   471   {
       
   472     printf("[KO]\n");
       
   473     *textLog << wxT("----------------------------------------------------Initialization [KO]\n");
       
   474   }
       
   475  else
       
   476   {
       
   477     printf("[OK]\n");
       
   478     *textLog << wxT("----------------------------------------------------Initialization [OK]\n");
       
   479     stop->Enable(true);
       
   480     start->Enable(false);
       
   481   }
   378 }
   482 }
   379 
   483 
   380 void MyFrame::OnStart(wxCommandEvent& WXUNUSED(event))
   484 void MyFrame::OnStart(wxCommandEvent& WXUNUSED(event))
   381 {	
   485 {	
   382  wxString	s;
   486  wxString	s;
   383 	
   487 	
   384  if (LibraryPath == NULL)
       
   385  	{
       
   386  		*textLog << wxT("No driver found!! ----------------------------------Initialization [KO]\n");
       
   387  		return;
       
   388  	}
       
   389  if (SlaveBoard.baudrate == NULL)
       
   390  	{
       
   391  		*textLog << wxT("No baudrate found!! ----------------------------------Initialization [KO]\n");
       
   392  		return;
       
   393  	}	
       
   394  	
       
   395  node_id_ext = node_id->GetValue();
   488  node_id_ext = node_id->GetValue();
   396  s = busname->GetValue();
   489  s = busname->GetValue();
   397  SlaveBoard.busname = wxstr_to_charbuf(s);
   490  strncpy(SlaveBoard.busname, s.mb_str(), MAXLENGTH_BUSNAME);
   398  *textLog << wxT("-- Bus name: ------> ") << s << wxT("\n");
   491  *textLog << wxT("-- Bus name: ------> ") << s << wxT("\n");
   399  *textLog << wxT("-- Node ID: -------> ") << node_id->GetValue() << wxT("\n");
   492  *textLog << wxT("-- Node ID: -------> ") << node_id->GetValue() << wxT("\n");
   400  
   493  Start();
   401  if (main_can(SlaveBoard, LibraryPath))
   494 }
   402   {
   495 
   403  	printf("[KO]\n");
       
   404  	*textLog << wxT("----------------------------------------------------Initialization [KO]\n");
       
   405   }
       
   406  else
       
   407   {
       
   408  	printf("[OK]\n");
       
   409  	*textLog << wxT("----------------------------------------------------Initialization [OK]\n");
       
   410  	stop->Enable(true);
       
   411  	start->Enable(false);
       
   412   }
       
   413 }
       
   414 
   496 
   415 void MyFrame::OnStop(wxCommandEvent& WXUNUSED(event))
   497 void MyFrame::OnStop(wxCommandEvent& WXUNUSED(event))
   416 {
   498 {
   417 	
   499 	
   418  	*textLog << wxT("----------------------------------------------------Slave [STOPPED]") << wxT("\n");
   500  	*textLog << wxT("----------------------------------------------------Slave [STOPPED]") << wxT("\n");
   419 	stop_slave();
   501 	stop_slave();
   420 	stop->Enable(false);
   502 	stop->Enable(false);
   421 	start->Enable(true);
   503 	start->Enable(true);
   422 	charbuf_free(SlaveBoard.busname);
       
   423 }
   504 }
   424 
   505 
   425 void MyFrame::OnLoad(wxCommandEvent& WXUNUSED(event))
   506 void MyFrame::OnLoad(wxCommandEvent& WXUNUSED(event))
   426 {
   507 {
   427     wxFileDialog fd(this, wxT("Choose a node configuration file"), wxT(""), wxT("Slave.od"), wxT("*.so"));
   508     wxFileDialog fd(this, wxT("Choose a node configuration file"), wxT(""), wxT("Slave.od"), wxT("*.so"));
   429     if(fd.ShowModal() == wxID_OK)
   510     if(fd.ShowModal() == wxID_OK)
   430     {
   511     {
   431     	drivername->Clear();
   512     	drivername->Clear();
   432         drivername->AppendText(fd.GetPath()); 
   513         drivername->AppendText(fd.GetPath()); 
   433         *textLog << wxT("LibraryPath: ") << fd.GetPath() << wxT("\n");
   514         *textLog << wxT("LibraryPath: ") << fd.GetPath() << wxT("\n");
   434         if (LibraryPath != NULL)
   515         strncpy(LibraryPath, fd.GetPath().mb_str(), MAXLENGTH_LIBPATH);
   435         	charbuf_free(LibraryPath);
       
   436         LibraryPath = wxstr_to_charbuf(fd.GetPath());
       
   437         printf("path=%s",LibraryPath);
   516         printf("path=%s",LibraryPath);
   438     }
   517     }
   439 }
   518 }
   440 
   519 
   441 void MyFrame::OnInbt1(wxCommandEvent& WXUNUSED(event))
   520 void MyFrame::OnInbt1(wxCommandEvent& WXUNUSED(event))
   570 void MyFrame::OnFreqBoxClick( wxCommandEvent &event )
   649 void MyFrame::OnFreqBoxClick( wxCommandEvent &event )
   571 {
   650 {
   572 	wxString	wxstr;
   651 	wxString	wxstr;
   573 	
   652 	
   574  	wxstr = event.GetString();
   653  	wxstr = event.GetString();
   575  	if (SlaveBoard.baudrate != NULL)
   654  	strncpy(SlaveBoard.baudrate, wxstr.mb_str(),MAXLENGTH_BAUDRATE);
   576  		charbuf_free(SlaveBoard.baudrate);
       
   577  	SlaveBoard.baudrate = wxstr_to_charbuf(wxstr);
       
   578  	*textLog << wxT("Baudrate:") << event.GetString() << wxT("\n");
   655  	*textLog << wxT("Baudrate:") << event.GetString() << wxT("\n");
   579 }
   656 }
   580 
   657 
   581 static int	is_set(int			i,
   658 static int	is_set(int			i,
   582 					wxListBox	*inlist,
   659 					wxListBox	*inlist,
   593 	return (0);
   670 	return (0);
   594 }
   671 }
   595 
   672 
   596 static int	get_bit(UNS8	input, int bit)
   673 static int	get_bit(UNS8	input, int bit)
   597 {
   674 {
   598 	if (bit == 1)
   675     return input & (((UNS8)1) <<  bit) ? 1 : 0; 
   599 		return ((input & 0x1) ? 1 : 0);
       
   600 	if (bit == 2)
       
   601 		return ((input & 0x2) ? 1 : 0);
       
   602 	if (bit == 3)
       
   603 		return ((input & 0x4) ? 1 : 0);
       
   604 	if (bit == 4)
       
   605 		return ((input & 0x8) ? 1 : 0);
       
   606 	if (bit == 5)
       
   607 		return ((input & 0x10) ? 1 : 0);
       
   608 	if (bit == 6)
       
   609 		return ((input & 0x20) ? 1 : 0);
       
   610 	if (bit == 7)
       
   611 		return ((input & 0x40) ? 1 : 0);
       
   612 	if (bit == 8)
       
   613 		return ((input & 0x80) ? 1 : 0);
       
   614 	return 0;
       
   615 }
   676 }
   616 
   677 
   617 void MyFrame::Paint()
   678 void MyFrame::Paint()
   618 {
   679 {
   619 	double		vmax = old_max;
   680 	double		vmax = old_max;