KiCad PCB EDA Suite
regulators_funct.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KICAD, a free EDA CAD application.
3  *
4  * Copyright (C) 1992-2011 jean-pierre.charras
5  * Copyright (C) 1992-2020 Kicad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
26 #include <wx/wx.h>
27 
28 #include <macros.h>
29 
30 #include "class_regulator_data.h"
31 #include "pcb_calculator_frame.h"
33 
35 
36 extern double DoubleFromString( const wxString& TextValue );
37 
38 
40 {
42 }
43 
44 
46 {
47  m_RegulR1Value->SetValue( wxT( "10" ) );
48  m_RegulR2Value->SetValue( wxT( "10" ) );
49  m_RegulVrefValue->SetValue( wxT( "3" ) );
50  m_RegulVoutValue->SetValue( wxT( "12" ) );
51  m_choiceRegType->SetSelection( 0 );
52  m_rbRegulR1->SetValue(1);
53  m_rbRegulR2->SetValue(0);
54  m_rbRegulVout->SetValue(0);
56 }
57 
58 
60 {
61  switch( m_choiceRegType->GetSelection() )
62  {
63  default:
64  case 0:
65  m_bitmapRegul4pins->Show( true );
66  m_bitmapRegul3pins->Show( false );
67  m_RegulIadjValue->Enable( false );
68  m_RegulFormula->SetLabel( wxT( "Vout = Vref * (R1 + R2) / R2" ) );
69  break;
70 
71  case 1:
72  m_bitmapRegul4pins->Show( false );
73  m_bitmapRegul3pins->Show( true );
74  m_RegulIadjValue->Enable( true );
75  m_RegulFormula->SetLabel( wxT( "Vout = Vref * (R1 + R2) / R1 + Iadj * R2" ) );
76  break;
77  }
78  // The new icon size must be taken in account
79  m_panelRegulators->GetSizer()->Layout();
80 
81  // Enable/disable buttons:
82  bool enbl = m_choiceRegulatorSelector->GetCount() > 0;
83  m_buttonEditItem->Enable( enbl );
84  m_buttonRemoveItem->Enable( enbl );
85 
86  m_panelRegulators->Refresh();
87 }
88 
89 
90 void PCB_CALCULATOR_FRAME::OnRegulTypeSelection( wxCommandEvent& event )
91 {
93 }
94 
95 
96 void PCB_CALCULATOR_FRAME::OnRegulatorSelection( wxCommandEvent& event )
97 {
98  wxString name = m_choiceRegulatorSelector->GetStringSelection();
100  if( item )
101  {
103  m_choiceRegType->SetSelection( item->m_Type );
104  wxString value;
105  value.Printf( wxT("%g"), item->m_Vref );
106  m_RegulVrefValue->SetValue( value );
107  value.Printf( wxT("%g"), item->m_Iadj );
108  m_RegulIadjValue->SetValue( value );
109  }
110 
111  // Call RegulatorPageUpdate to enable/sisable tools,
112  // even if no item selected
114 }
115 
116 
117 /*
118  * Called when ckicking on button Browse:
119  * Select a new data file, and load it on request
120  */
121 void PCB_CALCULATOR_FRAME::OnDataFileSelection( wxCommandEvent& event )
122 {
123  wxString fullfilename = GetDataFilename();
124 
125  wxString wildcard;
126  wildcard.Printf( _("PCB Calculator data file (*.%s)|*.%s"),
128 
129  wxFileDialog dlg( m_panelRegulators,
130  _("Select PCB Calculator Data File"),
131  wxEmptyString, fullfilename,
132  wildcard, wxFD_OPEN );
133 
134  if (dlg.ShowModal() == wxID_CANCEL)
135  return;
136 
137  fullfilename = dlg.GetPath();
138 
139  if( fullfilename == GetDataFilename() )
140  return;
141 
142  SetDataFilename( fullfilename );
143  if( wxFileExists( fullfilename ) && m_RegulatorList.GetCount() > 0 ) // Read file
144  {
145  if( wxMessageBox( _("Do you want to load this file and replace current regulator list?" ) )
146  != wxID_OK )
147  return;
148  }
149 
150  if( ReadDataFile() )
151  {
152  m_RegulatorListChanged = false;
153  m_choiceRegulatorSelector->Clear();
156  }
157 
158  else
159  {
160  wxString msg;
161  msg.Printf( _("Unable to read data file \"%s\""), fullfilename );
162  wxMessageBox( msg );
163  }
164 }
165 
166 
167 void PCB_CALCULATOR_FRAME::OnAddRegulator( wxCommandEvent& event )
168 {
169  DIALOG_REGULATOR_FORM dlg( this, wxEmptyString );
170  if( dlg.ShowModal() != wxID_OK )
171  return;
172  if( !dlg.IsOK() )
173  {
174  wxMessageBox( _("Bad or missing parameters!") );
175  return;
176  }
177 
178  REGULATOR_DATA * new_item = dlg.BuildRegulatorFromData();
179 
180  // Add new item, if not existing
181  if( m_RegulatorList.GetReg( new_item->m_Name ) == NULL )
182  {
183  // Add item in list
184  m_RegulatorList.Add( new_item );
185  m_RegulatorListChanged = true;
186  m_choiceRegulatorSelector->Clear();
190  }
191  else
192  {
193  wxMessageBox( _("This regulator is already in list. Aborted") );
194  delete new_item;
195  }
196 }
197 
198 
199 void PCB_CALCULATOR_FRAME::OnEditRegulator( wxCommandEvent& event )
200 {
201  wxString name = m_choiceRegulatorSelector->GetStringSelection();
203  if( item == NULL )
204  return;
205 
206  DIALOG_REGULATOR_FORM dlg( this, name );
207 
208  dlg.CopyRegulatorDataToDialog( item );
209  if( dlg.ShowModal() != wxID_OK )
210  return;
211 
212  REGULATOR_DATA * new_item = dlg.BuildRegulatorFromData();
213  m_RegulatorList.Replace( new_item );
214 
215  m_RegulatorListChanged = true;
216 
218 }
219 
220 
221 void PCB_CALCULATOR_FRAME::OnRemoveRegulator( wxCommandEvent& event )
222 {
223  wxString name = wxGetSingleChoice( _("Remove Regulator"), wxEmptyString,
225  if( name.IsEmpty() )
226  return;
227 
229  m_RegulatorListChanged = true;
230  m_choiceRegulatorSelector->Clear();
234 
236 }
237 
238 
240 {
241  // Find last selected in regulator list:
242  int idx = -1;
243  if( ! m_lastSelectedRegulatorName.IsEmpty() )
244  {
245  for( unsigned ii = 0; ii < m_RegulatorList.GetCount(); ii++ )
247  {
248  idx = ii;
249  break;
250  }
251  }
252 
253  m_choiceRegulatorSelector->SetSelection( idx );
254  wxCommandEvent event;
255  OnRegulatorSelection( event );
256 }
257 
258 
259 // Calculate a value from the 3 other values
260 // Vref is given by the regulator properties, so
261 // we can calculate only R1, R2 or Vout
263 {
264  int id;
265  if( m_rbRegulR1->GetValue() )
266  id = 0; // for R1 calculation
267  else if( m_rbRegulR2->GetValue() )
268  id = 1; // for R2 calculation
269  else if( m_rbRegulVout->GetValue() )
270  id = 2; // for Vout calculation
271  else
272  {
273  wxMessageBox( wxT("Selection error" ) );
274  return;
275  }
276 
277  double r1, r2, vref, vout;
278 
279  wxString txt;
280 
281  m_RegulMessage->SetLabel( wxEmptyString);
282 
283  // Convert r1 and r2 in ohms
284  int r1scale = 1000;
285  int r2scale = 1000;
286 
287  // Read values from panel:
288  txt = m_RegulR1Value->GetValue();
289  r1 = DoubleFromString(txt) * r1scale;
290  txt = m_RegulR2Value->GetValue();
291  r2 = DoubleFromString(txt) * r2scale;
292  txt = m_RegulVrefValue->GetValue();
293  vref = DoubleFromString(txt);
294  txt = m_RegulVoutValue->GetValue();
295  vout = DoubleFromString(txt);
296 
297 
298  // Some tests:
299  if( vout < vref && id != 2)
300  {
301  m_RegulMessage->SetLabel( _("Vout must be greater than vref" ) );
302  return;
303  }
304 
305  if( vref == 0.0 )
306  {
307  m_RegulMessage->SetLabel( _("Vref set to 0 !" ) );
308  return;
309  }
310 
311  if( (r1 < 0 && id != 0 ) || (r2 <= 0 && id != 1) )
312  {
313  m_RegulMessage->SetLabel( _("Incorrect value for R1 R2" ) );
314  return;
315  }
316 
317  // Calculate
318  if( m_choiceRegType->GetSelection() == 1)
319  {
320  // 3 terminal regulator
321  txt = m_RegulIadjValue->GetValue();
322  double iadj = DoubleFromString(txt);
323  // iadj is given in micro amp, so convert it in amp.
324  iadj /= 1000000;
325 
326  switch( id )
327  {
328  case 0:
329  r1 = vref * r2 / ( vout - vref - (r2 * iadj) );
330  break;
331 
332  case 1:
333  r2 = ( vout - vref ) / ( iadj + (vref/r1) );
334  break;
335 
336  case 2:
337  vout = vref * (r1 + r2) / r1;
338  vout += r2 * iadj;
339  break;
340  }
341  }
342  else
343  { // Standard 4 terminal regulator
344  switch( id )
345  {
346  case 0:
347  r1 = ( vout / vref - 1 ) * r2;
348  break;
349 
350  case 1:
351  r2 = r1 / ( vout / vref - 1);
352  break;
353 
354  case 2:
355  vout = vref * (r1 + r2) / r2;
356  break;
357  }
358  }
359  // write values to panel:
360  txt.Printf(wxT("%g"), r1 / r1scale );
361  m_RegulR1Value->SetValue(txt);
362  txt.Printf(wxT("%g"), r2 / r2scale);
363  m_RegulR2Value->SetValue(txt);
364  txt.Printf(wxT("%g"), vref);
365  m_RegulVrefValue->SetValue(txt);
366  txt.Printf(wxT("%g"), vout);
367  m_RegulVoutValue->SetValue(txt);
368 
369 }
370 
371 
373 {
374  // Save current parameter values in config.
375  aCfg->m_Regulators.r1 = m_RegulR1Value->GetValue();
376  aCfg->m_Regulators.r2 = m_RegulR2Value->GetValue();
377  aCfg->m_Regulators.vref = m_RegulVrefValue->GetValue();
378  aCfg->m_Regulators.vout = m_RegulVoutValue->GetValue();
381  aCfg->m_Regulators.type = m_choiceRegType->GetSelection();
382 
383  // Store the parameter selection that was recently calculated (R1, R2 or Vout)
384  wxRadioButton * regprms[3] =
385  {
387  };
388 
389  for( int ii = 0; ii < 3; ii++ )
390  {
391  if( regprms[ii]->GetValue() )
392  {
393  aCfg->m_Regulators.last_param = ii;
394  break;
395  }
396  }
397 
398 }
const wxString DataFileNameExt
void RegulatorPageUpdate()
Function RegulatorPageUpdate: Update the regulator page dialog display: enable the current regulator ...
void OnRegulatorSelection(wxCommandEvent &event) override
void OnRegulatorResetButtonClick(wxCommandEvent &event) override
REGULATOR_DATA * BuildRegulatorFromData()
Function BuildRegulatorFromData Creates a new REGULATOR_DATA from dialog.
std::vector< REGULATOR_DATA * > m_List
REGULATOR_DATA * GetReg(const wxString &aName)
void OnRegulTypeSelection(wxCommandEvent &event) override
void OnRemoveRegulator(wxCommandEvent &event) override
void OnRegulatorCalcButtonClick(wxCommandEvent &event) override
This file contains miscellaneous commonly used macros and functions.
double DoubleFromString(const wxString &TextValue)
const wxString GetDataFilename()
wxArrayString GetRegList() const
void OnAddRegulator(wxCommandEvent &event) override
#define NULL
Contains structures for storage of regulator data.
void Add(REGULATOR_DATA *aItem)
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:59
#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...
void CopyRegulatorDataToDialog(REGULATOR_DATA *aItem)
Function CopyRegulatorDataToDialog Transfert data from dialog to aItem.
bool IsOK()
Function IsOK()
void Regulators_WriteConfig(PCB_CALCULATOR_SETTINGS *aCfg)
Write regulators parameters in config.
void SelectLastSelectedRegulator()
Function SelectLastSelectedRegulator If m_lastSelectedRegulatorName is empty, just calls RegulatorPag...
Subclass of DIALOG_REGULATOR_FORM_BASE, which is generated by wxFormBuilder.