File indexing completed on 2024-04-14 05:36:40

0001 /***************************************************************************
0002  *   Copyright (C) 2005 by David Saxton                                    *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #include "pin.h"
0012 
0013 #include <QDebug>
0014 
0015 #include <cassert>
0016 
0017 #include <ktechlab_debug.h>
0018 
0019 Pin::Pin(ECNode *parent)
0020 {
0021     assert(parent);
0022     m_pECNode = parent;
0023     m_voltage = 0.;
0024     m_current = 0.;
0025     m_eqId = -2;
0026     m_bCurrentIsKnown = false;
0027     m_groundType = Pin::gt_never;
0028 }
0029 
0030 Pin::~Pin()
0031 {
0032     qDeleteAll(m_inputWireList);
0033     qDeleteAll(m_outputWireList);
0034 }
0035 
0036 PinList Pin::localConnectedPins() const
0037 {
0038     //  qCDebug(KTL_LOG) << "Input wires: "<<m_inputWireList.size()<<"   Output wires: " << m_outputWireList.size() << "   Switch connected: " << m_switchConnectedPins.size();
0039 
0040     PinList pins;
0041 
0042     WireList::const_iterator end = m_inputWireList.end();
0043     for (WireList::const_iterator it = m_inputWireList.begin(); it != end; ++it) {
0044         if (*it)
0045             pins << (*it)->startPin();
0046     }
0047 
0048     end = m_outputWireList.end();
0049     for (WireList::const_iterator it = m_outputWireList.begin(); it != end; ++it) {
0050         if (*it)
0051             pins << (*it)->endPin();
0052     }
0053 
0054     pins += m_switchConnectedPins;
0055 
0056     return pins;
0057 }
0058 
0059 void Pin::setSwitchConnected(Pin *pin, bool isConnected)
0060 {
0061     if (!pin)
0062         return;
0063 
0064     if (isConnected) {
0065         if (!m_switchConnectedPins.contains(pin))
0066             m_switchConnectedPins.append(pin);
0067     } else
0068         m_switchConnectedPins.removeAll(pin);
0069 }
0070 
0071 void Pin::setSwitchCurrentsUnknown()
0072 {
0073     if (!m_switchList.empty()) {
0074         m_switchList.removeAt(0l);
0075     } else {
0076         // 2022.06.26 - too noisy, better keep it disabled
0077         //qCDebug(KTL_LOG) << "Pin::setSwitchCurrentsUnknown - WARN - unexpected empty switch list";
0078     }
0079     m_unknownSwitchCurrents = m_switchList;
0080 }
0081 
0082 void Pin::addCircuitDependentPin(Pin *pin)
0083 {
0084     if (pin && !m_circuitDependentPins.contains(pin))
0085         m_circuitDependentPins.append(pin);
0086 }
0087 
0088 void Pin::addGroundDependentPin(Pin *pin)
0089 {
0090     if (pin && !m_groundDependentPins.contains(pin))
0091         m_groundDependentPins.append(pin);
0092 }
0093 
0094 void Pin::removeDependentPins()
0095 {
0096     m_circuitDependentPins.clear();
0097     m_groundDependentPins.clear();
0098 }
0099 
0100 void Pin::addElement(Element *e)
0101 {
0102     if (!e || m_elementList.contains(e))
0103         return;
0104     m_elementList.append(e);
0105 }
0106 
0107 void Pin::removeElement(Element *e)
0108 {
0109     m_elementList.removeAll(e);
0110 }
0111 
0112 void Pin::addSwitch(Switch *sw)
0113 {
0114     if (!sw || m_switchList.contains(sw))
0115         return;
0116     m_switchList << sw;
0117 }
0118 
0119 void Pin::removeSwitch(Switch *sw)
0120 {
0121     m_switchList.removeAll(sw);
0122 }
0123 
0124 void Pin::addInputWire(Wire *wire)
0125 {
0126     if (wire && !m_inputWireList.contains(wire))
0127         m_inputWireList << wire;
0128 }
0129 
0130 void Pin::addOutputWire(Wire *wire)
0131 {
0132     if (wire && !m_outputWireList.contains(wire))
0133         m_outputWireList << wire;
0134 }
0135 
0136 bool Pin::calculateCurrentFromWires()
0137 {
0138     m_inputWireList.removeAll(static_cast<Wire *>(nullptr));
0139     m_outputWireList.removeAll(static_cast<Wire *>(nullptr));
0140 
0141     const WireList inputs = m_inputWireList;
0142     const WireList outputs = m_outputWireList;
0143 
0144     m_current = 0.0;
0145 
0146     WireList::const_iterator end = inputs.end();
0147     for (WireList::const_iterator it = inputs.begin(); it != end; ++it) {
0148         if (!(*it)->currentIsKnown())
0149             return false;
0150 
0151         m_current -= (*it)->current();
0152     }
0153 
0154     end = outputs.end();
0155     for (WireList::const_iterator it = outputs.begin(); it != end; ++it) {
0156         if (!(*it)->currentIsKnown())
0157             return false;
0158 
0159         m_current += (*it)->current();
0160     }
0161 
0162     m_bCurrentIsKnown = true;
0163     return true;
0164 }