summaryrefslogtreecommitdiffstats
path: root/src/electronics/components/parallelportcomponent.cpp
blob: 9bc17b4164c036e70a8c2a8e37adb9fbffcfbc42 (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
/***************************************************************************
 *   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 "port.h"
#include "parallelportcomponent.h"

#include "ecnode.h"
#include "itemdocument.h"
#include "libraryitem.h"
#include "pin.h"
#include "resistance.h"

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

#include <cmath>
#include <termios.h>

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

LibraryItem* ParallelPortComponent::libraryItem()
{
	return new LibraryItem(
			"ec/parallel_port",
	i18n("Parallel Port"),
	i18n("Connections"),
	"ic1.png",
	LibraryItem::lit_component,
	ParallelPortComponent::construct
						  );
}

ParallelPortComponent::ParallelPortComponent( ICNDocument *icnDocument, bool newItem, const char *id )
	: Component( icnDocument, newItem, id ? id : "parallel_port" )
{
	m_name = i18n("Parallel Port");
	m_desc = i18n("The pins are divided into 3 registers.<br><br>"
			"<b>Data Pins</b><br><br>"
			"The data pins can be configured as either all input or all output. They are:"
			"<ul>"
			"<li><b>D<i>[0..7]</i></b></li>"
			"</ul><br>"
			"<b>Status Pins</b><br><br>"
			"The status pins are read-only. They area:"
			"<ul>"
			"<li><b>ERR</b> - Error</li>"
			"<li><b>ON</b> - Online</li>"
			"<li><b>PE</b> - Paper End</li>"
			"<li><b>ACK</b> - Acknowledge</li>"
			"<li><b>BUSY</b> - Busy</li>"
			"</ul><br>"
			"<b>Control Pins</b>"
			"<ul>"
			"<li><b>STR</b> - Strobe</li>"
			"<li><b>AUT</b> - Auto Feed</li>"
			"<li><b>INIT</b> - Init</li>"
			"<li><b>SEL</b> - Select</li>"
			"</ul><br>"
			"The remaining pins are all ground."
				 );
	
	QPointArray pa( 4 );
	pa[0] = QPoint( -32, -112 );
	pa[1] = QPoint( 32, -104 );
	pa[2] = QPoint( 32, 104 );
	pa[3] = QPoint( -32, 112 );
	setItemPoints( pa );
	
	m_pParallelPort = new ParallelPort();
	
	for ( unsigned i = 0; i < 24; ++i )
		m_pLogic[i] = 0;
	
	ECNode * pin = 0;
	
	//BEGIN Data register
	for ( int i = 0; i < 8; ++i )
	{
		QString id = QString("D%1").arg(i);
		QString name = id;
		
		pin = createPin( -40, -80 + 16*i, 0, id );
		addDisplayText( id, QRect( -28, -88 + 16*i, 28, 16 ), name, true, Qt::AlignLeft | Qt::AlignVCenter );
		
		m_pLogic[i] = createLogicOut( pin, false );
		m_pLogic[i]->setCallback( this, (CallbackPtr)(&ParallelPortComponent::dataCallback) );
	}
	//END Data register
	
	
	//BEGIN Status register
	QString statusNames[] = { "ERR", "ON", "PE", "ACK", "BUSY" };
	
	// The statusIDs are referenced in the save file and must not change
	QString statusIDs[] = { "ERROR", "ONLINE", "PE", "ACK", "BUSY" };
	
	// Bits 0...2 in the Status register are not used
	for ( int i = 3; i < 8; ++i )
	{
		QString id = statusIDs[i-3];
		QString name = statusNames[i-3];
		
		// Bit 3 (pin 15) doesn't not follow the same positioning pattern as
		// the other pins in the Status register.
		if ( i == 3 )
		{
			pin = createPin( 40, -72, 180, id );
			addDisplayText( id, QRect( 0, -80, 28, 16 ), name, true, Qt::AlignRight | Qt::AlignVCenter );
		}
		else
		{
			pin = createPin( -40, -16 + 16*i, 0, id );
			addDisplayText( id, QRect( -28, -24 + 16*i, 28, 16 ), name, true, Qt::AlignLeft | Qt::AlignVCenter );
		}
		
		m_pLogic[i+8] = createLogicOut( pin, false );
	}
	//END Status register
	
	
	//BEGIN Control register
	QString controlNames[] = { "STR", "AUT", "INIT", "SEL" };
	
	// The controlIDs are referenced in the save file and must not change
	QString controlIDs[] = { "STROBE", "AUTO", "INIT", "SELECT" };
	
	// Bits 4..7 are not used (well; bit 5 is, but not as a pin)
	for ( int i = 0; i < 4; ++i )
	{
		QString id = controlIDs[i];
		QString name = controlNames[i];
		
		if ( i == 0 )
		{
			pin = createPin( -40, -96, 0, id );
			addDisplayText( id, QRect( -28, -104, 28, 16 ), name, true, Qt::AlignLeft | Qt::AlignVCenter );
		}
		else if ( i == 1 )
		{
			pin = createPin( 40, -88, 180, id );
			addDisplayText( id, QRect( 0, -96, 28, 16 ), name, true, Qt::AlignRight | Qt::AlignVCenter );
		}
		else
		{
			pin = createPin( 40, -88 + i*16, 180, id );
			addDisplayText( id, QRect( 0, -96 + i*16, 28, 16 ), name, true, Qt::AlignRight | Qt::AlignVCenter );
		}
		
		m_pLogic[i+16] = createLogicOut( pin, false );
		m_pLogic[i+16]->setCallback( this, (CallbackPtr)(&ParallelPortComponent::controlCallback) );
	}
	//END Control register
	
	
#if 0
	// And make the rest of the pins ground
	for ( int i = 0; i < 8; ++i )
	{
		pin = createPin( 40, -24 + i*16, 180, QString("GND%1").arg( i ) );
		pin->pin()->setGroundType( Pin::gt_always );
	}
#endif
	
	Variant * v = createProperty( "port", Variant::Type::Combo );
	v->setAllowed( ParallelPort::ports( Port::ExistsAndRW ) );
	v->setCaption( i18n("Port") );
}


ParallelPortComponent::~ParallelPortComponent()
{
	delete m_pParallelPort;
}


void ParallelPortComponent::dataChanged()
{
	initPort( dataString("port") );
}


void ParallelPortComponent::initPort( const QString & port )
{
	if ( port.isEmpty() )
	{
		m_pParallelPort->closePort();
		return;
	}
	
	if ( ! m_pParallelPort->openPort( port ) )
	{
		p_itemDocument->canvas()->setMessage( i18n("Could not open port %1").arg( port ) );
		return;
	}
}


void ParallelPortComponent::dataCallback( bool )
{
	uchar value = 0;
	for ( unsigned i = 0; i < 8; ++ i )
		value |= m_pLogic[ i + 0 ]->isHigh() ? 0 : (1 << i);
	
	m_pParallelPort->writeToData( value );
}


void ParallelPortComponent::controlCallback( bool )
{
	uchar value = 0;
	for ( unsigned i = 0; i < 4; ++ i )
		value |= m_pLogic[ i + 16 ]->isHigh() ? 0 : (1 << i);
	
	m_pParallelPort->writeToControl( value );
}


void ParallelPortComponent::stepNonLogic()
{
	uchar status = m_pParallelPort->readFromRegister( ParallelPort::Status );
	// Bits 0...2 in the Status register are not used
	for ( int i = 3; i < 8; ++i )
		m_pLogic[i + 8]->setHigh( status | (1 << i) );
}


void ParallelPortComponent::drawShape( QPainter & p )
{
	drawPortShape( p );
}