summaryrefslogtreecommitdiffstats
path: root/src/electronics/components/probe.cpp
blob: 1915d510b9706bdda3ab236b1bd9cb8d0faeab0b (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
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
226
227
228
229
230
231
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
/***************************************************************************
 *   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 "ecnode.h"
#include "libraryitem.h"
#include "logic.h"
#include "pin.h"
#include "probe.h" //HACK: This has to be included before the oscilloscope headers
#include "oscilloscope.h"
#include "oscilloscopedata.h"
#include "simulator.h"
#include "voltagesource.h"

#include <klocale.h>
#include <tqpainter.h>

//BEGIN class Probe
Probe::Probe( ICNDocument *icnDocument, bool newItem, const char *id )
	: Component( icnDocument, newItem, id )
{
	p_probeData = 0l;
	setSize( -16, -8, 32, 16 );
	
	createProperty( "color", Variant::Type::Color );
	property("color")->setCaption( i18n("Color") );
	property("color")->setValue( TQt::black );
}


Probe::~ Probe()
{
	delete p_probeData;
}


void Probe::dataChanged()
{
	m_color = dataColor("color");
	if (p_probeData)
		p_probeData->setColor(m_color);
	setChanged();
}
//END class Probe



//BEGIN class FloatingProbe
FloatingProbe::FloatingProbe( ICNDocument *icnDocument, bool newItem, const char *id )
	: Probe( icnDocument, newItem, id )
{
	p_probeData = m_pFloatingProbeData = static_cast<FloatingProbeData*>(registerProbe(this));
	property("color")->setValue( p_probeData->color() );
	
	createProperty( "scaling", Variant::Type::Select );
	property("scaling")->setCaption( i18n("Scaling") );
	property("scaling")->setAllowed( TQStringList::split( ',', "Linear,Logarithmic") );
	property("scaling")->setValue("Linear");
	property("scaling")->setAdvanced( true );
	
	createProperty( "upper_abs_value", Variant::Type::Double );
	property("upper_abs_value")->setCaption( i18n("Upper Absolute Value") );
	property("upper_abs_value")->setValue(10.0);
	property("upper_abs_value")->setMinValue(0.0);
	property("upper_abs_value")->setUnit("V");
	property("upper_abs_value")->setAdvanced(true);
	
	createProperty( "lower_abs_value", Variant::Type::Double );
	property("lower_abs_value")->setCaption( i18n("Lower Absolute Value") );
	property("lower_abs_value")->setValue(0.1);
	property("lower_abs_value")->setMinValue(0.0);
	property("lower_abs_value")->setUnit("V");
	property("lower_abs_value")->setAdvanced(true);
}


FloatingProbe::~FloatingProbe()
{
}


void FloatingProbe::dataChanged()
{
	Probe::dataChanged();
	
	if ( dataString("scaling") == "Linear" )
		m_pFloatingProbeData->setScaling( FloatingProbeData::Linear );
	else
		m_pFloatingProbeData->setScaling( FloatingProbeData::Logarithmic );
	
	m_pFloatingProbeData->setUpperAbsValue( dataDouble("upper_abs_value") );
	m_pFloatingProbeData->setLowerAbsValue( dataDouble("lower_abs_value") );
}


void FloatingProbe::drawShape( TQPainter &p )
{
	initPainter(p);
	
	int _x = int(x())-16;
	int _y = int(y())-8;
	
	p.drawRect( _x, _y, 32, 16 );
	
	TQPointArray bezier(4);
	
	bezier[0] = TQPoint( _x+4, _y+10 );
	bezier[1] = TQPoint( _x+12, _y-6 );
	bezier[2] = TQPoint( _x+20, _y+24 );
	bezier[3] = TQPoint( _x+28, _y+4 );
	
	p.setPen( TQPen( m_color, 2 ) );
	p.drawCubicBezier(bezier);
	
	deinitPainter(p);
}
//END class FloatingProbe



//BEGIN class VoltageProbe
Item* VoltageProbe::construct( ItemDocument *itemDocument, bool newItem, const char *id )
{
	return new VoltageProbe( (ICNDocument*)itemDocument, newItem, id );
}

LibraryItem* VoltageProbe::libraryItem()
{
	return new LibraryItem(
			"ec/voltageprobe",
	i18n("Voltage Probe"),
	i18n("Outputs"),
	"floatingprobe.png",
	LibraryItem::lit_component,
	VoltageProbe::construct );
}

VoltageProbe::VoltageProbe( ICNDocument *icnDocument, bool newItem, const char *id )
	: FloatingProbe( icnDocument, newItem, id ? id : "voltageprobe" )
{
	m_name = i18n("Voltage Probe");
	m_desc = i18n("Displays the voltage at the probe point on the oscilloscope.");
	
	init1PinLeft();
	init1PinRight();
	m_pPin1 = m_pNNode[0]->pin();
	m_pPin2 = m_pPNode[0]->pin();
}


VoltageProbe::~VoltageProbe()
{
}


void VoltageProbe::stepNonLogic()
{
	m_pFloatingProbeData->addDataPoint( m_pPin1->voltage() - m_pPin2->voltage() );
}
//END class VoltageProbe



//BEGIN class CurrentProbe
Item* CurrentProbe::construct( ItemDocument *itemDocument, bool newItem, const char *id )
{
	return new CurrentProbe( (ICNDocument*)itemDocument, newItem, id );
}

LibraryItem* CurrentProbe::libraryItem()
{
	return new LibraryItem(
			"ec/currentprobe",
	i18n("Current Probe"),
	i18n("Outputs"),
	"floatingprobe.png",
	LibraryItem::lit_component,
	CurrentProbe::construct );
}

CurrentProbe::CurrentProbe( ICNDocument *icnDocument, bool newItem, const char *id )
	: FloatingProbe( icnDocument, newItem, id ? id : "currentprobe" )
{
	m_name = i18n("Current Probe");
	m_desc = i18n("Displays the current at the probe point on the oscilloscope.");
	
	
	init1PinLeft(0);
	init1PinRight(0);
	
	m_voltageSource = createVoltageSource( m_pNNode[0], m_pPNode[0], 0. );
}


CurrentProbe::~CurrentProbe()
{
}


void CurrentProbe::stepNonLogic()
{
	m_pFloatingProbeData->addDataPoint( -m_voltageSource->cbranchCurrent(0) );
}
//END class CurrentProbe




//BEGIN class LogicProbe
Item* LogicProbe::construct( ItemDocument *itemDocument, bool newItem, const char *id )
{
	return new LogicProbe( (ICNDocument*)itemDocument, newItem, id );
}

LibraryItem* LogicProbe::libraryItem()
{
	TQStringList ids;
	ids << "ec/probe" << "ec/logicprobe";
	return new LibraryItem(
		ids,
		i18n("Logic Probe"),
		i18n("Outputs"),
		"logicprobe.png",
		LibraryItem::lit_component,
		LogicProbe::construct );
}

LogicProbe::LogicProbe( ICNDocument *icnDocument, bool newItem, const char *id )
	: Probe( icnDocument, newItem, id ? id : "probe" )
{
	m_name = i18n("Logic Probe");
	m_desc = i18n("Connect this probe the the point in the circuit to measure the logic value. The output will be displayed in the Oscilloscope view.");
	
	init1PinRight();
	m_pIn = createLogicIn( m_pPNode[0] );
	
	p_probeData = p_logicProbeData = static_cast<LogicProbeData*>(registerProbe(this));
	property("color")->setValue( p_probeData->color() );
	
	m_pSimulator = Simulator::self();
	m_pIn->setCallback( this, (CallbackPtr)(&LogicProbe::logicCallback) );
	logicCallback(false);
}


LogicProbe::~LogicProbe()
{
}


void LogicProbe::logicCallback( bool value )
{
	p_logicProbeData->addDataPoint( LogicDataPoint( value, m_pSimulator->time() ) );
}


void LogicProbe::drawShape( TQPainter &p )
{
	initPainter(p);
	
	int _x = int(x())-16;
	int _y = int(y())-8;
	
	p.drawRect( _x, _y, 32, 16 );
	
	p.setPen( TQPen( m_color, 2 ) );
	
	p.drawLine( _x+4, _y+11, _x+6, _y+11 );
	p.drawLine( _x+6, _y+11, _x+6, _y+4 );
	p.drawLine( _x+6, _y+4, _x+10, _y+4 );
	p.drawLine( _x+10, _y+4, _x+10, _y+11 );
	p.drawLine( _x+10, _y+11, _x+16, _y+11 );
	p.drawLine( _x+16, _y+11, _x+16, _y+4 );
	p.drawLine( _x+16, _y+4, _x+23, _y+4 );
	p.drawLine( _x+23, _y+4, _x+23, _y+11 );
	p.drawLine( _x+23, _y+11, _x+28, _y+11 );
// 	p.drawLine( _x+23, _y+11, _x+26, _y+11 );
// 	p.drawLine( _x+26, _y+11, _x+26, _y+4 );
// 	p.drawLine( _x+26, _y+4, _x+28, _y+4 );
	
	deinitPainter(p);
}
//END class LogicProbe