1
/***************************************************************************
2
* Copyright (C) 2018 by santiago González *
3
* santigoro@gmail.com *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 3 of the License, or *
8
* (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program; if not, see <http://www.gnu.org/licenses/>. *
18
***************************************************************************/
20
#include "serialport.h"
21
#include "mcucomponent.h"
22
#include "baseprocessor.h"
23
#include "simulator.h"
25
#include "itemlibrary.h"
28
static const char* SerialPort_properties[] = {
29
QT_TRANSLATE_NOOP("App::Property","Steps"),
30
QT_TRANSLATE_NOOP("App::Property","Port Name"),
31
QT_TRANSLATE_NOOP("App::Property","BaudRate"),
32
QT_TRANSLATE_NOOP("App::Property","DataBits"),
33
QT_TRANSLATE_NOOP("App::Property","Parity"),
34
QT_TRANSLATE_NOOP("App::Property","StopBits"),
35
QT_TRANSLATE_NOOP("App::Property","FlowControl")
38
Component* SerialPort::construct( QObject* parent, QString type, QString id )
40
return new SerialPort( parent, type, id );
43
LibraryItem* SerialPort::libraryItem()
45
return new LibraryItem(
50
SerialPort::construct );
53
SerialPort::SerialPort( QObject* parent, QString type, QString id )
54
: Component( parent, type, id )
55
, eElement( (id+"-eElement") )
57
Q_UNUSED( SerialPort_properties );
59
m_area = QRect( -34, -26, 160, 38 );
60
setLabelPos(-34,-20 );
62
m_serial = new QSerialPort( this );
68
m_BaudRate = QSerialPort::Baud9600;
69
m_dataBits = QSerialPort::Data8;
70
m_parity = QSerialPort::NoParity;
71
m_stopBits = QSerialPort::OneStop;
72
m_flowControl = QSerialPort::NoFlowControl;
74
m_button = new QPushButton( );
75
m_button->setMaximumSize( 32,16 );
76
m_button->setGeometry(-36,-16,32,16);
77
m_button->setCheckable( true );
78
m_button->setText( "Open" );
80
m_proxy = Circuit::self()->addWidget( m_button );
81
m_proxy->setParentItem( this );
82
m_proxy->setPos( QPoint(-32, -8) );
84
connect( m_button, SIGNAL( clicked() ),
85
this, SLOT( onbuttonclicked() ), Qt::UniqueConnection);
87
connect( m_serial, SIGNAL( readyRead()),
88
this, SLOT( readData()), Qt::UniqueConnection );
90
Simulator::self()->addToUpdateList( this );
95
SerialPort::~SerialPort()
97
Simulator::self()->remFromUpdateList( this );
100
void SerialPort::setMcuId( QString mcu )
102
QString name = Circuit::self()->origId( mcu );
103
if( name == "" ) name = mcu;
106
Component* mcuComp = Circuit::self()->getCompById( m_mcuId );
107
if( mcuComp ) m_mcuComponent = static_cast<McuComponent*>(mcuComp);
109
//qDebug() << "SerialPort::setMcuId" << mcu << name;
112
void SerialPort::initialize()
116
int circVersion = Circuit::self()->circType().split(".").last().toInt();
118
if( circVersion < 5 )
120
m_mcuComponent = McuComponent::self();
121
m_mcuId = m_mcuComponent->objectName();
125
Component* mcu = Circuit::self()->getCompById( m_mcuId );
128
m_mcuComponent = static_cast<McuComponent*>(mcu);
129
//qDebug() << "SerialPort::resetState" << mcu->objectName();
134
m_processor = m_mcuComponent->processor();
136
connect( m_mcuComponent, SIGNAL( closeSerials()),
137
this, SLOT( slotClose()), Qt::UniqueConnection );
139
connect( m_mcuComponent, SIGNAL( openSerials()),
140
this, SLOT( slotAutoOpen()), Qt::UniqueConnection );
142
connect( m_processor, SIGNAL( uartDataOut( int, int )),
143
this, SLOT( slotWriteData( int, int )), Qt::UniqueConnection );
147
void SerialPort::updateStep()
152
void SerialPort::slotAutoOpen()
154
if( m_autoOpen ) open();
157
void SerialPort::open()
159
if( m_serial->isOpen() ) close();
161
m_serial->setPortName( m_portName );
162
m_serial->setBaudRate( m_BaudRate );
163
m_serial->setDataBits( m_dataBits );
164
m_serial->setParity( m_parity );
165
m_serial->setStopBits( m_stopBits );
166
m_serial->setFlowControl( m_flowControl );
168
if( m_serial->open(QIODevice::ReadWrite) )
170
qDebug()<<"Connected to" << m_portName;
171
m_button->setText( "Close" );
175
m_button->setChecked( false );
176
MessageBoxNB( "Error", tr("Cannot Open Port %1:\n%2.").arg(m_portName).arg(m_serial->errorString()) );
182
void SerialPort::close()
184
if( m_serial->isOpen() ) m_serial->close();
185
m_button->setText( "Open" );
188
//ui->openButton->setEnabled( true );
189
//ui->closeButton->setEnabled( false );
192
void SerialPort::readData()
194
QByteArray data = m_serial->readAll();
195
m_active = !m_active;
197
//qDebug()<<"SerialPort::readData" << data;
199
for( int i=0; i<data.size(); i++ ) m_processor->uartIn( m_uart, data.at(i) );
202
void SerialPort::slotWriteData( int uart, int value )
204
if( uart != m_uart ) return;
206
if( m_serial->isOpen() )
211
m_serial->write( ba );
212
m_active = !m_active;
214
//qDebug() << "SerialPort::slotWriteData"<<value;
218
void SerialPort::slotClose()
221
Circuit::self()->removeComp( this );
224
void SerialPort::setUart( int uart )
226
if ( uart<1 ) uart = 1;
227
else if ( uart>6 ) uart = 6;
232
void SerialPort::onbuttonclicked()
234
if( m_button->isChecked() ) open();
238
void SerialPort::paint( QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *widget )
240
Component::paint( p, option, widget );
242
p->setBrush( Qt::darkBlue );
243
p->drawRoundedRect( m_area, 4, 4 );
245
if( m_serial->isOpen() )
247
if( m_active ) p->setBrush( Qt::yellow );
248
else p->setBrush( Qt::red );
251
else p->setBrush( Qt::black );
252
p->drawEllipse( 2, -6, 12, 12);
254
p->setBrush( Qt::white );
257
pen.setColor( QColor( 250, 210, 150 ) );
259
p->drawText( 18 , 5, "Uart"+QString::number(m_uart+1)+"-"+m_portName );
261
QString mcuId = m_mcuId;
262
if( m_mcuComponent ) mcuId = m_mcuComponent->idLabel();
263
p->drawText(-30 ,-13, mcuId );
266
#include "moc_serialport.cpp"