KiCad PCB EDA Suite
eseries.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 <[email protected]>
5 * Copyright (C) 2020-2022 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#include <cstdint>
25
35// List of normalized values between 1 and 10
36// The terminal 0.0 value is a end of list value
37// Note also due to calculation time the E24 serie is the biggest usable.
38#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,\
39 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
40
41#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
42
43#define E6_VALUES 1.0, 1.5, 2.2, 3.3, 4.7, 6.8, 0.0
44
45#define E3_VALUES 1.0, 2.2, 4.7, 0.0
46
47#define E1_VALUES 1.0, 0.0
48
49
50// First value of resistor in ohm
51#define FIRST_VALUE 10
52
53// last value of resistor in ohm
54#define LAST_VALUE 1e6
55
62enum { E1, E3, E6, E12, E24 };
63
67enum { S2R, S3R, S4R };
68
69// R_DATA handles a resistor: string value, value and allowed to use
70struct R_DATA
71{
73 e_use( true ),
74 e_value( 0.0 )
75 {}
76
77 R_DATA( const std::string& aName, double aValue )
78 {
79 e_use = true;
80 e_name = aName;
81 e_value = aValue;
82 }
83
84 bool e_use;
85 std::string e_name;
86 double e_value;
87};
88
90{
91public:
92 E_SERIES();
93
100 void Exclude( double aValue );
101
105 void NewCalc();
106
110 void Calculate();
111
115 void SetSeries( uint32_t aSeries ) { m_series = aSeries; }
116 void SetRequiredValue( double aValue ) { m_required_value = aValue; }
117
118 // Accessor:
119 const std::array<R_DATA,S4R+1>& GetResults() { return m_results; }
120
121private:
126 int buildSeriesData( const double aList[] );
127
134 uint32_t combine2();
135
142 void simple_solution( uint32_t aSize );
143
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
182private:
183 std::vector<std::vector<R_DATA>> m_tables;
184
185 /* Note: intermediate calculations use m_combined_table
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_combined_table; // 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 double m_required_value = 0.0; // required Resistor
200};
void combine4(uint32_t aSize)
Check if there is a better four component solution.
Definition: eseries.cpp:176
void SetSeries(uint32_t aSeries)
Interface for CheckBox, RadioButton, RequriedResistor and calculated Results.
Definition: eseries.h:115
void Exclude(double aValue)
If any value of the selected E-series not available, it can be entered as an exclude value.
Definition: eseries.cpp:145
void strip3()
Definition: eseries.cpp:348
E_SERIES()
Definition: eseries.cpp:81
uint32_t combine2()
Build all 2R combinations from the selected E-series values.
Definition: eseries.cpp:245
int buildSeriesData(const double aList[])
Add values from aList to m_tables.
Definition: eseries.cpp:109
std::vector< R_DATA > m_combined_table
Definition: eseries.h:195
void Calculate()
called on calculate button to execute all the 2R, 3R and 4R calculations
Definition: eseries.cpp:333
std::vector< std::vector< R_DATA > > m_tables
Definition: eseries.h:183
void NewCalc()
initialize next calculation and erase results from previous calculation
Definition: eseries.cpp:232
std::array< R_DATA, S4R+1 > m_results
Definition: eseries.h:197
void simple_solution(uint32_t aSize)
Search for closest two component solution.
Definition: eseries.cpp:158
void SetRequiredValue(double aValue)
Definition: eseries.h:116
uint32_t m_series
Definition: eseries.h:198
double m_required_value
Definition: eseries.h:199
void strip4()
Definition: eseries.cpp:367
const std::array< R_DATA, S4R+1 > & GetResults()
Definition: eseries.h:119
void combine3(uint32_t aSize)
Check if there is a better 3 R solution than previous one using only two components.
Definition: eseries.cpp:278
@ S3R
Definition: eseries.h:67
@ S2R
Definition: eseries.h:67
@ S4R
Definition: eseries.h:67
@ E6
Definition: eseries.h:62
@ E3
Definition: eseries.h:62
@ E24
Definition: eseries.h:62
@ E1
Definition: eseries.h:62
@ E12
Definition: eseries.h:62
Definition: eseries.h:71
bool e_use
Definition: eseries.h:84
double e_value
Definition: eseries.h:86
R_DATA(const std::string &aName, double aValue)
Definition: eseries.h:77
R_DATA()
Definition: eseries.h:72
std::string e_name
Definition: eseries.h:85