attenuators.cpp 4.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/**
 * @file attenuators.cpp
 */

/*
 * This program source code file is part of KICAD, a free EDA CAD application.
 *
 * Copyright (C) 12011 jean-pierre.charras
 * Copyright (C) 2011 Kicad Developers, see change_log.txt for contributors.
 *
 * 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 2
 * 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:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */
28
#include <wx/wx.h>
29

30 31
#include <pcb_calculator.h>
#include <attenuator_classes.h>
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

extern double ReturnDoubleFromString( const wxString& TextValue );

// Called on a attenuator selection
void PCB_CALCULATOR_FRAME::OnAttenuatorSelection( wxCommandEvent& event )
{
    SetAttenuator( (unsigned) event.GetSelection() );
    Refresh();
}


void PCB_CALCULATOR_FRAME::SetAttenuator( unsigned aIdx )
{
    if( aIdx >=m_attenuator_list.size() )
        aIdx = m_attenuator_list.size() - 1;
    m_currAttenuator = m_attenuator_list[aIdx];
    TransfAttenuatorDataToPanel();
    m_Attenuator_Messages->Clear();
    m_Att_R1_Value->SetValue( wxEmptyString );
    m_Att_R2_Value->SetValue( wxEmptyString );
    m_Att_R3_Value->SetValue( wxEmptyString );
}


void PCB_CALCULATOR_FRAME::OnCalculateAttenuator( wxCommandEvent& event )
{
    TransfPanelDataToAttenuator();
    m_currAttenuator->Calculate();
    TransfAttenuatorResultsToPanel();
}


void PCB_CALCULATOR_FRAME::TransfPanelDataToAttenuator()
{
    wxString msg;

    msg = m_AttValueCtrl->GetValue();
    m_currAttenuator->m_Attenuation = ReturnDoubleFromString(msg);
    msg = m_ZinValueCtrl->GetValue();
    m_currAttenuator->m_Zin = ReturnDoubleFromString(msg);
    msg = m_ZoutValueCtrl->GetValue();
    m_currAttenuator->m_Zout = ReturnDoubleFromString(msg);
}


void PCB_CALCULATOR_FRAME::TransfAttenuatorDataToPanel()
{
    wxString msg;

81
    msg.Printf( wxT( "%g" ), m_currAttenuator->m_Attenuation );
82 83 84 85 86
    m_AttValueCtrl->SetValue( msg );
    m_AttValueCtrl->Enable( m_currAttenuator->m_Attenuation_Enable );

    m_ZinValueCtrl->Enable( m_currAttenuator->m_Zin_Enable );
    if( m_currAttenuator->m_Zin_Enable )
87
        msg.Printf( wxT( "%g" ), m_currAttenuator->m_Zin );
88 89 90 91
    else
        msg.Clear();;
    m_ZinValueCtrl->SetValue( msg );

92
    msg.Printf( wxT( "%g" ), m_currAttenuator->m_Zout );
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    m_ZoutValueCtrl->SetValue( msg );
}


void PCB_CALCULATOR_FRAME::TransfAttenuatorResultsToPanel()
{
    wxString msg;

    m_Attenuator_Messages->Clear();

    if( m_currAttenuator->m_Error )
    {
        msg.Printf( _( "Error!\nSet attenuation more than %f dB" ),
                    m_currAttenuator->m_MinimumATT );
        m_Attenuator_Messages->AppendText( msg );
        msg = wxT( "--" );
        m_Att_R1_Value->SetValue( msg );
        m_Att_R2_Value->SetValue( msg );
        if( m_currAttenuator->m_ResultCount  >= 3 )
            m_Att_R3_Value->SetValue( msg );

        return;
    }

117
    msg.Printf( wxT( "%g" ), m_currAttenuator->m_R1 );
118
    m_Att_R1_Value->SetValue( msg );
119
    msg.Printf( wxT( "%g" ), m_currAttenuator->m_R2 );
120 121 122 123 124
    m_Att_R2_Value->SetValue( msg );
    if( m_currAttenuator->m_ResultCount  < 3 )
        m_Att_R3_Value->SetValue( wxEmptyString );
    else
    {
125
        msg.Printf( wxT( "%g" ), m_currAttenuator->m_R3 );
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
        m_Att_R3_Value->SetValue( msg );
    }
}


void PCB_CALCULATOR_FRAME::OnPaintAttenuatorPanel( wxPaintEvent& event )
{
    wxPaintDC dc( m_panelDisplayAttenuator );

    if( m_currAttenuator && m_currAttenuator->m_SchBitMap )
    {
        wxSize size = m_panelDisplayAttenuator->GetSize();
        size.x -= m_currAttenuator->m_SchBitMap->GetWidth();
        size.y -= m_currAttenuator->m_SchBitMap->GetHeight();
        dc.DrawBitmap( *m_currAttenuator->m_SchBitMap, size.x / 2, size.y / 2 );
    }

    event.Skip();
}


void PCB_CALCULATOR_FRAME::OnPaintAttFormulaPanel( wxPaintEvent& event )
{
    wxPaintDC dc( m_panelAttFormula );

    if( m_currAttenuator && m_currAttenuator->m_FormulaBitMap )
    {
        wxSize size = m_panelAttFormula->GetSize();
        size.x -= m_currAttenuator->m_FormulaBitMap->GetWidth();
        size.y -= m_currAttenuator->m_FormulaBitMap->GetHeight();
        dc.DrawBitmap( *m_currAttenuator->m_FormulaBitMap, size.x / 2, size.y / 2 );
    }

    event.Skip();
}