summaryrefslogtreecommitdiffstats
path: root/src/electronics/pin.cpp
blob: 56848fc93f452d8c444ded22be602a4236bb8b81 (plain)
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
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
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
116
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
143
144
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
/***************************************************************************
 *   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 "pin.h"

#include <assert.h>
#include <kdebug.h>

Pin::Pin( ECNode * parent )
{
	assert(parent);
	m_pECNode = parent;
	m_voltage = 0.;
	m_current = 0.;
	m_eqId = -2;
	m_bCurrentIsKnown = false;
	m_groundType = Pin::gt_never;
}


Pin::~Pin()
{
	WireList::iterator end = m_inputWireList.end();
	for ( WireList::iterator it = m_inputWireList.begin(); it != end; ++it )
		delete *it;
	
	end = m_outputWireList.end();
	for ( WireList::iterator it = m_outputWireList.begin(); it != end; ++it )
		delete *it;
}


PinList Pin::localConnectedPins( ) const
{
// 	kdDebug() << k_funcinfo << "Input wires: "<<m_inputWireList.size()<<"   Output wires: " << m_outputWireList.size() << "   Switch connected: " << m_switchConnectedPins.size() << endl;
	
	PinList pins;
	
	WireList::const_iterator end = m_inputWireList.end();
	for ( WireList::const_iterator it = m_inputWireList.begin(); it != end; ++it )
	{
		if (*it)
			pins << (*it)->startPin();
	}
	
	end = m_outputWireList.end();
	for ( WireList::const_iterator it = m_outputWireList.begin(); it != end; ++it )
	{
		if (*it)
			pins << (*it)->endPin();
	}
	
	pins += m_switchConnectedPins;
	
	return pins;
}


void Pin::setSwitchConnected( Pin * pin, bool isConnected )
{
	if (!pin)
		return;
	
	if (isConnected)
	{
		if ( !m_switchConnectedPins.contains(pin) )
			m_switchConnectedPins.append(pin);
	}
	else
		m_switchConnectedPins.remove(pin);
}


void Pin::addCircuitDependentPin( Pin * pin )
{
	if ( pin && !m_circuitDependentPins.contains(pin) )
		m_circuitDependentPins.append(pin);
}


void Pin::addGroundDependentPin( Pin * pin )
{
	if ( pin && !m_groundDependentPins.contains(pin) )
		m_groundDependentPins.append(pin);
}


void Pin::removeDependentPins()
{
	m_circuitDependentPins.clear();
	m_groundDependentPins.clear();
}


void Pin::addElement( Element * e )
{
	if ( !e || m_elementList.contains(e) )
		return;
	m_elementList.append(e);
}


void Pin::removeElement( Element * e )
{
	m_elementList.remove(e);
}


void Pin::addSwitch( Switch * sw )
{
	if ( !sw || m_switchList.contains( sw ) )
		return;
	m_switchList << sw;
}


void Pin::removeSwitch( Switch * sw )
{
	m_switchList.remove( sw );
}


void Pin::addInputWire( Wire * wire )
{
	if ( wire && !m_inputWireList.contains(wire) )
		m_inputWireList << wire;
}


void Pin::addOutputWire( Wire * wire )
{
	if ( wire && !m_outputWireList.contains(wire) )
		m_outputWireList << wire;
}


bool Pin::calculateCurrentFromWires()
{
	m_inputWireList.remove( (Wire*)0l );
	m_outputWireList.remove( (Wire*)0l );
	
	const WireList inputs = m_inputWireList;
	const WireList outputs = m_outputWireList;
		
	m_current = 0.0;
		
	WireList::const_iterator end = inputs.end();
	for ( WireList::const_iterator it = inputs.begin(); it != end; ++it )
	{
		if ( !(*it)->currentIsKnown() )
			return false;
			
		m_current -= (*it)->current();
	}
		
	end = outputs.end();
	for ( WireList::const_iterator it = outputs.begin(); it != end; ++it )
	{
		if ( !(*it)->currentIsKnown() )
			return false;
			
		m_current += (*it)->current();
	}
	
	m_bCurrentIsKnown = true;
	return true;
}