00001
00002
00003
00004
00005
00006
00007
00008
00010
00011
00012 #if defined(__GNUG__) && !defined(__APPLE__)
00013 #pragma implementation "appsettings.h"
00014 #endif
00015
00016
00017 #include "wx/wxprec.h"
00018
00019 #ifdef __BORLANDC__
00020 #pragma hdrstop
00021 #endif
00022
00023 #ifndef WX_PRECOMP
00024 #include "wx/wx.h"
00025 #endif
00026 #include <wx/arrimpl.cpp>
00027 #include <wx/config.h>
00028 #include <wx/confbase.h>
00029 #include <wx/fileconf.h>
00030 #include <wx/filename.h>
00031 #include <wx/dir.h>
00032
00033
00034 #include "appsettings.h"
00035 #include "V1724_board.h"
00036 #include "rpn_handler.h"
00037
00038
00039 #ifndef CAEN_VME_INVALID_HANDLE
00040 #define CAEN_VME_INVALID_HANDLE -1
00041 #endif
00042
00043 AppSettings::AppSettings( void (* scope_refresh)( int, bool)): m_vme_link(0), m_vme_board_num(0), m_vme_handle( CAEN_VME_INVALID_HANDLE),
00044 ScopeRefresh( scope_refresh), m_mutex( wxMUTEX_RECURSIVE)
00045 {
00046
00047 wxString setting_filename= wxGetCwd()+ wxFileName::GetPathSeparator()+ _("settings.txt");
00048 if( wxTheApp->argc>= 2)
00049 setting_filename= wxGetCwd()+ wxFileName::GetPathSeparator()+ wxTheApp->argv[ 1];
00050
00051 wxConfigBase::Set( new wxFileConfig( wxEmptyString, wxEmptyString, setting_filename, setting_filename, wxCONFIG_USE_LOCAL_FILE ));
00052
00053 for( int i= 0; i< SCOPE_NUM_PANELS; i++)
00054 {
00055 m_grid_pen[ i]= NULL;
00056 m_mid_grid_pen[ i]= NULL;
00057 m_back_brush[ i]= NULL;
00058 }
00059 }
00060
00061 AppSettings::~AppSettings(void)
00062 {
00063 delete wxConfigBase::Set((wxConfigBase *) NULL);
00064
00065
00066 for( size_t i= 0; i< this->m_board_array.GetCount(); i++)
00067 {
00068 delete (GenericBoard*)this->m_board_array[i];
00069 }
00070 this->m_board_array.Clear();
00071
00072
00073 this->EndVME();
00074
00075 for( int i= 0; i< SCOPE_NUM_PANELS; i++)
00076 {
00077 if( this->m_grid_pen[ i]!= NULL)
00078 {
00079 delete this->m_grid_pen[ i];
00080 this->m_grid_pen[ i]= NULL;
00081 }
00082 if( this->m_mid_grid_pen[ i]!= NULL)
00083 {
00084 delete this->m_mid_grid_pen[ i];
00085 this->m_mid_grid_pen[ i]= NULL;
00086 }
00087 if( this->m_back_brush[ i]!= NULL)
00088 {
00089 delete this->m_back_brush[ i];
00090 this->m_back_brush[ i]= NULL;
00091 }
00092 }
00093 }
00097 bool AppSettings::Load( void)
00098 {
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 unsigned int i;
00166
00167 this->m_board_array.Clear();
00168 wxConfigBase* config= wxConfigBase::Get( false);
00169 if( !config)
00170 {
00171 wxLogError( _("Cannot get configuration file\n"));
00172 return false;
00173 }
00174
00175 wxMutexLocker lock( this->m_mutex);
00176
00177
00178
00179 config->Read( _("/RECORD/RECORD_FOLDER"), &this->m_record_folder, "./");
00180
00181 if( !wxDir::Exists( this->m_record_folder))
00182 {
00183
00184 this->m_record_folder= "./";
00185 }
00186
00187
00188
00189 int trigger_msk= 0;
00190 config->Read( _("/TRIGGER/TRIGGER_MSK"), &trigger_msk, 0);
00191 this->m_trigger_msk= (AppSettings::TRIGGER_MSK)trigger_msk;
00192
00193
00194
00195 this->m_post_trigger= 0;
00196 config->Read( _("/TRIGGER/POST_TRIGGER"), &this->m_post_trigger, 0);
00197
00198
00199
00200 int tmp;
00201 config->Read( _("/TRIGGER/TRIGGER_POSITION_KEEP"), &tmp, 0);
00202 this->m_trigger_position_keep= tmp? true: false;
00203
00204
00205
00206
00207 int edge_type;
00208 config->Read( _("/TRIGGER/TRIGGER_EDGE"), &edge_type, (int)NO_TRIGGER);
00209 this->m_trigger_edge_type= (TRIGGER_EDGE_TYPE)edge_type;
00210
00211
00212
00213 this->m_sample_block_size= 0;
00214 config->Read( _("/TRIGGER/SAMPLE_BLOCK_SIZE"), &this->m_sample_block_size, 0);
00215
00216
00217
00218 config->Read( _("/MISC/VME_BOARD_TYPE"), &this->m_vme_board_type_string, _("V1718"));
00219
00220
00221 config->Read( _("/MISC/VME_LINK"), &this->m_vme_link, 0);
00222
00223
00224 config->Read( _("/MISC/VME_BOARD_NUM"), &this->m_vme_board_num, 0);
00225
00226
00227
00228 config->Read( _("/MISC/STARTED"), &tmp, 0);
00229 this->m_started= tmp? true: false;
00230
00231
00232
00233 config->Read( _("/MISC/SHOW_LEFT_PANE"), &tmp, 0);
00234 this->m_show_left_pane= tmp? true: false;
00235
00236
00237
00238 config->Read( _("/MISC/SHOW_BOTTOM_PANE"), &tmp, 0);
00239 this->m_show_bottom_pane= tmp? true: false;
00240
00241
00242
00243 config->Read( _("/MISC/SHOW_SCOPE_0_PANE"), &tmp, 0);
00244 this->m_show_scope_left_top_pane= tmp? true: false;
00245
00246
00247
00248 config->Read( _("/MISC/SHOW_SCOPE_1_PANE"), &tmp, 0);
00249 this->m_show_scope_right_top_pane= tmp? true: false;
00250
00251
00252
00253 config->Read( _("/MISC/SHOW_SCOPE_2_PANE"), &tmp, 0);
00254 this->m_show_scope_left_bottom_pane= tmp? true: false;
00255
00256
00257
00258 config->Read( _("/MISC/SHOW_SCOPE_3_PANE"), &tmp, 0);
00259 this->m_show_scope_right_bottom_pane= tmp? true: false;
00260
00261 for( int scope_index= 0; scope_index< SCOPE_NUM_PANELS; scope_index++)
00262 {
00263 wxString scope_index_string= wxString::Format( "_%d", scope_index);
00264
00265
00266 wxString grid_line_color_rgb_string= _("");
00267 config->Read( _("/MISC/GRID_LINE_COLOR")+ scope_index_string, &grid_line_color_rgb_string, _("0xffffff"));
00268 unsigned long grid_line_color_rgb;
00269 wxString hex_part;
00270 if( grid_line_color_rgb_string.Upper().StartsWith(_("0X"), &hex_part))
00271 {
00272
00273 if( !hex_part.Trim().ToULong( &grid_line_color_rgb, 16))
00274 {
00275 grid_line_color_rgb= 0x00ffffff;
00276 }
00277 }
00278 else
00279 {
00280
00281 if( !grid_line_color_rgb_string.Trim().ToULong( &grid_line_color_rgb, 10))
00282 {
00283 grid_line_color_rgb= 0x00ffffff;
00284 }
00285 }
00286 this->m_grid_line_color[ scope_index].Set( (grid_line_color_rgb>> 16)& 0xff, (grid_line_color_rgb>> 8)& 0xff, grid_line_color_rgb& 0xff);
00287
00288
00289
00290 int grid_line_type;
00291 config->Read( _("/MISC/GRID_LINE_TYPE")+ scope_index_string, &grid_line_type, 1);
00292 this->m_grid_line_type[ scope_index]= (GRID_LINE_TYPE)grid_line_type;
00293
00294
00295
00296 config->Read( _("/MISC/GRID_LINE_WIDTH")+ scope_index_string, &this->m_grid_line_width[ scope_index], 1);
00297
00298
00299
00300 config->Read( _("/MISC/OFFSET_SEC")+ scope_index_string, &this->m_offset_sec[ scope_index], 0.0);
00301
00302
00303
00304 config->Read( _("/MISC/SEC_2_DIV")+ scope_index_string, &this->m_sec_2_div[ scope_index], 1000.0);
00305 this->SetSec2Div( scope_index, this->m_sec_2_div[ scope_index]);
00306
00307
00308
00309 wxString back_color_rgb_string= _("");
00310 config->Read( _("/MISC/BACKGROUND_COLOR")+ scope_index_string, &back_color_rgb_string, _("0x000000"));
00311 unsigned long back_color_rgb;
00312 if( back_color_rgb_string.Upper().StartsWith(_("0X"), &hex_part))
00313 {
00314
00315 if( !hex_part.Trim().ToULong( &back_color_rgb, 16))
00316 {
00317 back_color_rgb= 0x00ffffff;
00318 }
00319 }
00320 else
00321 {
00322
00323 if( !back_color_rgb_string.Trim().ToULong( &back_color_rgb, 10))
00324 {
00325 back_color_rgb= 0x00ffffff;
00326 }
00327 }
00328 this->m_background_color[ scope_index].Set( (back_color_rgb>> 16)& 0xff, (back_color_rgb>> 8)& 0xff, back_color_rgb& 0xff);
00329 }
00330
00331
00332 config->Read( _("/MISC/INT_RATE_MHZ"), &this->m_int_rate_MHz, 100000000);
00333
00334
00335 config->Read( _("/MISC/EXT_RATE_MHZ"), &this->m_ext_rate_MHz, 100000000);
00336
00337
00338 config->Read( _("/MISC/USE_EXT_CLOCK"), &tmp, 0);
00339 this->m_use_ext_clock= tmp? true: false;
00340
00341 this->SetClock( this->m_use_ext_clock, this->m_ext_rate_MHz);
00342
00343
00344
00345 config->Read( _("/MISC/USE_TTL"), &tmp, 0);
00346 this->m_use_TTL= tmp? true: false;
00347
00348
00349
00350 config->Read( _("/MISC/MAX_LOG_X"), &this->m_max_log_X, 1024);
00351
00352
00353 int acq_board_num= 0;
00354 config->Read( _("/MISC/ACQ_BOARD_NUM"), &acq_board_num, 0);
00355
00356 this->InitVME();
00357 wxString board_base_string= _("/ACQ_BOARDS/");
00358
00359
00360 for( i= 0; i< (unsigned int)acq_board_num; i++)
00361 {
00362 wxString board_string= wxString::Format( "%s%i/", board_base_string.c_str(), i);
00363
00364
00365 wxString board_type= _("");
00366 config->Read( board_string+ _("BOARD_TYPE"), &board_type, _("V1724"));
00367 if( !board_type.Trim().CmpNoCase( _("V1724")))
00368 {
00369
00370 this->m_board_array.Add( new V1724Board( i, this->ScopeRefresh, &this->m_mutex, this));
00371 }
00372 else
00373 {
00374
00375 wxLogError( wxString::Format( "Unknown board type '%s'\n", board_type.c_str()));
00376
00377 }
00378
00379
00380 if( !((GenericBoard*)this->m_board_array[i])->Open( this->m_vme_handle ))
00381 {
00382 wxLogError( _("Board open failure\n"));
00383
00384 }
00385
00386 if( !((GenericBoard*)this->m_board_array[i])->LoadConfig( config, board_string))
00387 {
00388 wxLogError( _("Error loading board settings\n"));
00389
00390 }
00391
00392 if( !((GenericBoard*)this->m_board_array[i])->Init( this->m_vme_handle))
00393 {
00394 wxLogError( _("Board init failure\n"));
00395
00396 }
00397 }
00398
00399 return true;
00400 }
00401
00405 bool AppSettings::Save( void)
00406 {
00407 wxConfigBase* config= wxConfigBase::Get( false);
00408 if( !config)
00409 return false;
00410 wxMutexLocker lock( this->m_mutex);
00411
00412
00413
00414 wxString group_id;
00415 long group_index;
00416 while( config->GetFirstGroup( group_id, group_index))
00417 {
00418 config->DeleteGroup( group_id);
00419 }
00420
00421
00422 if( !config->Write( _("/RECORD/RECORD_FOLDER"), this->m_record_folder))
00423 return false;
00424
00425
00426
00427 if( !config->Write( _("/TRIGGER/TRIGGER_MSK"), (int)this->m_trigger_msk))
00428 return false;
00429
00430
00431
00432 if( !config->Write( _("/TRIGGER/POST_TRIGGER"), this->m_post_trigger))
00433 return false;
00434
00435
00436
00437 config->Write( _("/TRIGGER/TRIGGER_POSITION_KEEP"), this->m_trigger_position_keep? 1: 0);
00438
00439
00440
00441 if( !config->Write( _("/TRIGGER/TRIGGER_EDGE"), (int)this->m_trigger_edge_type))
00442 return false;
00443
00444
00445
00446 if( !config->Write( _("/TRIGGER/SAMPLE_BLOCK_SIZE"), this->m_sample_block_size))
00447 return false;
00448
00449
00450
00451 if( !config->Write( _("/MISC/VME_BOARD_TYPE"), this->m_vme_board_type_string))
00452 return false;
00453
00454
00455 if( !config->Write( _("/MISC/VME_LINK"), this->m_vme_link))
00456 return false;
00457
00458
00459 if( !config->Write( _("/MISC/VME_BOARD_NUM"), this->m_vme_board_num))
00460 return false;
00461
00462
00463
00464 config->Write( _("/MISC/STARTED"), this->m_started? 1: 0);
00465
00466
00467
00468 config->Write( _("/MISC/SHOW_LEFT_PANE"), this->m_show_left_pane? 1: 0);
00469
00470
00471
00472 config->Write( _("/MISC/SHOW_BOTTOM_PANE"), this->m_show_bottom_pane? 1: 0);
00473
00474
00475
00476 config->Write( _("/MISC/SHOW_SCOPE_0_PANE"), m_show_scope_left_top_pane? 1: 0);
00477
00478
00479
00480 config->Write( _("/MISC/SHOW_SCOPE_1_PANE"), m_show_scope_right_top_pane? 1: 0);
00481
00482
00483
00484 config->Write( _("/MISC/SHOW_SCOPE_2_PANE"), m_show_scope_left_bottom_pane? 1: 0);
00485
00486
00487
00488 config->Write( _("/MISC/SHOW_SCOPE_3_PANE"), m_show_scope_right_bottom_pane? 1: 0);
00489
00490 for( int scope_index= 0; scope_index< SCOPE_NUM_PANELS; scope_index++)
00491 {
00492 wxString scope_index_string= wxString::Format( "_%d", scope_index);
00493
00494
00495
00496 wxString grid_line_color_rgb_string= wxString::Format( "0x%02x%02x%02x", this->m_grid_line_color[ scope_index].Red(), this->m_grid_line_color[ scope_index].Green(), this->m_grid_line_color[ scope_index].Blue());
00497 if( !config->Write( _("/MISC/GRID_LINE_COLOR")+ scope_index_string, grid_line_color_rgb_string))
00498 return false;
00499
00500
00501
00502 if( !config->Write( _("/MISC/GRID_LINE_TYPE")+ scope_index_string, (int)this->m_grid_line_type[ scope_index]))
00503 return false;
00504
00505
00506
00507 if( !config->Write( _("/MISC/GRID_LINE_WIDTH")+ scope_index_string, this->m_grid_line_width[ scope_index]))
00508 return false;
00509
00510
00511
00512 if( !config->Write( _("/MISC/OFFSET_SEC")+ scope_index_string, this->m_offset_sec[ scope_index]))
00513 return false;
00514
00515
00516
00517 if( !config->Write( _("/MISC/SEC_2_DIV")+ scope_index_string, this->m_sec_2_div[ scope_index]))
00518 return false;
00519
00520
00521
00522 wxString back_color_rgb_string= wxString::Format( "0x%02x%02x%02x", this->m_background_color[ scope_index].Red(), this->m_background_color[ scope_index].Green(), this->m_background_color[ scope_index].Blue());
00523 if( !config->Write( _("/MISC/BACKGROUND_COLOR")+ scope_index_string, back_color_rgb_string))
00524 return false;
00525 }
00526
00527
00528 if( !config->Write( _("/MISC/INT_RATE_MHZ"), this->m_int_rate_MHz))
00529 return false;
00530
00531
00532
00533 if( !config->Write( _("/MISC/EXT_RATE_MHZ"), this->m_ext_rate_MHz))
00534 return false;
00535
00536
00537 if( !config->Write( _("/MISC/USE_EXT_CLOCK"), this->m_use_ext_clock? 1: 0))
00538 return false;
00539
00540
00541
00542 if( !config->Write( _("/MISC/USE_TTL"), this->m_use_TTL? 1: 0))
00543 return false;
00544
00545
00546
00547 if( !config->Write( _("/MISC/MAX_LOG_X"), this->m_max_log_X))
00548 return false;
00549
00550
00551
00552 if( !config->Write( _("/MISC/ACQ_BOARD_NUM"), (int)this->m_board_array.GetCount()))
00553 return false;
00554
00555 wxString board_base_string= _("/ACQ_BOARDS/");
00556
00557
00558 for( size_t i= 0; i< this->m_board_array.GetCount(); i++)
00559 {
00560 wxString board_string= wxString::Format( "%s%i/", board_base_string.c_str(), i);
00561
00562
00563 if( !config->Write( board_string+ _("BOARD_TYPE"), ((GenericBoard*)this->m_board_array[i])->GetType()))
00564 return false;
00565
00566
00567 if( !((GenericBoard*)this->m_board_array[i])->SaveConfig( config, board_string))
00568 return false;
00569 }
00570
00571 if( !config->Flush())
00572 return false;
00573 return true;
00574 }
00575 bool AppSettings::InitVME( void)
00576 {
00577
00578 CVBoardTypes vme_board_type;
00579 if( !this->m_vme_board_type_string.CmpNoCase(_("V1718")))
00580 {
00581 vme_board_type= cvV1718;
00582 }
00583 else if( !this->m_vme_board_type_string.CmpNoCase(_("V2718")))
00584 {
00585 vme_board_type= cvV2718;
00586 }
00587 else
00588 {
00589 wxLogError( wxString::Format( _("Unhandled board type: %s"), this->m_vme_board_type_string.c_str()));
00590 return false;
00591 }
00592 CVErrorCodes cv_error_code;
00593 if( ( cv_error_code= CAENVME_Init( vme_board_type, this->m_vme_link, this->m_vme_board_num, &this->m_vme_handle))!= cvSuccess)
00594 {
00595 wxLogError( wxString::Format( _("VME Init failure board type= %s link= %i board num= %i error= %s"), this->m_vme_board_type_string.c_str(), this->m_vme_link, this->m_vme_board_num, CAENVME_DecodeError( cv_error_code) ));
00596 return false;
00597 }
00598 return true;
00599 }
00600 bool AppSettings::EndVME( void)
00601 {
00602 CVErrorCodes cv_error_code;
00603 if( ( cv_error_code= CAENVME_End( this->m_vme_handle))!= cvSuccess)
00604 {
00605 wxLogError( wxString::Format( _("VME End failure board type= %s link= %i board num= %i error= %s\n"), this->m_vme_board_type_string.c_str(), this->m_vme_link, this->m_vme_board_num, CAENVME_DecodeError( cv_error_code)));
00606 return false;
00607 }
00608 this->m_vme_handle= CAEN_VME_INVALID_HANDLE;
00609 return true;
00610 }
00611
00612 bool AppSettings::SetGridPen( int scope_index, const wxPen* grid_pen, const wxPen* mid_grid_pen)
00613 {
00614 if( this->m_grid_pen[ scope_index]!= NULL)
00615 {
00616 delete this->m_grid_pen[ scope_index];
00617 this->m_grid_pen[ scope_index]= NULL;
00618 }
00619 this->m_grid_pen[ scope_index]= ( grid_pen== NULL)? NULL: new wxPen( *grid_pen);
00620
00621 if( this->m_mid_grid_pen[ scope_index]!= NULL)
00622 {
00623 delete this->m_mid_grid_pen[ scope_index];
00624 this->m_mid_grid_pen[ scope_index]= NULL;
00625 }
00626 if( mid_grid_pen== NULL)
00627 {
00628 this->m_mid_grid_pen[ scope_index]= ( grid_pen== NULL)? NULL: new wxPen( *grid_pen);
00629 }
00630 else
00631 {
00632 this->m_mid_grid_pen[ scope_index]= new wxPen( *mid_grid_pen);
00633 }
00634 return true;
00635 }
00636
00637 bool AppSettings::SetBackBrush( int scope_index, const wxBrush* back_brush)
00638 {
00639 if( this->m_back_brush[ scope_index]!= NULL)
00640 {
00641 delete this->m_back_brush[ scope_index];
00642 this->m_back_brush[ scope_index]= NULL;
00643 }
00644 this->m_back_brush[ scope_index]= ( back_brush== NULL)? NULL: new wxBrush( *back_brush);
00645 return true;
00646 }
00647
00648 void AppSettings::SetDiv2Pix( int scope_index, double div_2_pix_X)
00649 {
00650 wxMutexLocker lock( this->m_mutex);
00651
00652 this->m_div_2_pix_X[ scope_index]= div_2_pix_X;
00653 this->m_sec_2_pix[ scope_index]= this->m_sec_2_div[ scope_index]* this->m_div_2_pix_X[ scope_index];
00654 }
00655
00656 void AppSettings::SetSec2Div( int scope_index, double sec_2_div)
00657 {
00658 if( sec_2_div< 0.0000001)
00659 sec_2_div= 0.0000001;
00660 this->m_sec_2_div[ scope_index]= sec_2_div;
00661
00662 this->m_sec_2_pix[ scope_index]= this->m_sec_2_div[ scope_index]* this->m_div_2_pix_X[ scope_index];
00663 }
00664
00665 void AppSettings::SetSec2Sample( double sec_2_sample)
00666 {
00667 if( sec_2_sample< 0.0000000001)
00668 sec_2_sample= 0.0000000001;
00669 this->m_sec_2_sample= sec_2_sample;
00670
00671 this->m_sample_2_sec= 1/ this->m_sec_2_sample;
00672 }
00673 void AppSettings::SetClock( bool use_ext_clock, int ext_rate_MHz)
00674 {
00675 if( ext_rate_MHz< 1)
00676 ext_rate_MHz= 1;
00677 else if( ext_rate_MHz> 100)
00678 ext_rate_MHz= 100;
00679 this->m_ext_rate_MHz= ext_rate_MHz;
00680
00681 this->m_use_ext_clock= use_ext_clock;
00682
00683 this->SetSec2Sample( this->m_use_ext_clock? (( double) ext_rate_MHz)* 1000000.0: ((double)this->m_int_rate_MHz)* 1000000.0);
00684 }