]> rtime.felk.cvut.cz Git - CanFestival-3.git/blob - examples/DS401_Slave_Gui/main.cpp
Bugs that prevents to compile with wx2.8 fixed
[CanFestival-3.git] / examples / DS401_Slave_Gui / main.cpp
1 #include <wx/wxprec.h>
2 #include <wx/wx.h>
3 #include <wx/textctrl.h>
4 #include <wx/spinctrl.h>
5 #include <wx/tglbtn.h>
6 #include <wx/slider.h>
7 #include <iostream>
8 #include <wx/brush.h>
9 #include <sstream>
10 #include <string.h>
11 #include <wx/listbox.h>
12 #include <wx/dynarray.h>
13 #include <wx/dcclient.h>
14 #include <wx/gauge.h>
15 #include <wx/spinctrl.h>
16
17 //#include "monicone.xpm"
18
19 #include "main.h"
20 #include "TestSlaveGui.h"
21 extern "C" {
22         #include "canfestival.h"
23         #include "ObjDict.h"
24 }
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28     #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32     #include "wx/wx.h"
33 #endif
34
35
36 wxTextCtrl              *textLog;
37 int                             node_id_ext;
38 int to_start = 0;
39 MyFrame                 *frame; 
40 #define MAXLENGTH_BUSNAME 32
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";
47 double                  Gtime = 0;
48 double                  y[28][45];
49 double                  hdelta = 0;
50 double                  old_max = 5;
51 double                  old_min = -5;
52
53 // Declare some IDs. These are arbitrary.
54 const int BOOKCTRL = 100;
55 const int FILE_QUIT = wxID_EXIT;
56 const int HELP_ABOUT = wxID_ABOUT;
57 const int ADD = 105;
58 const int REMOVE = 108;
59 const int QUIT = 109;
60 const int FREQBOX = 110;
61 const int START = 112;
62 const int TEXTBOX1 = 113;
63 const int STOP = 114;
64 const int LOAD = 115;
65 const int INBT1 = 118;
66 const int INBT2 = 119;
67 const int INBT3 = 120;
68 const int INBT4 = 121;
69 const int INBT5 = 122;
70 const int INBT6 = 123;
71 const int INBT7 = 124;
72 const int INBT8 = 125;
73 const int INS1 = 126;
74 const int INS2 = 127;
75 const int INS3 = 128;
76 const int INS4 = 129;
77 const int INS5 = 135;
78 const int INS6 = 136;
79 const int INS7 = 137;
80 const int INS8 = 138;
81 const int TIMER_ID = 130;
82 const int INST1 = 131;
83 const int INST2 = 132;
84 const int INST3 = 133;
85 const int INST4 = 134;
86 const int INST5 = 135;
87 const int INST6 = 136;
88 const int INST7 = 137;
89 const int INST8 = 138;
90
91 // Attach the event handlers. Put this after MyFrame declaration.
92 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
93  EVT_BUTTON(STOP, MyFrame::OnStop)
94  EVT_BUTTON(LOAD, MyFrame::OnLoad)
95  EVT_BUTTON(START, MyFrame::OnStart)
96  EVT_TOGGLEBUTTON(INBT1, MyFrame::OnInbt1)
97  EVT_TOGGLEBUTTON(INBT2, MyFrame::OnInbt2)
98  EVT_TOGGLEBUTTON(INBT3, MyFrame::OnInbt3)
99  EVT_TOGGLEBUTTON(INBT4, MyFrame::OnInbt4)
100  EVT_TOGGLEBUTTON(INBT5, MyFrame::OnInbt5)
101  EVT_TOGGLEBUTTON(INBT6, MyFrame::OnInbt6)
102  EVT_TOGGLEBUTTON(INBT7, MyFrame::OnInbt7)
103  EVT_TOGGLEBUTTON(INBT8, MyFrame::OnInbt8)
104  EVT_COMMAND_SCROLL(INS1, MyFrame::OnIns1)
105  EVT_COMMAND_SCROLL(INS2, MyFrame::OnIns2)
106  EVT_COMMAND_SCROLL(INS3, MyFrame::OnIns3)
107  EVT_COMMAND_SCROLL(INS4, MyFrame::OnIns4)
108  EVT_COMMAND_SCROLL(INS5, MyFrame::OnIns5)
109  EVT_COMMAND_SCROLL(INS6, MyFrame::OnIns6)
110  EVT_COMMAND_SCROLL(INS7, MyFrame::OnIns7)
111  EVT_COMMAND_SCROLL(INS8, MyFrame::OnIns8)
112  EVT_SPINCTRL(INST1, MyFrame::OnInst1)
113  EVT_SPINCTRL(INST2, MyFrame::OnInst2)
114  EVT_SPINCTRL(INST3, MyFrame::OnInst3)
115  EVT_SPINCTRL(INST4, MyFrame::OnInst4)
116  EVT_SPINCTRL(INST5, MyFrame::OnInst5)
117  EVT_SPINCTRL(INST6, MyFrame::OnInst6)
118  EVT_SPINCTRL(INST7, MyFrame::OnInst7)
119  EVT_SPINCTRL(INST8, MyFrame::OnInst8)
120  
121  EVT_LISTBOX(FREQBOX, MyFrame::OnFreqBoxClick)
122  EVT_MENU(FILE_QUIT, MyFrame::OnQuit)
123  EVT_BUTTON(QUIT, MyFrame::OnQuit)
124  EVT_MENU(HELP_ABOUT, MyFrame::OnAbout)
125  EVT_PAINT(MyFrame::OnPaint)
126  
127  EVT_TIMER(TIMER_ID, MyFrame::OnTimer)
128 END_EVENT_TABLE()
129
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
209
210 bool MyApp::OnInit()
211 {
212         frame = new MyFrame( _T("I-O simulator"));
213         frame->Show(true);
214  return true;
215 }
216
217 MyFrame::MyFrame(const wxString& title)
218   : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(800, 740), wxDEFAULT_FRAME_STYLE, wxT("frame"))
219   ,m_timer(this, TIMER_ID)
220 {
221  wxMenu                 *fileMenu = new wxMenu;
222  wxMenu                 *helpMenu = new wxMenu;
223  wxPanel                *panel;
224  wxButton               *quit;
225  wxButton               *load;
226  wxBoxSizer             *mysizer;
227  wxBoxSizer             *mysssizer;
228  wxBoxSizer             *myhsizer;
229  wxBoxSizer             *mybsizer;
230  wxBoxSizer             *myentrysizer;
231  wxListBox              *freqlist;
232  wxStaticBox    *namebox;
233  wxStaticBox    *freqbox;
234
235 // SetIcon(wxICON(monicone));
236  SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
237  helpMenu->Append(HELP_ABOUT, _T("&About...\tF1"),
238  _T("Show about dialog"));
239  fileMenu->Append(FILE_QUIT, _T("E&xit\tAlt-X"),
240  _T("Quit this program"));
241  wxMenuBar *menuBar = new wxMenuBar();
242  menuBar->Append(fileMenu, _T("&File"));
243  menuBar->Append(helpMenu, _T("&Help"));
244  SetMenuBar(menuBar);
245  CreateStatusBar(2);
246  SetStatusText(_T("Lolitech."), 0);
247  SetStatusText(_T("Welcome."), 1);
248
249  book = new wxNotebook(this, BOOKCTRL);
250   
251  panel = new wxPanel(book);
252  book->AddPage(panel, _T("Configuration"), true);
253  mysizer = new wxBoxSizer( wxVERTICAL );
254  panel->SetSizer(mysizer);
255  wxString choices[] =
256  {
257   _T("10K"), _T("20K"), _T("50K"), _T("100K"), 
258   _T("125K"), _T("250K"), _T("500K"), _T("1M")
259  };
260  
261  myhsizer = new wxStaticBoxSizer( new wxStaticBox(panel, wxID_ANY, _T("Bus driver")), wxHORIZONTAL );
262  mysizer->Add(myhsizer, 0, wxEXPAND | wxALL, 5);
263  load = new wxButton( panel, LOAD, _T("Load can driver"));
264  myhsizer->Add(load, 0, wxLEFT, 5);
265  drivername = new wxTextCtrl(panel, wxID_ANY, wxString((const char*)LibraryPath,wxConvLocal));
266  myhsizer->Add(drivername, 1, wxEXPAND | wxALL, 5);
267  myentrysizer = new wxBoxSizer( wxHORIZONTAL );
268  mysizer->Add(myentrysizer, 0, wxEXPAND | wxALL, 5);
269  myhsizer = new wxStaticBoxSizer( new wxStaticBox(panel, wxID_ANY, _T("Bus name")), wxHORIZONTAL );
270  myentrysizer->Add(myhsizer, 0, wxEXPAND | wxALL, 5);
271  busname = new wxTextCtrl(panel, wxID_ANY, wxString((const char*)SlaveBoard.busname, wxConvLocal));
272  myhsizer->Add(busname, 0, wxLEFT, 5);
273  myhsizer = new wxStaticBoxSizer( new wxStaticBox(panel, wxID_ANY, _T("Node ID (Hexa)")), wxHORIZONTAL );
274  myentrysizer->Add(myhsizer, 0, wxEXPAND | wxALL, 5);
275  node_id = new wxSpinCtrl(panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, 1, 127, node_id_ext, _T("wxSpinCtrl"));
276  myhsizer->Add(node_id, 0, wxRIGHT, 5);
277  myhsizer = new wxStaticBoxSizer( new wxStaticBox(panel, wxID_ANY, _T("Baudrate")), wxHORIZONTAL );
278  mysizer->Add(myhsizer, wxEXPAND | wxALL, wxEXPAND | wxALL, 5);
279  freqlist = new wxListBox( panel, FREQBOX, wxDefaultPosition, wxDefaultSize, 8, choices, wxLB_SINGLE | wxLB_HSCROLL);
280  myhsizer->Add(freqlist, wxEXPAND | wxALL, wxEXPAND | wxALL, 5);
281  quit = new wxButton( panel, QUIT, _T("QUIT"));
282  mysizer->Add(quit, 0, wxALL, 5);
283  
284  
285  panel = new wxPanel(book);
286  book->AddPage(panel, wxT("Inputs"), true);
287  mysizer = new wxBoxSizer( wxVERTICAL );
288  panel->SetSizer(mysizer);
289  myhsizer = new wxStaticBoxSizer( new wxStaticBox(panel, wxID_ANY, _T("Read Inputs UNSIGNED8")), wxHORIZONTAL );
290  mysizer->Add(myhsizer, 0, wxEXPAND | wxALL, 30);
291  inbt1 = new wxToggleButton(panel, INBT1, wxT("1"));
292  inbt2 = new wxToggleButton(panel, INBT2, wxT("2"));
293  inbt3 = new wxToggleButton(panel, INBT3, wxT("3"));
294  inbt4 = new wxToggleButton(panel, INBT4, wxT("4"));
295  inbt5 = new wxToggleButton(panel, INBT5, wxT("5"));
296  inbt6 = new wxToggleButton(panel, INBT6, wxT("6"));
297  inbt7 = new wxToggleButton(panel, INBT7, wxT("7"));
298  inbt8 = new wxToggleButton(panel, INBT8, wxT("8"));
299  myhsizer->Add(inbt1, wxEXPAND | wxALL, 0, 5);
300  myhsizer->Add(inbt2, wxEXPAND | wxALL, 0, 5);
301  myhsizer->Add(inbt3, wxEXPAND | wxALL, 0, 5);
302  myhsizer->Add(inbt4, wxEXPAND | wxALL, 0, 5);
303  myhsizer->Add(inbt5, wxEXPAND | wxALL, 0, 5);
304  myhsizer->Add(inbt6, wxEXPAND | wxALL, 0, 5);
305  myhsizer->Add(inbt7, wxEXPAND | wxALL, 0, 5);
306  myhsizer->Add(inbt8, wxEXPAND | wxALL, 0, 5);
307  myhsizer = new wxStaticBoxSizer( new wxStaticBox(panel, wxID_ANY, _T("Analogue Inputs INTEGER16")), wxVERTICAL );
308  mysizer->Add(myhsizer, wxEXPAND | wxALL, wxEXPAND | wxALL, 30);
309  mysssizer = new wxBoxSizer( wxHORIZONTAL );
310  myhsizer->Add(mysssizer, 0, wxEXPAND | wxALL, 0); 
311  inst1 = new wxSpinCtrl(panel, INST1, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, -32768, 32767, 0, _T("0"));
312  inst2 = new wxSpinCtrl(panel, INST2, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, -32768, 32767, 0, _T("0"));
313  inst3 = new wxSpinCtrl(panel, INST3, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, -32768, 32767, 0, _T("0"));
314  inst4 = new wxSpinCtrl(panel, INST4, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, -32768, 32767, 0, _T("0"));
315  inst5 = new wxSpinCtrl(panel, INST5, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, -32768, 32767, 0, _T("0"));
316  inst6 = new wxSpinCtrl(panel, INST6, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, -32768, 32767, 0, _T("0"));
317  inst7 = new wxSpinCtrl(panel, INST7, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, -32768, 32767, 0, _T("0"));
318  inst8 = new wxSpinCtrl(panel, INST8, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, -32768, 32767, 0, _T("0"));
319  mysssizer->Add(inst1, wxALL, 0, 5);
320  mysssizer->Add(inst2, wxALL, 0, 5);
321  mysssizer->Add(inst3, wxALL, 0, 5);
322  mysssizer->Add(inst4, wxALL, 0, 5);
323  mysssizer->Add(inst5, wxALL, 0, 5);
324  mysssizer->Add(inst6, wxALL, 0, 5);
325  mysssizer->Add(inst7, wxALL, 0, 5);
326  mysssizer->Add(inst8, wxALL, 0, 5);
327  mysssizer = new wxBoxSizer( wxHORIZONTAL );
328  myhsizer->Add(mysssizer, wxEXPAND | wxALL, wxEXPAND | wxALL, 0);
329  ins1 = new wxSlider(panel, INS1, 0, -32768, 32767, wxDefaultPosition, wxDefaultSize, wxSL_VERTICAL | wxSL_INVERSE, wxDefaultValidator, wxT("slider"));
330  ins2 = new wxSlider(panel, INS2, 0, -32768, 32767, wxDefaultPosition, wxDefaultSize, wxSL_VERTICAL | wxSL_INVERSE, wxDefaultValidator, wxT("slider"));
331  ins3 = new wxSlider(panel, INS3, 0, -32768, 32767, wxDefaultPosition, wxDefaultSize, wxSL_VERTICAL | wxSL_INVERSE, wxDefaultValidator, wxT("slider"));
332  ins4 = new wxSlider(panel, INS4, 0, -32768, 32767, wxDefaultPosition, wxDefaultSize, wxSL_VERTICAL | wxSL_INVERSE, wxDefaultValidator, wxT("slider"));
333  ins5 = new wxSlider(panel, INS5, 0, -32768, 32767, wxDefaultPosition, wxDefaultSize, wxSL_VERTICAL | wxSL_INVERSE, wxDefaultValidator, wxT("slider"));
334  ins6 = new wxSlider(panel, INS6, 0, -32768, 32767, wxDefaultPosition, wxDefaultSize, wxSL_VERTICAL | wxSL_INVERSE, wxDefaultValidator, wxT("slider"));
335  ins7 = new wxSlider(panel, INS7, 0, -32768, 32767, wxDefaultPosition, wxDefaultSize, wxSL_VERTICAL | wxSL_INVERSE, wxDefaultValidator, wxT("slider"));
336  ins8 = new wxSlider(panel, INS8, 0, -32768, 32767, wxDefaultPosition, wxDefaultSize, wxSL_VERTICAL | wxSL_INVERSE, wxDefaultValidator, wxT("slider"));
337  mysssizer->Add(ins1, wxEXPAND | wxALL, wxEXPAND | wxALL, 15);
338  mysssizer->Add(ins2, wxEXPAND | wxALL, wxEXPAND | wxALL, 15);
339  mysssizer->Add(ins3, wxEXPAND | wxALL, wxEXPAND | wxALL, 15);
340  mysssizer->Add(ins4, wxEXPAND | wxALL, wxEXPAND | wxALL, 15);
341  mysssizer->Add(ins5, wxEXPAND | wxALL, wxEXPAND | wxALL, 15);
342  mysssizer->Add(ins6, wxEXPAND | wxALL, wxEXPAND | wxALL, 15);
343  mysssizer->Add(ins7, wxEXPAND | wxALL, wxEXPAND | wxALL, 15);
344  mysssizer->Add(ins8, wxEXPAND | wxALL, wxEXPAND | wxALL, 15);
345  quit = new wxButton( panel, QUIT, _T("QUIT"));
346  mysizer->Add(quit, 0, wxALL, 5);
347  
348  panel = new wxPanel(book);
349  book->AddPage(panel, wxT("Outputs"), true);
350  mysizer = new wxBoxSizer( wxVERTICAL );
351  panel->SetSizer(mysizer);
352  myhsizer = new wxStaticBoxSizer( new wxStaticBox(panel, wxID_ANY, _T("Write Outputs UNSIGNED8")), wxHORIZONTAL );
353  mysizer->Add(myhsizer, 0, wxEXPAND | wxALL, 30);
354  outbt1 = new wxToggleButton(panel, wxID_ANY, wxT("1"));
355  outbt2 = new wxToggleButton(panel, wxID_ANY, wxT("2"));
356  outbt3 = new wxToggleButton(panel, wxID_ANY, wxT("3"));
357  outbt4 = new wxToggleButton(panel, wxID_ANY, wxT("4"));
358  outbt5 = new wxToggleButton(panel, wxID_ANY, wxT("5"));
359  outbt6 = new wxToggleButton(panel, wxID_ANY, wxT("6"));
360  outbt7 = new wxToggleButton(panel, wxID_ANY, wxT("7"));
361  outbt8 = new wxToggleButton(panel, wxID_ANY, wxT("8"));
362  myhsizer->Add(outbt1, wxEXPAND | wxALL, 0, 5);
363  myhsizer->Add(outbt2, wxEXPAND | wxALL, 0, 5);
364  myhsizer->Add(outbt3, wxEXPAND | wxALL, 0, 5);
365  myhsizer->Add(outbt4, wxEXPAND | wxALL, 0, 5);
366  myhsizer->Add(outbt5, wxEXPAND | wxALL, 0, 5);
367  myhsizer->Add(outbt6, wxEXPAND | wxALL, 0, 5);
368  myhsizer->Add(outbt7, wxEXPAND | wxALL, 0, 5);
369  myhsizer->Add(outbt8, wxEXPAND | wxALL, 0, 5);
370  myhsizer = new wxStaticBoxSizer( new wxStaticBox(panel, wxID_ANY, _T("Analogue Output INTEGER16")), wxVERTICAL );
371  mysizer->Add(myhsizer, wxEXPAND | wxALL, wxEXPAND | wxALL, 30);
372  mysssizer = new wxBoxSizer( wxHORIZONTAL );
373  myhsizer->Add(mysssizer, 0, wxEXPAND | wxALL, 0);
374  outst1 = new wxStaticText(panel, wxID_ANY, wxT("label"), wxDefaultPosition, wxDefaultSize, 0, wxT("staticText"));
375  outst2 = new wxStaticText(panel, wxID_ANY, wxT("label"), wxDefaultPosition, wxDefaultSize, 0, wxT("staticText"));
376  outst3 = new wxStaticText(panel, wxID_ANY, wxT("label"), wxDefaultPosition, wxDefaultSize, 0, wxT("staticText"));
377  outst4 = new wxStaticText(panel, wxID_ANY, wxT("label"), wxDefaultPosition, wxDefaultSize, 0, wxT("staticText"));
378  mysssizer->Add(outst1, wxEXPAND | wxALL, wxEXPAND | wxALL, 5);
379  mysssizer->Add(outst2, wxEXPAND | wxALL, wxEXPAND | wxALL, 5);
380  mysssizer->Add(outst3, wxEXPAND | wxALL, wxEXPAND | wxALL, 5);
381  mysssizer->Add(outst4, wxEXPAND | wxALL, wxEXPAND | wxALL, 5);
382  mysssizer = new wxBoxSizer( wxHORIZONTAL );
383  myhsizer->Add(mysssizer, wxEXPAND | wxALL, wxEXPAND | wxALL, 0);
384  outs1 = new wxGauge(panel, wxID_ANY, 65535, wxDefaultPosition, wxDefaultSize, wxSL_VERTICAL | wxSL_INVERSE, wxDefaultValidator, wxT("gauge"));
385  outs2 = new wxGauge(panel, wxID_ANY, 65535, wxDefaultPosition, wxDefaultSize, wxSL_VERTICAL | wxSL_INVERSE, wxDefaultValidator, wxT("gauge"));
386  outs3 = new wxGauge(panel, wxID_ANY, 65535, wxDefaultPosition, wxDefaultSize, wxSL_VERTICAL | wxSL_INVERSE, wxDefaultValidator, wxT("gauge"));
387  outs4 = new wxGauge(panel, wxID_ANY, 65535, wxDefaultPosition, wxDefaultSize, wxSL_VERTICAL | wxSL_INVERSE, wxDefaultValidator, wxT("gauge"));
388  mysssizer->Add(outs1, wxEXPAND | wxALL, wxEXPAND | wxALL, 15);
389  mysssizer->Add(outs2, wxEXPAND | wxALL, wxEXPAND | wxALL, 15);
390  mysssizer->Add(outs3, wxEXPAND | wxALL, wxEXPAND | wxALL, 15);
391  mysssizer->Add(outs4, wxEXPAND | wxALL, wxEXPAND | wxALL, 15);
392  quit = new wxButton( panel, QUIT, _T("QUIT"));
393  mysizer->Add(quit, 0, wxALL, 5); 
394  
395   wxString choice[] =
396  {
397   _T("Analogue Out 1"), _T("Analogue Out 2"),
398   _T("Analogue Out 3"), _T("Analogue Out 4"),
399   _T("Bool Out 1"), _T("Bool Out 2"), 
400   _T("Bool Out 3"), _T("Bool Out 4"), 
401   _T("Bool Out 5"), _T("Bool Out 6"), 
402   _T("Bool Out 7"), _T("Bool Out 8"), 
403   _T("Analogue Input 1"), _T("Analogue Input 2"),
404   _T("Analogue Input 3"), _T("Analogue Input 4"),
405   _T("Analogue Input 5"), _T("Analogue Input 6"),
406   _T("Analogue Input 7"), _T("Analogue Input 8"),
407   _T("Bool Input 1"), _T("Bool Input 2"), 
408   _T("Bool Input 3"), _T("Bool Input 4"), 
409   _T("Bool Input 5"), _T("Bool Input 6"), 
410   _T("Bool Input 7"), _T("Bool Input 8"), };
411  panel = new wxPanel(book); 
412  book->AddPage(panel, wxT("Graphic"), true); 
413  mysizer = new wxBoxSizer( wxVERTICAL ); 
414  panel->SetSizer(mysizer); 
415  myhsizer = new wxStaticBoxSizer( new wxStaticBox(panel, wxID_ANY, _T("Graphic")), wxVERTICAL ); 
416  mysizer->Add(myhsizer, 0, wxEXPAND | wxALL, 10); 
417  mygraphpan = new wxPanel(panel, wxID_ANY, wxDefaultPosition, wxSize(0, 350), wxTAB_TRAVERSAL, wxT("Graphic")); 
418  myhsizer->Add(mygraphpan, wxEXPAND | wxALL, wxEXPAND | wxALL, 15); 
419  echelle = new wxSlider(panel, wxID_ANY, 44, 44, 1000, wxDefaultPosition, wxDefaultSize, wxSL_HORIZONTAL , wxDefaultValidator, wxT("slider")); 
420  myhsizer->Add(echelle, 0, wxEXPAND | wxALL, 0); 
421  mybsizer = new wxBoxSizer( wxHORIZONTAL ); 
422  mysizer->Add(mybsizer, wxEXPAND | wxALL, wxEXPAND | wxALL, 5); 
423  myhsizer = new wxStaticBoxSizer( new wxStaticBox(panel, wxID_ANY, _T("Select viewable Inputs")), wxHORIZONTAL ); 
424  mybsizer->Add(myhsizer, wxEXPAND | wxALL, wxEXPAND | wxALL, 10); 
425  inlist = new wxListBox( panel, wxID_ANY, wxDefaultPosition, wxDefaultSize, 28, choice, wxLB_EXTENDED | wxLB_HSCROLL); 
426  myhsizer->Add(inlist, wxEXPAND | wxALL, wxEXPAND | wxALL, 5); 
427  myhsizer = new wxStaticBoxSizer( new wxStaticBox(panel, wxID_ANY, _T("Legende")), wxHORIZONTAL ); 
428  mybsizer->Add(myhsizer, wxEXPAND | wxALL, wxEXPAND | wxALL, 10); 
429  mylegpan = new wxPanel(panel, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, wxT("Legende")); 
430  myhsizer->Add(mylegpan, wxEXPAND | wxALL, wxEXPAND | wxALL, 15); 
431  quit = new wxButton( panel, QUIT, _T("QUIT")); 
432  mysizer->Add(quit, 0, wxALL, 5);
433  
434  panel = new wxPanel(book);
435  book->InsertPage(0, panel, wxT("Control"), true);
436  mysizer = new wxBoxSizer( wxVERTICAL );
437  panel->SetSizer(mysizer);
438  myhsizer = new wxBoxSizer( wxHORIZONTAL );
439  myhsizer = new wxStaticBoxSizer( new wxStaticBox(panel, wxID_ANY, _T("Control your node")), wxHORIZONTAL );
440  mysizer->Add(myhsizer, 0, wxALL, 5);
441  start = new wxButton( panel, START, _T("START")); 
442  stop = new wxButton( panel, STOP, _T("STOP")); 
443  stop->Enable(false);
444  myhsizer->Add(start, 0, wxRIGHT, 5);
445  myhsizer->Add(stop, 0, wxLEFT, 5);
446  myhsizer = new wxStaticBoxSizer( new wxStaticBox(panel, wxID_ANY, _T("Operation log")), wxHORIZONTAL );
447  mysizer->Add(myhsizer, wxALL, wxEXPAND | wxALL, 5);
448  textLog = new wxTextCtrl( panel, TEXTBOX1, _T(""), wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE, wxDefaultValidator, wxTextCtrlNameStr);
449  myhsizer->Add(textLog, wxALL, wxEXPAND | wxALL, 5);
450  quit = new wxButton( panel, QUIT, _T("QUIT"));
451  mysizer->Add(quit, 0, wxALL, 5);
452  
453  for (int i= 0; i < 28; i++)
454         for (int j = 0; j < 44; j++)                                    
455                 y[i][j] = 0;
456  m_timer.Start(100);
457  if(to_start) 
458  {
459     Start();
460  }
461 }
462
463 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
464 {
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   }
482 }
483
484 void MyFrame::OnStart(wxCommandEvent& WXUNUSED(event))
485 {       
486  wxString       s;
487         
488  node_id_ext = node_id->GetValue();
489  s = busname->GetValue();
490  strncpy(SlaveBoard.busname, s.mb_str(), MAXLENGTH_BUSNAME);
491  *textLog << wxT("-- Bus name: ------> ") << s << wxT("\n");
492  *textLog << wxT("-- Node ID: -------> ") << node_id->GetValue() << wxT("\n");
493  Start();
494 }
495
496
497 void MyFrame::OnStop(wxCommandEvent& WXUNUSED(event))
498 {
499         
500         *textLog << wxT("----------------------------------------------------Slave [STOPPED]") << wxT("\n");
501         stop_slave();
502         stop->Enable(false);
503         start->Enable(true);
504 }
505
506 void MyFrame::OnLoad(wxCommandEvent& WXUNUSED(event))
507 {
508     wxFileDialog fd(this, wxT("Choose a node configuration file"), wxT(""), wxT("Slave.od"), wxT("*.so"));
509
510     if(fd.ShowModal() == wxID_OK)
511     {
512         drivername->Clear();
513         drivername->AppendText(fd.GetPath()); 
514         *textLog << wxT("LibraryPath: ") << fd.GetPath() << wxT("\n");
515         strncpy(LibraryPath, fd.GetPath().mb_str(), MAXLENGTH_LIBPATH);
516         printf("path=%s",LibraryPath);
517     }
518 }
519
520 void MyFrame::OnInbt1(wxCommandEvent& WXUNUSED(event))
521 {       
522  Read_Inputs_8_Bit[0] = Read_Inputs_8_Bit[0] ^ 0x1;
523 }
524 void MyFrame::OnInbt2(wxCommandEvent& WXUNUSED(event))
525 {       
526  Read_Inputs_8_Bit[0] = Read_Inputs_8_Bit[0] ^ 0x2;
527 }
528 void MyFrame::OnInbt3(wxCommandEvent& WXUNUSED(event))
529 {       
530  Read_Inputs_8_Bit[0] = Read_Inputs_8_Bit[0] ^ 0x4;
531 }
532 void MyFrame::OnInbt4(wxCommandEvent& WXUNUSED(event))
533 {       
534  Read_Inputs_8_Bit[0] = Read_Inputs_8_Bit[0] ^ 0x8;
535 }
536 void MyFrame::OnInbt5(wxCommandEvent& WXUNUSED(event))
537 {       
538  Read_Inputs_8_Bit[0] = Read_Inputs_8_Bit[0] ^ 0x10;
539 }
540 void MyFrame::OnInbt6(wxCommandEvent& WXUNUSED(event))
541 {       
542  Read_Inputs_8_Bit[0] = Read_Inputs_8_Bit[0] ^ 0x20;
543 }
544 void MyFrame::OnInbt7(wxCommandEvent& WXUNUSED(event))
545 {       
546  Read_Inputs_8_Bit[0] = Read_Inputs_8_Bit[0] ^ 0x40;
547 }
548 void MyFrame::OnInbt8(wxCommandEvent& WXUNUSED(event))
549 {       
550  Read_Inputs_8_Bit[0] = Read_Inputs_8_Bit[0] ^ 0x80;
551 }
552
553
554
555 void MyFrame::OnIns1(wxScrollEvent &event)
556 {
557         Read_Analogue_Input_16_Bit[0] = ins1->GetValue();
558         inst1->SetValue(ins1->GetValue());
559 }
560 void MyFrame::OnIns2(wxScrollEvent &event)
561 {
562         Read_Analogue_Input_16_Bit[1] = ins2->GetValue();
563         inst2->SetValue(ins2->GetValue());
564 }
565 void MyFrame::OnIns3(wxScrollEvent &event)
566 {
567         Read_Analogue_Input_16_Bit[2] = ins3->GetValue();
568         inst3->SetValue(ins3->GetValue());
569 }
570 void MyFrame::OnIns4(wxScrollEvent &event)
571 {
572         Read_Analogue_Input_16_Bit[3] = ins4->GetValue();
573         inst4->SetValue(ins4->GetValue());
574 }
575 void MyFrame::OnIns5(wxScrollEvent &event)
576 {
577         Read_Analogue_Input_16_Bit[4] = ins5->GetValue();
578         inst5->SetValue(ins5->GetValue());
579 }
580 void MyFrame::OnIns6(wxScrollEvent &event)
581 {
582         Read_Analogue_Input_16_Bit[5] = ins6->GetValue();
583         inst6->SetValue(ins6->GetValue());
584 }
585 void MyFrame::OnIns7(wxScrollEvent &event)
586 {
587         Read_Analogue_Input_16_Bit[6] = ins7->GetValue();
588         inst7->SetValue(ins7->GetValue());
589 }
590 void MyFrame::OnIns8(wxScrollEvent &event)
591 {
592         Read_Analogue_Input_16_Bit[7] = ins8->GetValue();
593         inst8->SetValue(ins8->GetValue());
594 }
595
596
597 void MyFrame::OnInst1 ( wxSpinEvent &ev )
598 {
599         Read_Analogue_Input_16_Bit[0] = inst1->GetValue();
600         ins1->SetValue(inst1->GetValue());      
601 }
602 void MyFrame::OnInst2 ( wxSpinEvent &ev )
603 {
604         Read_Analogue_Input_16_Bit[1] = inst2->GetValue();
605         ins2->SetValue(inst2->GetValue());      
606 }
607 void MyFrame::OnInst3 ( wxSpinEvent &ev )
608 {
609         Read_Analogue_Input_16_Bit[2] = inst3->GetValue();
610         ins3->SetValue(inst3->GetValue());      
611 }
612 void MyFrame::OnInst4 ( wxSpinEvent &ev )
613 {
614         Read_Analogue_Input_16_Bit[3] = inst4->GetValue();
615         ins4->SetValue(inst4->GetValue());      
616 }
617 void MyFrame::OnInst5 ( wxSpinEvent &ev )
618 {
619         Read_Analogue_Input_16_Bit[4] = inst5->GetValue();
620         ins5->SetValue(inst5->GetValue());      
621 }
622 void MyFrame::OnInst6 ( wxSpinEvent &ev )
623 {
624         Read_Analogue_Input_16_Bit[5] = inst6->GetValue();
625         ins6->SetValue(inst6->GetValue());      
626 }
627 void MyFrame::OnInst7 ( wxSpinEvent &ev )
628 {
629         Read_Analogue_Input_16_Bit[6] = inst7->GetValue();
630         ins7->SetValue(inst7->GetValue());      
631 }
632 void MyFrame::OnInst8 ( wxSpinEvent &ev )
633 {
634         Read_Analogue_Input_16_Bit[7] = inst8->GetValue();
635         ins8->SetValue(inst8->GetValue());      
636 }
637
638
639 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
640 {
641  wxString msg;
642  msg.Printf( _T("About.\n\n")
643     _T("Input-Output simulation by GRANDEMANGE Nicolas for CanFestival!!"));
644
645  wxMessageBox(msg, _T("About !!!"), wxOK | wxICON_INFORMATION, this);
646 }
647
648
649 void MyFrame::OnFreqBoxClick( wxCommandEvent &event )
650 {
651         wxString        wxstr;
652         
653         wxstr = event.GetString();
654         strncpy(SlaveBoard.baudrate, wxstr.mb_str(),MAXLENGTH_BAUDRATE);
655         *textLog << wxT("Baudrate:") << event.GetString() << wxT("\n");
656 }
657
658 static int      is_set(int                      i,
659                                         wxListBox       *inlist,
660                                         wxArrayInt      in)
661 {
662         inlist->GetSelections(in);
663         for (int j = 0; j < in.GetCount(); j++)
664                 {
665                         if (i == (in[j] + 1))
666                         {
667                                 return (i);
668                         }
669                 }
670         return (0);
671 }
672
673 static int      get_bit(UNS8    input, int bit)
674 {
675     return input & (((UNS8)1) <<  bit-1) ? 1 : 0; 
676 }
677
678 void MyFrame::Paint()
679 {
680         double          vmax = old_max;
681         double          vmin = old_min;
682         double          vdelta = vmax - vmin;
683         double          vech = ((float)vdelta) / 10.0;
684         
685         double          hmax = frame->echelle->GetValue();
686         double          hech = hdelta / 11;
687         
688         double          d = 300 / vdelta;
689         double          g = 0;
690         double          top = 5;
691         double          left = 50;
692
693         double          tmpi = 0;
694         wxString        tmps;
695         double          p = 0;
696         double          q = 0;
697         int                     i = 0;
698         int                     j = 0;
699         wxColor         col[8] = {wxColor(wxT("BLUE")), wxColor(wxT("RED")), wxColor(wxT("GREEN")), wxColor(wxT("GREY")),
700                                                 wxColor(wxT("ORANGE")), wxColor(wxT("YELLOW")), wxColor(wxT("PINK")), wxColor(wxT("BLACK"))};
701         hdelta = hmax - 0;
702         g = (Gtime / hdelta) * 44.00;
703                                                 
704         wxClientDC MonDc(mygraphpan);   
705         MonDc.Clear();
706         
707     MonDc.SetPen(wxPen(wxColour(200,200,200), 5, wxSOLID));
708     MonDc.DrawRectangle((int) left, (int)top, 660, 300);
709
710     MonDc.SetPen(wxPen(wxColor(150,200,150), 2, wxSOLID));
711     MonDc.SetFont(wxFont(9, wxSWISS , wxNORMAL, wxNORMAL, false, wxT("Arial Black")));
712     for (i=0; i < 11; i++)    
713         {
714                         tmpi = (vmin + (vech * i));
715                         tmps.Printf(wxT("%d"), (int)tmpi);
716                 MonDc.DrawRotatedText(tmps, 0, (int)(293 - (30 * i) + top), 0);
717         }
718         for (i=0; i < 12; i++)    
719         {
720                 tmpi = (hech * i * 100);
721                 if (tmpi < 1000)
722                                 tmps.Printf(wxT("%ims"), (int)tmpi);
723                 if (tmpi > 1000)
724                                 tmps.Printf(wxT("%is"), (int)(tmpi / 1000));
725                 MonDc.DrawRotatedText(tmps, (int)((60 * i) + left) - 10, (int)(317 + top), 0);
726         }
727     for (j=1; j < 10; j++)    
728                 MonDc.DrawLine((int)left, (int)(300 - (30 * j) + top), (int)(left + 660), (int)(300 - (30 * j) + top));
729     MonDc.SetPen(wxPen(wxColor(150,200,150), 2, wxSOLID));
730     for (j=1; j < 22; j++)    
731                 MonDc.DrawLine((int)(30 * j + left), (int)(300 + top), (int)(30 * j + left), (int)(top));
732           MonDc.SetPen(wxPen(wxColor(150,170,150), 1, wxSOLID));
733         for (j=1; j < 22; j++)    
734         MonDc.DrawLine((int)(30 * j + left - 15), (int)(300 + top), (int)(30 * j + left - 15), (int)(top));
735     MonDc.DrawLine((int)(30 * 22 + left - 15), (int)(300 + top), (int)(30 * 22 + left - 15), (int)(top));
736     
737     double m = (- vmin) * d;
738         MonDc.SetPen(wxPen(wxColor(170,170,150), 3, wxSOLID));
739         MonDc.DrawLine((int)(left), (int)(300 + top - m), (int)(left + 660), (int)(300 + top - m));
740
741         wxClientDC MaLeg(mylegpan);
742         MaLeg.Clear();
743         
744     MaLeg.SetPen(wxPen(wxColour(150,200,100), 2, wxSOLID));
745     MaLeg.SetFont(wxFont(7, wxSWISS , wxNORMAL, wxNORMAL, false, wxT("Arial Black")));
746     for (int    k=1, tmpi = 0; k < 30; k++)  
747     {
748         if ((tmpi < 8) && is_set(k, inlist, in))
749                         {
750                                 if (k <= 4)
751                                         tmps.Printf(wxT("Analogue Output %d"), k);
752                                 if ((k > 4) && (k <= 12))
753                                         tmps.Printf(wxT("Bool Output %d"), k - 4);
754                                 if ((k > 12) && (k <= 20))
755                                         tmps.Printf(wxT("Analogue Input %d"), k - 12);
756                                 if ((k > 20) && (k <= 28))
757                                         tmps.Printf(wxT("Bool Input %d"), k - 20);
758                                 
759                                 MaLeg.DrawRotatedText(tmps, 0, 10 * tmpi , 0);
760                                 MaLeg.SetPen(wxPen(col[tmpi], 4, wxSOLID));
761                                 MaLeg.DrawLine(100, 10 * tmpi + 6, 115, 10 * tmpi + 6);
762         //////////////////////////////////////////DAW////////////////////////////////////////
763                                 MonDc.SetPen(wxPen(col[tmpi], 4, wxSOLID));
764                                 if (k <= 4)
765                                         y[k - 1][(int)g] = Write_Analogue_Output_16_Bit[k - 1];
766                                 if ((k > 4) && (k <= 12))
767                                         y[k - 1][(int)g] = get_bit(Write_Outputs_8_Bit[0] ,k - 4);
768                                 if ((k > 12) && (k <= 20))
769                                         y[k - 1][(int)g] = Read_Analogue_Input_16_Bit[k - 1 -12];
770                                 if ((k > 20) && (k <= 28))
771                                         y[k - 1][(int)g] = get_bit(Read_Inputs_8_Bit[0] ,k - 20);
772                                         
773                                 if (y[k - 1][(int)g] > old_max)
774                                         old_max = y[k - 1][(int)g];
775                                 if (y[k - 1][(int)g] < old_min)
776                                         old_min = y[k - 1][(int)g];
777                             for (j=1; j < (g + 1) ; j++)    
778                         {                               
779                                         p = (y[k - 1][j - 1] - vmin) * d;
780                                         q = (y[k - 1][j    ] - vmin) * d;
781                                 MonDc.DrawLine((int)(15 * (j - 1) + left), (int)(300 + top - p), (int)(15 * j + left), (int)(300 + top - q));
782                         }       
783         /////////////////////////////////////////////////////////////////////////////////////
784                                 tmpi++;
785                         }
786     }
787 }
788
789 void actu_output(void)
790 {
791         wxString        tmp;
792         
793         frame->outbt1->SetValue(get_bit(Write_Outputs_8_Bit[0], 1));
794         frame->outbt2->SetValue(get_bit(Write_Outputs_8_Bit[0], 2));
795         frame->outbt3->SetValue(get_bit(Write_Outputs_8_Bit[0], 3));
796         frame->outbt4->SetValue(get_bit(Write_Outputs_8_Bit[0], 4));
797         frame->outbt5->SetValue(get_bit(Write_Outputs_8_Bit[0], 5));
798         frame->outbt6->SetValue(get_bit(Write_Outputs_8_Bit[0], 6));
799         frame->outbt7->SetValue(get_bit(Write_Outputs_8_Bit[0], 7));
800         frame->outbt8->SetValue(get_bit(Write_Outputs_8_Bit[0], 8));
801
802         frame->outs1->SetValue(Write_Analogue_Output_16_Bit[0] + 32768);
803         frame->outs2->SetValue(Write_Analogue_Output_16_Bit[1] + 32768);
804         frame->outs3->SetValue(Write_Analogue_Output_16_Bit[2] + 32768);
805         frame->outs4->SetValue(Write_Analogue_Output_16_Bit[3] + 32768);
806         tmp.Printf(wxT("       %i"), Write_Analogue_Output_16_Bit[0]);
807         frame->outst1->SetLabel(tmp);
808         tmp.Printf(wxT("       %i"), Write_Analogue_Output_16_Bit[1]);
809         frame->outst2->SetLabel(tmp);
810         tmp.Printf(wxT("       %i"), Write_Analogue_Output_16_Bit[2]);
811         frame->outst3->SetLabel(tmp);
812         tmp.Printf(wxT("       %i"), Write_Analogue_Output_16_Bit[3]);
813         frame->outst4->SetLabel(tmp);
814 }
815
816 void actu(void)
817 {
818         actu_output();
819         
820         Gtime++;
821         if (Gtime > hdelta)
822         {
823                 old_max = 5;
824                 old_min = -5;
825                 Gtime = 0;
826         }
827         frame->Paint();
828 }
829
830 void MyFrame::OnTimer(wxTimerEvent& event)
831 {
832     actu();// do whatever you want to do every second here
833 }
834
835 void MyFrame::OnPaint( wxPaintEvent &event )
836 {
837         //Paint();
838 }