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 extern double DoubleFromString( const wxString& TextValue );
22 
28 //#define BENCHMARK
29 
39 enum { E1, E3, E6, E12 };
40 
45 enum { S2R, S3R, S4R };
46 
52 #define E1_VAL { true, "1K", 1000 },\
53  { true, "10K", 10000 },\
54  { true, "100K", 100000 },\
55  { true, "10R", 10 },\
56  { true, "100R", 100 },\
57  { true, "1M", 1000000 }
58 
59 #define E3_ADD { true, "22R", 22 },\
60  { true, "47R", 47 },\
61  { true, "220R", 220 },\
62  { true, "470R", 470 },\
63  { true, "2K2", 2200 },\
64  { true, "4K7", 4700 },\
65  { true, "22K", 22000 },\
66  { true, "47K", 47000 },\
67  { true, "220K", 220000 },\
68  { true, "470K", 470000 }
69 
70 #define E6_ADD { true, "15R", 15 },\
71  { true, "33R", 33 },\
72  { true, "68R", 68 },\
73  { true, "150R", 150 },\
74  { true, "330R", 330 },\
75  { true, "680R", 680 },\
76  { true, "1K5", 1500 },\
77  { true, "3K3", 3300 },\
78  { true, "6K8", 6800 },\
79  { true, "15K", 15000 },\
80  { true, "33K", 33000 },\
81  { true, "68K", 68000 },\
82  { true, "150K", 150000 },\
83  { true, "330K", 330000 },\
84  { true, "680K", 680000 }
85 
86 #define E12_ADD { true, "12R", 12 },\
87  { true, "18R", 18 },\
88  { true, "27R", 27 },\
89  { true, "39R", 39 },\
90  { true, "56R", 56 },\
91  { true, "82R", 82 },\
92  { true, "120R", 120 },\
93  { true, "180R", 180 },\
94  { true, "270R", 270 },\
95  { true, "390R", 390 },\
96  { true, "560R", 560 },\
97  { true, "820R", 820 },\
98  { true, "1K2", 1200 },\
99  { true, "1K8", 1800 },\
100  { true, "2K7", 2700 },\
101  { true, "3K9", 3900 },\
102  { true, "5K6", 5600 },\
103  { true, "8K2", 8200 },\
104  { true, "12K", 12000 },\
105  { true, "18K", 18000 },\
106  { true, "27K", 27000 },\
107  { true, "39K", 39000 },\
108  { true, "56K", 56000 },\
109  { true, "82K", 82000 },\
110  { true, "120K", 120000 },\
111  { true, "180K", 180000 },\
112  { true, "270K", 270000 },\
113  { true, "390K", 390000 },\
114  { true, "560K", 560000 },\
115  { true, "820K", 820000 }
116 
117 struct r_data {
118  bool e_use;
119  std::string e_name;
120  double e_value;
121  };
122 
123 class eserie
124 {
125 public:
132  void Exclude( double aValue );
133 
137  void NewCalc( void );
138 
142  void Calculate( void );
143 
147  void SetSeries( uint32_t aSeries ) { m_series = aSeries; }
148  void SetRequiredValue( double aValue ) { m_required_value = aValue; }
149 
150  std::array<r_data,S4R+1> get_rslt( void ) { return m_results; }
151 
152 private:
159  uint32_t combine2( void );
160 
167  void simple_solution( uint32_t aSize );
168 
176  void combine3( uint32_t aSize );
177 
186  void combine4( uint32_t aSize );
187 
188  /*
189  * Strip redundant braces from three component result
190  *
191  * Example: R1+(R2+R3) become R1+R2+R3
192  * and R1|(R2|R3) become R1|R2|R3
193  * while R1+(R2|R3) or (R1+R2)|R3) remains untouched
194  */
195  void strip3( void );
196 
197  /*
198  * Strip redundant braces from four component result
199  *
200  * Example: (R1+R2)+(R3+R4) become R1+R2+R3+R4
201  * and (R1|R2)|(R2|R3) become R1|R2|R3|R4
202  * while (R1+R2)|(R3+R4) remains untouched
203  */
204  void strip4( void );
205 
206 private:
207  std::vector<std::vector<r_data>> luts {
208  { E1_VAL },
209  { E1_VAL, E3_ADD },
210  { E1_VAL, E3_ADD, E6_ADD },
211  { E1_VAL, E3_ADD, E6_ADD, E12_ADD }
212  };
213  /*
214  * TODO: Manual array size calculation is dangerous. Unlike legacy ANSI-C Arrays
215  * std::array can not drop length param by providing aggregate init list up
216  * to C++17. Reserved array size should be 2*E12² of std::vector primary list.
217  * Exceeding memory limit 7442 will crash the calculator without any warnings !
218  * Compare to previous MAX_COMB macro for legacy ANSI-C array automatic solution
219  * #define E12_SIZE sizeof ( e12_lut ) / sizeof ( r_data )
220  * #define MAX_COMB (2 * E12_SIZE * E12_SIZE)
221  * 2 component combinations including redundant swappable terms are for the moment
222  * 72 combinations for E1
223  * 512 combinations for E3
224  * 1922 combinations for E6
225  * 7442 combinations for E12
226  */
227 
228 #define MAX_CMB 7442 // maximum combinations for E12
229 
230  std::array<r_data, MAX_CMB> m_cmb_lut; // intermediate 2R combinations
231  std::array<r_data, S4R+1> m_results; // 2R, 3R and 4R results
232  uint32_t m_series = E6; // Radio Button State
233  uint32_t m_enable_4R = false; // Check Box 4R enable
234  double m_required_value; // required Resistor
235 };
double DoubleFromString(const wxString &TextValue)
bool e_use
Definition: eserie.h:118
Definition: eserie.h:45
Definition: eserie.h:39
void Calculate(void)
called on calculate button to execute all the 2R, 3R and 4R calculations
Definition: eserie.cpp:234
Definition: eserie.h:123
void combine3(uint32_t aSize)
Check if there is a better 3 R solution than previous one using only two components.
Definition: eserie.cpp:180
Definition: eserie.h:39
void strip4(void)
Definition: eserie.cpp:268
Definition: eserie.h:39
Definition: eserie.h:45
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:45
void NewCalc(void)
initialize next calculation and erase results from previous calculation
Definition: eserie.cpp:133
std::array< r_data, S4R+1 > m_results
Definition: eserie.h:231
double e_value
Definition: eserie.h:120
double m_required_value
Definition: eserie.h:234
Definition: eserie.h:117
Definition: eserie.h:45
void SetSeries(uint32_t aSeries)
Interface for CheckBox, RadioButton, RequriedResistor and calculated Results.
Definition: eserie.h:147
#define E3_ADD
Definition: eserie.h:59
void SetRequiredValue(double aValue)
Definition: eserie.h:148
std::array< r_data, MAX_CMB > m_cmb_lut
Definition: eserie.h:230
void combine4(uint32_t aSize)
Check if there is a better four component solution.
Definition: eserie.cpp:76
#define E1_VAL
6 decade E-series values from 10 Ohms to 1M and its associated BOM strings.
Definition: eserie.h:52
#define E12_ADD
Definition: eserie.h:86
std::array< r_data, S4R+1 > get_rslt(void)
Definition: eserie.h:150
void strip3(void)
Definition: eserie.cpp:249
uint32_t m_series
Definition: eserie.h:232
void simple_solution(uint32_t aSize)
Search for closest two component solution.
Definition: eserie.cpp:58
uint32_t combine2(void)
Build all 2R combinations from the selected E-serie values.
Definition: eserie.cpp:146
#define E6_ADD
Definition: eserie.h:70
std::string e_name
Definition: eserie.h:119
uint32_t m_enable_4R
Definition: eserie.h:233
Definition: eserie.h:39
std::vector< std::vector< r_data > > luts
Definition: eserie.h:207