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-2021 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 
27 #include <macros.h>
28 
29 #include <wx/choicdlg.h>
30 #include <wx/filedlg.h>
31 #include <wx/msgdlg.h>
32 
33 #include "class_regulator_data.h"
34 #include "pcb_calculator_frame.h"
36 
38 
39 extern double DoubleFromString( const wxString& TextValue );
40 
41 
43 {
45 }
46 
47 
49 {
50  m_RegulR1Value->SetValue( wxT( "10" ) );
51  m_RegulR2Value->SetValue( wxT( "10" ) );
52  m_RegulVrefValue->SetValue( wxT( "3" ) );
53  m_RegulVoutValue->SetValue( wxT( "12" ) );
54  m_choiceRegType->SetSelection( 0 );
55  m_rbRegulR1->SetValue( 1 );
56  m_rbRegulR2->SetValue( 0 );
57  m_rbRegulVout->SetValue( 0 );
59 }
60 
61 
63 {
64  switch( m_choiceRegType->GetSelection() )
65  {
66  default:
67  case 0:
68  m_bitmapRegul4pins->Show( true );
69  m_bitmapRegul3pins->Show( false );
70  m_RegulIadjValue->Enable( false );
71  m_RegulFormula->SetLabel( wxT( "Vout = Vref * (R1 + R2) / R2" ) );
72  break;
73 
74  case 1:
75  m_bitmapRegul4pins->Show( false );
76  m_bitmapRegul3pins->Show( true );
77  m_RegulIadjValue->Enable( true );
78  m_RegulFormula->SetLabel( wxT( "Vout = Vref * (R1 + R2) / R1 + Iadj * R2" ) );
79  break;
80  }
81 
82  // The new icon size must be taken in account
83  m_panelRegulators->GetSizer()->Layout();
84 
85  // Enable/disable buttons:
86  bool enbl = m_choiceRegulatorSelector->GetCount() > 0;
87  m_buttonEditItem->Enable( enbl );
88  m_buttonRemoveItem->Enable( enbl );
89 
90  m_panelRegulators->Refresh();
91 }
92 
93 
94 void PCB_CALCULATOR_FRAME::OnRegulTypeSelection( wxCommandEvent& event )
95 {
97 }
98 
99 
100 void PCB_CALCULATOR_FRAME::OnRegulatorSelection( wxCommandEvent& event )
101 {
102  wxString name = m_choiceRegulatorSelector->GetStringSelection();
104 
105  if( item )
106  {
108  m_choiceRegType->SetSelection( item->m_Type );
109  wxString value;
110  value.Printf( wxT( "%g" ), item->m_Vref );
111  m_RegulVrefValue->SetValue( value );
112  value.Printf( wxT( "%g" ), item->m_Iadj );
113  m_RegulIadjValue->SetValue( value );
114  }
115 
116  // Call RegulatorPageUpdate to enable/disable tools,
117  // even if no item selected
119 }
120 
121 
122 void PCB_CALCULATOR_FRAME::OnDataFileSelection( wxCommandEvent& event )
123 {
124  wxString fullfilename = GetDataFilename();
125 
126  wxString wildcard;
127  wildcard.Printf( _("PCB Calculator data file" ) + wxT( " (*.%s)|*.%s"),
129 
130  wxFileDialog dlg( m_panelRegulators, _("Select PCB Calculator Data File"),
131  wxEmptyString, fullfilename, wildcard, wxFD_OPEN );
132 
133  if (dlg.ShowModal() == wxID_CANCEL)
134  return;
135 
136  fullfilename = dlg.GetPath();
137 
138  if( fullfilename == GetDataFilename() )
139  return;
140 
141  SetDataFilename( fullfilename );
142 
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  else
158  {
159  wxString msg;
160  msg.Printf( _("Unable to read data file '%s'."), fullfilename );
161  wxMessageBox( msg );
162  }
163 }
164 
165 
166 void PCB_CALCULATOR_FRAME::OnAddRegulator( wxCommandEvent& event )
167 {
168  DIALOG_REGULATOR_FORM dlg( this, wxEmptyString );
169 
170  if( dlg.ShowModal() != wxID_OK )
171  return;
172 
173  REGULATOR_DATA* new_item = dlg.BuildRegulatorFromData();
174 
175  // Add new item, if not existing
176  if( m_RegulatorList.GetReg( new_item->m_Name ) == nullptr )
177  {
178  // Add item in list
179  m_RegulatorList.Add( new_item );
180  m_RegulatorListChanged = true;
181  m_choiceRegulatorSelector->Clear();
185  }
186  else
187  {
188  wxMessageBox( _("This regulator is already in list. Aborted") );
189  delete new_item;
190  }
191 }
192 
193 
194 void PCB_CALCULATOR_FRAME::OnEditRegulator( wxCommandEvent& event )
195 {
196  wxString name = m_choiceRegulatorSelector->GetStringSelection();
198 
199  if( item == nullptr )
200  return;
201 
202  DIALOG_REGULATOR_FORM dlg( this, name );
203 
204  dlg.CopyRegulatorDataToDialog( item );
205 
206  if( dlg.ShowModal() != wxID_OK )
207  return;
208 
209  REGULATOR_DATA * new_item = dlg.BuildRegulatorFromData();
210  m_RegulatorList.Replace( new_item );
211 
212  m_RegulatorListChanged = true;
213 
215 }
216 
217 
218 void PCB_CALCULATOR_FRAME::OnRemoveRegulator( wxCommandEvent& event )
219 {
220  wxString name = wxGetSingleChoice( _("Remove Regulator"), wxEmptyString,
222  if( name.IsEmpty() )
223  return;
224 
226  m_RegulatorListChanged = true;
227  m_choiceRegulatorSelector->Clear();
229 
232 
234 }
235 
236 
238 {
239  // Find last selected in regulator list:
240  int idx = -1;
241 
242  if( !m_lastSelectedRegulatorName.IsEmpty() )
243  {
244  for( unsigned ii = 0; ii < m_RegulatorList.GetCount(); ii++ )
245  {
247  {
248  idx = ii;
249  break;
250  }
251  }
252  }
253 
254  m_choiceRegulatorSelector->SetSelection( idx );
255  wxCommandEvent event;
256  OnRegulatorSelection( event );
257 }
258 
259 
261 {
262  int id;
263 
264  if( m_rbRegulR1->GetValue() )
265  {
266  id = 0; // for R1 calculation
267  }
268  else if( m_rbRegulR2->GetValue() )
269  {
270  id = 1; // for R2 calculation
271  }
272  else if( m_rbRegulVout->GetValue() )
273  {
274  id = 2; // for Vout calculation
275  }
276  else
277  {
278  wxMessageBox( wxT("Selection error" ) );
279  return;
280  }
281 
282  double r1, r2, vref, vout;
283 
284  wxString txt;
285 
286  m_RegulMessage->SetLabel( wxEmptyString);
287 
288  // Convert r1 and r2 in ohms
289  int r1scale = 1000;
290  int r2scale = 1000;
291 
292  // Read values from panel:
293  txt = m_RegulR1Value->GetValue();
294  r1 = DoubleFromString(txt) * r1scale;
295  txt = m_RegulR2Value->GetValue();
296  r2 = DoubleFromString(txt) * r2scale;
297  txt = m_RegulVrefValue->GetValue();
298  vref = DoubleFromString(txt);
299  txt = m_RegulVoutValue->GetValue();
300  vout = DoubleFromString(txt);
301 
302  // Some tests:
303  if( vout < vref && id != 2 )
304  {
305  m_RegulMessage->SetLabel( _("Vout must be greater than vref" ) );
306  return;
307  }
308 
309  if( vref == 0.0 )
310  {
311  m_RegulMessage->SetLabel( _( "Vref set to 0 !" ) );
312  return;
313  }
314 
315  if( ( r1 < 0 && id != 0 ) || ( r2 <= 0 && id != 1 ) )
316  {
317  m_RegulMessage->SetLabel( _("Incorrect value for R1 R2" ) );
318  return;
319  }
320 
321  // Calculate
322  if( m_choiceRegType->GetSelection() == 1)
323  {
324  // 3 terminal regulator
325  txt = m_RegulIadjValue->GetValue();
326  double iadj = DoubleFromString(txt);
327 
328  // iadj is given in micro amp, so convert it in amp.
329  iadj /= 1000000;
330 
331  switch( id )
332  {
333  case 0:
334  r1 = vref * r2 / ( vout - vref - ( r2 * iadj ) );
335  break;
336 
337  case 1:
338  r2 = ( vout - vref ) / ( iadj + ( vref / r1 ) );
339  break;
340 
341  case 2:
342  vout = vref * ( r1 + r2 ) / r1;
343  vout += r2 * iadj;
344  break;
345  }
346  }
347  else
348  { // Standard 4 terminal regulator
349  switch( id )
350  {
351  case 0: r1 = ( vout / vref - 1 ) * r2; break;
352  case 1: r2 = r1 / ( vout / vref - 1 ); break;
353  case 2: vout = vref * ( r1 + r2 ) / r2; break;
354  }
355  }
356 
357  // write values to panel:
358  txt.Printf( wxT( "%g" ), r1 / r1scale );
359  m_RegulR1Value->SetValue( txt );
360  txt.Printf( wxT( "%g" ), r2 / r2scale );
361  m_RegulR2Value->SetValue( txt );
362  txt.Printf( wxT( "%g" ), vref );
363  m_RegulVrefValue->SetValue( txt );
364  txt.Printf( wxT( "%g" ), vout );
365  m_RegulVoutValue->SetValue( txt );
366 }
367 
368 
370 {
371  // Save current parameter values in config.
372  aCfg->m_Regulators.r1 = m_RegulR1Value->GetValue();
373  aCfg->m_Regulators.r2 = m_RegulR2Value->GetValue();
374  aCfg->m_Regulators.vref = m_RegulVrefValue->GetValue();
375  aCfg->m_Regulators.vout = m_RegulVoutValue->GetValue();
378  aCfg->m_Regulators.type = m_choiceRegType->GetSelection();
379 
380  // Store the parameter selection that was recently calculated (R1, R2 or Vout)
381  wxRadioButton * regprms[3] =
382  {
384  };
385 
386  for( int ii = 0; ii < 3; ii++ )
387  {
388  if( regprms[ii]->GetValue() )
389  {
390  aCfg->m_Regulators.last_param = ii;
391  break;
392  }
393  }
394 }
const wxString DataFileNameExt
void RegulatorPageUpdate()
Update the regulator page dialog display.
void OnRegulatorSelection(wxCommandEvent &event) override
void OnRegulatorResetButtonClick(wxCommandEvent &event) override
REGULATOR_DATA * BuildRegulatorFromData()
Create a new REGULATOR_DATA from dialog data.
std::vector< REGULATOR_DATA * > m_List
REGULATOR_DATA * GetReg(const wxString &aName)
void OnRegulTypeSelection(wxCommandEvent &event) override
Subclass of DIALOG_REGULATOR_FORM_BASE, which is generated by wxFormBuilder.
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
Contains structures for storage of regulator data.
void Add(REGULATOR_DATA *aItem)
#define _(s)
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:56
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)
Transfer data from dialog to aItem.
void Regulators_WriteConfig(PCB_CALCULATOR_SETTINGS *aCfg)
Write regulators parameters in configuration.
void SelectLastSelectedRegulator()
If m_lastSelectedRegulatorName is empty, just calls RegulatorPageUpdate()