2016-08-11 12:41:44 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 CERN
|
2022-11-12 19:01:47 +00:00
|
|
|
* Copyright (C) 2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2021-02-24 13:48:02 +00:00
|
|
|
*
|
2016-08-11 12:41:44 +00:00
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 3
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* https://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 3 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <sim/sim_plot_frame.h>
|
2021-02-24 13:48:02 +00:00
|
|
|
#include <sch_symbol.h>
|
2016-08-11 12:41:44 +00:00
|
|
|
#include <template_fieldnames.h>
|
2022-03-05 23:49:25 +00:00
|
|
|
#include <sim/ngspice_helpers.h>
|
2016-08-11 12:41:44 +00:00
|
|
|
|
2020-10-23 19:32:18 +00:00
|
|
|
#include <cmath> // log log1p expm1
|
|
|
|
#include <complex> // norm
|
|
|
|
|
2022-11-12 19:01:47 +00:00
|
|
|
// Must be after other includes to avoid conflict with a window header on msys2
|
|
|
|
#include "tuner_slider.h"
|
2020-10-23 19:32:18 +00:00
|
|
|
|
2021-12-14 13:00:00 +00:00
|
|
|
TUNER_SLIDER::TUNER_SLIDER( SIM_PLOT_FRAME* aFrame, wxWindow* aParent, SCH_SYMBOL* aSymbol ) :
|
|
|
|
TUNER_SLIDER_BASE( aParent ),
|
|
|
|
m_symbol( aSymbol ),
|
|
|
|
m_min( 0.0 ),
|
|
|
|
m_max( 0.0 ),
|
2020-10-23 19:28:03 +00:00
|
|
|
m_value( 0.0 ),
|
2021-12-30 20:24:58 +00:00
|
|
|
m_changed( false ),
|
2021-12-14 13:00:00 +00:00
|
|
|
m_frame ( aFrame )
|
2016-08-11 12:41:44 +00:00
|
|
|
{
|
2022-10-24 01:47:48 +00:00
|
|
|
wxString ref = aSymbol->GetField( REFERENCE_FIELD )->GetShownText();
|
|
|
|
m_item = aFrame->GetExporter()->FindItem( std::string( ref.ToUTF8() ) );
|
|
|
|
|
|
|
|
if( !m_item )
|
|
|
|
throw KI_PARAM_ERROR( wxString::Format( _( "Could not find Spice item with reference '%s'" ),
|
|
|
|
ref ) );
|
|
|
|
|
|
|
|
m_name->SetLabel( ref );
|
2022-04-12 14:37:06 +00:00
|
|
|
|
2020-10-23 19:28:03 +00:00
|
|
|
|
2022-10-24 01:47:48 +00:00
|
|
|
const SIM_MODEL::PARAM* tunerParam = m_item->model->GetTunerParam();
|
|
|
|
|
|
|
|
if( !tunerParam )
|
|
|
|
throw KI_PARAM_ERROR( wxString::Format(
|
|
|
|
_( "Symbol '%s' has simulation model of type '%s %s', which cannot be tuned" ),
|
|
|
|
ref,
|
|
|
|
m_item->model->GetDeviceTypeInfo().fieldValue,
|
|
|
|
m_item->model->GetTypeInfo().fieldValue ) );
|
|
|
|
|
|
|
|
|
|
|
|
// Special case for potentiometers because we don't have value ranges implemented yet.
|
|
|
|
if( m_item->model->GetType() == SIM_MODEL::TYPE::R_POT )
|
2020-10-23 19:28:03 +00:00
|
|
|
{
|
2022-10-24 09:04:28 +00:00
|
|
|
std::string valueStr = m_item->model->GetTunerParam()->value->ToSpiceString();
|
|
|
|
|
|
|
|
if( valueStr != "" )
|
|
|
|
m_value = SPICE_VALUE( valueStr );
|
|
|
|
else
|
|
|
|
m_value = SPICE_VALUE( "0.5" );
|
|
|
|
|
2022-10-24 01:47:48 +00:00
|
|
|
m_min = SPICE_VALUE( 0 );
|
|
|
|
m_max = SPICE_VALUE( 1 );
|
2020-10-23 19:28:03 +00:00
|
|
|
}
|
2021-12-14 13:00:00 +00:00
|
|
|
else
|
2020-10-23 19:28:03 +00:00
|
|
|
{
|
2022-10-24 09:04:28 +00:00
|
|
|
m_value = SPICE_VALUE( m_item->model->GetTunerParam()->value->ToSpiceString() );
|
2022-10-24 01:47:48 +00:00
|
|
|
m_min = SPICE_VALUE( 0.5 ) * m_value;
|
|
|
|
m_max = SPICE_VALUE( 2.0 ) * m_value;
|
2020-10-23 19:28:03 +00:00
|
|
|
}
|
2016-08-11 12:41:44 +00:00
|
|
|
|
2016-08-11 12:42:00 +00:00
|
|
|
m_minText->SetValue( m_min.ToOrigString() );
|
|
|
|
m_maxText->SetValue( m_max.ToOrigString() );
|
|
|
|
|
|
|
|
updateValueText();
|
|
|
|
updateSlider();
|
2016-08-11 12:41:44 +00:00
|
|
|
|
|
|
|
m_simTimer.SetOwner( this );
|
2022-10-24 01:47:48 +00:00
|
|
|
Connect( wxEVT_TIMER, wxTimerEventHandler( TUNER_SLIDER::onSimTimer ), nullptr, this );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string TUNER_SLIDER::GetTunerCommand() const
|
|
|
|
{
|
|
|
|
return m_item->model->SpiceGenerator().TunerCommand( *m_item,
|
|
|
|
SIM_VALUE_FLOAT( m_value.ToDouble() ) );
|
2016-08-11 12:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool TUNER_SLIDER::SetValue( const SPICE_VALUE& aVal )
|
|
|
|
{
|
|
|
|
// Get the value into the current range boundaries
|
|
|
|
if( aVal > m_max )
|
|
|
|
m_value = m_max;
|
|
|
|
else if( aVal < m_min )
|
|
|
|
m_value = m_min;
|
|
|
|
else
|
|
|
|
m_value = aVal;
|
|
|
|
|
|
|
|
updateValueText();
|
|
|
|
updateSlider();
|
|
|
|
updateComponentValue();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool TUNER_SLIDER::SetMin( const SPICE_VALUE& aVal )
|
|
|
|
{
|
|
|
|
if( aVal >= m_max )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_min = aVal;
|
|
|
|
|
2021-06-09 19:32:58 +00:00
|
|
|
if( m_value < aVal ) // Limit the current value
|
2016-08-11 12:41:44 +00:00
|
|
|
SetValue( aVal );
|
|
|
|
|
|
|
|
m_minText->SetValue( aVal.ToOrigString() );
|
|
|
|
updateSlider();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool TUNER_SLIDER::SetMax( const SPICE_VALUE& aVal )
|
|
|
|
{
|
|
|
|
if( aVal <= m_min )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_max = aVal;
|
|
|
|
|
|
|
|
if( m_value > aVal ) // Limit the current value
|
|
|
|
SetValue( aVal );
|
|
|
|
|
|
|
|
m_maxText->SetValue( aVal.ToOrigString() );
|
|
|
|
updateSlider();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TUNER_SLIDER::updateComponentValue()
|
|
|
|
{
|
|
|
|
// Start simulation in 100 ms, if the value does not change meanwhile
|
|
|
|
m_simTimer.StartOnce( 100 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TUNER_SLIDER::updateSlider()
|
|
|
|
{
|
2021-12-30 20:24:58 +00:00
|
|
|
wxASSERT( m_max >= m_value && m_value >= m_min );
|
2022-10-24 01:47:48 +00:00
|
|
|
m_slider->SetValue( ( ( m_value - m_min ) / ( m_max - m_min ) ).ToDouble() * 100 );
|
2016-08-11 12:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TUNER_SLIDER::updateValueText()
|
|
|
|
{
|
|
|
|
bool spiceString = m_min.IsSpiceString() || m_max.IsSpiceString();
|
|
|
|
m_valueText->SetValue( spiceString ? m_value.ToSpiceString() : m_value.ToString() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-20 23:33:30 +00:00
|
|
|
void TUNER_SLIDER::updateMax()
|
2016-08-11 12:41:44 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
SPICE_VALUE newMax( m_maxText->GetValue() );
|
|
|
|
SetMax( newMax );
|
|
|
|
}
|
2018-06-05 11:26:37 +00:00
|
|
|
catch( const KI_PARAM_ERROR& )
|
2016-08-11 12:41:44 +00:00
|
|
|
{
|
|
|
|
// Restore the previous value
|
|
|
|
m_maxText->SetValue( m_max.ToOrigString() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-20 23:33:30 +00:00
|
|
|
void TUNER_SLIDER::updateValue()
|
2016-08-11 12:41:44 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
SPICE_VALUE newCur( m_valueText->GetValue() );
|
|
|
|
SetValue( newCur );
|
2016-08-11 12:41:49 +00:00
|
|
|
m_changed = true;
|
2016-08-11 12:41:44 +00:00
|
|
|
}
|
2018-06-05 11:26:37 +00:00
|
|
|
catch( const KI_PARAM_ERROR& )
|
2016-08-11 12:41:44 +00:00
|
|
|
{
|
|
|
|
// Restore the previous value
|
|
|
|
m_valueText->SetValue( m_value.ToOrigString() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-20 23:33:30 +00:00
|
|
|
void TUNER_SLIDER::updateMin()
|
2016-08-11 12:41:44 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
SPICE_VALUE newMin( m_minText->GetValue() );
|
|
|
|
SetMin( newMin );
|
|
|
|
}
|
2018-06-05 11:26:37 +00:00
|
|
|
catch( const KI_PARAM_ERROR& )
|
2016-08-11 12:41:44 +00:00
|
|
|
{
|
|
|
|
// Restore the previous value
|
|
|
|
m_minText->SetValue( m_min.ToOrigString() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-20 23:33:30 +00:00
|
|
|
void TUNER_SLIDER::onClose( wxCommandEvent& event )
|
|
|
|
{
|
|
|
|
m_frame->RemoveTuner( this );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TUNER_SLIDER::onSave( wxCommandEvent& event )
|
|
|
|
{
|
2022-10-24 01:47:48 +00:00
|
|
|
m_frame->UpdateTunerValue( m_symbol, m_value.ToOrigString() );
|
2021-10-20 23:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TUNER_SLIDER::onSliderChanged( wxScrollEvent& event )
|
|
|
|
{
|
2022-10-24 01:47:48 +00:00
|
|
|
m_value = m_min + ( m_max - m_min ) * SPICE_VALUE( m_slider->GetValue() / 100.0 );
|
2021-10-20 23:33:30 +00:00
|
|
|
updateValueText();
|
|
|
|
updateComponentValue();
|
|
|
|
m_changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TUNER_SLIDER::onMaxKillFocus( wxFocusEvent& event )
|
|
|
|
{
|
|
|
|
updateMax();
|
2021-10-27 18:33:51 +00:00
|
|
|
event.Skip(); // Mandatory in wxFocusEvent
|
2021-10-20 23:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TUNER_SLIDER::onValueKillFocus( wxFocusEvent& event )
|
|
|
|
{
|
|
|
|
updateValue();
|
2021-10-27 18:33:51 +00:00
|
|
|
event.Skip(); // Mandatory in wxFocusEvent
|
2021-10-20 23:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TUNER_SLIDER::onMinKillFocus( wxFocusEvent& event )
|
|
|
|
{
|
|
|
|
updateMin();
|
2021-10-27 18:33:51 +00:00
|
|
|
event.Skip(); // Mandatory in wxFocusEvent
|
2021-10-20 23:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TUNER_SLIDER::onMaxTextEnter( wxCommandEvent& event )
|
|
|
|
{
|
|
|
|
updateMax();
|
2021-10-27 18:33:51 +00:00
|
|
|
event.Skip(); // Mandatory in wxFocusEvent
|
2021-10-20 23:33:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TUNER_SLIDER::onValueTextEnter( wxCommandEvent& event )
|
|
|
|
{
|
|
|
|
updateValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TUNER_SLIDER::onMinTextEnter( wxCommandEvent& event )
|
|
|
|
{
|
|
|
|
updateMin();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-11 12:41:44 +00:00
|
|
|
void TUNER_SLIDER::onSimTimer( wxTimerEvent& event )
|
|
|
|
{
|
2021-07-16 20:13:26 +00:00
|
|
|
if( m_changed )
|
2016-08-11 12:41:49 +00:00
|
|
|
{
|
2016-08-11 12:42:00 +00:00
|
|
|
wxQueueEvent( m_frame, new wxCommandEvent( EVT_SIM_UPDATE ) );
|
2016-08-11 12:41:49 +00:00
|
|
|
m_changed = false;
|
|
|
|
}
|
2016-08-11 12:41:44 +00:00
|
|
|
}
|