00001
00002
00003
00004
00005
00006
00007
00008
00009
00011
00012
00013 #if defined(__GNUG__) && !defined(__APPLE__)
00014 #pragma implementation "generic_board.h"
00015 #endif
00016
00017
00018 #include "wx/wxprec.h"
00019
00020 #ifdef __BORLANDC__
00021 #pragma hdrstop
00022 #endif
00023
00024 #ifndef WX_PRECOMP
00025 #include "wx/wx.h"
00026 #endif
00027
00028 #include "generic_board.h"
00029 #include "appsettings.h"
00030 #include "boardworkerthread.h"
00031 #include "record_ch_control.h"
00032
00033 GenericBoard::GenericBoard( int board_index, void (* scope_refresh)( int, bool), wxMutex *p_data_mutex, AppSettings *p_app_settings):
00034 ScopeRefresh( scope_refresh), m_p_data_mutex( p_data_mutex), m_p_worker_thread(NULL),
00035 m_p_app_settings( p_app_settings), m_board_index( board_index)
00036 {
00037 this->m_p_data= NULL;
00038 this->m_is_running= false;
00039 this->m_is_recording= false;
00040 }
00041
00042 GenericBoard::~GenericBoard( )
00043 {
00044
00045 this->DoStopRunning();
00046
00047
00048 for( size_t i= 0; i< this->m_channel_array.GetCount(); i++)
00049 {
00050 delete (GenericBoardChannel*)this->m_channel_array[i];
00051 }
00052 this->m_channel_array.Clear();
00053
00054 for( size_t i= 0; i< this->m_virtual_channel_array.GetCount(); i++)
00055 {
00056 delete (GenericBoardChannel*)this->m_virtual_channel_array[i];
00057 }
00058 this->m_virtual_channel_array.Clear();
00059
00060 }
00061 bool GenericBoard::LoadConfig( wxConfigBase* p_config, const wxString& base_section)
00062 {
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 if( this->m_p_data== NULL)
00081 return false;
00082
00083
00084 wxString board_add_string= _("");
00085 p_config->Read( base_section+ _("ADDRESS"), &board_add_string, _("0"));
00086 unsigned long board_add= 0;
00087 wxString hex_part;
00088 if( board_add_string.Upper().StartsWith(_("0X"), &hex_part))
00089 {
00090
00091 if( !hex_part.Trim().ToULong( &board_add, 16))
00092 {
00093 board_add= 0;
00094 }
00095 }
00096 else
00097 {
00098
00099 if( !board_add_string.Trim().ToULong( &board_add, 10))
00100 {
00101 board_add= 0;
00102 }
00103 }
00104 this->m_p_data->m_base_address= (UINT16)board_add;
00105
00106
00107
00108 int channel_number= 0;
00109 p_config->Read( base_section+ _("NUM_CHANNEL"), &channel_number, 0);
00110 for( int i= 0; i< channel_number; i++)
00111 {
00112
00113
00114 wxString channel_string= wxString::Format( "%s%i/", base_section.c_str(), i);
00115 this->m_channel_array.Add( this->CreateChannel( i, this->ScopeRefresh, this->m_p_data_mutex));
00116
00117
00118 if( !((GenericBoardChannel*)this->m_channel_array[i])->LoadConfig( p_config, channel_string))
00119 return false;
00120 }
00121
00122
00123 int virtual_channel_number= 0;
00124 p_config->Read( base_section+ _("NUM_VIRTUAL_CHANNEL"), &virtual_channel_number, 0);
00125 for( int i= 0; i< virtual_channel_number; i++)
00126 {
00127
00128
00129 wxString channel_string= wxString::Format( "%sVIRT_%i/", base_section.c_str(), i);
00130 this->m_virtual_channel_array.Add( this->CreateVirtualChannel( i, this->ScopeRefresh, this->m_p_data_mutex));
00131
00132
00133 if( !((GenericBoardChannel*)this->m_virtual_channel_array[i])->LoadConfig( p_config, channel_string))
00134 return false;
00135 }
00136
00137 return true;
00138
00139
00140 }
00141 bool GenericBoard::SaveConfig( wxConfigBase* p_config, const wxString& base_section)
00142 {
00143 if( this->m_p_data== NULL)
00144 return false;
00145
00146
00147 wxString board_add_string= wxString::Format( "0x%04x", this->m_p_data->m_base_address);
00148 if( !p_config->Write( base_section+ _("ADDRESS"), board_add_string))
00149 return false;
00150
00151
00152
00153 if( !p_config->Write( base_section+ _("NUM_CHANNEL"), (int)this->m_channel_array.GetCount()))
00154 return false;
00155 for( size_t i= 0; i< this->m_channel_array.GetCount(); i++)
00156 {
00157
00158
00159 wxString channel_string= wxString::Format( "%s%i/", base_section.c_str(), i);
00160
00161
00162 if( !((GenericBoardChannel*)this->m_channel_array[i])->SaveConfig( p_config, channel_string))
00163 return false;
00164 }
00165
00166
00167
00168 if( !p_config->Write( base_section+ _("NUM_VIRTUAL_CHANNEL"), (int)this->m_virtual_channel_array.GetCount()))
00169 return false;
00170 for( size_t i= 0; i< this->m_virtual_channel_array.GetCount(); i++)
00171 {
00172
00173
00174 wxString channel_string= wxString::Format( "%sVIRT_%i/", base_section.c_str(), i);
00175
00176
00177 if( !((GenericBoardChannel*)this->m_virtual_channel_array[i])->SaveConfig( p_config, channel_string))
00178 return false;
00179 }
00180 return true;
00181 }
00182 bool GenericBoard::DoStartRunning( void)
00183 {
00184 if( this->IsRunning())
00185 return true;
00186
00187
00188 this->m_p_worker_thread= new BoardWorkerThread( this);
00189 switch( this->m_p_worker_thread->Create( ))
00190 {
00191 case wxTHREAD_NO_ERROR:
00192 break;
00193 default:
00194 this->DoStopRunning();
00195 return false;
00196 }
00197
00198 if( !this->DoStartAcquisition())
00199 {
00200 this->DoStopRunning();
00201 }
00202
00203
00204
00205 switch( this->m_p_worker_thread->Run( ))
00206 {
00207 case wxTHREAD_NO_ERROR:
00208 break;
00209 default:
00210 this->DoStopRunning();
00211 return false;
00212 }
00213 this->SetRunning( true);
00214 return true;
00215 }
00216
00217 void GenericBoard::DoStopRunning( void)
00218 {
00219
00220 if( !this->IsRunning())
00221 return;
00222 this->m_thread_exited= false;
00223
00224
00225 this->DoStopAcquisition();
00226
00227
00228 if( this->m_p_worker_thread)
00229 {
00230 switch( this->m_p_worker_thread->Delete( ))
00231 {
00232 case wxTHREAD_NO_ERROR:
00233 while( !this->m_thread_exited)
00234 {
00235
00236 }
00237 break;
00238 default:
00239 this->m_p_worker_thread->Kill();
00240 break;
00241 }
00242 this->m_p_worker_thread= NULL;
00243 this->SetRunning( false);
00244 }
00245 }
00246
00247 void GenericBoard::DrawCursor( int scope_index, wxDC &dc)
00248 {
00249
00250
00251 for( size_t j= 0; j< this->m_channel_array.GetCount(); j++)
00252 {
00253 GenericBoardChannel *board_channel= ( GenericBoardChannel *)this->m_channel_array[ j];
00254 board_channel->DrawCursor( scope_index, dc);
00255 }
00256
00257
00258 for( size_t j= 0; j< this->m_virtual_channel_array.GetCount(); j++)
00259 {
00260 GenericBoardChannel *board_channel= ( GenericBoardChannel *)this->m_virtual_channel_array[ j];
00261 board_channel->DrawCursor( scope_index, dc);
00262 }
00263 }
00264 void GenericBoard::DrawTrigger( int scope_index, wxDC &dc)
00265 {
00266
00267
00268 for( size_t j= 0; j< this->m_channel_array.GetCount(); j++)
00269 {
00270 GenericBoardChannel *board_channel= ( GenericBoardChannel *)this->m_channel_array[ j];
00271 board_channel->DrawTrigger( scope_index, dc);
00272 }
00273
00274
00275 for( size_t j= 0; j< this->m_virtual_channel_array.GetCount(); j++)
00276 {
00277 GenericBoardChannel *board_channel= ( GenericBoardChannel *)this->m_virtual_channel_array[ j];
00278 board_channel->DrawTrigger( scope_index, dc);
00279 }
00280 }
00281 void GenericBoard::DrawPosition( int scope_index, wxDC &dc)
00282 {
00283
00284
00285 for( size_t j= 0; j< this->m_channel_array.GetCount(); j++)
00286 {
00287 GenericBoardChannel *board_channel= ( GenericBoardChannel *)this->m_channel_array[ j];
00288 board_channel->DrawPosition( scope_index, dc);
00289 }
00290
00291
00292 for( size_t j= 0; j< this->m_virtual_channel_array.GetCount(); j++)
00293 {
00294 GenericBoardChannel *board_channel= ( GenericBoardChannel *)this->m_virtual_channel_array[ j];
00295 board_channel->DrawPosition( scope_index, dc);
00296 }
00297 }
00298
00299 void GenericBoard::SetDiv2Pix( int scope_index, double div_2_pix_Y)
00300 {
00301
00302
00303 for( size_t j= 0; j< this->m_channel_array.GetCount(); j++)
00304 {
00305 GenericBoardChannel *board_channel= ( GenericBoardChannel *)this->m_channel_array[ j];
00306 board_channel->SetDiv2Pix( scope_index, div_2_pix_Y);
00307 }
00308
00309
00310 for( size_t j= 0; j< this->m_virtual_channel_array.GetCount(); j++)
00311 {
00312 GenericBoardChannel *board_channel= ( GenericBoardChannel *)this->m_virtual_channel_array[ j];
00313 board_channel->SetDiv2Pix( scope_index, div_2_pix_Y);
00314 }
00315 }
00316
00317 void GenericBoard::SetLeftDiv2Pix( int scope_index, double div_2_pix_Y)
00318 {
00319
00320
00321 for( size_t j= 0; j< this->m_channel_array.GetCount(); j++)
00322 {
00323 GenericBoardChannel *board_channel= ( GenericBoardChannel *)this->m_channel_array[ j];
00324 board_channel->SetLeftDiv2Pix( scope_index, div_2_pix_Y);
00325 }
00326
00327
00328 for( size_t j= 0; j< this->m_virtual_channel_array.GetCount(); j++)
00329 {
00330 GenericBoardChannel *board_channel= ( GenericBoardChannel *)this->m_virtual_channel_array[ j];
00331 board_channel->SetLeftDiv2Pix( scope_index, div_2_pix_Y);
00332 }
00333 }
00334 void GenericBoard::SetPix( int scope_index, int pix_X, int pix_Y)
00335 {
00336
00337
00338 for( size_t j= 0; j< this->m_channel_array.GetCount(); j++)
00339 {
00340 GenericBoardChannel *board_channel= ( GenericBoardChannel *)this->m_channel_array[ j];
00341 board_channel->SetPix( scope_index, pix_X, pix_Y);
00342 }
00343
00344
00345 for( size_t j= 0; j< this->m_virtual_channel_array.GetCount(); j++)
00346 {
00347 GenericBoardChannel *board_channel= ( GenericBoardChannel *)this->m_virtual_channel_array[ j];
00348 board_channel->SetPix( scope_index, pix_X, pix_Y);
00349 }
00350 }
00351 void GenericBoard::SetLeftPix( int scope_index, int pix_X, int pix_Y)
00352 {
00353
00354
00355 for( size_t j= 0; j< this->m_channel_array.GetCount(); j++)
00356 {
00357 GenericBoardChannel *board_channel= ( GenericBoardChannel *)this->m_channel_array[ j];
00358 board_channel->SetLeftPix( scope_index, pix_X, pix_Y);
00359 }
00360
00361
00362 for( size_t j= 0; j< this->m_virtual_channel_array.GetCount(); j++)
00363 {
00364 GenericBoardChannel *board_channel= ( GenericBoardChannel *)this->m_virtual_channel_array[ j];
00365 board_channel->SetLeftPix( scope_index, pix_X, pix_Y);
00366 }
00367 }
00368
00369 void GenericBoard::DrawSamples( int scope_index, wxDC &dc)
00370 {
00371
00372
00373 for( size_t j= 0; j< this->m_channel_array.GetCount(); j++)
00374 {
00375 GenericBoardChannel *board_channel= ( GenericBoardChannel *)this->m_channel_array[ j];
00376 board_channel->DrawSamples( scope_index, dc);
00377 }
00378
00379
00380 for( size_t j= 0; j< this->m_virtual_channel_array.GetCount(); j++)
00381 {
00382 GenericBoardChannel *board_channel= ( GenericBoardChannel *)this->m_virtual_channel_array[ j];
00383 board_channel->DrawSamples( scope_index, dc);
00384 }
00385 }
00386
00387 bool GenericBoard::ToggleRecordStatus( const wxString &path, const wxString &file_name, const wxString ×tamp, bool start)
00388 {
00389 bool ret= true;
00390
00391
00392 wxString new_file_name= wxString::Format( "%sB%02d", file_name.c_str(), this->m_board_index);
00393
00394
00395 for( size_t j= 0; j< this->m_channel_array.GetCount(); j++)
00396 {
00397 GenericBoardChannel *board_channel= ( GenericBoardChannel *)this->m_channel_array[ j];
00398 if( !board_channel->ToggleRecordStatus( path, new_file_name, timestamp, start))
00399 ret= false;
00400 board_channel->m_p_record_ch_control->Enable( !start);
00401 }
00402
00403
00404 for( size_t j= 0; j< this->m_virtual_channel_array.GetCount(); j++)
00405 {
00406 GenericBoardChannel *board_channel= ( GenericBoardChannel *)this->m_virtual_channel_array[ j];
00407 if( !board_channel->ToggleRecordStatus( path, new_file_name, timestamp, start))
00408 ret= false;
00409 board_channel->m_p_record_ch_control->Enable( !start);
00410 }
00411 this->m_is_recording= start;
00412
00413 return ret;
00414 }
00415
00416 bool GenericBoard::IsRunning( void)
00417 {
00418 wxMutexLocker lock( this->m_p_app_settings->m_mutex);
00419 return this->m_is_running;
00420 }
00421
00422 void GenericBoard::SetRunning( bool value)
00423 {
00424 wxMutexLocker lock( this->m_p_app_settings->m_mutex);
00425 this->m_is_running= value;
00426 }
00427 GenericBoardChannel* GenericBoard::CreateVirtualChannel( int ch_index, void (* scope_refresh)( int, bool), wxMutex *p_data_mutex)
00428 {
00429 return new VirtualBoardChannel( this, ch_index, this->m_p_data, scope_refresh, p_data_mutex, this->m_p_app_settings);
00430 }
00431
00432 bool GenericBoard::GetPhysicalChannelValue( int ch_index, int sample_index, double &result)
00433 {
00434 result= 0;
00435 if( ch_index>= (int)this->m_channel_array.GetCount())
00436 return false;
00437 if( sample_index>= (int)((GenericBoardChannel*)this->m_channel_array[ ch_index])->GetBufferCount())
00438 return false;
00439 result= ((GenericBoardChannel*)this->m_channel_array[ ch_index])->GetSample( sample_index);
00440 return true;
00441 }
00442 bool GenericBoard::GetPhysicalChannelValueVolt( int ch_index, int sample_index, double &result)
00443 {
00444 result= 0;
00445 if( ch_index>= (int)this->m_channel_array.GetCount())
00446 return false;
00447 if( sample_index>= (int)((GenericBoardChannel*)this->m_channel_array[ ch_index])->GetBufferCount())
00448 return false;
00449 result= ((GenericBoardChannel*)this->m_channel_array[ ch_index])->GetSampleVolt( sample_index);
00450 return true;
00451 }
00452
00453 bool GenericBoard::GetPhysicalChannelBufferCount( int ch_index, UINT32 &result)
00454 {
00455 result= 0;
00456 if( ch_index>= (int)this->m_channel_array.GetCount())
00457 return false;
00458 result= ((GenericBoardChannel*)this->m_channel_array[ ch_index])->GetBufferCount();
00459 return true;
00460 }
00461
00462
00463
00464