regulators_funct.cpp 12.9 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 28 29 30 31 32 33 34 35
/**
 * @file regulators_funct.cpp
 */
/*
 * This program source code file is part of KICAD, a free EDA CAD application.
 *
 * Copyright (C) 1992-2011 jean-pierre.charras
 * Copyright (C) 1992-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
 */
#include <wx/wx.h>
#include <wx/config.h>

#include <macros.h>
#include <pcb_calculator.h>
#include <class_regulator_data.h>
#include <dialog_regulator_data_base.h>


36
extern double DoubleFromString( const wxString& TextValue );
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

class DIALOG_EDITOR_DATA: public DIALOG_EDITOR_DATA_BASE
{
public:
    DIALOG_EDITOR_DATA( PCB_CALCULATOR_FRAME * parent, const wxString & aRegName )
        : DIALOG_EDITOR_DATA_BASE( parent )
    {
        m_textCtrlName->SetValue( aRegName );
        m_textCtrlName->Enable( aRegName.IsEmpty() );
        UpdateDialog();
    }

    ~DIALOG_EDITOR_DATA() {};

    // Event called functions:
52
    void OnOKClick( wxCommandEvent& event );
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

    /**
     * Function IsOK()
     * @return true if regulator parameters are acceptable
     */
    bool IsOK();

    /**
     * Function CopyRegulatorDataToDialog
     * Transfert data from dialog to aItem
     * @param aItem = a pointer to the REGULATOR_DATA
     */
    void CopyRegulatorDataToDialog( REGULATOR_DATA * aItem );

    /**
     * Function BuildRegulatorFromData
     * Creates a new REGULATOR_DATA from dialog
     * @return a pointer to the new REGULATOR_DATA
     */
    REGULATOR_DATA * BuildRegulatorFromData();

    /**
     * Enable/disable Iadj realted widgets, according to
     * the regulator type
     */
    void UpdateDialog()
    {
        bool enbl = m_choiceRegType->GetSelection() == 1;
        m_RegulIadjValue->Enable( enbl );
        m_RegulIadjTitle->Enable( enbl );
        m_IadjUnitLabel->Enable( enbl );
    }

    /**
     * called when the current regulator type is changed
     */
    void OnRegTypeSelection( wxCommandEvent& event )
    {
        UpdateDialog();
    }
};


void DIALOG_EDITOR_DATA::OnOKClick( wxCommandEvent& event )
{
    if( !IsOK() )
    {
        wxMessageBox( _("Bad or missing parameters!") );
        return;
    }

    EndModal( wxID_OK );
}

bool DIALOG_EDITOR_DATA::IsOK()
{
    bool ok = true;
    if( m_textCtrlName->GetValue().IsEmpty() )
        ok = false;
    if( m_textCtrlVref->GetValue().IsEmpty() )
        ok = false;
    else
    {
116
        double vref = DoubleFromString( m_textCtrlVref->GetValue() );
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
        if( fabs(vref) < 0.01 )
            ok = false;
    }
    if( m_choiceRegType->GetSelection() == 1 )
    {
        if( m_RegulIadjValue->GetValue().IsEmpty() )
        ok = false;
    }

    return ok;
}

void DIALOG_EDITOR_DATA::CopyRegulatorDataToDialog( REGULATOR_DATA * aItem )
{
    m_textCtrlName->SetValue( aItem->m_Name );
    wxString value;
    value.Printf( wxT("%g"), aItem->m_Vref );
    m_textCtrlVref->SetValue( value );
    value.Printf( wxT("%g"), aItem->m_Iadj );
    m_RegulIadjValue->SetValue( value );
    m_choiceRegType->SetSelection( aItem->m_Type );
    UpdateDialog();
}

REGULATOR_DATA * DIALOG_EDITOR_DATA::BuildRegulatorFromData()
{
143 144
    double vref = DoubleFromString( m_textCtrlVref->GetValue() );
    double iadj = DoubleFromString( m_RegulIadjValue->GetValue() );
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    int type = m_choiceRegType->GetSelection();
    if( type != 1 )
        iadj = 0.0;
    REGULATOR_DATA * item = new REGULATOR_DATA( m_textCtrlName->GetValue(),
                                                vref, type, iadj );
    return item;
}

void PCB_CALCULATOR_FRAME::OnRegulatorCalcButtonClick( wxCommandEvent& event )
{
    RegulatorsSolve();
}

void PCB_CALCULATOR_FRAME::RegulatorPageUpdate()
{
    switch( m_choiceRegType->GetSelection() )
    {
        default:
        case 0:
            m_bitmapRegul4pins->Show( true );
            m_bitmapRegul3pins->Show( false );
            m_RegulIadjValue->Enable( false );
            m_RegulIadjTitle->Enable( false );
            m_IadjUnitLabel->Enable( false );
            m_RegulFormula->SetLabel( wxT("Vout = Vref * (R1 + R2) / R2") );
            break;

        case 1:
            m_bitmapRegul4pins->Show( false );
            m_bitmapRegul3pins->Show( true );
            m_RegulIadjValue->Enable( true );
            m_RegulIadjTitle->Enable( true );
            m_IadjUnitLabel->Enable( true );
            m_RegulFormula->SetLabel( wxT("Vout = Vref * (R1 + R2) / R1 + Iadj * R2") );
            break;
    }
    // The new icon size must be taken in account
    m_panelRegulators->GetSizer()->Layout();

    // Enable/disable buttons:
    bool enbl = m_choiceRegulatorSelector->GetCount() > 0;
    m_buttonEditItem->Enable( enbl );
    m_buttonRemoveItem->Enable( enbl );

    m_panelRegulators->Refresh();
}

void PCB_CALCULATOR_FRAME::OnRegulTypeSelection( wxCommandEvent& event )
{
    RegulatorPageUpdate();
}

void PCB_CALCULATOR_FRAME::OnRegulatorSelection( wxCommandEvent& event )
{
    wxString name = m_choiceRegulatorSelector->GetStringSelection();
    REGULATOR_DATA * item = m_RegulatorList.GetReg( name );
    if( item )
    {
        m_lastSelectedRegulatorName = item->m_Name;
        m_choiceRegType->SetSelection( item->m_Type );
        wxString value;
        value.Printf( wxT("%g"), item->m_Vref );
        m_RegulVrefValue->SetValue( value );
        value.Printf( wxT("%g"), item->m_Iadj );
        m_RegulIadjValue->SetValue( value );
    }

    // Call RegulatorPageUpdate to enable/sisable tools,
    // even if no item selected
    RegulatorPageUpdate();
}

/*
 * Called when ckicking on button Browse:
 * Select a new data file, and load it on request
 */
void PCB_CALCULATOR_FRAME::OnDataFileSelection( wxCommandEvent& event )
{
    wxString fullfilename = GetDataFilename();

    wxString wildcard;
226
    wildcard.Printf( _("PCB Calculator data  file (*.%s)|*.%s"),
227 228 229 230
                     GetChars( DataFileNameExt ),
                     GetChars( DataFileNameExt ) );

    wxFileDialog dlg( m_panelRegulators,
231
                      _("Select a PCB Calculator data file"),
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
                      wxEmptyString, fullfilename,
                      wildcard, wxFD_OPEN );

    if (dlg.ShowModal() == wxID_CANCEL)
        return;

    fullfilename = dlg.GetPath();

    if( fullfilename == GetDataFilename() )
        return;

    SetDataFilename( fullfilename );
    if( wxFileExists( fullfilename ) && m_RegulatorList.GetCount() > 0 )  // Read file
    {
        if( wxMessageBox( _("Do you want to load this file and replace current regulator list?" ) )
                         != wxID_OK )
            return;
    }

    if( ReadDataFile() )
    {
        m_RegulatorListChanged = false;
        m_choiceRegulatorSelector->Clear();
        m_choiceRegulatorSelector->Append( m_RegulatorList.GetRegList() );
        SelectLastSelectedRegulator();
    }

    else
    {
        wxString msg;
        msg.Printf( _("Unable to read data file <%s>"), GetChars( fullfilename ) );
        wxMessageBox( msg );
    }
}

void PCB_CALCULATOR_FRAME::OnAddRegulator( wxCommandEvent& event )
{
    DIALOG_EDITOR_DATA dlg( this, wxEmptyString );
    if( dlg.ShowModal() != wxID_OK )
        return;
    if( !dlg.IsOK() )
    {
        wxMessageBox( _("Bad or missing parameters!") );
        return;
    }

    REGULATOR_DATA * new_item = dlg.BuildRegulatorFromData();

    // Add new item, if not existing
    if( m_RegulatorList.GetReg( new_item->m_Name ) == NULL )
    {
        // Add item in list
        m_RegulatorList.Add( new_item );
        m_RegulatorListChanged = true;
        m_choiceRegulatorSelector->Clear();
        m_choiceRegulatorSelector->Append( m_RegulatorList.GetRegList() );
        m_lastSelectedRegulatorName = new_item->m_Name;
        SelectLastSelectedRegulator();
    }
    else
    {
        wxMessageBox( _("This regulator is already in list. Aborted") );
        delete new_item;
    }
}

void PCB_CALCULATOR_FRAME::OnEditRegulator( wxCommandEvent& event )
{
    wxString name = m_choiceRegulatorSelector->GetStringSelection();
    REGULATOR_DATA * item  = m_RegulatorList.GetReg( name );
    if( item == NULL )
        return;

    DIALOG_EDITOR_DATA dlg( this, name );

    dlg.CopyRegulatorDataToDialog( item );
    if( dlg.ShowModal() != wxID_OK )
        return;

    REGULATOR_DATA * new_item = dlg.BuildRegulatorFromData();
    m_RegulatorList.Replace( new_item );

    m_RegulatorListChanged = true;

    SelectLastSelectedRegulator();
}

void PCB_CALCULATOR_FRAME::OnRemoveRegulator( wxCommandEvent& event )
{
    wxString name = wxGetSingleChoice( _("Remove Regulator"), wxEmptyString,
                                       m_RegulatorList.GetRegList() );
    if( name.IsEmpty() )
        return;

    m_RegulatorList.Remove( name );
    m_RegulatorListChanged = true;
    m_choiceRegulatorSelector->Clear();
    m_choiceRegulatorSelector->Append( m_RegulatorList.GetRegList() );
    if( m_lastSelectedRegulatorName == name )
        m_lastSelectedRegulatorName.Empty();

    SelectLastSelectedRegulator();
}

void PCB_CALCULATOR_FRAME::SelectLastSelectedRegulator()
{
    // Find last selected in regulator list:
    int idx = -1;
    if( ! m_lastSelectedRegulatorName.IsEmpty() )
    {
        for( unsigned ii = 0; ii < m_RegulatorList.GetCount(); ii++ )
            if( m_RegulatorList.m_List[ii]->m_Name == m_lastSelectedRegulatorName )
            {
                idx = ii;
                break;
            }
    }

    m_choiceRegulatorSelector->SetSelection( idx );
    wxCommandEvent event;
    OnRegulatorSelection( event );
}

// Calculate a value from the 3 other values
// Vref is given by the regulator properties, so
// we can calculate only R1, R2 or Vout
void PCB_CALCULATOR_FRAME::RegulatorsSolve()
{
    int id;
    if( m_rbRegulR1->GetValue() )
        id = 0;     // for R1 calculation
    else if( m_rbRegulR2->GetValue() )
        id = 1;     // for R2 calculation
    else if( m_rbRegulVout->GetValue() )
        id = 2;     // for Vout calculation
    else
    {
        wxMessageBox( wxT("Selection error" ) );
        return;
    }

    double r1, r2, vref, vout;

    wxString txt;

    m_RegulMessage->SetLabel( wxEmptyString);

    // Convert r1 and r2 in ohms
    int r1scale = 1000;
    int r2scale = 1000;

    // Read values from panel:
    txt = m_RegulR1Value->GetValue();
385
    r1 = DoubleFromString(txt) * r1scale;
386
    txt = m_RegulR2Value->GetValue();
387
    r2 = DoubleFromString(txt) * r2scale;
388
    txt = m_RegulVrefValue->GetValue();
389
    vref = DoubleFromString(txt);
390
    txt = m_RegulVoutValue->GetValue();
391
    vout = DoubleFromString(txt);
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417


    // Some tests:
    if( vout < vref && id != 2)
    {
        m_RegulMessage->SetLabel( _(" Vout must be greater than vref" ) );
        return;
    }

    if( vref == 0.0 )
    {
        m_RegulMessage->SetLabel( _(" Vref set to 0 !" ) );
        return;
    }

    if( (r1 < 0 && id != 0 ) || (r2 <= 0 && id != 1) )
    {
        m_RegulMessage->SetLabel( _("Incorrect value for R1 R2" ) );
        return;
    }

    // Calculate
    if( m_choiceRegType->GetSelection() == 1)
    {
        // 3 terminal regulator
        txt = m_RegulIadjValue->GetValue();
418
        double iadj = DoubleFromString(txt);
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
        // iadj is given in micro amp, so convert it in amp.
        iadj /= 1000000;

        switch( id )
        {
            case 0:
                r1 = vref * r2 / ( vout - vref - (r2 * iadj) );
                break;

            case 1:
                r2 = ( vout - vref ) / ( iadj + (vref/r1) );
                break;

            case 2:
                vout = vref * (r1 + r2) / r1;
                vout += r2 * iadj;
                break;
        }
    }
    else
    {   // Standard 4 terminal regulator
        switch( id )
        {
            case 0:
                r1 = ( vout / vref - 1 ) * r2;
                break;

            case 1:
                r2 = r1 / ( vout / vref - 1);
                break;

            case 2:
                vout = vref * (r1 + r2) / r2;
                break;
        }
    }
    // write values to panel:
    txt.Printf(wxT("%g"), r1 / r1scale );
    m_RegulR1Value->SetValue(txt);
    txt.Printf(wxT("%g"), r2 / r2scale);
    m_RegulR2Value->SetValue(txt);
    txt.Printf(wxT("%g"), vref);
    m_RegulVrefValue->SetValue(txt);
    txt.Printf(wxT("%g"), vout);
    m_RegulVoutValue->SetValue(txt);

}