2016-08-11 12:41:07 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 CERN
|
2021-07-09 20:20:23 +00:00
|
|
|
* Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
2016-08-11 12:41:07 +00:00
|
|
|
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
|
2016-08-11 12:41:21 +00:00
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
2016-08-11 12:41:07 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
2016-08-11 12:41:43 +00:00
|
|
|
* as published by the Free Software Foundation; either version 3
|
2016-08-11 12:41:07 +00:00
|
|
|
* 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:
|
2016-08-11 12:41:43 +00:00
|
|
|
* https://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 3 license,
|
2016-08-11 12:41:07 +00:00
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2021-01-21 21:17:13 +00:00
|
|
|
#include "sim_plot_colors.h"
|
2016-08-11 12:41:01 +00:00
|
|
|
#include "sim_plot_panel.h"
|
2020-02-01 12:05:44 +00:00
|
|
|
#include "sim_plot_frame.h"
|
2016-08-11 12:41:01 +00:00
|
|
|
|
2016-08-11 12:41:16 +00:00
|
|
|
#include <algorithm>
|
2016-08-11 12:41:14 +00:00
|
|
|
#include <limits>
|
|
|
|
|
2021-07-09 20:20:23 +00:00
|
|
|
|
2016-08-11 12:42:17 +00:00
|
|
|
static wxString formatFloat( double x, int nDigits )
|
2016-08-11 12:41:49 +00:00
|
|
|
{
|
|
|
|
wxString rv, fmt;
|
|
|
|
|
2016-08-11 12:42:17 +00:00
|
|
|
if( nDigits )
|
2016-08-11 12:41:49 +00:00
|
|
|
{
|
2021-02-17 23:52:56 +00:00
|
|
|
fmt.Printf( "%%.0%df", nDigits );
|
2016-08-11 12:42:17 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fmt = wxT( "%.0f" );
|
2016-08-11 12:41:49 +00:00
|
|
|
}
|
|
|
|
|
2016-08-11 12:42:17 +00:00
|
|
|
rv.Printf( fmt, x );
|
2016-08-11 12:41:49 +00:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-08-11 12:42:17 +00:00
|
|
|
|
|
|
|
static void getSISuffix( double x, const wxString& unit, int& power, wxString& suffix )
|
2016-08-11 12:41:49 +00:00
|
|
|
{
|
|
|
|
const int n_powers = 11;
|
2016-08-11 12:42:17 +00:00
|
|
|
|
|
|
|
const struct
|
|
|
|
{
|
2016-08-11 12:42:10 +00:00
|
|
|
double exponent;
|
|
|
|
char suffix;
|
2016-08-11 12:42:17 +00:00
|
|
|
} powers[] =
|
|
|
|
{
|
|
|
|
{ -18, 'a' },
|
|
|
|
{ -15, 'f' },
|
|
|
|
{ -12, 'p' },
|
|
|
|
{ -9, 'n' },
|
|
|
|
{ -6, 'u' },
|
|
|
|
{ -3, 'm' },
|
|
|
|
{ 0, 0 },
|
|
|
|
{ 3, 'k' },
|
|
|
|
{ 6, 'M' },
|
|
|
|
{ 9, 'G' },
|
|
|
|
{ 12, 'T' },
|
|
|
|
{ 14, 'P' }
|
2016-08-11 12:41:49 +00:00
|
|
|
};
|
|
|
|
|
2016-08-11 12:42:10 +00:00
|
|
|
power = 0;
|
|
|
|
suffix = unit;
|
|
|
|
|
2016-08-11 12:42:17 +00:00
|
|
|
if( x == 0.0 )
|
2016-08-11 12:42:10 +00:00
|
|
|
return;
|
2016-08-11 12:41:49 +00:00
|
|
|
|
2016-08-11 12:42:17 +00:00
|
|
|
for( int i = 0; i < n_powers - 1; i++ )
|
2016-08-11 12:41:49 +00:00
|
|
|
{
|
2016-08-11 12:42:17 +00:00
|
|
|
double r_cur = pow( 10, powers[i].exponent );
|
2016-08-11 12:41:49 +00:00
|
|
|
|
2016-08-11 12:42:17 +00:00
|
|
|
if( fabs( x ) >= r_cur && fabs( x ) < r_cur * 1000.0 )
|
2016-08-11 12:41:49 +00:00
|
|
|
{
|
2016-08-11 12:42:10 +00:00
|
|
|
power = powers[i].exponent;
|
2016-08-11 12:42:17 +00:00
|
|
|
|
|
|
|
if( powers[i].suffix )
|
|
|
|
suffix = wxString( powers[i].suffix ) + unit;
|
2016-08-11 12:42:10 +00:00
|
|
|
else
|
|
|
|
suffix = unit;
|
2016-08-11 12:42:17 +00:00
|
|
|
|
2016-08-11 12:42:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-11 12:41:49 +00:00
|
|
|
|
2016-08-11 12:42:17 +00:00
|
|
|
|
|
|
|
static int countDecimalDigits( double x, int maxDigits )
|
2016-08-11 12:42:10 +00:00
|
|
|
{
|
2021-02-17 23:52:56 +00:00
|
|
|
if( std::isnan( x ) )
|
|
|
|
{
|
|
|
|
// avoid trying to count the decimals of NaN
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-11 12:42:17 +00:00
|
|
|
int64_t k = (int)( ( x - floor( x ) ) * pow( 10.0, (double) maxDigits ) );
|
2016-08-11 12:42:10 +00:00
|
|
|
int n = 0;
|
2016-08-11 12:41:49 +00:00
|
|
|
|
2016-08-11 12:42:17 +00:00
|
|
|
while( k && ( ( k % 10LL ) == 0LL || ( k % 10LL ) == 9LL ) )
|
2016-08-11 12:42:10 +00:00
|
|
|
{
|
|
|
|
k /= 10LL;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
|
2016-08-11 12:42:17 +00:00
|
|
|
while( k != 0LL )
|
2016-08-11 12:42:10 +00:00
|
|
|
{
|
|
|
|
n++;
|
|
|
|
k /= 10LL;
|
2016-08-11 12:41:49 +00:00
|
|
|
}
|
|
|
|
|
2016-08-11 12:42:10 +00:00
|
|
|
return n;
|
2016-08-11 12:41:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-25 21:29:13 +00:00
|
|
|
template <typename parent>
|
|
|
|
class LIN_SCALE : public parent
|
2016-08-11 12:41:49 +00:00
|
|
|
{
|
2021-01-25 21:29:13 +00:00
|
|
|
public:
|
|
|
|
LIN_SCALE( wxString name, wxString unit, int flags ) : parent( name, flags ), m_unit( unit ){};
|
2016-08-11 12:41:52 +00:00
|
|
|
|
2021-01-25 21:29:13 +00:00
|
|
|
void formatLabels() override
|
2016-08-11 12:41:52 +00:00
|
|
|
{
|
2021-01-25 21:29:13 +00:00
|
|
|
double maxVis = parent::AbsVisibleMaxValue();
|
2016-08-11 12:42:10 +00:00
|
|
|
|
2021-01-25 21:29:13 +00:00
|
|
|
wxString suffix;
|
|
|
|
int power, digits = 0;
|
|
|
|
int constexpr DIGITS = 3;
|
2016-08-11 12:41:52 +00:00
|
|
|
|
2021-01-25 21:29:13 +00:00
|
|
|
getSISuffix( maxVis, m_unit, power, suffix );
|
2016-08-11 12:41:52 +00:00
|
|
|
|
2021-01-25 21:29:13 +00:00
|
|
|
double sf = pow( 10.0, power );
|
2016-08-11 12:42:17 +00:00
|
|
|
|
2021-01-25 21:29:13 +00:00
|
|
|
for( auto& l : parent::TickLabels() )
|
|
|
|
{
|
|
|
|
int k = countDecimalDigits( l.pos / sf, DIGITS );
|
2016-08-11 12:41:49 +00:00
|
|
|
|
2021-01-25 21:29:13 +00:00
|
|
|
digits = std::max( digits, k );
|
|
|
|
}
|
2016-08-11 12:42:10 +00:00
|
|
|
|
2021-01-25 21:29:13 +00:00
|
|
|
for( auto& l : parent::TickLabels() )
|
2016-08-11 12:42:10 +00:00
|
|
|
{
|
2021-01-25 21:29:13 +00:00
|
|
|
l.label = formatFloat( l.pos / sf, digits ) + suffix;
|
2016-08-11 12:42:10 +00:00
|
|
|
l.visible = true;
|
|
|
|
}
|
|
|
|
}
|
2021-07-09 20:20:23 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
const wxString m_unit;
|
2016-08-11 12:42:10 +00:00
|
|
|
};
|
|
|
|
|
2016-08-11 12:42:17 +00:00
|
|
|
|
2021-01-25 21:29:13 +00:00
|
|
|
template <typename parent>
|
|
|
|
class LOG_SCALE : public parent
|
2016-08-11 12:41:49 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-01-25 21:29:13 +00:00
|
|
|
LOG_SCALE( wxString name, wxString unit, int flags ) : parent( name, flags ), m_unit( unit ){};
|
2016-08-11 12:41:49 +00:00
|
|
|
|
2016-09-27 08:58:05 +00:00
|
|
|
void formatLabels() override
|
2016-08-11 12:41:49 +00:00
|
|
|
{
|
2021-01-25 21:29:13 +00:00
|
|
|
wxString suffix;
|
|
|
|
int power;
|
2016-08-11 12:42:17 +00:00
|
|
|
|
2021-01-25 21:29:13 +00:00
|
|
|
for( auto& l : parent::TickLabels() )
|
|
|
|
{
|
|
|
|
getSISuffix( l.pos, m_unit, power, suffix );
|
|
|
|
double sf = pow( 10.0, power );
|
|
|
|
int k = countDecimalDigits( l.pos / sf, 3 );
|
2016-08-11 12:41:49 +00:00
|
|
|
|
2021-01-25 21:29:13 +00:00
|
|
|
l.label = formatFloat( l.pos / sf, k ) + suffix;
|
|
|
|
l.visible = true;
|
|
|
|
}
|
2016-08-11 12:41:49 +00:00
|
|
|
}
|
2021-07-09 20:20:23 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
const wxString m_unit;
|
2016-08-11 12:41:49 +00:00
|
|
|
};
|
2016-08-11 12:42:04 +00:00
|
|
|
|
2016-08-11 12:42:07 +00:00
|
|
|
|
2016-08-11 12:41:22 +00:00
|
|
|
void CURSOR::Plot( wxDC& aDC, mpWindow& aWindow )
|
|
|
|
{
|
2016-08-11 12:41:24 +00:00
|
|
|
if( !m_window )
|
|
|
|
m_window = &aWindow;
|
|
|
|
|
2016-08-11 12:41:22 +00:00
|
|
|
if( !m_visible )
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto& dataX = m_trace->GetDataX();
|
|
|
|
const auto& dataY = m_trace->GetDataY();
|
|
|
|
|
|
|
|
if( dataX.size() <= 1 )
|
|
|
|
return;
|
|
|
|
|
2016-08-11 12:41:25 +00:00
|
|
|
if( m_updateRequired )
|
2016-08-11 12:41:22 +00:00
|
|
|
{
|
2016-08-11 12:41:55 +00:00
|
|
|
m_coords.x = m_trace->s2x( aWindow.p2x( m_dim.x ) );
|
2016-08-11 12:41:22 +00:00
|
|
|
|
|
|
|
// Find the closest point coordinates
|
|
|
|
auto maxXIt = std::upper_bound( dataX.begin(), dataX.end(), m_coords.x );
|
|
|
|
int maxIdx = maxXIt - dataX.begin();
|
|
|
|
int minIdx = maxIdx - 1;
|
|
|
|
|
|
|
|
// Out of bounds checks
|
|
|
|
if( minIdx < 0 )
|
|
|
|
{
|
|
|
|
minIdx = 0;
|
|
|
|
maxIdx = 1;
|
|
|
|
m_coords.x = dataX[0];
|
|
|
|
}
|
|
|
|
else if( maxIdx >= (int) dataX.size() )
|
|
|
|
{
|
|
|
|
maxIdx = dataX.size() - 1;
|
|
|
|
minIdx = maxIdx - 1;
|
|
|
|
m_coords.x = dataX[maxIdx];
|
|
|
|
}
|
|
|
|
|
|
|
|
const double leftX = dataX[minIdx];
|
|
|
|
const double rightX = dataX[maxIdx];
|
|
|
|
const double leftY = dataY[minIdx];
|
|
|
|
const double rightY = dataY[maxIdx];
|
|
|
|
|
2016-08-11 12:41:55 +00:00
|
|
|
// Linear interpolation
|
2016-08-11 12:41:22 +00:00
|
|
|
m_coords.y = leftY + ( rightY - leftY ) / ( rightX - leftX ) * ( m_coords.x - leftX );
|
2016-08-11 12:41:25 +00:00
|
|
|
m_updateRequired = false;
|
2016-08-11 12:41:27 +00:00
|
|
|
|
|
|
|
// Notify the parent window about the changes
|
|
|
|
wxQueueEvent( aWindow.GetParent(), new wxCommandEvent( EVT_SIM_CURSOR_UPDATE ) );
|
2016-08-11 12:41:22 +00:00
|
|
|
}
|
|
|
|
else
|
2016-08-11 12:42:14 +00:00
|
|
|
{
|
|
|
|
m_updateRef = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( m_updateRef )
|
2016-08-11 12:41:22 +00:00
|
|
|
{
|
|
|
|
UpdateReference();
|
2016-08-11 12:42:14 +00:00
|
|
|
m_updateRef = false;
|
2016-08-11 12:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Line length in horizontal and vertical dimensions
|
2016-08-11 12:41:55 +00:00
|
|
|
const wxPoint cursorPos( aWindow.x2p( m_trace->x2s( m_coords.x ) ),
|
|
|
|
aWindow.y2p( m_trace->y2s( m_coords.y ) ) );
|
2016-08-11 12:41:55 +00:00
|
|
|
|
|
|
|
wxCoord leftPx = m_drawOutsideMargins ? 0 : aWindow.GetMarginLeft();
|
2021-07-09 20:20:23 +00:00
|
|
|
wxCoord rightPx = m_drawOutsideMargins ? aWindow.GetScrX() :
|
|
|
|
aWindow.GetScrX() - aWindow.GetMarginRight();
|
2016-08-11 12:41:55 +00:00
|
|
|
wxCoord topPx = m_drawOutsideMargins ? 0 : aWindow.GetMarginTop();
|
2021-07-09 20:20:23 +00:00
|
|
|
wxCoord bottomPx = m_drawOutsideMargins ? aWindow.GetScrY() :
|
|
|
|
aWindow.GetScrY() - aWindow.GetMarginBottom();
|
2016-08-11 12:41:55 +00:00
|
|
|
|
2021-01-21 21:17:13 +00:00
|
|
|
wxPen pen = GetPen();
|
|
|
|
pen.SetStyle( m_continuous ? wxPENSTYLE_SOLID : wxPENSTYLE_LONG_DASH );
|
|
|
|
aDC.SetPen( pen );
|
2016-08-11 12:41:55 +00:00
|
|
|
|
|
|
|
if( topPx < cursorPos.y && cursorPos.y < bottomPx )
|
|
|
|
aDC.DrawLine( leftPx, cursorPos.y, rightPx, cursorPos.y );
|
|
|
|
|
|
|
|
if( leftPx < cursorPos.x && cursorPos.x < rightPx )
|
|
|
|
aDC.DrawLine( cursorPos.x, topPx, cursorPos.x, bottomPx );
|
2016-08-11 12:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-11 12:41:55 +00:00
|
|
|
bool CURSOR::Inside( wxPoint& aPoint )
|
|
|
|
{
|
|
|
|
if( !m_window )
|
|
|
|
return false;
|
|
|
|
|
2021-07-09 20:20:23 +00:00
|
|
|
return ( std::abs( (double) aPoint.x -
|
|
|
|
m_window->x2p( m_trace->x2s( m_coords.x ) ) ) <= DRAG_MARGIN )
|
|
|
|
|| ( std::abs( (double) aPoint.y -
|
|
|
|
m_window->y2p( m_trace->y2s( m_coords.y ) ) ) <= DRAG_MARGIN );
|
2016-08-11 12:41:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CURSOR::UpdateReference()
|
|
|
|
{
|
|
|
|
if( !m_window )
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_reference.x = m_window->x2p( m_trace->x2s( m_coords.x ) );
|
|
|
|
m_reference.y = m_window->y2p( m_trace->y2s( m_coords.y ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-27 12:22:27 +00:00
|
|
|
SIM_PLOT_PANEL::SIM_PLOT_PANEL( const wxString& aCommand, wxWindow* parent,
|
|
|
|
SIM_PLOT_FRAME* aMainFrame, wxWindowID id, const wxPoint& pos,
|
|
|
|
const wxSize& size, long style, const wxString& name )
|
2021-07-09 20:20:23 +00:00
|
|
|
: SIM_PANEL_BASE( aCommand, parent, id, pos, size, style, name ),
|
|
|
|
m_axis_x( nullptr ),
|
|
|
|
m_axis_y1( nullptr ),
|
|
|
|
m_axis_y2( nullptr ),
|
|
|
|
m_dotted_cp( false ),
|
|
|
|
m_masterFrame( aMainFrame )
|
2016-08-11 12:41:01 +00:00
|
|
|
{
|
2020-04-15 01:51:58 +00:00
|
|
|
m_sizer = new wxBoxSizer( wxVERTICAL );
|
|
|
|
m_plotWin = new mpWindow( this, wxID_ANY, pos, size, style );
|
2016-08-11 12:41:49 +00:00
|
|
|
|
2020-04-15 01:51:58 +00:00
|
|
|
m_plotWin->LimitView( true );
|
|
|
|
m_plotWin->SetMargins( 50, 80, 50, 80 );
|
2016-08-11 12:41:01 +00:00
|
|
|
|
2021-01-21 21:17:13 +00:00
|
|
|
UpdatePlotColors();
|
2020-04-15 01:51:58 +00:00
|
|
|
|
2021-10-18 17:01:08 +00:00
|
|
|
updateAxes();
|
|
|
|
|
|
|
|
// a mpInfoLegend displays le name of traces on the left top panel corner:
|
|
|
|
m_legend = new mpInfoLegend( wxRect( 0, 40, 200, 40 ), wxTRANSPARENT_BRUSH );
|
|
|
|
m_legend->SetVisible( false );
|
|
|
|
m_plotWin->AddLayer( m_legend );
|
|
|
|
|
|
|
|
m_plotWin->EnableDoubleBuffer( true );
|
|
|
|
m_plotWin->UpdateAll();
|
|
|
|
|
|
|
|
m_sizer->Add( m_plotWin, 1, wxALL | wxEXPAND, 1 );
|
|
|
|
SetSizer( m_sizer );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SIM_PLOT_PANEL::~SIM_PLOT_PANEL()
|
|
|
|
{
|
|
|
|
// ~mpWindow destroys all the added layers, so there is no need to destroy m_traces contents
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SIM_PLOT_PANEL::updateAxes()
|
|
|
|
{
|
|
|
|
if( m_axis_x )
|
|
|
|
return;
|
|
|
|
|
2020-04-15 01:51:58 +00:00
|
|
|
switch( GetType() )
|
2016-08-11 12:41:35 +00:00
|
|
|
{
|
|
|
|
case ST_AC:
|
2021-01-25 21:29:13 +00:00
|
|
|
m_axis_x = new LOG_SCALE<mpScaleXLog>( _( "Frequency" ), wxT( "Hz" ), mpALIGN_BOTTOM );
|
|
|
|
m_axis_y1 = new LIN_SCALE<mpScaleY>( _( "Gain" ), wxT( "dBV" ), mpALIGN_LEFT );
|
|
|
|
m_axis_y2 = new LIN_SCALE<mpScaleY>( _( "Phase" ), wxT( "\u00B0" ),
|
|
|
|
mpALIGN_RIGHT ); // degree sign
|
2016-08-11 12:42:17 +00:00
|
|
|
m_axis_y2->SetMasterScale( m_axis_y1 );
|
2016-08-11 12:41:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ST_DC:
|
2021-01-25 21:29:13 +00:00
|
|
|
prepareDCAxes();
|
2016-08-11 12:41:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ST_NOISE:
|
2021-01-25 21:29:13 +00:00
|
|
|
m_axis_x = new LOG_SCALE<mpScaleXLog>( _( "Frequency" ), wxT( "Hz" ), mpALIGN_BOTTOM );
|
2018-05-25 09:24:25 +00:00
|
|
|
m_axis_y1 = new mpScaleY( _( "noise [(V or A)^2/Hz]" ), mpALIGN_LEFT );
|
2016-08-11 12:41:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ST_TRANSIENT:
|
2021-01-25 21:29:13 +00:00
|
|
|
m_axis_x = new LIN_SCALE<mpScaleX>( _( "Time" ), wxT( "s" ), mpALIGN_BOTTOM );
|
|
|
|
m_axis_y1 = new LIN_SCALE<mpScaleY>( _( "Voltage" ), wxT( "V" ), mpALIGN_LEFT );
|
|
|
|
m_axis_y2 = new LIN_SCALE<mpScaleY>( _( "Current" ), wxT( "A" ), mpALIGN_RIGHT );
|
2016-08-11 12:42:17 +00:00
|
|
|
m_axis_y2->SetMasterScale( m_axis_y1 );
|
2016-08-11 12:41:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// suppress warnings
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( m_axis_x )
|
|
|
|
{
|
|
|
|
m_axis_x->SetTicks( false );
|
2016-08-11 12:42:04 +00:00
|
|
|
m_axis_x->SetNameAlign ( mpALIGN_BOTTOM );
|
|
|
|
|
2020-04-15 01:51:58 +00:00
|
|
|
m_plotWin->AddLayer( m_axis_x );
|
2016-08-11 12:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( m_axis_y1 )
|
|
|
|
{
|
|
|
|
m_axis_y1->SetTicks( false );
|
2016-08-11 12:42:04 +00:00
|
|
|
m_axis_y1->SetNameAlign ( mpALIGN_LEFT );
|
2020-04-15 01:51:58 +00:00
|
|
|
m_plotWin->AddLayer( m_axis_y1 );
|
2016-08-11 12:41:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( m_axis_y2 )
|
|
|
|
{
|
|
|
|
m_axis_y2->SetTicks( false );
|
2016-08-11 12:42:04 +00:00
|
|
|
m_axis_y2->SetNameAlign ( mpALIGN_RIGHT );
|
2020-04-15 01:51:58 +00:00
|
|
|
m_plotWin->AddLayer( m_axis_y2 );
|
2016-08-11 12:41:35 +00:00
|
|
|
}
|
2016-08-11 12:41:14 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 21:29:13 +00:00
|
|
|
void SIM_PLOT_PANEL::prepareDCAxes()
|
|
|
|
{
|
2021-10-18 17:01:08 +00:00
|
|
|
wxString sim_cmd = getSimCommand().Lower();
|
|
|
|
wxString rem;
|
2021-01-25 21:29:13 +00:00
|
|
|
|
2021-10-18 17:09:36 +00:00
|
|
|
if( sim_cmd.StartsWith( ".dc", &rem ) )
|
2021-01-25 21:29:13 +00:00
|
|
|
{
|
2021-10-18 17:01:08 +00:00
|
|
|
wxChar ch;
|
|
|
|
|
2021-10-18 17:09:36 +00:00
|
|
|
rem.Trim( false );
|
|
|
|
|
2021-10-18 17:01:08 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
ch = rem.GetChar( 0 );
|
|
|
|
}
|
|
|
|
catch( ... )
|
|
|
|
{;}
|
|
|
|
|
|
|
|
switch( ch )
|
2021-01-25 21:29:13 +00:00
|
|
|
{
|
2021-10-18 17:01:08 +00:00
|
|
|
// Make sure that we have a reliable default (even if incorrectly labeled)
|
|
|
|
default:
|
2021-01-25 21:29:13 +00:00
|
|
|
case 'v':
|
|
|
|
m_axis_x =
|
|
|
|
new LIN_SCALE<mpScaleX>( _( "Voltage (swept)" ), wxT( "V" ), mpALIGN_BOTTOM );
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
m_axis_x =
|
|
|
|
new LIN_SCALE<mpScaleX>( _( "Current (swept)" ), wxT( "A" ), mpALIGN_BOTTOM );
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
m_axis_x = new LIN_SCALE<mpScaleX>( _( "Resistance (swept)" ), wxT( "\u03A9" ),
|
|
|
|
mpALIGN_BOTTOM );
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
m_axis_x = new LIN_SCALE<mpScaleX>( _( "Temperature (swept)" ), wxT( "\u00B0C" ),
|
|
|
|
mpALIGN_BOTTOM );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_axis_y1 = new LIN_SCALE<mpScaleY>( _( "Voltage (measured)" ), wxT( "V" ), mpALIGN_LEFT );
|
|
|
|
m_axis_y2 = new LIN_SCALE<mpScaleY>( _( "Current" ), wxT( "A" ), mpALIGN_RIGHT );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-01 12:05:44 +00:00
|
|
|
void SIM_PLOT_PANEL::UpdatePlotColors()
|
|
|
|
{
|
|
|
|
// Update bg and fg colors:
|
2021-01-21 21:17:13 +00:00
|
|
|
m_plotWin->SetColourTheme( m_colors.GetPlotColor( SIM_PLOT_COLORS::COLOR_SET::BACKGROUND ),
|
|
|
|
m_colors.GetPlotColor( SIM_PLOT_COLORS::COLOR_SET::FOREGROUND ),
|
|
|
|
m_colors.GetPlotColor( SIM_PLOT_COLORS::COLOR_SET::AXIS ) );
|
2020-02-01 12:05:44 +00:00
|
|
|
|
2021-01-21 21:17:13 +00:00
|
|
|
// Update color of all traces
|
|
|
|
for( auto& t : m_traces )
|
|
|
|
if( t.second->GetCursor() )
|
|
|
|
t.second->GetCursor()->SetPen(
|
|
|
|
wxPen( m_colors.GetPlotColor( SIM_PLOT_COLORS::COLOR_SET::CURSOR ) ) );
|
2020-02-01 12:05:44 +00:00
|
|
|
|
2021-01-21 21:17:13 +00:00
|
|
|
m_plotWin->UpdateAll();
|
2020-02-01 12:05:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-19 16:17:37 +00:00
|
|
|
void SIM_PLOT_PANEL::UpdateTraceStyle( TRACE* trace )
|
|
|
|
{
|
2021-05-24 20:17:29 +00:00
|
|
|
int type = trace->GetType();
|
2021-10-18 17:01:08 +00:00
|
|
|
wxPenStyle penStyle = ( ( ( type & SPT_AC_PHASE ) || ( type & SPT_CURRENT ) ) && m_dotted_cp )
|
2021-05-24 20:17:29 +00:00
|
|
|
? wxPENSTYLE_DOT
|
|
|
|
: wxPENSTYLE_SOLID;
|
2019-11-19 16:17:37 +00:00
|
|
|
trace->SetPen( wxPen( trace->GetTraceColour(), 2, penStyle ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-07 21:18:22 +00:00
|
|
|
bool SIM_PLOT_PANEL::addTrace( const wxString& aTitle, const wxString& aName, int aPoints,
|
2022-04-12 14:37:06 +00:00
|
|
|
const double* aX, const double* aY, SIM_PLOT_TYPE aType )
|
2016-08-11 12:41:01 +00:00
|
|
|
{
|
2021-07-16 20:13:26 +00:00
|
|
|
TRACE* trace = nullptr;
|
2021-09-07 21:18:22 +00:00
|
|
|
wxString name = aTitle;
|
2016-08-11 12:41:21 +00:00
|
|
|
|
2021-10-18 17:01:08 +00:00
|
|
|
updateAxes();
|
|
|
|
|
2016-08-11 12:41:16 +00:00
|
|
|
// Find previous entry, if there is one
|
2021-05-24 20:17:29 +00:00
|
|
|
auto prev = m_traces.find( name );
|
2016-08-11 12:41:24 +00:00
|
|
|
bool addedNewEntry = ( prev == m_traces.end() );
|
2016-08-11 12:41:01 +00:00
|
|
|
|
2016-08-11 12:41:24 +00:00
|
|
|
if( addedNewEntry )
|
2016-08-11 12:41:16 +00:00
|
|
|
{
|
2020-04-15 01:51:58 +00:00
|
|
|
if( GetType() == ST_TRANSIENT )
|
2016-08-11 12:42:14 +00:00
|
|
|
{
|
|
|
|
bool hasVoltageTraces = false;
|
|
|
|
|
2019-12-05 15:20:59 +00:00
|
|
|
for( const auto& tr : m_traces )
|
2016-08-11 12:42:14 +00:00
|
|
|
{
|
2021-05-24 20:17:29 +00:00
|
|
|
if( !( tr.second->GetType() & SPT_CURRENT ) )
|
2016-08-11 12:42:14 +00:00
|
|
|
{
|
|
|
|
hasVoltageTraces = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-11 12:42:17 +00:00
|
|
|
if( !hasVoltageTraces )
|
2016-08-11 12:42:14 +00:00
|
|
|
m_axis_y2->SetMasterScale( nullptr );
|
|
|
|
else
|
|
|
|
m_axis_y2->SetMasterScale( m_axis_y1 );
|
|
|
|
}
|
|
|
|
|
2016-08-11 12:41:16 +00:00
|
|
|
// New entry
|
2022-04-12 14:37:06 +00:00
|
|
|
trace = new TRACE( aName, aType );
|
2021-01-21 21:17:13 +00:00
|
|
|
trace->SetTraceColour( m_colors.GenerateColor( m_traces ) );
|
2019-11-19 16:17:37 +00:00
|
|
|
UpdateTraceStyle( trace );
|
2021-05-24 20:17:29 +00:00
|
|
|
m_traces[name] = trace;
|
2016-08-11 12:41:21 +00:00
|
|
|
|
2016-08-11 12:41:27 +00:00
|
|
|
// It is a trick to keep legend & coords always on the top
|
|
|
|
for( mpLayer* l : m_topLevel )
|
2020-04-15 01:51:58 +00:00
|
|
|
m_plotWin->DelLayer( l );
|
2016-08-11 12:41:27 +00:00
|
|
|
|
2020-04-15 01:51:58 +00:00
|
|
|
m_plotWin->AddLayer( (mpLayer*) trace );
|
2016-08-11 12:41:27 +00:00
|
|
|
|
|
|
|
for( mpLayer* l : m_topLevel )
|
2020-04-15 01:51:58 +00:00
|
|
|
m_plotWin->AddLayer( l );
|
2016-08-11 12:41:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-08-12 09:00:58 +00:00
|
|
|
trace = prev->second;
|
2016-08-11 12:41:16 +00:00
|
|
|
}
|
2016-08-11 12:41:14 +00:00
|
|
|
|
2016-08-11 12:41:50 +00:00
|
|
|
std::vector<double> tmp( aY, aY + aPoints );
|
|
|
|
|
2020-04-15 01:51:58 +00:00
|
|
|
if( GetType() == ST_AC )
|
2016-08-11 12:41:50 +00:00
|
|
|
{
|
2021-05-24 20:17:29 +00:00
|
|
|
if( aType & SPT_AC_PHASE )
|
2016-08-11 12:41:50 +00:00
|
|
|
{
|
2016-08-11 12:42:17 +00:00
|
|
|
for( int i = 0; i < aPoints; i++ )
|
2016-08-11 12:41:52 +00:00
|
|
|
tmp[i] = tmp[i] * 180.0 / M_PI; // convert to degrees
|
2016-08-11 12:41:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-08-11 12:42:17 +00:00
|
|
|
for( int i = 0; i < aPoints; i++ )
|
2021-07-09 20:20:23 +00:00
|
|
|
{
|
|
|
|
// log( 0 ) is not valid.
|
|
|
|
if( tmp[i] != 0 )
|
|
|
|
tmp[i] = 20 * log( tmp[i] ) / log( 10.0 ); // convert to dB
|
|
|
|
}
|
2016-08-11 12:41:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-12 09:00:58 +00:00
|
|
|
trace->SetData( std::vector<double>( aX, aX + aPoints ), tmp );
|
2016-08-11 12:41:50 +00:00
|
|
|
|
2021-05-24 20:17:29 +00:00
|
|
|
if( ( aType & SPT_AC_PHASE ) || ( aType & SPT_CURRENT ) )
|
2016-08-12 09:00:58 +00:00
|
|
|
trace->SetScale( m_axis_x, m_axis_y2 );
|
2016-08-11 12:41:50 +00:00
|
|
|
else
|
2016-08-12 09:00:58 +00:00
|
|
|
trace->SetScale( m_axis_x, m_axis_y1 );
|
2016-08-11 12:41:50 +00:00
|
|
|
|
2020-04-15 01:51:58 +00:00
|
|
|
m_plotWin->UpdateAll();
|
2016-08-11 12:41:24 +00:00
|
|
|
|
|
|
|
return addedNewEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-30 00:13:10 +00:00
|
|
|
bool SIM_PLOT_PANEL::deleteTrace( const wxString& aName )
|
2016-08-11 12:41:24 +00:00
|
|
|
{
|
2016-08-11 12:41:52 +00:00
|
|
|
auto it = m_traces.find( aName );
|
2016-08-11 12:41:24 +00:00
|
|
|
|
2016-08-11 12:41:25 +00:00
|
|
|
if( it != m_traces.end() )
|
2016-08-11 12:41:24 +00:00
|
|
|
{
|
2016-08-11 12:41:25 +00:00
|
|
|
TRACE* trace = it->second;
|
2017-06-16 21:17:34 +00:00
|
|
|
m_traces.erase( it );
|
2016-08-11 12:41:25 +00:00
|
|
|
|
|
|
|
if( CURSOR* cursor = trace->GetCursor() )
|
2020-04-15 01:51:58 +00:00
|
|
|
m_plotWin->DelLayer( cursor, true );
|
2016-08-11 12:41:25 +00:00
|
|
|
|
2020-04-15 01:51:58 +00:00
|
|
|
m_plotWin->DelLayer( trace, true, true );
|
2016-08-11 12:42:04 +00:00
|
|
|
ResetScales();
|
2016-08-11 12:42:03 +00:00
|
|
|
|
2016-08-11 12:41:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2016-08-11 12:41:01 +00:00
|
|
|
}
|
|
|
|
|
2016-08-11 12:41:07 +00:00
|
|
|
|
2021-06-30 00:13:10 +00:00
|
|
|
void SIM_PLOT_PANEL::deleteAllTraces()
|
2016-08-11 12:41:01 +00:00
|
|
|
{
|
2016-08-11 12:41:24 +00:00
|
|
|
for( auto& t : m_traces )
|
2016-08-11 12:41:12 +00:00
|
|
|
{
|
2021-06-30 00:13:10 +00:00
|
|
|
deleteTrace( t.first );
|
2016-08-11 12:41:12 +00:00
|
|
|
}
|
|
|
|
|
2016-08-11 12:41:21 +00:00
|
|
|
m_traces.clear();
|
|
|
|
}
|
2016-08-11 12:41:12 +00:00
|
|
|
|
2016-08-11 12:41:14 +00:00
|
|
|
|
2016-08-11 12:41:25 +00:00
|
|
|
bool SIM_PLOT_PANEL::HasCursorEnabled( const wxString& aName ) const
|
|
|
|
{
|
|
|
|
TRACE* t = GetTrace( aName );
|
|
|
|
|
|
|
|
return t ? t->HasCursor() : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SIM_PLOT_PANEL::EnableCursor( const wxString& aName, bool aEnable )
|
|
|
|
{
|
|
|
|
TRACE* t = GetTrace( aName );
|
|
|
|
|
|
|
|
if( t == nullptr || t->HasCursor() == aEnable )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( aEnable )
|
|
|
|
{
|
2020-02-01 12:05:44 +00:00
|
|
|
CURSOR* c = new CURSOR( t, this );
|
2020-04-15 01:51:58 +00:00
|
|
|
int plotCenter = GetPlotWin()->GetMarginLeft()
|
|
|
|
+ ( GetPlotWin()->GetXScreen() - GetPlotWin()->GetMarginLeft()
|
|
|
|
- GetPlotWin()->GetMarginRight() )
|
|
|
|
/ 2;
|
2016-08-11 12:42:14 +00:00
|
|
|
c->SetX( plotCenter );
|
2021-01-21 21:17:13 +00:00
|
|
|
c->SetPen( wxPen( m_colors.GetPlotColor( SIM_PLOT_COLORS::COLOR_SET::CURSOR ) ) );
|
2016-08-11 12:41:25 +00:00
|
|
|
t->SetCursor( c );
|
2020-04-15 01:51:58 +00:00
|
|
|
m_plotWin->AddLayer( c );
|
2016-08-11 12:41:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CURSOR* c = t->GetCursor();
|
2021-07-16 20:13:26 +00:00
|
|
|
t->SetCursor( nullptr );
|
2020-04-15 01:51:58 +00:00
|
|
|
m_plotWin->DelLayer( c, true );
|
2016-08-11 12:41:25 +00:00
|
|
|
}
|
2016-08-11 12:41:27 +00:00
|
|
|
|
|
|
|
// Notify the parent window about the changes
|
|
|
|
wxQueueEvent( GetParent(), new wxCommandEvent( EVT_SIM_CURSOR_UPDATE ) );
|
2016-08-11 12:41:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-11 12:42:04 +00:00
|
|
|
void SIM_PLOT_PANEL::ResetScales()
|
|
|
|
{
|
|
|
|
if( m_axis_x )
|
|
|
|
m_axis_x->ResetDataRange();
|
|
|
|
|
|
|
|
if( m_axis_y1 )
|
|
|
|
m_axis_y1->ResetDataRange();
|
|
|
|
|
|
|
|
if( m_axis_y2 )
|
|
|
|
m_axis_y2->ResetDataRange();
|
|
|
|
|
2022-09-24 02:17:32 +00:00
|
|
|
for( auto& t : m_traces )
|
2016-08-11 12:42:04 +00:00
|
|
|
t.second->UpdateScales();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-11 12:41:27 +00:00
|
|
|
wxDEFINE_EVENT( EVT_SIM_CURSOR_UPDATE, wxCommandEvent );
|