KiCad PCB EDA Suite
regulators_funct.cpp
Go to the documentation of this file.
1 
4 /*
5  * This program source code file is part of KICAD, a free EDA CAD application.
6  *
7  * Copyright (C) 1992-2011 jean-pierre.charras
8  * Copyright (C) 1992-2011 Kicad Developers, see AUTHORS.txt for contributors.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 3
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 #include <wx/wx.h>
24 #include <wx/config.h>
25 
26 #include <macros.h>
27 #include <pcb_calculator.h>
28 #include <class_regulator_data.h>
30 
31 
32 extern double DoubleFromString( const wxString& TextValue );
33 
35 {
36 public:
37  DIALOG_EDITOR_DATA( PCB_CALCULATOR_FRAME * parent, const wxString & aRegName )
38  : DIALOG_EDITOR_DATA_BASE( parent )
39  {
40  m_textCtrlName->SetValue( aRegName );
41  m_textCtrlName->Enable( aRegName.IsEmpty() );
42  UpdateDialog();
43 
44  m_sdbSizerOK->SetDefault();
45 
46  // Now all widgets have the size fixed, call FinishDialogSettings
48  }
49 
51 
52  // Event called functions:
53  void OnOKClick( wxCommandEvent& event ) override;
54 
59  bool IsOK();
60 
67 
74 
79  void UpdateDialog()
80  {
81  bool enbl = m_choiceRegType->GetSelection() == 1;
82  m_RegulIadjValue->Enable( enbl );
83  m_RegulIadjTitle->Enable( enbl );
84  m_IadjUnitLabel->Enable( enbl );
85  }
86 
90  void OnRegTypeSelection( wxCommandEvent& event ) override
91  {
92  UpdateDialog();
93  }
94 };
95 
96 
97 void DIALOG_EDITOR_DATA::OnOKClick( wxCommandEvent& event )
98 {
99  if( !IsOK() )
100  {
101  wxMessageBox( _("Bad or missing parameters!") );
102  return;
103  }
104 
105  EndModal( wxID_OK );
106 }
107 
109 {
110  bool success = true;
111 
112  if( m_textCtrlName->GetValue().IsEmpty() )
113  success = false;
114  if( m_textCtrlVref->GetValue().IsEmpty() )
115  success = false;
116  else
117  {
118  double vref = DoubleFromString( m_textCtrlVref->GetValue() );
119  if( fabs(vref) < 0.01 )
120  success = false;
121  }
122  if( m_choiceRegType->GetSelection() == 1 )
123  {
124  if( m_RegulIadjValue->GetValue().IsEmpty() )
125  success = false;
126  }
127 
128  return success;
129 }
130 
132 {
133  m_textCtrlName->SetValue( aItem->m_Name );
134  wxString value;
135  value.Printf( wxT("%g"), aItem->m_Vref );
136  m_textCtrlVref->SetValue( value );
137  value.Printf( wxT("%g"), aItem->m_Iadj );
138  m_RegulIadjValue->SetValue( value );
139  m_choiceRegType->SetSelection( aItem->m_Type );
140  UpdateDialog();
141 }
142 
144 {
145  double vref = DoubleFromString( m_textCtrlVref->GetValue() );
146  double iadj = DoubleFromString( m_RegulIadjValue->GetValue() );
147  int type = m_choiceRegType->GetSelection();
148  if( type != 1 )
149  iadj = 0.0;
150  REGULATOR_DATA * item = new REGULATOR_DATA( m_textCtrlName->GetValue(),
151  vref, type, iadj );
152  return item;
153 }
154 
156 {
157  RegulatorsSolve();
158 }
159 
161 {
162  m_RegulR1Value->SetValue( wxT( "10" ) );
163  m_RegulR2Value->SetValue( wxT( "10" ) );
164  m_RegulVrefValue->SetValue( wxT( "3" ) );
165  m_RegulVoutValue->SetValue( wxT( "12" ) );
166  m_choiceRegType->SetSelection( 0 );
167  m_rbRegulR1->SetValue(1);
168  m_rbRegulR2->SetValue(0);
169  m_rbRegulVout->SetValue(0);
171 }
172 
174 {
175  switch( m_choiceRegType->GetSelection() )
176  {
177  default:
178  case 0:
179  m_bitmapRegul4pins->Show( true );
180  m_bitmapRegul3pins->Show( false );
181  m_RegulIadjValue->Enable( false );
182  m_RegulIadjTitle->Enable( false );
183  m_IadjUnitLabel->Enable( false );
184  m_RegulFormula->SetLabel( wxT("Vout = Vref * (R1 + R2) / R2") );
185  break;
186 
187  case 1:
188  m_bitmapRegul4pins->Show( false );
189  m_bitmapRegul3pins->Show( true );
190  m_RegulIadjValue->Enable( true );
191  m_RegulIadjTitle->Enable( true );
192  m_IadjUnitLabel->Enable( true );
193  m_RegulFormula->SetLabel( wxT("Vout = Vref * (R1 + R2) / R1 + Iadj * R2") );
194  break;
195  }
196  // The new icon size must be taken in account
197  m_panelRegulators->GetSizer()->Layout();
198 
199  // Enable/disable buttons:
200  bool enbl = m_choiceRegulatorSelector->GetCount() > 0;
201  m_buttonEditItem->Enable( enbl );
202  m_buttonRemoveItem->Enable( enbl );
203 
204  m_panelRegulators->Refresh();
205 }
206 
207 void PCB_CALCULATOR_FRAME::OnRegulTypeSelection( wxCommandEvent& event )
208 {
210 }
211 
212 void PCB_CALCULATOR_FRAME::OnRegulatorSelection( wxCommandEvent& event )
213 {
214  wxString name = m_choiceRegulatorSelector->GetStringSelection();
216  if( item )
217  {
219  m_choiceRegType->SetSelection( item->m_Type );
220  wxString value;
221  value.Printf( wxT("%g"), item->m_Vref );
222  m_RegulVrefValue->SetValue( value );
223  value.Printf( wxT("%g"), item->m_Iadj );
224  m_RegulIadjValue->SetValue( value );
225  }
226 
227  // Call RegulatorPageUpdate to enable/sisable tools,
228  // even if no item selected
230 }
231 
232 /*
233  * Called when ckicking on button Browse:
234  * Select a new data file, and load it on request
235  */
236 void PCB_CALCULATOR_FRAME::OnDataFileSelection( wxCommandEvent& event )
237 {
238  wxString fullfilename = GetDataFilename();
239 
240  wxString wildcard;
241  wildcard.Printf( _("PCB Calculator data file (*.%s)|*.%s"),
243 
244  wxFileDialog dlg( m_panelRegulators,
245  _("Select PCB Calculator Data File"),
246  wxEmptyString, fullfilename,
247  wildcard, wxFD_OPEN );
248 
249  if (dlg.ShowModal() == wxID_CANCEL)
250  return;
251 
252  fullfilename = dlg.GetPath();
253 
254  if( fullfilename == GetDataFilename() )
255  return;
256 
257  SetDataFilename( fullfilename );
258  if( wxFileExists( fullfilename ) && m_RegulatorList.GetCount() > 0 ) // Read file
259  {
260  if( wxMessageBox( _("Do you want to load this file and replace current regulator list?" ) )
261  != wxID_OK )
262  return;
263  }
264 
265  if( ReadDataFile() )
266  {
267  m_RegulatorListChanged = false;
268  m_choiceRegulatorSelector->Clear();
271  }
272 
273  else
274  {
275  wxString msg;
276  msg.Printf( _("Unable to read data file \"%s\""), fullfilename );
277  wxMessageBox( msg );
278  }
279 }
280 
281 void PCB_CALCULATOR_FRAME::OnAddRegulator( wxCommandEvent& event )
282 {
283  DIALOG_EDITOR_DATA dlg( this, wxEmptyString );
284  if( dlg.ShowModal() != wxID_OK )
285  return;
286  if( !dlg.IsOK() )
287  {
288  wxMessageBox( _("Bad or missing parameters!") );
289  return;
290  }
291 
292  REGULATOR_DATA * new_item = dlg.BuildRegulatorFromData();
293 
294  // Add new item, if not existing
295  if( m_RegulatorList.GetReg( new_item->m_Name ) == NULL )
296  {
297  // Add item in list
298  m_RegulatorList.Add( new_item );
299  m_RegulatorListChanged = true;
300  m_choiceRegulatorSelector->Clear();
304  }
305  else
306  {
307  wxMessageBox( _("This regulator is already in list. Aborted") );
308  delete new_item;
309  }
310 }
311 
312 void PCB_CALCULATOR_FRAME::OnEditRegulator( wxCommandEvent& event )
313 {
314  wxString name = m_choiceRegulatorSelector->GetStringSelection();
316  if( item == NULL )
317  return;
318 
319  DIALOG_EDITOR_DATA dlg( this, name );
320 
321  dlg.CopyRegulatorDataToDialog( item );
322  if( dlg.ShowModal() != wxID_OK )
323  return;
324 
325  REGULATOR_DATA * new_item = dlg.BuildRegulatorFromData();
326  m_RegulatorList.Replace( new_item );
327 
328  m_RegulatorListChanged = true;
329 
331 }
332 
333 void PCB_CALCULATOR_FRAME::OnRemoveRegulator( wxCommandEvent& event )
334 {
335  wxString name = wxGetSingleChoice( _("Remove Regulator"), wxEmptyString,
337  if( name.IsEmpty() )
338  return;
339 
341  m_RegulatorListChanged = true;
342  m_choiceRegulatorSelector->Clear();
346 
348 }
349 
351 {
352  // Find last selected in regulator list:
353  int idx = -1;
354  if( ! m_lastSelectedRegulatorName.IsEmpty() )
355  {
356  for( unsigned ii = 0; ii < m_RegulatorList.GetCount(); ii++ )
358  {
359  idx = ii;
360  break;
361  }
362  }
363 
364  m_choiceRegulatorSelector->SetSelection( idx );
365  wxCommandEvent event;
366  OnRegulatorSelection( event );
367 }
368 
369 // Calculate a value from the 3 other values
370 // Vref is given by the regulator properties, so
371 // we can calculate only R1, R2 or Vout
373 {
374  int id;
375  if( m_rbRegulR1->GetValue() )
376  id = 0; // for R1 calculation
377  else if( m_rbRegulR2->GetValue() )
378  id = 1; // for R2 calculation
379  else if( m_rbRegulVout->GetValue() )
380  id = 2; // for Vout calculation
381  else
382  {
383  wxMessageBox( wxT("Selection error" ) );
384  return;
385  }
386 
387  double r1, r2, vref, vout;
388 
389  wxString txt;
390 
391  m_RegulMessage->SetLabel( wxEmptyString);
392 
393  // Convert r1 and r2 in ohms
394  int r1scale = 1000;
395  int r2scale = 1000;
396 
397  // Read values from panel:
398  txt = m_RegulR1Value->GetValue();
399  r1 = DoubleFromString(txt) * r1scale;
400  txt = m_RegulR2Value->GetValue();
401  r2 = DoubleFromString(txt) * r2scale;
402  txt = m_RegulVrefValue->GetValue();
403  vref = DoubleFromString(txt);
404  txt = m_RegulVoutValue->GetValue();
405  vout = DoubleFromString(txt);
406 
407 
408  // Some tests:
409  if( vout < vref && id != 2)
410  {
411  m_RegulMessage->SetLabel( _("Vout must be greater than vref" ) );
412  return;
413  }
414 
415  if( vref == 0.0 )
416  {
417  m_RegulMessage->SetLabel( _("Vref set to 0 !" ) );
418  return;
419  }
420 
421  if( (r1 < 0 && id != 0 ) || (r2 <= 0 && id != 1) )
422  {
423  m_RegulMessage->SetLabel( _("Incorrect value for R1 R2" ) );
424  return;
425  }
426 
427  // Calculate
428  if( m_choiceRegType->GetSelection() == 1)
429  {
430  // 3 terminal regulator
431  txt = m_RegulIadjValue->GetValue();
432  double iadj = DoubleFromString(txt);
433  // iadj is given in micro amp, so convert it in amp.
434  iadj /= 1000000;
435 
436  switch( id )
437  {
438  case 0:
439  r1 = vref * r2 / ( vout - vref - (r2 * iadj) );
440  break;
441 
442  case 1:
443  r2 = ( vout - vref ) / ( iadj + (vref/r1) );
444  break;
445 
446  case 2:
447  vout = vref * (r1 + r2) / r1;
448  vout += r2 * iadj;
449  break;
450  }
451  }
452  else
453  { // Standard 4 terminal regulator
454  switch( id )
455  {
456  case 0:
457  r1 = ( vout / vref - 1 ) * r2;
458  break;
459 
460  case 1:
461  r2 = r1 / ( vout / vref - 1);
462  break;
463 
464  case 2:
465  vout = vref * (r1 + r2) / r2;
466  break;
467  }
468  }
469  // write values to panel:
470  txt.Printf(wxT("%g"), r1 / r1scale );
471  m_RegulR1Value->SetValue(txt);
472  txt.Printf(wxT("%g"), r2 / r2scale);
473  m_RegulR2Value->SetValue(txt);
474  txt.Printf(wxT("%g"), vref);
475  m_RegulVrefValue->SetValue(txt);
476  txt.Printf(wxT("%g"), vout);
477  m_RegulVoutValue->SetValue(txt);
478 
479 }
480 
void RegulatorPageUpdate()
Function RegulatorPageUpdate: Update the regulator page dialog display: enable the current regulator ...
void OnRegulatorSelection(wxCommandEvent &event) override
void CopyRegulatorDataToDialog(REGULATOR_DATA *aItem)
Function CopyRegulatorDataToDialog Transfert data from dialog to aItem.
void OnRegulatorResetButtonClick(wxCommandEvent &event) override
wxString m_lastSelectedRegulatorName
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
std::vector< REGULATOR_DATA * > m_List
REGULATOR_DATA * GetReg(const wxString &aName)
void OnRegulTypeSelection(wxCommandEvent &event) override
void OnRemoveRegulator(wxCommandEvent &event) override
void OnRegTypeSelection(wxCommandEvent &event) override
called when the current regulator type is changed
void OnRegulatorCalcButtonClick(wxCommandEvent &event) override
This file contains miscellaneous commonly used macros and functions.
double DoubleFromString(const wxString &TextValue)
DIALOG_EDITOR_DATA(PCB_CALCULATOR_FRAME *parent, const wxString &aRegName)
wxArrayString GetRegList()
const wxString GetDataFilename()
void OnAddRegulator(wxCommandEvent &event) override
#define NULL
void Add(REGULATOR_DATA *aItem)
void OnOKClick(wxCommandEvent &event) override
bool IsOK()
Function IsOK()
unsigned int GetCount()
void Remove(const wxString &aRegName)
void OnDataFileSelection(wxCommandEvent &event) override
void OnEditRegulator(wxCommandEvent &event) override
void SetDataFilename(const wxString &aFilename)
Initialize the full filename of the selected pcb_calculator data file force the standard extension of...
REGULATOR_LIST m_RegulatorList
const char * name
Definition: DXF_plotter.cpp:60
#define _(s)
Definition: 3d_actions.cpp:31
void Replace(REGULATOR_DATA *aItem)
Replace an old REGULATOR_DATA by a new one The old one is deleted the 2 items must have the same name...
REGULATOR_DATA * BuildRegulatorFromData()
Function BuildRegulatorFromData Creates a new REGULATOR_DATA from dialog.
void UpdateDialog()
Enable/disable Iadj realted widgets, according to the regulator type.
const wxString DataFileNameExt
void SelectLastSelectedRegulator()
Function SelectLastSelectedRegulator select in choice box the last selected regulator (name in m_last...
Class DIALOG_EDITOR_DATA_BASE.