You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

336 lines
15 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include <array>
  24. #include <pcb_calculator_settings.h>
  25. #include <settings/common_settings.h>
  26. #include <settings/parameters.h>
  27. #include <wx/config.h>
  28. ///! Update the schema version whenever a migration is required
  29. const int pcbCalculatorSchemaVersion = 0;
  30. PCB_CALCULATOR_SETTINGS::PCB_CALCULATOR_SETTINGS() :
  31. APP_SETTINGS_BASE( "pcb_calculator", pcbCalculatorSchemaVersion )
  32. {
  33. // Make Coverity happy:
  34. m_BoardClassUnits = 0;
  35. m_ColorCodeTolerance = 0;
  36. m_LastPage = 0;
  37. // Build settings:
  38. m_params.emplace_back( new PARAM<int>( "board_class_units", &m_BoardClassUnits, 0 ) );
  39. m_params.emplace_back( new PARAM<int>( "color_code_tolerance", &m_ColorCodeTolerance, 0 ) );
  40. m_params.emplace_back( new PARAM<int>( "last_page", &m_LastPage, 0 ) );
  41. m_params.emplace_back( new PARAM<int>( "translines.type", &m_TransLine.type, 0 ) );
  42. m_params.emplace_back( new PARAM<int>( "attenuators.type", &m_Attenuators.type, 0 ) );
  43. const std::array<std::string, 4> att_names = { "att_pi", "att_tee",
  44. "att_bridge", "att_splitter" };
  45. for( const auto& att_name : att_names )
  46. {
  47. std::string path = "attenuators." + att_name;
  48. m_Attenuators.attenuators[ att_name ] = ATTENUATOR();
  49. ATTENUATOR* att = &m_Attenuators.attenuators[ att_name ];
  50. m_params.emplace_back( new PARAM<double>( path + ".attenuation", &att->attenuation, 6.0 ) );
  51. m_params.emplace_back( new PARAM<double>( path + ".zin", &att->zin, 50.0 ) );
  52. m_params.emplace_back( new PARAM<double>( path + ".zout", &att->zout, 50.0 ) );
  53. }
  54. m_params.emplace_back( new PARAM<int>( "electrical.spacing_units",
  55. &m_Electrical.spacing_units, 0 ) );
  56. m_params.emplace_back( new PARAM<wxString>( "electrical.spacing_voltage",
  57. &m_Electrical.spacing_voltage, "500" ) );
  58. m_params.emplace_back( new PARAM<wxString>( "regulators.r1", &m_Regulators.r1, "10" ) );
  59. m_params.emplace_back( new PARAM<wxString>( "regulators.r2", &m_Regulators.r2, "10" ) );
  60. m_params.emplace_back( new PARAM<wxString>( "regulators.vref", &m_Regulators.vref, "3" ) );
  61. m_params.emplace_back( new PARAM<wxString>( "regulators.vout", &m_Regulators.vout, "12" ) );
  62. m_params.emplace_back( new PARAM<wxString>( "regulators.data_file",
  63. &m_Regulators.data_file, "" ) );
  64. m_params.emplace_back( new PARAM<wxString>( "regulators.selected_regulator",
  65. &m_Regulators.selected_regulator, "" ) );
  66. m_params.emplace_back( new PARAM<int>( "regulators.type", &m_Regulators.type, 0 ) );
  67. m_params.emplace_back( new PARAM<int>( "regulators.last_param", &m_Regulators.last_param, 0 ) );
  68. m_params.emplace_back( new PARAM<wxString>( "track_width.current",
  69. &m_TrackWidth.current, "1.0" ) );
  70. m_params.emplace_back( new PARAM<wxString>( "track_width.delta_tc",
  71. &m_TrackWidth.delta_tc, "10.0" ) );
  72. m_params.emplace_back( new PARAM<wxString>( "track_width.track_len",
  73. &m_TrackWidth.track_len, "20" ) );
  74. m_params.emplace_back( new PARAM<int>( "track_width.track_len_units",
  75. &m_TrackWidth.track_len_units, 0 ) );
  76. m_params.emplace_back( new PARAM<wxString>( "track_width.resistivity",
  77. &m_TrackWidth.resistivity, "1.72e-8" ) );
  78. m_params.emplace_back( new PARAM<wxString>( "track_width.ext_track_width",
  79. &m_TrackWidth.ext_track_width, "0.2" ) );
  80. m_params.emplace_back( new PARAM<int>( "track_width.ext_track_width_units",
  81. &m_TrackWidth.ext_track_width_units, 0 ) );
  82. m_params.emplace_back( new PARAM<wxString>( "track_width.ext_track_thickness",
  83. &m_TrackWidth.ext_track_thickness, "0.035" ) );
  84. m_params.emplace_back( new PARAM<int>( "track_width.ext_track_thickness_units",
  85. &m_TrackWidth.ext_track_thickness_units, 0 ) );
  86. m_params.emplace_back( new PARAM<wxString>( "track_width.int_track_width",
  87. &m_TrackWidth.int_track_width, "0.2" ) );
  88. m_params.emplace_back( new PARAM<int>( "track_width.int_track_width_units",
  89. &m_TrackWidth.int_track_width_units, 0 ) );
  90. m_params.emplace_back( new PARAM<wxString>( "track_width.int_track_thickness",
  91. &m_TrackWidth.int_track_thickness, "0.035" ) );
  92. m_params.emplace_back( new PARAM<int>( "track_width.int_track_thickness_units",
  93. &m_TrackWidth.int_track_thickness_units, 0 ) );
  94. {
  95. const std::array<std::string, 8> transline_names = { "MicroStrip", "CoPlanar", "GrCoPlanar",
  96. "RectWaveGuide", "Coax", "Coupled_MicroStrip", "StripLine", "TwistedPair" };
  97. for( const auto& name : transline_names )
  98. {
  99. m_TransLine.param_values[ name ] = TL_PARAM_MAP();
  100. m_TransLine.param_units[ name ] = TL_PARAM_UNITS_MAP();
  101. std::string path = "trans_line." + name + ".";
  102. m_params.emplace_back( new PARAM_MAP<double>( path + "values",
  103. &m_TransLine.param_values.at( name ), {} ) );
  104. m_params.emplace_back( new PARAM_MAP<int>( path + "units",
  105. &m_TransLine.param_units.at( name ), {} ) );
  106. }
  107. }
  108. m_params.emplace_back( new PARAM<wxString>( "via_size.hole_diameter",
  109. &m_ViaSize.hole_diameter, "0.4" ) );
  110. m_params.emplace_back( new PARAM<int>( "via_size.hole_diameter_units",
  111. &m_ViaSize.hole_diameter_units, 0 ) );
  112. m_params.emplace_back( new PARAM<wxString>( "via_size.thickness",
  113. &m_ViaSize.thickness, "0.035" ) );
  114. m_params.emplace_back( new PARAM<int>( "via_size.thickness_units",
  115. &m_ViaSize.thickness_units, 0 ) );
  116. m_params.emplace_back( new PARAM<wxString>( "via_size.length",
  117. &m_ViaSize.length, "1.6" ) );
  118. m_params.emplace_back( new PARAM<int>( "via_size.length_units", &m_ViaSize.length_units, 0 ) );
  119. m_params.emplace_back( new PARAM<wxString>( "via_size.pad_diameter",
  120. &m_ViaSize.pad_diameter, "0.6" ) );
  121. m_params.emplace_back( new PARAM<int>( "via_size.pad_diameter_units",
  122. &m_ViaSize.pad_diameter_units, 0 ) );
  123. m_params.emplace_back( new PARAM<wxString>( "via_size.clearance_diameter",
  124. &m_ViaSize.clearance_diameter, "1.0" ) );
  125. m_params.emplace_back( new PARAM<int>( "via_size.clearance_diameter_units",
  126. &m_ViaSize.clearance_diameter_units, 0 ) );
  127. m_params.emplace_back( new PARAM<wxString>( "via_size.characteristic_impedance",
  128. &m_ViaSize.characteristic_impedance, "50" ) );
  129. m_params.emplace_back( new PARAM<int>( "via_size.characteristic_impedance_units",
  130. &m_ViaSize.characteristic_impedance_units, 0 ) );
  131. m_params.emplace_back( new PARAM<wxString>( "via_size.applied_current",
  132. &m_ViaSize.applied_current, "1" ) );
  133. m_params.emplace_back( new PARAM<wxString>( "via_size.plating_resistivity",
  134. &m_ViaSize.plating_resistivity, "1.72e-8" ) );
  135. m_params.emplace_back( new PARAM<wxString>( "via_size.permittivity",
  136. &m_ViaSize.permittivity, "4.5" ) );
  137. m_params.emplace_back( new PARAM<wxString>( "via_size.temp_rise",
  138. &m_ViaSize.temp_rise, "10" ) );
  139. m_params.emplace_back( new PARAM<wxString>( "via_size.pulse_rise_time",
  140. &m_ViaSize.pulse_rise_time, "1" ) );
  141. }
  142. bool PCB_CALCULATOR_SETTINGS::MigrateFromLegacy( wxConfigBase* aCfg )
  143. {
  144. bool ret = APP_SETTINGS_BASE::MigrateFromLegacy( aCfg );
  145. ret &= fromLegacy<int>( aCfg, "BrdClass_selection", "board_class_units" );
  146. ret &= fromLegacy<int>( aCfg, "CC_selection", "color_code_tolerance" );
  147. ret &= fromLegacy<int>( aCfg, "Page_selection", "last_page" );
  148. ret &= fromLegacy<int>( aCfg, "Att_selection", "attenuators.type" );
  149. {
  150. nlohmann::json::json_pointer ptr = PointerFromString( "attenuators" );
  151. const std::array<std::string, 4> att_names = { "att_pi", "att_tee",
  152. "att_bridge", "att_splitter" };
  153. double val = 0;
  154. for( const auto& att : att_names )
  155. {
  156. aCfg->SetPath( "Attenuators/" + att );
  157. ptr.push_back( att );
  158. if( aCfg->Read( "Attenuation", &val ) )
  159. ( *this )[ptr]["attenuation"] = val;
  160. if( aCfg->Read( "Zin", &val ) )
  161. ( *this )[ptr]["zin"] = val;
  162. if( aCfg->Read( "Zout", &val ) )
  163. ( *this )[ptr]["zout"] = val;
  164. ptr.pop_back();
  165. aCfg->SetPath( "../.." );
  166. }
  167. }
  168. ret &= fromLegacy<int>( aCfg, "ElectSpacing_selection", "electrical.spacing_units" );
  169. ret &= fromLegacyString( aCfg, "ElectSpacing_voltage", "electrical.spacing_voltage" );
  170. ret &= fromLegacyString( aCfg, "RegulR1", "regulators.r1" );
  171. ret &= fromLegacyString( aCfg, "RegulR2", "regulators.r2" );
  172. ret &= fromLegacyString( aCfg, "RegulVREF", "regulators.vref" );
  173. ret &= fromLegacyString( aCfg, "RegulVOUT", "regulators.vout" );
  174. ret &= fromLegacyString( aCfg, "DataFilename", "regulators.data_file" );
  175. ret &= fromLegacyString( aCfg, "RegulName", "regulators.selected_regulator" );
  176. ret &= fromLegacy<int>( aCfg, "RegulType", "regulators.type" );
  177. ret &= fromLegacy<int>( aCfg, "RegulLastParam", "regulators.last_param" );
  178. ret &= fromLegacyString( aCfg, "TW_Track_Current", "track_width.current" );
  179. ret &= fromLegacyString( aCfg, "TW_Delta_TC", "track_width.delta_tc" );
  180. ret &= fromLegacyString( aCfg, "TW_Track_Len", "track_width.track_len" );
  181. ret &= fromLegacy<int>( aCfg, "TW_Track_Len_Unit", "track_width.track_len_units" );
  182. ret &= fromLegacyString( aCfg, "TW_Resistivity", "track_width.resistivity" );
  183. ret &= fromLegacyString( aCfg, "TW_ExtTrack_Width", "track_width.ext_track_width" );
  184. ret &= fromLegacy<int>( aCfg, "TW_ExtTrack_Width_Unit", "track_width.ext_track_width_units" );
  185. ret &= fromLegacyString( aCfg, "TW_ExtTrack_Thickness", "track_width.ext_track_thickness" );
  186. ret &= fromLegacy<int>( aCfg, "TW_ExtTrack_Thickness_Unit",
  187. "track_width.ext_track_thickness_units" );
  188. ret &= fromLegacyString( aCfg, "TW_IntTrack_Width", "track_width.int_track_width" );
  189. ret &= fromLegacy<int>( aCfg, "TW_IntTrack_Width_Unit", "track_width.int_track_width_units" );
  190. ret &= fromLegacyString( aCfg, "TW_IntTrack_Thickness", "track_width.int_track_thickness" );
  191. ret &= fromLegacy<int>( aCfg, "TW_IntTrack_Thickness_Unit",
  192. "track_width.int_track_thickness_units" );
  193. ret &= fromLegacy<int>( aCfg, "Transline_selection", "trans_line.selection" );
  194. {
  195. nlohmann::json::json_pointer ptr = PointerFromString( "trans_line" );
  196. wxString key;
  197. double value = 0;
  198. int units = 0;
  199. const std::array<std::string, 8> transline_names = { "MicroStrip", "CoPlanar", "GrCoPlanar",
  200. "RectWaveGuide", "Coax", "Coupled_MicroStrip", "StripLine", "TwistedPair" };
  201. for( const auto& name : transline_names )
  202. {
  203. long index = 0;
  204. aCfg->SetPath( name );
  205. ptr.push_back( name );
  206. while( aCfg->GetNextEntry( key, index ) )
  207. {
  208. // Keys look like "translineprmN" and "translineprmNunit"
  209. wxString dest = key;
  210. dest.Replace( "translineprm", wxEmptyString );
  211. if( dest.EndsWith( "unit" ) )
  212. {
  213. dest.Replace( "unit", wxEmptyString );
  214. aCfg->Read( key, &units );
  215. ptr.push_back( "units" );
  216. ( *this )[ptr].push_back( { { dest.ToStdString(), units } } );
  217. ptr.pop_back();
  218. }
  219. else
  220. {
  221. aCfg->Read( key, &value );
  222. ptr.push_back( "values" );
  223. ( *this )[ptr].push_back( { { dest.ToStdString(), value } } );
  224. ptr.pop_back();
  225. }
  226. }
  227. ptr.pop_back();
  228. aCfg->SetPath( ".." );
  229. }
  230. }
  231. ret &= fromLegacyString( aCfg, "VS_Hole_Dia", "via_size.hole_diameter" );
  232. ret &= fromLegacy<int>( aCfg, "VS_Hole_Dia_Unit", "via_size.hole_diameter_units" );
  233. ret &= fromLegacyString( aCfg, "VS_Plating_Thickness", "via_size.thickness" );
  234. ret &= fromLegacy<int>( aCfg, "VS_Plating_Thickness_Unit", "via_size.thickness_units" );
  235. ret &= fromLegacyString( aCfg, "VS_Via_Length", "via_size.length" );
  236. ret &= fromLegacy<int>( aCfg, "VS_Via_Length_Unit", "via_size.length_units" );
  237. ret &= fromLegacyString( aCfg, "VS_Pad_Dia", "via_size.pad_diameter" );
  238. ret &= fromLegacy<int>( aCfg, "VS_Pad_Dia_Unit", "via_size.pad_diameter_units" );
  239. ret &= fromLegacyString( aCfg, "VS_Clearance_Dia", "via_size.clearance_diameter" );
  240. ret &= fromLegacy<int>( aCfg, "VS_Clearance_Dia_Unit",
  241. "via_size.clearance_diameter_units" );
  242. ret &= fromLegacyString( aCfg, "VS_Characteristic_Impedance",
  243. "via_size.characteristic_impedance" );
  244. ret &= fromLegacy<int>( aCfg, "VS_Characteristic_Impedance_Unit",
  245. "via_size.characteristic_impedance_units" );
  246. ret &= fromLegacyString( aCfg, "VS_Current", "via_size.applied_current" );
  247. ret &= fromLegacyString( aCfg, "VS_Resistivity", "via_size.plating_resistivity" );
  248. ret &= fromLegacyString( aCfg, "VS_Permittivity", "via_size.permittivity" );
  249. ret &= fromLegacyString( aCfg, "VS_Temperature_Differential", "via_size.temp_rise" );
  250. ret &= fromLegacyString( aCfg, "VS_Pulse_Rise_Time", "via_size.pulse_rise_time" );
  251. return ret;
  252. }