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 
25 #include <macros.h>
26 #include <pcb_calculator.h>
27 #include <class_regulator_data.h>
29 
30 
31 extern double DoubleFromString( const wxString& TextValue );
32 
34 {
35 public:
36  DIALOG_EDITOR_DATA( PCB_CALCULATOR_FRAME * parent, const wxString & aRegName )
37  : DIALOG_EDITOR_DATA_BASE( parent )
38  {
39  m_textCtrlName->SetValue( aRegName );
40  m_textCtrlName->Enable( aRegName.IsEmpty() );
41  UpdateDialog();
42 
43  m_sdbSizerOK->SetDefault();
44 
45  // Now all widgets have the size fixed, call FinishDialogSettings
47  }
48 
50 
51  // Event called functions:
52  void OnOKClick( wxCommandEvent& event ) override;
53 
58  bool IsOK();
59 
66 
73 
78  void UpdateDialog()
79  {
80  bool enbl = m_choiceRegType->GetSelection() == 1;
81  m_RegulIadjValue->Enable( enbl );
82  m_RegulIadjTitle->Enable( enbl );
83  m_IadjUnitLabel->Enable( enbl );
84  }
85 
89  void OnRegTypeSelection( wxCommandEvent& event ) override
90  {
91  UpdateDialog();
92  }
93 };
94 
95 
96 void DIALOG_EDITOR_DATA::OnOKClick( wxCommandEvent& event )
97 {
98  if( !IsOK() )
99  {
100  wxMessageBox( _("Bad or missing parameters!") );
101  return;
102  }
103 
104  EndModal( wxID_OK );
105 }
106 
108 {
109  bool success = true;
110 
111  if( m_textCtrlName->GetValue().IsEmpty() )
112  success = false;
113  if( m_textCtrlVref->GetValue().IsEmpty() )
114  success = false;
115  else
116  {
117  double vref = DoubleFromString( m_textCtrlVref->GetValue() );
118  if( fabs(vref) < 0.01 )
119  success = false;
120  }
121  if( m_choiceRegType->GetSelection() == 1 )
122  {
123  if( m_RegulIadjValue->GetValue().IsEmpty() )
124  success = false;
125  }
126 
127  return success;
128 }
129 
131 {
132  m_textCtrlName->SetValue( aItem->m_Name );
133  wxString value;
134  value.Printf( wxT("%g"), aItem->m_Vref );
135  m_textCtrlVref->SetValue( value );
136  value.Printf( wxT("%g"), aItem->m_Iadj );
137  m_RegulIadjValue->SetValue( value );
138  m_choiceRegType->SetSelection( aItem->m_Type );
139  UpdateDialog();
140 }
141 
143 {
144  double vref = DoubleFromString( m_textCtrlVref->GetValue() );
145  double iadj = DoubleFromString( m_RegulIadjValue->GetValue() );
146  int type = m_choiceRegType->GetSelection();
147  if( type != 1 )
148  iadj = 0.0;
149  REGULATOR_DATA * item = new REGULATOR_DATA( m_textCtrlName->GetValue(),
150  vref, type, iadj );
151  return item;
152 }
153 
155 {
156  RegulatorsSolve();
157 }
158 
160 {
161  m_RegulR1Value->SetValue( wxT( "10" ) );
162  m_RegulR2Value->SetValue( wxT( "10" ) );
163  m_RegulVrefValue->SetValue( wxT( "3" ) );
164  m_RegulVoutValue->SetValue( wxT( "12" ) );
165  m_choiceRegType->SetSelection( 0 );
166  m_rbRegulR1->SetValue(1);
167  m_rbRegulR2->SetValue(0);
168  m_rbRegulVout->SetValue(0);
170 }
171 
173 {
174  switch( m_choiceRegType->GetSelection() )
175  {
176  default:
177  case 0:
178  m_bitmapRegul4pins->Show( true );
179  m_bitmapRegul3pins->Show( false );
180  m_RegulIadjValue->Enable( false );
181  m_RegulIadjTitle->Enable( false );
182  m_IadjUnitLabel->Enable( false );
183  m_RegulFormula->SetLabel( wxT("Vout = Vref * (R1 + R2) / R2") );
184  break;
185 
186  case 1:
187  m_bitmapRegul4pins->Show( false );
188  m_bitmapRegul3pins->Show( true );
189  m_RegulIadjValue->Enable( true );
190  m_RegulIadjTitle->Enable( true );
191  m_IadjUnitLabel->Enable( true );
192  m_RegulFormula->SetLabel( wxT("Vout = Vref * (R1 + R2) / R1 + Iadj * R2") );
193  break;
194  }
195  // The new icon size must be taken in account
196  m_panelRegulators->GetSizer()->Layout();
197 
198  // Enable/disable buttons:
199  bool enbl = m_choiceRegulatorSelector->GetCount() > 0;
200  m_buttonEditItem->Enable( enbl );
201  m_buttonRemoveItem->Enable( enbl );
202 
203  m_panelRegulators->Refresh();
204 }
205 
206 void PCB_CALCULATOR_FRAME::OnRegulTypeSelection( wxCommandEvent& event )
207 {
209 }
210 
211 void PCB_CALCULATOR_FRAME::OnRegulatorSelection( wxCommandEvent& event )
212 {
213  wxString name = m_choiceRegulatorSelector->GetStringSelection();
215  if( item )
216  {
218  m_choiceRegType->SetSelection( item->m_Type );
219  wxString value;
220  value.Printf( wxT("%g"), item->m_Vref );
221  m_RegulVrefValue->SetValue( value );
222  value.Printf( wxT("%g"), item->m_Iadj );
223  m_RegulIadjValue->SetValue( value );
224  }
225 
226  // Call RegulatorPageUpdate to enable/sisable tools,
227  // even if no item selected
229 }
230 
231 /*
232  * Called when ckicking on button Browse:
233  * Select a new data file, and load it on request
234  */
235 void PCB_CALCULATOR_FRAME::OnDataFileSelection( wxCommandEvent& event )
236 {
237  wxString fullfilename = GetDataFilename();
238 
239  wxString wildcard;
240  wildcard.Printf( _("PCB Calculator data file (*.%s)|*.%s"),
242 
243  wxFileDialog dlg( m_panelRegulators,
244  _("Select PCB Calculator Data File"),
245  wxEmptyString, fullfilename,
246  wildcard, wxFD_OPEN );
247 
248  if (dlg.ShowModal() == wxID_CANCEL)
249  return;
250 
251  fullfilename = dlg.GetPath();
252 
253  if( fullfilename == GetDataFilename() )
254  return;
255 
256  SetDataFilename( fullfilename );
257  if( wxFileExists( fullfilename ) && m_RegulatorList.GetCount() > 0 ) // Read file
258  {
259  if( wxMessageBox( _("Do you want to load this file and replace current regulator list?" ) )
260  != wxID_OK )
261  return;
262  }
263 
264  if( ReadDataFile() )
265  {
266  m_RegulatorListChanged = false;
267  m_choiceRegulatorSelector->Clear();
270  }
271 
272  else
273  {
274  wxString msg;
275  msg.Printf( _("Unable to read data file \"%s\""), fullfilename );
276  wxMessageBox( msg );
277  }
278 }
279 
280 void PCB_CALCULATOR_FRAME::OnAddRegulator( wxCommandEvent& event )
281 {
282  DIALOG_EDITOR_DATA dlg( this, wxEmptyString );
283  if( dlg.ShowModal() != wxID_OK )
284  return;
285  if( !dlg.IsOK() )
286  {
287  wxMessageBox( _("Bad or missing parameters!") );
288  return;
289  }
290 
291  REGULATOR_DATA * new_item = dlg.BuildRegulatorFromData();
292 
293  // Add new item, if not existing
294  if( m_RegulatorList.GetReg( new_item->m_Name ) == NULL )
295  {
296  // Add item in list
297  m_RegulatorList.Add( new_item );
298  m_RegulatorListChanged = true;
299  m_choiceRegulatorSelector->Clear();
303  }
304  else
305  {
306  wxMessageBox( _("This regulator is already in list. Aborted") );
307  delete new_item;
308  }
309 }
310 
311 void PCB_CALCULATOR_FRAME::OnEditRegulator( wxCommandEvent& event )
312 {
313  wxString name = m_choiceRegulatorSelector->GetStringSelection();
315  if( item == NULL )
316  return;
317 
318  DIALOG_EDITOR_DATA dlg( this, name );
319 
320  dlg.CopyRegulatorDataToDialog( item );
321  if( dlg.ShowModal() != wxID_OK )
322  return;
323 
324  REGULATOR_DATA * new_item = dlg.BuildRegulatorFromData();
325  m_RegulatorList.Replace( new_item );
326 
327  m_RegulatorListChanged = true;
328 
330 }
331 
332 void PCB_CALCULATOR_FRAME::OnRemoveRegulator( wxCommandEvent& event )
333 {
334  wxString name = wxGetSingleChoice( _("Remove Regulator"), wxEmptyString,
336  if( name.IsEmpty() )
337  return;
338 
340  m_RegulatorListChanged = true;
341  m_choiceRegulatorSelector->Clear();
345 
347 }
348 
350 {
351  // Find last selected in regulator list:
352  int idx = -1;
353  if( ! m_lastSelectedRegulatorName.IsEmpty() )
354  {
355  for( unsigned ii = 0; ii < m_RegulatorList.GetCount(); ii++ )
357  {
358  idx = ii;
359  break;
360  }
361  }
362 
363  m_choiceRegulatorSelector->SetSelection( idx );
364  wxCommandEvent event;
365  OnRegulatorSelection( event );
366 }
367 
368 // Calculate a value from the 3 other values
369 // Vref is given by the regulator properties, so
370 // we can calculate only R1, R2 or Vout
372 {
373  int id;
374  if( m_rbRegulR1->GetValue() )
375  id = 0; // for R1 calculation
376  else if( m_rbRegulR2->GetValue() )
377  id = 1; // for R2 calculation
378  else if( m_rbRegulVout->GetValue() )
379  id = 2; // for Vout calculation
380  else
381  {
382  wxMessageBox( wxT("Selection error" ) );
383  return;
384  }
385 
386  double r1, r2, vref, vout;
387 
388  wxString txt;
389 
390  m_RegulMessage->SetLabel( wxEmptyString);
391 
392  // Convert r1 and r2 in ohms
393  int r1scale = 1000;
394  int r2scale = 1000;
395 
396  // Read values from panel:
397  txt = m_RegulR1Value->GetValue();
398  r1 = DoubleFromString(txt) * r1scale;
399  txt = m_RegulR2Value->GetValue();
400  r2 = DoubleFromString(txt) * r2scale;
401  txt = m_RegulVrefValue->GetValue();
402  vref = DoubleFromString(txt);
403  txt = m_RegulVoutValue->GetValue();
404  vout = DoubleFromString(txt);
405 
406 
407  // Some tests:
408  if( vout < vref && id != 2)
409  {
410  m_RegulMessage->SetLabel( _("Vout must be greater than vref" ) );
411  return;
412  }
413 
414  if( vref == 0.0 )
415  {
416  m_RegulMessage->SetLabel( _("Vref set to 0 !" ) );
417  return;
418  }
419 
420  if( (r1 < 0 && id != 0 ) || (r2 <= 0 && id != 1) )
421  {
422  m_RegulMessage->SetLabel( _("Incorrect value for R1 R2" ) );
423  return;
424  }
425 
426  // Calculate
427  if( m_choiceRegType->GetSelection() == 1)
428  {
429  // 3 terminal regulator
430  txt = m_RegulIadjValue->GetValue();
431  double iadj = DoubleFromString(txt);
432  // iadj is given in micro amp, so convert it in amp.
433  iadj /= 1000000;
434 
435  switch( id )
436  {
437  case 0:
438  r1 = vref * r2 / ( vout - vref - (r2 * iadj) );
439  break;
440 
441  case 1:
442  r2 = ( vout - vref ) / ( iadj + (vref/r1) );
443  break;
444 
445  case 2:
446  vout = vref * (r1 + r2) / r1;
447  vout += r2 * iadj;
448  break;
449  }
450  }
451  else
452  { // Standard 4 terminal regulator
453  switch( id )
454  {
455  case 0:
456  r1 = ( vout / vref - 1 ) * r2;
457  break;
458 
459  case 1:
460  r2 = r1 / ( vout / vref - 1);
461  break;
462 
463  case 2:
464  vout = vref * (r1 + r2) / r2;
465  break;
466  }
467  }
468  // write values to panel:
469  txt.Printf(wxT("%g"), r1 / r1scale );
470  m_RegulR1Value->SetValue(txt);
471  txt.Printf(wxT("%g"), r2 / r2scale);
472  m_RegulR2Value->SetValue(txt);
473  txt.Printf(wxT("%g"), vref);
474  m_RegulVrefValue->SetValue(txt);
475  txt.Printf(wxT("%g"), vout);
476  m_RegulVoutValue->SetValue(txt);
477 
478 }
479 
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:33
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.