KiCad PCB EDA Suite
Loading...
Searching...
No Matches
panel_r_calculator.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) 2011 jean-pierre.charras
5 * Copyright The 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
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21/* see
22 * http://www.desmith.net/NMdS/Electronics/TraceWidth.html
23 * http://www.ultracad.com/articles/pcbtemp.pdf
24 * for more info
25 */
26
30#include <string_utils.h>
31#include <wx/msgdlg.h>
32#include <eseries.h>
34
35#include <i18n_utility.h> // For _HKI definition in r_calculator_help.h
37#include "r_calculator_help.h"
38
39/* RES_EQUIV_CALC class considers resistor values from a limited range
40 * and only combinations of up to 4 resistors, so target values less than
41 * parallel combination of minimum resistors or greater than serial combination
42 * of maximum resistors cannot be reasonably looked for
43 */
44static const double min_target_value = static_cast<double>(RES_EQUIV_CALC_FIRST_VALUE) / 4;
45static const double max_target_value = static_cast<double>(RES_EQUIV_CALC_LAST_VALUE) * 4;
46
47PANEL_R_CALCULATOR::PANEL_R_CALCULATOR( wxWindow* parent, wxWindowID id, const wxPoint& pos,
48 const wxSize& size, long style, const wxString& name ) :
49 PANEL_R_CALCULATOR_BASE( parent, id, pos, size, style, name )
50{
51 m_reqResUnits->SetLabel( wxT( "kΩ" ) );
52 m_exclude1Units->SetLabel( wxT( "kΩ" ) );
53 m_exclude2Units->SetLabel( wxT( "kΩ" ) );
54
55 wxSize minSize = m_ResRequired->GetMinSize();
56 int minWidth = m_ResRequired->GetTextExtent( wxT( "XXX.XXX" ) ).GetWidth();
57
58 m_ResRequired->SetMinSize( wxSize( minWidth, minSize.GetHeight() ) );
59 m_ResExclude1->SetMinSize( wxSize( minWidth, minSize.GetHeight() ) );
60 m_ResExclude2->SetMinSize( wxSize( minWidth, minSize.GetHeight() ) );
61
62 minSize = m_ESeriesError2R->GetMinSize();
63 minWidth = m_ESeriesError2R->GetTextExtent( wxT( "XX" ) + _( "Exact" ) ).GetWidth();
64
65 m_ESeriesError2R->SetMinSize( wxSize( minWidth, minSize.GetHeight() ) );
66 m_ESeriesError3R->SetMinSize( wxSize( minWidth, minSize.GetHeight() ) );
67 m_ESeriesError4R->SetMinSize( wxSize( minWidth, minSize.GetHeight() ) );
68
69 // show markdown formula explanation in lower help panel
70 wxString msg;
71 ConvertMarkdown2Html( wxGetTranslation( r_calculator_help ), msg );
72 m_panelESeriesHelp->SetPage( msg );
73
74 // Needed on wxWidgets 3.0 to ensure sizers are correctly set
75 GetSizer()->SetSizeHints( this );
76}
77
78
82
83
85{
86 // Update the HTML window with the help text
87 m_panelESeriesHelp->ThemeChanged();
88}
89
90
94
95
99
100
101void PANEL_R_CALCULATOR::OnCalculateESeries( wxCommandEvent& event )
102{
103 double reqr = 1000 * DoubleFromString( m_ResRequired->GetValue() );
104
105 if( std::isnan( reqr ) || reqr < min_target_value || reqr > max_target_value )
106 {
107 wxMessageBox( wxString::Format( _( "Incorrect required resistance value: %s" ),
108 m_ResRequired->GetValue() ) );
109 return;
110 }
111
112 wxBusyCursor busyCursor; // As long as this variable exists, the cursor will be 'busy'
113
114 m_eSeries.NewCalc( reqr ); // assume all values available
115 /*
116 * Exclude itself. For the case, a value from the available series is found as required value,
117 * the calculator assumes this value needs a replacement for the reason of being not available.
118 * Two further exclude values can be entered to exclude and are skipped as not being available.
119 * All values entered in KiloOhms are converted to Ohm for internal calculation
120 */
121 m_eSeries.Exclude( reqr );
122 m_eSeries.Exclude( 1000 * DoubleFromString( m_ResExclude1->GetValue() ) );
123 m_eSeries.Exclude( 1000 * DoubleFromString( m_ResExclude2->GetValue() ) );
124
125 try
126 {
127 m_eSeries.Calculate();
128 }
129 catch( const std::exception& exc )
130 {
131 wxMessageBox( wxString::Format( "Internal error: %s", exc.what() ) );
132 return;
133 }
134
135 auto showResult = [reqr]( const std::optional<RESISTANCE>& aResult, wxTextCtrl* aFormulaField,
136 wxTextCtrl* aErrorField )
137 {
138 wxString fs, es; // formula and error string
139
140 if( aResult ) // if value is present
141 {
142 fs = aResult->name;
143 double sol = aResult->value;
144 double error = ( sol / reqr - 1 ) * 100; // relative error in percent
145
146 if( std::abs( error ) < epsilon )
147 es = _( "Exact" );
148 else if( std::abs( error ) < 0.01 )
149 es.Printf( "<%.2f", 0.01 );
150 else
151 es.Printf( "%+.2f", error );
152 }
153 else
154 {
155 fs = _( "Not worth using" );
156 es = wxEmptyString;
157 }
158
159 aFormulaField->SetValue( fs );
160 aErrorField->SetValue( es );
161 };
162
166}
167
168
169void PANEL_R_CALCULATOR::OnESeriesSelection( wxCommandEvent& event )
170{
171 if( event.GetEventObject() == m_e1 )
172 m_eSeries.SetSeries( ESERIES::E1 );
173 else if( event.GetEventObject() == m_e3 )
174 m_eSeries.SetSeries( ESERIES::E3 );
175 else if( event.GetEventObject() == m_e12 )
176 m_eSeries.SetSeries( ESERIES::E12 );
177 else if( event.GetEventObject() == m_e24 )
178 m_eSeries.SetSeries( ESERIES::E24 );
179 else
180 m_eSeries.SetSeries( ESERIES::E6 );
181}
const char * name
PANEL_R_CALCULATOR_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxTAB_TRAVERSAL, const wxString &name=wxEmptyString)
PANEL_R_CALCULATOR(wxWindow *parent, wxWindowID id=wxID_ANY, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxTAB_TRAVERSAL, const wxString &name=wxEmptyString)
void OnCalculateESeries(wxCommandEvent &event) override
Called on calculate button and executes all E-series calculations.
void LoadSettings(PCB_CALCULATOR_SETTINGS *aCfg) override
Load the settings into the panel.
void ThemeChanged() override
Update UI elements of the panel when the theme changes to ensure the images and fonts/colors are appr...
RES_EQUIV_CALC m_eSeries
void OnESeriesSelection(wxCommandEvent &event) override
Radio Buttons to select the E-series for the resistor calculator.
void SaveSettings(PCB_CALCULATOR_SETTINGS *aCfg) override
Save the settings from the panel.
const int minSize
Push and Shove router track width and via size dialog.
#define _(s)
Some functions to handle hotkeys in KiCad.
@ E24
Definition eseries.h:74
@ E12
Definition eseries.h:73
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:411
static const double max_target_value
wxString r_calculator_help
double DoubleFromString(const wxString &TextValue)
#define RES_EQUIV_CALC_FIRST_VALUE
#define RES_EQUIV_CALC_LAST_VALUE
const double epsilon
void ConvertMarkdown2Html(const wxString &aMarkdownInput, wxString &aHtmlOutput)