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-2020 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>
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  }
84 
88  void OnRegTypeSelection( wxCommandEvent& event ) override
89  {
90  UpdateDialog();
91  }
92 };
93 
94 
95 void DIALOG_EDITOR_DATA::OnOKClick( wxCommandEvent& event )
96 {
97  if( !IsOK() )
98  {
99  wxMessageBox( _("Bad or missing parameters!") );
100  return;
101  }
102 
103  EndModal( wxID_OK );
104 }
105 
107 {
108  bool success = true;
109 
110  if( m_textCtrlName->GetValue().IsEmpty() )
111  success = false;
112  if( m_textCtrlVref->GetValue().IsEmpty() )
113  success = false;
114  else
115  {
116  double vref = DoubleFromString( m_textCtrlVref->GetValue() );
117  if( fabs(vref) < 0.01 )
118  success = false;
119  }
120  if( m_choiceRegType->GetSelection() == 1 )
121  {
122  if( m_RegulIadjValue->GetValue().IsEmpty() )
123  success = false;
124  }
125 
126  return success;
127 }
128 
130 {
131  m_textCtrlName->SetValue( aItem->m_Name );
132  wxString value;
133  value.Printf( wxT("%g"), aItem->m_Vref );
134  m_textCtrlVref->SetValue( value );
135  value.Printf( wxT("%g"), aItem->m_Iadj );
136  m_RegulIadjValue->SetValue( value );
137  m_choiceRegType->SetSelection( aItem->m_Type );
138  UpdateDialog();
139 }
140 
142 {
143  double vref = DoubleFromString( m_textCtrlVref->GetValue() );
144  double iadj = DoubleFromString( m_RegulIadjValue->GetValue() );
145  int type = m_choiceRegType->GetSelection();
146 
147  if( type != 1 )
148  iadj = 0.0;
149 
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_RegulFormula->SetLabel( wxT("Vout = Vref * (R1 + R2) / R2") );
183  break;
184 
185  case 1:
186  m_bitmapRegul4pins->Show( false );
187  m_bitmapRegul3pins->Show( true );
188  m_RegulIadjValue->Enable( true );
189  m_RegulFormula->SetLabel( wxT("Vout = Vref * (R1 + R2) / R1 + Iadj * R2") );
190  break;
191  }
192  // The new icon size must be taken in account
193  m_panelRegulators->GetSizer()->Layout();
194 
195  // Enable/disable buttons:
196  bool enbl = m_choiceRegulatorSelector->GetCount() > 0;
197  m_buttonEditItem->Enable( enbl );
198  m_buttonRemoveItem->Enable( enbl );
199 
200  m_panelRegulators->Refresh();
201 }
202 
203 void PCB_CALCULATOR_FRAME::OnRegulTypeSelection( wxCommandEvent& event )
204 {
206 }
207 
208 void PCB_CALCULATOR_FRAME::OnRegulatorSelection( wxCommandEvent& event )
209 {
210  wxString name = m_choiceRegulatorSelector->GetStringSelection();
212  if( item )
213  {
215  m_choiceRegType->SetSelection( item->m_Type );
216  wxString value;
217  value.Printf( wxT("%g"), item->m_Vref );
218  m_RegulVrefValue->SetValue( value );
219  value.Printf( wxT("%g"), item->m_Iadj );
220  m_RegulIadjValue->SetValue( value );
221  }
222 
223  // Call RegulatorPageUpdate to enable/sisable tools,
224  // even if no item selected
226 }
227 
228 /*
229  * Called when ckicking on button Browse:
230  * Select a new data file, and load it on request
231  */
232 void PCB_CALCULATOR_FRAME::OnDataFileSelection( wxCommandEvent& event )
233 {
234  wxString fullfilename = GetDataFilename();
235 
236  wxString wildcard;
237  wildcard.Printf( _("PCB Calculator data file (*.%s)|*.%s"),
239 
240  wxFileDialog dlg( m_panelRegulators,
241  _("Select PCB Calculator Data File"),
242  wxEmptyString, fullfilename,
243  wildcard, wxFD_OPEN );
244 
245  if (dlg.ShowModal() == wxID_CANCEL)
246  return;
247 
248  fullfilename = dlg.GetPath();
249 
250  if( fullfilename == GetDataFilename() )
251  return;
252 
253  SetDataFilename( fullfilename );
254  if( wxFileExists( fullfilename ) && m_RegulatorList.GetCount() > 0 ) // Read file
255  {
256  if( wxMessageBox( _("Do you want to load this file and replace current regulator list?" ) )
257  != wxID_OK )
258  return;
259  }
260 
261  if( ReadDataFile() )
262  {
263  m_RegulatorListChanged = false;
264  m_choiceRegulatorSelector->Clear();
267  }
268 
269  else
270  {
271  wxString msg;
272  msg.Printf( _("Unable to read data file \"%s\""), fullfilename );
273  wxMessageBox( msg );
274  }
275 }
276 
277 void PCB_CALCULATOR_FRAME::OnAddRegulator( wxCommandEvent& event )
278 {
279  DIALOG_EDITOR_DATA dlg( this, wxEmptyString );
280  if( dlg.ShowModal() != wxID_OK )
281  return;
282  if( !dlg.IsOK() )
283  {
284  wxMessageBox( _("Bad or missing parameters!") );
285  return;
286  }
287 
288  REGULATOR_DATA * new_item = dlg.BuildRegulatorFromData();
289 
290  // Add new item, if not existing
291  if( m_RegulatorList.GetReg( new_item->m_Name ) == NULL )
292  {
293  // Add item in list
294  m_RegulatorList.Add( new_item );
295  m_RegulatorListChanged = true;
296  m_choiceRegulatorSelector->Clear();
300  }
301  else
302  {
303  wxMessageBox( _("This regulator is already in list. Aborted") );
304  delete new_item;
305  }
306 }
307 
308 void PCB_CALCULATOR_FRAME::OnEditRegulator( wxCommandEvent& event )
309 {
310  wxString name = m_choiceRegulatorSelector->GetStringSelection();
312  if( item == NULL )
313  return;
314 
315  DIALOG_EDITOR_DATA dlg( this, name );
316 
317  dlg.CopyRegulatorDataToDialog( item );
318  if( dlg.ShowModal() != wxID_OK )
319  return;
320 
321  REGULATOR_DATA * new_item = dlg.BuildRegulatorFromData();
322  m_RegulatorList.Replace( new_item );
323 
324  m_RegulatorListChanged = true;
325 
327 }
328 
329 void PCB_CALCULATOR_FRAME::OnRemoveRegulator( wxCommandEvent& event )
330 {
331  wxString name = wxGetSingleChoice( _("Remove Regulator"), wxEmptyString,
333  if( name.IsEmpty() )
334  return;
335 
337  m_RegulatorListChanged = true;
338  m_choiceRegulatorSelector->Clear();
342 
344 }
345 
347 {
348  // Find last selected in regulator list:
349  int idx = -1;
350  if( ! m_lastSelectedRegulatorName.IsEmpty() )
351  {
352  for( unsigned ii = 0; ii < m_RegulatorList.GetCount(); ii++ )
354  {
355  idx = ii;
356  break;
357  }
358  }
359 
360  m_choiceRegulatorSelector->SetSelection( idx );
361  wxCommandEvent event;
362  OnRegulatorSelection( event );
363 }
364 
365 // Calculate a value from the 3 other values
366 // Vref is given by the regulator properties, so
367 // we can calculate only R1, R2 or Vout
369 {
370  int id;
371  if( m_rbRegulR1->GetValue() )
372  id = 0; // for R1 calculation
373  else if( m_rbRegulR2->GetValue() )
374  id = 1; // for R2 calculation
375  else if( m_rbRegulVout->GetValue() )
376  id = 2; // for Vout calculation
377  else
378  {
379  wxMessageBox( wxT("Selection error" ) );
380  return;
381  }
382 
383  double r1, r2, vref, vout;
384 
385  wxString txt;
386 
387  m_RegulMessage->SetLabel( wxEmptyString);
388 
389  // Convert r1 and r2 in ohms
390  int r1scale = 1000;
391  int r2scale = 1000;
392 
393  // Read values from panel:
394  txt = m_RegulR1Value->GetValue();
395  r1 = DoubleFromString(txt) * r1scale;
396  txt = m_RegulR2Value->GetValue();
397  r2 = DoubleFromString(txt) * r2scale;
398  txt = m_RegulVrefValue->GetValue();
399  vref = DoubleFromString(txt);
400  txt = m_RegulVoutValue->GetValue();
401  vout = DoubleFromString(txt);
402 
403 
404  // Some tests:
405  if( vout < vref && id != 2)
406  {
407  m_RegulMessage->SetLabel( _("Vout must be greater than vref" ) );
408  return;
409  }
410 
411  if( vref == 0.0 )
412  {
413  m_RegulMessage->SetLabel( _("Vref set to 0 !" ) );
414  return;
415  }
416 
417  if( (r1 < 0 && id != 0 ) || (r2 <= 0 && id != 1) )
418  {
419  m_RegulMessage->SetLabel( _("Incorrect value for R1 R2" ) );
420  return;
421  }
422 
423  // Calculate
424  if( m_choiceRegType->GetSelection() == 1)
425  {
426  // 3 terminal regulator
427  txt = m_RegulIadjValue->GetValue();
428  double iadj = DoubleFromString(txt);
429  // iadj is given in micro amp, so convert it in amp.
430  iadj /= 1000000;
431 
432  switch( id )
433  {
434  case 0:
435  r1 = vref * r2 / ( vout - vref - (r2 * iadj) );
436  break;
437 
438  case 1:
439  r2 = ( vout - vref ) / ( iadj + (vref/r1) );
440  break;
441 
442  case 2:
443  vout = vref * (r1 + r2) / r1;
444  vout += r2 * iadj;
445  break;
446  }
447  }
448  else
449  { // Standard 4 terminal regulator
450  switch( id )
451  {
452  case 0:
453  r1 = ( vout / vref - 1 ) * r2;
454  break;
455 
456  case 1:
457  r2 = r1 / ( vout / vref - 1);
458  break;
459 
460  case 2:
461  vout = vref * (r1 + r2) / r2;
462  break;
463  }
464  }
465  // write values to panel:
466  txt.Printf(wxT("%g"), r1 / r1scale );
467  m_RegulR1Value->SetValue(txt);
468  txt.Printf(wxT("%g"), r2 / r2scale);
469  m_RegulR2Value->SetValue(txt);
470  txt.Printf(wxT("%g"), vref);
471  m_RegulVrefValue->SetValue(txt);
472  txt.Printf(wxT("%g"), vout);
473  m_RegulVoutValue->SetValue(txt);
474 
475 }
476 
477 
479 {
480  // Save current parameter values in config.
481  aCfg->m_Regulators.r1 = m_RegulR1Value->GetValue();
482  aCfg->m_Regulators.r2 = m_RegulR2Value->GetValue();
483  aCfg->m_Regulators.vref = m_RegulVrefValue->GetValue();
484  aCfg->m_Regulators.vout = m_RegulVoutValue->GetValue();
487  aCfg->m_Regulators.type = m_choiceRegType->GetSelection();
488 
489  // Store the parameter selection that was recently calculated (R1, R2 or Vout)
490  wxRadioButton * regprms[3] =
491  {
493  };
494 
495  for( int ii = 0; ii < 3; ii++ )
496  {
497  if( regprms[ii]->GetValue() )
498  {
499  aCfg->m_Regulators.last_param = ii;
500  break;
501  }
502  }
503 
504 }
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 Regulators_WriteConfig(PCB_CALCULATOR_SETTINGS *aCfg)
Write regulators parameters in config.
void SelectLastSelectedRegulator()
Function SelectLastSelectedRegulator select in choice box the last selected regulator (name in m_last...
Class DIALOG_EDITOR_DATA_BASE.