summaryrefslogtreecommitdiffstats
path: root/src/electronics/components/ecsignallamp.cpp
blob: c7034f7198a2c43a62d0d8b5e071dbcc44454601 (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
/***************************************************************************
 *   Copyright (C) 2003-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 "ecsignallamp.h"
#include "element.h"
#include "libraryitem.h"
#include "pin.h"

#include <klocale.h>
#include <qpainter.h>

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

LibraryItem* ECSignalLamp::libraryItem()
{
	return new LibraryItem(
		QString("ec/signal_lamp"),
		i18n("Signal Lamp"),
		i18n("Outputs"),
		"signal_lamp.png",
		LibraryItem::lit_component,
		ECSignalLamp::construct );
}

ECSignalLamp::ECSignalLamp( ICNDocument *icnDocument, bool newItem, const char *id )
	: Component( icnDocument, newItem, (id) ? id : "signal_lamp" )
{
	m_name = i18n("Signal Lamp");
	m_desc = i18n("A simple filament signal lamp, with a 100 ohms series resistance.");
	setSize( -8, -8, 16, 16 );
	
	init1PinLeft();
	init1PinRight();
	
	createResistance( m_pPNode[0], m_pNNode[0], 100. );
	
	advanceSinceUpdate = 0;
	avgPower = 0.;
	m_bDynamicContent = true;
}

ECSignalLamp::~ECSignalLamp()
{
}

void ECSignalLamp::stepNonLogic()
{
	const double voltage = m_pPNode[0]->pin()->voltage()-m_pNNode[0]->pin()->voltage();
	avgPower = QABS(avgPower*advanceSinceUpdate + (voltage*voltage/100))/++advanceSinceUpdate;
}

void ECSignalLamp::drawShape( QPainter &p )
{
	initPainter(p);
	
	int _x = int(x());
	int _y = int(y());
	
	// Calculate the brightness as a linear function of power, bounded below by
	// 25 milliWatts and above by 500 milliWatts.
	int brightness = (avgPower<0.025) ? 255 : ((avgPower>0.5) ? 0 : (int)(255*(1-((avgPower-0.025)/0.475))));
	advanceSinceUpdate = 0;
	
	p.setBrush( QColor( 255, 255, brightness ) );
	p.drawEllipse( _x-8, _y-8, 16, 16 );
	
	// 2*sqrt(2) = 2.828427125...
	int pos = 8 - int(16/2.828);
	
	p.drawLine( _x-8+pos,	_y-8+pos, _x+8-pos,	_y+8-pos );
	p.drawLine( _x+8-pos,	_y-8+pos, _x-8+pos,	_y+8-pos );
	
	deinitPainter(p);
}