summaryrefslogtreecommitdiffstats
path: root/src/electronics/components/ecsevensegment.cpp
blob: 8037517ac0f9ead59b8c908acc49dec7e72d5d66 (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
/***************************************************************************
 *   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 "colorcombo.h"
#include "diode.h"
#include "ecled.h"
#include "ecnode.h"
#include "ecsevensegment.h"
#include "libraryitem.h"
#include "simulator.h"

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

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

LibraryItem* ECSevenSegment::libraryItem()
{
	return new LibraryItem(
		"ec/seven_segment",
		i18n("Seven Segment"),
		i18n("Outputs"),
		"seven_segment.png",
		LibraryItem::lit_component,
		ECSevenSegment::construct );
}

ECSevenSegment::ECSevenSegment( ICNDocument *icnDocument, bool newItem, const char *id )
	: Component( icnDocument, newItem, id ? id : "seven_segment" )
{
	m_name = i18n("Seven Segment LED");
	m_desc = i18n("A seven segment display with a decimal point. This can be configured to either have a common cathode or a common anode.");
	m_bDynamicContent = true;
	
	TQStringList pins = TQStringList::split( ',', "g,f,e,d,"+TQString(TQChar(0xB7))+",c,b,a" );
	
	createProperty( "0-color", Variant::Type::Color );
	property("0-color")->setCaption( i18n("Color") );
	property("0-color")->setColorScheme( ColorCombo::LED );
	
	createProperty( "diode-polarity", Variant::Type::Select );
	property("diode-polarity")->setCaption( i18n("Configuration") );
	property("diode-polarity")->setAllowed( TQStringList::split(',',"Common Cathode,Common Anode") );
	property("diode-polarity")->setValue("Common Cathode");
	
	for ( int i=0; i<8; i++ )
	{
		m_diodes[i] = 0L;
		m_nodes[i] = 0L;
		avg_brightness[i] = 0.;
		last_brightness[i] = 255;
	}
	m_nNode = 0L;
	
	lastUpdatePeriod = 0.;
	
	initDIPSymbol( pins, 64 );
	initDIP(pins);
	
	m_nNode = createPin( width()/2+offsetX(), height()+8+offsetY(), 270, "-v" );
	
	for ( int i=0; i<7; i++ )
		m_nodes[i] = ecNodeWithID( TQChar('a'+i) );
	
	m_nodes[7] = ecNodeWithID(TQChar(0xB7));
	
	m_bCommonCathode = false; // Force update
}


ECSevenSegment::~ECSevenSegment()
{
}


void ECSevenSegment::dataChanged()
{
	TQColor color = dataColor("0-color");
	r = color.red();
	g = color.green();
	b = color.blue();
	r /= 0x100;
	g /= 0x100;
	b /= 0x100;
	
	bool commonCathode = dataString("diode-polarity") == "Common Cathode";
	if ( commonCathode != m_bCommonCathode )
	{
		m_bCommonCathode = commonCathode;
		for ( int i=0; i<7; i++ )
		{
			removeElement( m_diodes[i], false );
			if (commonCathode)
				m_diodes[i] = createDiode( m_nodes[i], m_nNode );
			else
				m_diodes[i] = createDiode( m_nNode, m_nodes[i] );
		}
	
		removeElement( m_diodes[7], false );
		if (commonCathode)
			m_diodes[7] = createDiode( m_nodes[7], m_nNode );
		else
			m_diodes[7] = createDiode( m_nNode, m_nodes[7] );
	}
	
	update();
}


void ECSevenSegment::stepNonLogic()
{
	double interval = 1./LINEAR_UPDATE_RATE;
	
	for ( int i=0; i<8; i++ ) {
		avg_brightness[i] += ECLed::brightness( m_diodes[i]->current() )*interval;
	}
	
	lastUpdatePeriod += interval;
}

void ECSevenSegment::drawShape( TQPainter &p )
{
	CNItem::drawShape(p);
	
	initPainter(p);
	
	const int _width = 20;
	const int _height = 32;
	
	const int x1 = (int)x()+offsetX() + (width()-_width)/2 - 1;
	const int x2 = x1 + _width;
	const int y1 = (int)y()+offsetY() + (height()-_height)/2;
	const int y2 = y1 + _height/2;
	const int y3 = y1 + _height;
	const int ds = 2; // "Slope"
	
// 	TQPen pen;
// 	pen.setWidth(2);
// 	pen.setCapStyle(TQt::RoundCap);
// 	p.setPen(pen);
	
	if ( lastUpdatePeriod != 0. )
	{
		for ( uint i=0; i<8; ++i )
		{
			last_brightness[i] = (uint)(avg_brightness[i]/lastUpdatePeriod);
		}
	}
	
	double _b;
	
	// Top
	_b = last_brightness[0];
	p.setPen( TQPen( TQColor( uint(255-(255-_b)*(1-r)), uint(255-(255-_b)*(1-g)), uint(255-(255-_b)*(1-b)) ), 2 ) );
	p.drawLine( x1+3+ds, y1+0, x2-3+ds, y1+0 );
	
	// Top right
	_b = last_brightness[1];
	p.setPen( TQPen( TQColor( uint(255-(255-_b)*(1-r)), uint(255-(255-_b)*(1-g)), uint(255-(255-_b)*(1-b)) ), 2 ) );
	p.drawLine( x2+0+ds, y1+3, x2+0, y2-3 );
	
	// Bottom right
	_b = last_brightness[2];
	p.setPen( TQPen( TQColor( uint(255-(255-_b)*(1-r)), uint(255-(255-_b)*(1-g)), uint(255-(255-_b)*(1-b)) ), 2 ) );
	p.drawLine( x2+0, y2+3, x2+0-ds, y3-3 );
	
	// Bottom
	_b = last_brightness[3];
	p.setPen( TQPen( TQColor( uint(255-(255-_b)*(1-r)), uint(255-(255-_b)*(1-g)), uint(255-(255-_b)*(1-b)) ), 2 ) );
	p.drawLine( x2-3-ds, y3+0, x1+3-ds, y3+0 );
	
	// Bottom left
	_b = last_brightness[4];
	p.setPen( TQPen( TQColor( uint(255-(255-_b)*(1-r)), uint(255-(255-_b)*(1-g)), uint(255-(255-_b)*(1-b)) ), 2 ) );
	p.drawLine( x1+0-ds, y3-3, x1+0, y2+3 );
	
	// Top left
	_b = last_brightness[5];
	p.setPen( TQPen( TQColor( uint(255-(255-_b)*(1-r)), uint(255-(255-_b)*(1-g)), uint(255-(255-_b)*(1-b)) ), 2 ) );
	p.drawLine( x1+0, y2-3, x1+0+ds, y1+3 );
	
	// Middle
	_b = last_brightness[6];
	p.setPen( TQPen( TQColor( uint(255-(255-_b)*(1-r)), uint(255-(255-_b)*(1-g)), uint(255-(255-_b)*(1-b)) ), 2 ) );
	p.drawLine( x1+3, y2+0, x2-3, y2+0 );
	
	// Decimal point
	_b = last_brightness[7];
	p.setBrush( TQBrush( TQColor( uint(255-(255-_b)*(1-r)), uint(255-(255-_b)*(1-g)), uint(255-(255-_b)*(1-b)) ) ) );
	p.setPen( TQt::NoPen );
	p.drawPie( x2+3, y3-2, 3, 3, 0, 16*360 );
	
	lastUpdatePeriod = 0.;
	for ( uint i=0; i<8; ++i ) {
		avg_brightness[i] = 0.;
	}
	
	deinitPainter(p);
}