KiCad PCB EDA Suite
eserie.h
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) 2020 <janvi@veith.net>
5  * Copyright (C) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation, either version 3 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * 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 
21 #include <array>
22 #include <vector>
23 #include <string>
24 
34 // List of normalized values between 1 and 10
35 // The terminal 0.0 value is a end of list value
36 // Note also due to calculation time the E24 serie is the biggest usable.
37 #define E24_VALUES 1.0, 1.1, 1.2, 1.3, 1.5, 1.6, 1.8, 2.0, 2.2, 2.4, 2.7, 3.0,\
38  3.3, 3.6, 3.9, 4.3, 4.7, 5.1, 5.6, 6.2, 6.8, 7.5, 8.2, 9.1, 0.0
39 
40 #define E12_VALUES 1.0, 1.2, 1.5, 1.8, 2.2, 2.7, 3.3, 3.9, 4.7, 5.6, 6.8, 8.2, 0.0
41 
42 #define E6_VALUES 1.0, 1.5, 2.2, 3.3, 4.7, 6.8, 0.0
43 
44 #define E3_VALUES 1.0, 2.2, 4.7, 0.0
45 
46 #define E1_VALUES 1.0, 0.0
47 
48 
49 // First value of resistor in ohm
50 #define FIRST_VALUE 10
51 
52 // last value of resistor in ohm
53 #define LAST_VALUE 1e6
54 
61 enum { E1, E3, E6, E12, E24 };
62 
66 enum { S2R, S3R, S4R };
67 
68 // R_DATA handles a resitor: string value, value and allowed to use
69 struct R_DATA
70 {
71  R_DATA() :
72  e_use( true ),
73  e_value( 0.0 )
74  {}
75 
76  R_DATA( const std::string& aName, double aValue )
77  {
78  e_use = true;
79  e_name = aName;
80  e_value = aValue;
81  }
82 
83  bool e_use;
84  std::string e_name;
85  double e_value;
86 };
87 
88 class E_SERIE
89 {
90 public:
91  E_SERIE();
92 
99  void Exclude( double aValue );
100 
104  void NewCalc();
105 
109  void Calculate();
110 
114  void SetSeries( uint32_t aSeries ) { m_series = aSeries; }
115  void SetRequiredValue( double aValue ) { m_required_value = aValue; }
116 
117  // Accessor:
118  const std::array<R_DATA,S4R+1>& GetResults() { return m_results; }
119 
120 private:
127  int buildSerieData( int aEserie, double aList[] );
128 
135  uint32_t combine2();
136 
143  void simple_solution( uint32_t aSize );
144 
152  void combine3( uint32_t aSize );
153 
162  void combine4( uint32_t aSize );
163 
164  /*
165  * Strip redundant braces from three component result
166  *
167  * Example: R1+(R2+R3) become R1+R2+R3
168  * and R1|(R2|R3) become R1|R2|R3
169  * while R1+(R2|R3) or (R1+R2)|R3) remains untouched
170  */
171  void strip3();
172 
173  /*
174  * Strip redundant braces from four component result
175  *
176  * Example: (R1+R2)+(R3+R4) become R1+R2+R3+R4
177  * and (R1|R2)|(R2|R3) become R1|R2|R3|R4
178  * while (R1+R2)|(R3+R4) remains untouched
179  */
180  void strip4();
181 
182 private:
183  std::vector<std::vector<R_DATA>> m_luts;
184 
185  /* Note: intermediate calculations use m_cmb_lut
186  * if the biggest list is En, reserved array size should be 2*En*En of std::vector primary list.
187  * 2 component combinations including redundant swappable terms are for the moment
188  * ( using values between 10 ohms and 1Mohm )
189  * 72 combinations for E1
190  * 512 combinations for E3
191  * 1922 combinations for E6
192  * 7442 combinations for E12
193  * 29282 combinations for E24
194  */
195  std::vector<R_DATA> m_cmb_lut; // intermediate 2R combinations
196 
197  std::array<R_DATA, S4R+1> m_results; // 2R, 3R and 4R results
198  uint32_t m_series = E6; // Radio Button State
199  uint32_t m_enable_4R = false; // Check Box 4R enable
200  double m_required_value = 0.0; // required Resistor
201 };
std::vector< R_DATA > m_cmb_lut
Definition: eserie.h:195
void NewCalc()
initialize next calculation and erase results from previous calculation
Definition: eserie.cpp:238
void SetRequiredValue(double aValue)
Definition: eserie.h:115
void combine3(uint32_t aSize)
Check if there is a better 3 R solution than previous one using only two components.
Definition: eserie.cpp:284
Definition: eserie.h:66
R_DATA(const std::string &aName, double aValue)
Definition: eserie.h:76
uint32_t m_enable_4R
Definition: eserie.h:199
Definition: eserie.h:69
Definition: eserie.h:61
double e_value
Definition: eserie.h:85
std::vector< std::vector< R_DATA > > m_luts
Definition: eserie.h:183
double m_required_value
Definition: eserie.h:200
void SetSeries(uint32_t aSeries)
Interface for CheckBox, RadioButton, RequriedResistor and calculated Results.
Definition: eserie.h:114
Definition: eserie.h:61
uint32_t combine2()
Build all 2R combinations from the selected E-serie values.
Definition: eserie.cpp:251
void Calculate()
called on calculate button to execute all the 2R, 3R and 4R calculations
Definition: eserie.cpp:338
Definition: eserie.h:61
const std::array< R_DATA, S4R+1 > & GetResults()
Definition: eserie.h:118
uint32_t m_series
Definition: eserie.h:198
Definition: eserie.h:66
E_SERIE()
Definition: eserie.cpp:86
R_DATA()
Definition: eserie.h:71
void Exclude(double aValue)
If any value of the selected E-serie not available, it can be entered as an exclude value.
Definition: eserie.cpp:150
Definition: eserie.h:66
Definition: eserie.h:88
void combine4(uint32_t aSize)
Check if there is a better four component solution.
Definition: eserie.cpp:181
Definition: eserie.h:61
std::string e_name
Definition: eserie.h:84
Definition: eserie.h:61
bool e_use
Definition: eserie.h:83
int buildSerieData(int aEserie, double aList[])
Build the list of R_DATA existing for a given serie Series are E1, E6 .
Definition: eserie.cpp:114
void strip4()
Definition: eserie.cpp:372
std::array< R_DATA, S4R+1 > m_results
Definition: eserie.h:197
void simple_solution(uint32_t aSize)
Search for closest two component solution.
Definition: eserie.cpp:163
void strip3()
Definition: eserie.cpp:353