File indexing completed on 2024-05-05 05:46:09

0001 /***************************************************************************
0002  *   Copyright (C) 2005 by David Saxton                                    *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #include "serialportcomponent.h"
0012 #include "port.h"
0013 
0014 #include "ecnode.h"
0015 #include "itemdocument.h"
0016 #include "libraryitem.h"
0017 #include "pin.h"
0018 #include "resistance.h"
0019 
0020 #include <KLocalizedString>
0021 
0022 #include <QPainter>
0023 
0024 #include <cmath>
0025 
0026 #include <ktechlab_debug.h>
0027 
0028 void SerialPortComponent_tdCallback(void *objV, bool state) {
0029     SerialPortComponent *objT = static_cast<SerialPortComponent*>(objV);
0030     objT->tdCallback(state);
0031 }
0032 void SerialPortComponent_dtrCallback(void *objV, bool state) {
0033     SerialPortComponent *objT = static_cast<SerialPortComponent*>(objV);
0034     objT->dtrCallback(state);
0035 }
0036 
0037 Item *SerialPortComponent::construct(ItemDocument *itemDocument, bool newItem, const char *id)
0038 {
0039     return new SerialPortComponent(static_cast<ICNDocument *>(itemDocument), newItem, id);
0040 }
0041 
0042 LibraryItem *SerialPortComponent::libraryItem()
0043 {
0044     return new LibraryItem(QStringList(QString("ec/serial_port")), i18n("Serial Port"), i18n("Connections"), "ic1.png", LibraryItem::lit_component, SerialPortComponent::construct);
0045 }
0046 
0047 SerialPortComponent::SerialPortComponent(ICNDocument *icnDocument, bool newItem, const char *id)
0048     : Component(icnDocument, newItem, id ? id : "serial_port")
0049 {
0050     m_name = i18n("Serial Port");
0051 
0052     QPolygon pa(4);
0053     pa[0] = QPoint(-32, -48);
0054     pa[1] = QPoint(32, -40);
0055     pa[2] = QPoint(32, 40);
0056     pa[3] = QPoint(-32, 48);
0057 
0058     setItemPoints(pa);
0059 
0060     m_pSerialPort = new SerialPort();
0061 
0062     ECNode *pin = nullptr;
0063 
0064     // Works
0065     pin = createPin(-40, 32, 0, "CD");
0066     addDisplayText("CD", QRect(-28, 24, 28, 16), "CD", true, Qt::AlignLeft | Qt::AlignVCenter);
0067     m_pCD = createLogicOut(pin, false);
0068 
0069     // Doesn't work
0070     //  pin = createPin( -40,  16,   0, "RD" );
0071     addDisplayText("RD", QRect(-28, 8, 28, 16), "RD", true, Qt::AlignLeft | Qt::AlignVCenter);
0072     //  m_pRD = createLogicOut( pin, false  );
0073 
0074     // Works
0075     pin = createPin(-40, 0, 0, "TD");
0076     addDisplayText("TD", QRect(-28, -8, 28, 16), "TD", true, Qt::AlignLeft | Qt::AlignVCenter);
0077     m_pTD = createLogicIn(pin);
0078     //m_pTD->setCallback(this, (CallbackPtr)(&SerialPortComponent::tdCallback));
0079     m_pTD->setCallback2(SerialPortComponent_tdCallback, this);
0080 
0081     // Works
0082     pin = createPin(-40, -16, 0, "DTR");
0083     addDisplayText("DTR", QRect(-28, -24, 28, 16), "DTR", true, Qt::AlignLeft | Qt::AlignVCenter);
0084     m_pDTR = createLogicIn(pin);
0085     //m_pDTR->setCallback(this, (CallbackPtr)(&SerialPortComponent::dtrCallback));
0086     m_pDTR->setCallback2(SerialPortComponent_dtrCallback, this);
0087 
0088     // N/A
0089     pin = createPin(-40, -32, 0, "GND");
0090     addDisplayText("GND", QRect(-28, -40, 28, 16), "GND", true, Qt::AlignLeft | Qt::AlignVCenter);
0091     pin->pin()->setGroundType(Pin::gt_always);
0092 
0093     // Doesn't work
0094     //  pin = createPin(  40,  24, 180, "DSR" );
0095     addDisplayText("DSR", QRect(0, 16, 28, 16), "DSR", true, Qt::AlignRight | Qt::AlignVCenter);
0096     //  m_pDSR = createLogicIn( pin );
0097     //  //m_pDSR->setCallback( this, (CallbackPtr)(&SerialPortComponent::dsrCallback) );
0098     //  m_pDSR->setCallback2( SerialPortComponent_dsrCallback, this);
0099 
0100     // Doesn't work
0101     //  pin = createPin(  40,   8, 180, "RTS" );
0102     addDisplayText("RTS", QRect(0, 0, 28, 16), "RTS", true, Qt::AlignRight | Qt::AlignVCenter);
0103     //  m_pRTS = createLogicIn( pin );
0104     //  //m_pRTS->setCallback( this, (CallbackPtr)(&SerialPortComponent::rtsCallback) );
0105     //  m_pDSR->setCallback2( SerialPortComponent_rtsCallback, this);
0106 
0107     // Works
0108     pin = createPin(40, -8, 180, "CTS");
0109     addDisplayText("CTS", QRect(0, -16, 28, 16), "CTS", true, Qt::AlignRight | Qt::AlignVCenter);
0110     m_pCTS = createLogicOut(pin, false);
0111 
0112     // Works
0113     pin = createPin(40, -24, 180, "RI");
0114     addDisplayText("RI", QRect(0, -32, 28, 16), "RI", true, Qt::AlignRight | Qt::AlignVCenter);
0115     m_pRI = createLogicOut(pin, false);
0116 
0117     Variant *v = createProperty("port", Variant::Type::Combo);
0118     v->setAllowed(SerialPort::ports());
0119     v->setCaption(i18n("Port"));
0120 
0121     //  v = createProperty( "baudRate", Variant::Type::Select );
0122     //  v->setAllowed( QStringList::split( ",", "B0,B50,B75,B110,B134,B150,B200,B300,B600,B1200,B1800,B2400,B4800,B9600,B19200,B38400" ) );
0123     //  v->setCaption( i18n("Baud rate") );
0124     //  v->setValue("B9600");
0125 }
0126 
0127 SerialPortComponent::~SerialPortComponent()
0128 {
0129     delete m_pSerialPort;
0130 }
0131 
0132 void SerialPortComponent::dataChanged()
0133 {
0134 #if 0
0135     QString baudString = dataString("baudRate");
0136     unsigned baudRate = 0;
0137     
0138     if ( baudString == "B0" )
0139         baudRate = B0;
0140     else if ( baudString == "B50" )
0141         baudRate = B50;
0142     else if ( baudString == "B75" )
0143         baudRate = B75;
0144     else if ( baudString == "B110" )
0145         baudRate = B110;
0146     else if ( baudString == "B134" )
0147         baudRate = B134;
0148     else if ( baudString == "B150" )
0149         baudRate = B150;
0150     else if ( baudString == "B200" )
0151         baudRate = B200;
0152     else if ( baudString == "B300" )
0153         baudRate = B300;
0154     else if ( baudString == "B600" )
0155         baudRate = B600;
0156     else if ( baudString == "B1200" )
0157         baudRate = B1200;
0158     else if ( baudString == "B1800" )
0159         baudRate = B1800;
0160     else if ( baudString == "B2400" )
0161         baudRate = B2400;
0162     else if ( baudString == "B4800" )
0163         baudRate = B4800;
0164     else if ( baudString == "B9600" )
0165         baudRate = B9600;
0166     else if ( baudString == "B19200" )
0167         baudRate = B19200;
0168     else if ( baudString == "B38400" )
0169         baudRate = B38400;
0170     else
0171     {
0172         qCCritical(KTL_LOG) << "Unknown baud rate = \""<<baudString<<"\"";
0173         return;
0174     }
0175     
0176     initPort( dataString("port"), baudRate );
0177 #endif
0178 
0179     initPort(dataString("port"), 200);
0180 }
0181 
0182 void SerialPortComponent::initPort(const QString &port, qint32 baudRate)
0183 {
0184     if (port.isEmpty()) {
0185         m_pSerialPort->closePort();
0186         return;
0187     }
0188 
0189     if (!m_pSerialPort->openPort(port, baudRate)) {
0190         p_itemDocument->canvas()->setMessage(i18n("Could not open port %1", port));
0191         return;
0192     }
0193 }
0194 
0195 void SerialPortComponent::stepNonLogic()
0196 {
0197     m_pCD->setHigh(m_pSerialPort->getDataCarrierDetectSignal());
0198     //  m_pRD->setHigh(m_pSerialPort->getSecondaryReceivedDataSignal());
0199     m_pCTS->setHigh(m_pSerialPort->getClearToSendSignal());
0200     m_pRI->setHigh(m_pSerialPort->getRingIndicatorSignal());
0201 }
0202 
0203 void SerialPortComponent::tdCallback(bool isHigh)
0204 {
0205     m_pSerialPort->setBreakEnabled(isHigh);
0206 }
0207 
0208 void SerialPortComponent::dtrCallback(bool isHigh)
0209 {
0210     m_pSerialPort->setDataTerminalReady(isHigh);
0211 }
0212 
0213 void SerialPortComponent::dsrCallback(bool isHigh)
0214 {
0215     m_pSerialPort->setDataSetReady(isHigh);
0216 }
0217 
0218 void SerialPortComponent::rtsCallback(bool isHigh)
0219 {
0220     m_pSerialPort->setRequestToSend(isHigh);
0221 }
0222 
0223 void SerialPortComponent::drawShape(QPainter &p)
0224 {
0225     drawPortShape(p);
0226 }