summaryrefslogtreecommitdiffstats
path: root/src/electronics/components/discretelogic.cpp
blob: f850208a5d487dc1573ac1da1f7d509f5a5c6775 (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/***************************************************************************
 *   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 "canvasitemparts.h"
#include "discretelogic.h"
#include "ecnode.h"
#include "logic.h"
#include "libraryitem.h"
#include "simulator.h"

#include <tdelocale.h>
#include <tqpainter.h>


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

LibraryItem* Inverter::libraryItem()
{
	TQStringList ids;
	ids << "ec/inverter" << "ec/not";
	return new LibraryItem(
		ids,
		i18n("Inverter"),
		i18n("Logic"),
		"not.png",
		LibraryItem::lit_component,
		Inverter::construct );
}

Inverter::Inverter( ICNDocument *icnDocument, bool newItem, const char *id )
	: Component( icnDocument, newItem, id ? id : "not" )
{
	m_name = i18n("Inverter");
	m_desc = i18n("The output is the logical inverse of the logic-input state.");
	setSize( -8, -8, 16, 16 );

	init1PinLeft();
	init1PinRight();
	
	m_pIn = createLogicIn(m_pNNode[0]);
	m_pOut = createLogicOut( m_pPNode[0], true );
	
	m_pIn->setCallback( this, (CallbackPtr)(&Inverter::inStateChanged) );
	inStateChanged(false);
}


Inverter::~Inverter()
{
}


void Inverter::inStateChanged( bool newState )
{
	(static_cast<LogicOut*>(m_pOut))->setHigh( !newState );
}


void Inverter::drawShape( TQPainter &p )
{
	initPainter(p);
	int _x = (int)x()-8;
	int _y = (int)y()-8;
	TQPointArray pa(3);
	pa[0] = TQPoint( _x, _y );
	pa[1] = TQPoint( _x+width()-6, _y+(height()/2) );
	pa[2] = TQPoint( _x, _y+height() );
	p.drawPolygon(pa);
	p.drawPolyline(pa);
	p.drawEllipse( _x+width()-6, _y+(height()/2)-3, 7, 7 );
	deinitPainter(p);
}
//END class Inverter


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

LibraryItem* Buffer::libraryItem()
{
	return new LibraryItem(
		TQString("ec/buffer"),
		i18n("Buffer"),
		i18n("Logic"),
		"buffer.png",
		LibraryItem::lit_component,
		Buffer::construct );
}

Buffer::Buffer( ICNDocument *icnDocument, bool newItem, const char *id )
	: Component( icnDocument, newItem, id ? id : "buffer" )
{
	m_name = i18n("Buffer");
	m_desc = i18n("Cleans the logic input, with the output high or low depending on input trigger levels.");
	setSize( -8, -8, 16, 16 );

	init1PinLeft();
	init1PinRight();
	
	m_pIn = createLogicIn(m_pNNode[0]);
	m_pOut = createLogicOut( m_pPNode[0], true );
	
	m_pIn->setCallback( this, (CallbackPtr)(&Buffer::inStateChanged) );
	inStateChanged(false);
}


Buffer::~Buffer()
{
}


void Buffer::inStateChanged( bool newState )
{
	m_pOut->setHigh(newState);
}


void Buffer::drawShape( TQPainter &p )
{
	initPainter(p);
	int _x = (int)x()-8;
	int _y = (int)y()-8;
	TQPointArray pa(3);
	pa[0] = TQPoint( _x, _y );
	pa[1] = TQPoint( _x+width(), _y+(height()/2) );
	pa[2] = TQPoint( _x, _y+height() );
	p.drawPolygon(pa);
	p.drawPolyline(pa);
	deinitPainter(p);
}
//END class Buffer


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

LibraryItem* ECLogicInput::libraryItem()
{
	return new LibraryItem(
		TQString("ec/logic_input"),
		i18n("Logic Input"),
		i18n("Logic"),
		"logic_input.png",
		LibraryItem::lit_component,
		ECLogicInput::construct );
}

ECLogicInput::ECLogicInput( ICNDocument *icnDocument, bool newItem, const char *id )
	: Component( icnDocument, newItem, (id) ? id : "logic_input" )
{
	m_name = i18n("Logic Input");
	m_desc = i18n("Provides a user-adjustable logic state.<br><br>"
				"Click to pulse high, or drag the mouse off to keep the output high.");
	setSize( -8, -8, 16, 16 );
	
	b_state = false;
	addButton( "button", TQRect( -24, -8, 16, 16 ), "", true );
	
	createProperty( "useToggle", Variant::Type::Bool );
	property("useToggle")->setCaption( i18n("Use Toggle") );
	property("useToggle")->setValue(true);

	init1PinRight();
	
	m_pOut = createLogicOut( m_pPNode[0], false );
}


ECLogicInput::~ECLogicInput()
{
}


void ECLogicInput::dataChanged()
{
	button("button")->setToggle( dataBool("useToggle") );
}


void ECLogicInput::drawShape( TQPainter &p )
{
	initPainter(p);
	if (b_state)
		p.setBrush( TQColor( 255, 166, 0 ) );
	else
		p.setBrush( TQt::white );
	p.drawEllipse( (int)x()-4, (int)y()-6, 12, 12 );
	deinitPainter(p);
}


void ECLogicInput::buttonStateChanged( const TQString &, bool state )
{
	b_state = state;
	m_pOut->setHigh(b_state);
}
//END class ECLogicInput


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

LibraryItem* ECLogicOutput::libraryItem()
{
	return new LibraryItem(
		TQString("ec/logic_output"),
		i18n("Logic Output"),
		i18n("Logic"),
		"logic_output.png",
		LibraryItem::lit_component,
		ECLogicOutput::construct );
}

ECLogicOutput::ECLogicOutput( ICNDocument *icnDocument, bool newItem, const char *id )
	: Component( icnDocument, newItem, (id) ? id : "logic_output" )
{
	m_name = i18n("Logic Output");
	m_desc = i18n("Shows the logic-state of the input.");
	setSize( -8, -8, 16, 16 );
	
	init1PinLeft();
	m_pIn = createLogicIn(m_pNNode[0]);
	
	m_pSimulator = Simulator::self();
	
	m_lastDrawState = 0.0;
	m_lastSwitchTime = m_lastDrawTime = m_pSimulator->time();
	m_highTime = 0;
	m_bLastState = false;
	m_bDynamicContent = true;
	
	m_pIn->setCallback( this, (CallbackPtr)(&ECLogicOutput::inStateChanged) );
}

ECLogicOutput::~ECLogicOutput()
{
}

void ECLogicOutput::inStateChanged( bool newState )
{
	if ( m_bLastState == newState )
		return;
	
	unsigned long long newTime = m_pSimulator->time();
	unsigned long long dt = newTime - m_lastSwitchTime;
	
	m_lastSwitchTime = newTime;
	
	m_bLastState = newState;
	if (!newState)
	{
		// Gone from high to low
		m_highTime += dt;
	}
}


void ECLogicOutput::drawShape( TQPainter &p )
{
	unsigned long long newTime = m_pSimulator->time();
	unsigned long long runTime = newTime - m_lastDrawTime;
	m_lastDrawTime = newTime;
	
	if (m_bLastState)
	{
		// Logic in is currently high
		m_highTime += newTime - m_lastSwitchTime;
	}
	
	double state;
	
	if ( runTime == 0 )
		state = m_lastDrawState;
	
	else
		state = m_lastDrawState = double(m_highTime)/double(runTime);
	
	initPainter(p);
	p.setBrush( TQColor( 255, uint(255-state*(255-166)), uint((1-state)*255) ) );
	p.drawEllipse( int(x()-8), int(y()-8), width(), height() );
	deinitPainter(p);
	
	m_lastSwitchTime = newTime;
	m_highTime = 0;
}
//END class ECLogicOutput