KiCad PCB EDA Suite
sim_model_r_pot.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) 2022 Mikolaj Wielgus
5 * Copyright (C) 2022 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, you may find one here:
19 * https://www.gnu.org/licenses/gpl-3.0.html
20 * or you may search the http://www.gnu.org website for the version 3 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <sim/sim_model_r_pot.h>
26#include <fmt/core.h>
27
28
29std::string SPICE_GENERATOR_R_POT::ModelLine( const SPICE_ITEM& aItem ) const
30{
31 std::string r = m_model.FindParam( "r" )->value->ToSpiceString();
32 std::string position = m_model.FindParam( "pos" )->value->ToSpiceString();
33
34 if( position != "" )
35 {
36 return fmt::format( ".model {} potentiometer( r={} position={} )\n", aItem.modelName, r,
37 position );
38 }
39 else
40 return fmt::format( ".model {} potentiometer( r={} )\n", aItem.modelName, r );
41}
42
43
44std::string SPICE_GENERATOR_R_POT::ItemLine( const SPICE_ITEM& aItem ) const
45{
46 // Swap pin order so that pos=1 is all +, and pos=0 is all -.
47 SPICE_ITEM item = aItem;
48
49 // FIXME: This `if` is there because Preview() calls this function with empty pinNetNames vector.
50 // This shouldn't be necessary.
51 if( item.pinNetNames.size() >= 3 )
52 std::swap( item.pinNetNames.at( 0 ), item.pinNetNames.at( 2 ) );
53
54 return SPICE_GENERATOR::ItemLine( item );
55}
56
57
59 const SIM_VALUE_FLOAT& aValue ) const
60{
61 return fmt::format( "altermod @{}[position]={}",
62 aItem.model->SpiceGenerator().ItemName( aItem ), aValue.ToSpiceString() );
63}
64
65
67 SIM_MODEL( TYPE::R_POT, std::make_unique<SPICE_GENERATOR_R_POT>( *this ) )
68{
69 static std::vector<PARAM::INFO> paramInfos = makeParamInfos();
70
71 for( const SIM_MODEL::PARAM::INFO& paramInfo : paramInfos )
72 AddParam( paramInfo );
73}
74
75
76const std::vector<SIM_MODEL::PARAM::INFO> SIM_MODEL_R_POT::makeParamInfos()
77{
78 std::vector<PARAM::INFO> paramInfos;
79 PARAM::INFO paramInfo;
80
81 paramInfo.name = "r";
82 paramInfo.type = SIM_VALUE::TYPE_STRING;
83 paramInfo.unit = "Ω";
85 paramInfo.defaultValue = "";
86 paramInfo.description = "Resistance";
87 paramInfos.push_back( paramInfo );
88
89 paramInfo.name = "pos";
90 paramInfo.type = SIM_VALUE::TYPE_FLOAT;
91 paramInfo.unit = "";
93 paramInfo.defaultValue = "0.5";
94 paramInfo.description = "Wiper position";
95 paramInfos.push_back( paramInfo );
96
97 return paramInfos;
98}
static const std::vector< PARAM::INFO > makeParamInfos()
const SPICE_GENERATOR & SpiceGenerator() const
Definition: sim_model.h:430
const PARAM * FindParam(const std::string &aParamName) const
Definition: sim_model.cpp:794
void AddParam(const PARAM::INFO &aInfo, bool aIsOtherVariant=false)
Definition: sim_model.cpp:714
@ TYPE_FLOAT
Definition: sim_value.h:69
@ TYPE_STRING
Definition: sim_value.h:71
std::string ToSpiceString() const
Definition: sim_value.h:97
std::string ModelLine(const SPICE_ITEM &aItem) const override
std::string ItemLine(const SPICE_ITEM &aItem) const override
std::string TunerCommand(const SPICE_ITEM &aItem, const SIM_VALUE_FLOAT &aValue) const override
virtual std::string ItemName(const SPICE_ITEM &aItem) const
virtual std::string ItemLine(const SPICE_ITEM &aItem) const
const SIM_MODEL & m_model
Definition: bitmap.cpp:64
SIM_MODEL::TYPE TYPE
Definition: sim_model.cpp:54
SIM_VALUE::TYPE type
Definition: sim_model.h:332
std::string defaultValue
Definition: sim_model.h:336
std::string description
Definition: sim_model.h:338
std::unique_ptr< SIM_VALUE > value
Definition: sim_model.h:380
std::string modelName
const SIM_MODEL * model
std::vector< std::string > pinNetNames