summaryrefslogtreecommitdiffstats
path: root/src/kserialview.cpp
blob: b2f5a58c30a61c80b74df49125fdba0f1eb207fe (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/***************************************************************************
 *   Copyright (C) 2005 by Mark Six                                        *
 *   marksix@xs4all.nl                                                     *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/
#include "kserialview.h"
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqgroupbox.h>
#include <tqframe.h>

KSerialView::KSerialView( CPicoBlaze *cpu, TQWidget * parent ) : TQWidget( parent )
{
	m_cpu = cpu ;
	
	txPort = new KPort( 1 ) ;
	txPort->setMode( PortWriteable ) ;
	
	rxPort = new KPort( 1 ) ;
	rxPort->setMode( PortReadable ) ;
	
	statusPort = new KPort( 0 ) ;
	statusPort->setMode( PortReadable ) ;
	
	view = new KSerialWindow( this ) ;
	TQWidget *settings = new TQWidget( this ) ;
	settings->setMinimumSize( 90, 90 ) ;
	
	TQVBoxLayout *layout = new TQVBoxLayout( this ) ;
	layout->addWidget( view ) ;
	layout->addWidget( settings ) ;
	
	TQGroupBox *groupBox = new TQGroupBox( "Serial Settings", settings ) ;
	groupBox->setFixedSize( 200, 80 ) ;
	groupBox->move( 10, 0 ) ;
	
	TQLabel *label = new TQLabel( groupBox ) ;
	label->setText( "Transmit" ) ;
	label->move( 5, 15 ) ;
	label->setFixedSize( 55, 18 ) ;
	
	label = new TQLabel( groupBox ) ;
	label->setText( "Receive" ) ;
	label->move( 5, 35 ) ;
	label->setFixedSize( 55, 18 ) ;
	
	label = new TQLabel( groupBox ) ;
	label->setText( "Status" ) ;
	label->move( 5, 55 ) ;
	label->setFixedSize( 55, 18 ) ;
	
	txPortID = new KLineEdit( groupBox ) ;
	txPortID->move( 65, 15 ) ;
	txPortID->setFixedSize( 40, 18 ) ;
	txPortID->setText( "1" ) ;
	
	rxPortID = new KLineEdit(  groupBox ) ;
	rxPortID->move( 65, 35 ) ;
	rxPortID->setText( "1" ) ;
	rxPortID->setFixedSize( 40, 18 ) ;
	
	statusPortID = new KLineEdit( groupBox );
	statusPortID->move( 65, 55 ) ;
	statusPortID->setText( "0" ) ;
	statusPortID->setFixedSize( 40, 18 ) ;
	
	statusPort->setReadValue( 0 ) ;	// Buffers are empty, nothing received.
	connect( txPort, TQT_SIGNAL( write( unsigned char ) ), this, TQT_SLOT( transmit( unsigned char ) ) ) ;
	connect( rxPort, TQT_SIGNAL( read() ), this, TQT_SLOT( receive() ) ) ;
	connect( view, TQT_SIGNAL( keyPressed( int ) ), this, TQT_SLOT( keyPressed( int ) ) ) ;

	connect( txPortID, TQT_SIGNAL( textChanged( const TQString & ) ), this, TQT_SLOT( updateTxId( const TQString & ) ) ) ;
	connect( rxPortID, TQT_SIGNAL( textChanged( const TQString & ) ), this, TQT_SLOT( updateRxId( const TQString & ) ) ) ;
	connect( statusPortID, TQT_SIGNAL( textChanged( const TQString & ) ), this, TQT_SLOT( updateStatusId( const TQString & ) ) ) ;
	
	groupBox = new TQGroupBox( "Status Register", settings ) ;
	groupBox->setFixedSize( 200, 80 ) ;
	groupBox->move( 250, 0 ) ;
	
	label = new TQLabel( groupBox ) ;
	label->setText( "RX" ) ;
	label->move( 106, 15 ) ;
	label->setFixedSize( 20, 20 ) ;

	label = new TQLabel( groupBox ) ;
	label->setText( "TX" ) ;
	label->move( 168, 15 ) ;
	label->setFixedSize( 20, 20 ) ;
	
	label = new TQLabel( groupBox ) ;
	label->setText( "DR" ) ;
	label->move( 80, 30 ) ;
	label->setFixedSize( 20, 20 ) ;
	
	label = new TQLabel( groupBox ) ;
	label->setText( "F" ) ;
	label->move( 110, 30 ) ;
	label->setFixedSize( 20, 20 ) ;
	
	label = new TQLabel( groupBox ) ;
	label->setText( "HF" ) ;
	label->move( 130, 30 ) ;
	label->setFixedSize( 20, 20 ) ;
		
	label = new TQLabel( groupBox ) ;
	label->setText( "F" ) ;
	label->move( 160, 30 ) ;
	label->setFixedSize( 20, 20 ) ;
	
	label = new TQLabel( groupBox ) ;
	label->setText( "HF" ) ;
	label->move( 179, 30 ) ;
	label->setFixedSize( 20, 20 ) ;
	
	int i ;
	for ( i = 0 ; i < 8 ; i++ ) {
		m_statusBits[ i ] = new TQCheckBox( groupBox ) ;
		m_statusBits[ i ]->move( 5 + i * 25, 50 ) ;
		m_statusBits[ i ]->setFixedSize( 15, 20 ) ;
	}
	
	for ( i = 0 ; i < 6 ; i++ ) {
		m_statusBits[ i ]->setEnabled( false ) ;
	}
	
	TQFrame *frame = new TQFrame( groupBox ) ;
	frame->setFrameRect( TQRect( 0, 0, 1, 50 ) ) ;
	frame->setFrameShape( TQFrame::VLine ) ;
	frame->move( 73, 20 ) ;
	frame->setFixedSize( 1, 50 ) ;
	
	frame = new TQFrame( groupBox ) ;
	frame->setFrameRect( TQRect( 0, 0, 1, 50 ) ) ;
	frame->setFrameShape( TQFrame::VLine ) ;
	frame->move( 149, 20 ) ;
	frame->setFixedSize( 1, 50 ) ;
		
	connect( m_statusBits[ 6 ], TQT_SIGNAL( toggled( bool ) ), this, TQT_SLOT( txFlagsChanged( bool ) ) ) ;
	connect( m_statusBits[ 7 ], TQT_SIGNAL( toggled( bool ) ), this, TQT_SLOT( txFlagsChanged( bool ) ) ) ;
	
	fifoPtr = 0 ;
	
	m_cpu->addPort( txPort ) ;
	m_cpu->addPort( rxPort ) ;
	m_cpu->addPort( statusPort ) ;

	m_backgroundColor = rxPortID->backgroundColor() ;
}


KSerialView::~KSerialView()
{
	m_cpu->deletePort( txPort ) ;
	m_cpu->deletePort( rxPort ) ;
	m_cpu->deletePort( statusPort ) ;
	
	delete txPort ;
	delete rxPort ;
	delete statusPort ;
	delete view ;
}

void KSerialView::updateTxId( const TQString & str )
{
	bool ok ;
	int val ;
	TQString s ;
	
	val = str.toInt( &ok ) ;
	if ( ok && val >= 0 && val <= 255 ) {
		txPortID->setBackgroundColor( m_backgroundColor ) ;
		txPort->setID( val ) ;
	} else {
		txPortID->setBackgroundColor( TQColor( 255, 128, 128 ) ) ; 
	}


	/*else {
		s.sprintf( "%u", txPort->getID() ) ;
		txPortID->setText( s ) ;
	}
	*/
}

void KSerialView::updateRxId( const TQString & str )
{
	bool ok ;
	int val ;
	TQString s ;
	
	val = str.toInt( &ok ) ;
	if ( ok && val >= 0 && val <= 255 ) {
		rxPortID->setBackgroundColor( m_backgroundColor ) ;
		rxPort->setID( val ) ;
	} else {
		rxPortID->setBackgroundColor( TQColor( 255, 128, 128 ) ) ; 
	}
	
	/*else {
		s.sprintf( "%u", rxPort->getID() ) ;
		rxPortID->setText( s ) ;
	}
	*/
}

void KSerialView::updateStatusId( const TQString & str )
{
	bool ok ;
	int val ;
	TQString s ;
	
	val = str.toInt( &ok ) ;
	if ( ok && val >= 0 && val <= 255 ) {
		statusPort->setID( val ) ;
		statusPortID->setBackgroundColor( m_backgroundColor ) ;
	} else {
		statusPortID->setBackgroundColor( TQColor( 255, 128, 128 ) ) ; 
	}
	/*else {
		s.sprintf( "%u", statusPort->getID() ) ;
		statusPortID->setText( s ) ;
	}
*/
}

void KSerialView::transmit( unsigned char b )
{
	if ( b == '\r' )
		b = '\n' ;
	
	if ( b == 0x08 ) { // Backspace
		view->doKeyboardAction( TQTextEdit::ActionBackspace ) ;
	} else {
		TQString str ;
		view->insert( (str+=b)  ) ;
	}
}

unsigned char KSerialView::getReceiveFlags()
{	
	unsigned char flags = 0 ;
	if ( fifoPtr != 0 ) flags |= 0x10 ;			// Receive
	else                flags &= ~0x10 ;
		
	if ( fifoPtr > 7 ) flags |= 0x04 ;			// Halffull Marker
	else               flags &= ~0x04 ;
	
	if ( fifoPtr == 15 ) flags |= 0x08 ;		// Full Marker
	else                 flags &= ~0x08 ;
	
	return flags ;			
}		

unsigned char KSerialView::getTransmitFlags()
{
	unsigned char flags = 0 ;
	if ( m_statusBits[ 6 ]->isChecked() )
		flags |= 0x02 ;
	if ( m_statusBits[ 7 ]->isChecked() )
		flags |= 0x01 ;
		
	return flags ;
}

void KSerialView::receive()
{	
	int i ;	
	
	if ( fifoPtr == 0 ) {					// Fifo empty
		statusPort->setReadValue( 0x00 ) ;
		return ;
	}

	rxPort->setReadValue( rxFifo[ 0 ] ) ;
	for ( i = 1 ; i < 16 ; i++ )
		rxFifo[ i - 1 ] = rxFifo[ i ] ;
	fifoPtr -= 1 ; 

	statusPort->setReadValue( getReceiveFlags() | getTransmitFlags() ) ;
	setStatusBits( getReceiveFlags() ) ;
}

void KSerialView::keyPressed( int key )
{
	if ( key == '\n' || key == 0 )
		return ;

	rxFifo[ fifoPtr ] = key ;
	if ( fifoPtr != 15 )
		fifoPtr += 1 ;
	
	statusPort->setReadValue( getReceiveFlags() | getTransmitFlags() ) ;
	setStatusBits( getReceiveFlags() ) ;
}

void KSerialView::txFlagsChanged( bool en )
{
	en = en ;
	statusPort->setReadValue( getReceiveFlags() | getTransmitFlags() ) ;	
}

void KSerialView::setStatusBits( unsigned char value )
{
	m_statusBits[ 3 ]->setChecked( (value & 0x10) != 0 ) ;
	m_statusBits[ 4 ]->setChecked( (value & 0x08) != 0 ) ;		
	m_statusBits[ 5 ]->setChecked( (value & 0x04) != 0 ) ;
}

#include "kserialview.moc"