summaryrefslogtreecommitdiffstats
path: root/src/electronics/components/ecbcdto7segment.cpp
blob: fb210ea8a9ecbc4bd2cd999c7c9d9bf9bea8a66b (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
/***************************************************************************
 *   Copyright (C) 2003-2004 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 "ecbcdto7segment.h"

#include "logic.h"
#include "libraryitem.h"

#include <kiconloader.h>
#include <klocale.h>

// Values for a,b,c,d,e,f,g of common-anode 7 segment display
bool numbers[16][7] =
	{ { 1, 1, 1, 1, 1, 1, 0 }, // 0
	  { 0, 1, 1, 0, 0, 0, 0 }, // 1
	  { 1, 1, 0, 1, 1, 0, 1 }, // 2
	  { 1, 1, 1, 1, 0, 0, 1 }, // 3
	  { 0, 1, 1, 0 ,0, 1, 1 }, // 4
	  { 1, 0, 1, 1, 0, 1, 1 }, // 5
	  { 1, 0, 1, 1, 1, 1, 1 }, // 6
	  { 1, 1, 1, 0, 0, 0, 0 }, // 7
	  { 1, 1, 1, 1, 1, 1, 1 }, // 8
	  { 1, 1, 1, 0, 0, 1, 1 }, // 9
	  { 1, 1, 1, 0, 1, 1, 1 }, // A
	  { 0, 0, 1, 1, 1, 1, 1 }, // b
	  { 1, 0, 0, 1, 1, 1, 0 }, // C
	  { 0, 1, 1, 1, 1, 0, 1 }, // d
	  { 1, 0, 0, 1, 1, 1, 1 }, // E
	  { 1, 0, 0, 0, 1, 1, 1 } }; // F

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

LibraryItem* ECBCDTo7Segment::libraryItem()
{
	return new LibraryItem(
		QString::QString("ec/bcd_to_seven_segment"),
		i18n("BCD to 7 Segment"),
		i18n("Integrated Circuits"),
		"ic2.png",
		LibraryItem::lit_component,
		ECBCDTo7Segment::construct
			);
}

ECBCDTo7Segment::ECBCDTo7Segment( ICNDocument *icnDocument, bool newItem, const char *id )
	: Component( icnDocument, newItem, (id) ? id : "bcd_to_seven_segment" )
{
	m_name = i18n("BCD to Seven Segment");
	m_desc = i18n("Converts a binary-coded-input to a form displayable by a seven segment display.<br><br>"
				"Normal operation: <i>lt</i> (Lamp Test) and the <i>rb</i> (Ripple Blanking) are held high, <i>en</i> (Enable) is held low.");
				
	ALogic = BLogic = CLogic = DLogic = 0L;
	ltLogic = rbLogic = enLogic = 0L;
	
	for ( int i=0; i<7; i++ )
	{
		outLogic[i] = 0L;
		oldOut[i] = false;
	}

	QStringList pins = QStringList::split( ',', "A,B,C,D,,lt,rb,en,d,e,f,g,,a,b,c", true );
	initDIPSymbol( pins, 48 );
	initDIP(pins);
	
	ALogic = createLogicIn( ecNodeWithID("A") );
	BLogic = createLogicIn( ecNodeWithID("B") );
	CLogic = createLogicIn( ecNodeWithID("C") );
	DLogic = createLogicIn( ecNodeWithID("D") );
	ltLogic = createLogicIn( ecNodeWithID("lt") );
	rbLogic = createLogicIn( ecNodeWithID("rb") );
	enLogic = createLogicIn( ecNodeWithID("en") );
	
	ALogic->setCallback( this, (CallbackPtr)(&ECBCDTo7Segment::inStateChanged) );
	BLogic->setCallback( this, (CallbackPtr)(&ECBCDTo7Segment::inStateChanged) );
	CLogic->setCallback( this, (CallbackPtr)(&ECBCDTo7Segment::inStateChanged) );
	DLogic->setCallback( this, (CallbackPtr)(&ECBCDTo7Segment::inStateChanged) );
	ltLogic->setCallback( this, (CallbackPtr)(&ECBCDTo7Segment::inStateChanged) );
	rbLogic->setCallback( this, (CallbackPtr)(&ECBCDTo7Segment::inStateChanged) );
	enLogic->setCallback( this, (CallbackPtr)(&ECBCDTo7Segment::inStateChanged) );
	
	for ( uint i=0; i<7; ++i )
	{
		outLogic[i] = createLogicOut( ecNodeWithID( QChar('a'+i) ), false );
		outLogic[i]->setCallback( this, (CallbackPtr)(&ECBCDTo7Segment::inStateChanged) );
	}
	inStateChanged(false);
}

ECBCDTo7Segment::~ECBCDTo7Segment()
{
}

void ECBCDTo7Segment::inStateChanged(bool)
{
	bool A = ALogic->isHigh();
	bool B = BLogic->isHigh();
	bool C = CLogic->isHigh();
	bool D = DLogic->isHigh();
	bool lt = ltLogic->isHigh(); // Lamp test
	bool rb = rbLogic->isHigh(); // Ripple Blank
	bool en = enLogic->isHigh(); // Enable (store)
	
	int n = A + 2*B + 4*C + 8*D;
// 	if ( n > 9 ) n = 0;
	
	bool out[7];
	
	if (lt) // Lamp test
	{
		if (rb) // Ripple blanking
		{
			if (en) // Enable (store)
			{
				for ( int i=0; i<7; i++ )
				{
					out[i] = oldOut[i];
				}
			}
			else
			{
				for ( int i=0; i<7; i++ )
				{
					out[i] = numbers[n][i];
					oldOut[i] = out[i];
				}
			}
		}
		else
		{
			for ( int i=0; i<7; i++ )
			{
				out[i] = false;
			}
		}
	}
	else
	{
		for ( int i=0; i<7; i++ )
		{
			out[i] = true;
		}
	}
	
	for ( int i=0; i<7; i++ )
	{
		outLogic[i]->setHigh( out[i] );
	}
}