/*************************************************************************** * Copyright (C) 2005 by David Saxton * * david@bluehaze.org * * * * 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. * ***************************************************************************/ #include "circuitdocument.h" #include "component.h" #include "ecnode.h" #include "pin.h" #include "resistance.h" #include "simulator.h" #include "switch.h" #include #include #include #include // for rand #include Switch::Switch( Component * parent, Pin * p1, Pin * p2, State state ) { m_bouncePeriod_ms = 5; m_bBounce = false; m_bounceStart = 0; m_pBounceResistance = 0l; m_pP1 = p1; m_pP2 = p2; m_pComponent = parent; m_pStopBouncingTimer = new TQTimer( this ); connect( m_pStopBouncingTimer, TQ_SIGNAL(timeout()), this, TQ_SLOT(stopBouncing()) ); // Force update m_state = (state == Open) ? Closed : Open; setState(state); } Switch::~ Switch( ) { if (m_pP1) m_pP1->setSwitchConnected( m_pP2, false ); if (m_pP2) m_pP2->setSwitchConnected( m_pP1, false ); } void Switch::setState( State state ) { if ( m_state == state ) return; m_state = state; if ( m_bBounce ) startBouncing(); else { // I'm being lazy...calling stopBouncing will connect the stuff stopBouncing(); } } void Switch::setBounce( bool bounce, int msec ) { m_bBounce = bounce; m_bouncePeriod_ms = msec; } void Switch::startBouncing() { if ( m_pBounceResistance ) { // Already active? return; } CircuitDocument * cd = m_pComponent->circuitDocument(); if ( !cd ) return; // kdDebug() << k_funcinfo << endl; m_pBounceResistance = m_pComponent->createResistance( m_pP1, m_pP2, 10000 ); m_bounceStart = Simulator::self()->time(); Simulator::self()->attachSwitch( this ); // kdDebug() << "m_bounceStart="<