1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-11-29 07:24:13 +01:00

GCS/connectionManager: some light cleanup and refactoring

GCS/serialconnection: added a new connection plugin for serial port

git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@483 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
julien 2010-04-11 21:08:46 +00:00 committed by julien
parent a862f9638e
commit a4147d5fb2
12 changed files with 297 additions and 31 deletions

View File

@ -51,7 +51,7 @@ namespace Core {
ConnectionManager::ConnectionManager(Internal::MainWindow *mainWindow, Internal::FancyTabWidget *modeStack)
: m_availableDevList(0),
m_connectBtn(0),
m_deviceConnected(false)
m_ioDev(NULL)
{
QVBoxLayout *top = new QVBoxLayout;
top->setSpacing(0);
@ -128,37 +128,24 @@ void ConnectionManager::aboutToRemoveObject(QObject *obj)
*/
void ConnectionManager::onConnectPressed()
{
if(!m_deviceConnected)
//check if we are trying to connect a new device
//or if we are disconnecting it
if(!m_ioDev)
{
m_connectBtn->setText("Disconnect");
m_availableDevList->setEnabled(false);
m_deviceConnected = true;
m_connectionDevice = findDevice(m_availableDevList->currentText());
if(m_connectionDevice.connection)
{
QIODevice *ioDev = m_connectionDevice.connection->openDevice(m_connectionDevice.devName);
/*QextSerialPort *ioDev = new QextSerialPort("COM5");
ioDev->setBaudRate(BAUD57600);
ioDev->setFlowControl(FLOW_OFF);*/
ioDev->open(QIODevice::ReadWrite);
/*if(ioDev->isOpen())
ioDev->putChar('M');*/
QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();
qDebug() << "List of ports:";
for (int i = 0; i < ports.size(); i++) {
qDebug() << "port name:" << ports.at(i).portName;
qDebug() << "friendly name:" << ports.at(i).friendName;
qDebug() << "physical name:" << ports.at(i).physName;
qDebug() << "enumerator name:" << ports.at(i).enumName;
qDebug() << "vendor ID:" << QString::number(ports.at(i).vendorID, 16);
qDebug() << "product ID:" << QString::number(ports.at(i).productID, 16);
qDebug() << "===================================";
}
if(ioDev)
m_ioDev = m_connectionDevice.connection->openDevice(m_connectionDevice.devName);
if(m_ioDev)
{
emit deviceConnected(ioDev);
m_ioDev->open(QIODevice::ReadWrite);
//signal interested plugins that the user wants to connect to the device
emit deviceConnected(m_ioDev);
return;
}
else
@ -172,13 +159,20 @@ void ConnectionManager::onConnectPressed()
m_connectBtn->setText("Connect");
m_availableDevList->setEnabled(true);
m_deviceConnected = false;
//signal interested plugins that user is disconnecting his device
emit deviceDisconnected();
//close the device
if(m_ioDev)
{
m_ioDev->close();
m_ioDev = NULL;
}
if(m_connectionDevice.connection)
m_connectionDevice.connection->closeDevice(m_connectionDevice.devName);
m_connectionDevice.connection = NULL;
emit deviceDisconnected();
}
/**

View File

@ -90,8 +90,12 @@ protected:
QComboBox *m_availableDevList;
QPushButton *m_connectBtn;
QLinkedList<devListItem> m_devList;
//currently connected connection plugin
devListItem m_connectionDevice;
bool m_deviceConnected;
//currently connected QIODevice
QIODevice *m_ioDev;
};
} //namespace Core

View File

@ -18,11 +18,16 @@ plugin_welcome.subdir = welcome
plugin_welcome.depends = plugin_coreplugin
SUBDIRS += plugin_welcome
# RawHID plug-in
# RawHID connection plugin
SUBDIRS += plugin_rawhid
plugin_rawhid.subdir = rawhid
plugin_rawhid.depends = plugin_coreplugin
# Serial port connection plugin
SUBDIRS += plugin_serial
plugin_serial.subdir = serialconnection
plugin_serial.depends = plugin_coreplugin
# UAVObjects plug-in
SUBDIRS += plugin_uavobjects
plugin_uavobjects.subdir = uavobjects

View File

@ -126,7 +126,7 @@ int pjrc_rawhid::open(int max, int vid, int pid, int usage_page, int usage)
if (h == INVALID_HANDLE_VALUE) continue;
attrib.Size = sizeof(HIDD_ATTRIBUTES);
ret = HidD_GetAttributes(h, &attrib);
printf("vid: %4x\n", attrib.VendorID);
//printf("vid: %4x\n", attrib.VendorID);
if (!ret || (vid > 0 && attrib.VendorID != vid) ||
(pid > 0 && attrib.ProductID != pid) ||
!HidD_GetPreparsedData(h, &hid_data)) {

View File

@ -0,0 +1,10 @@
<plugin name="Serial" version="1.0.0" compatVersion="1.0.0">
<vendor>The OpenPilot Project</vendor>
<copyright>(C) 2010 OpenPilot Project</copyright>
<license>GNU Public License (GPL) Version 3</license>
<description>Connection to OpenPilot board using serial link interface</description>
<url>http://www.openpilot.org</url>
<dependencyList>
<dependency name="Core" version="1.0.0"/>
</dependencyList>
</plugin>

View File

@ -0,0 +1,3 @@
include(serial_dependencies.pri)
LIBS *= -l$$qtLibraryTarget(Serial)

View File

@ -0,0 +1 @@
include(../../plugins/coreplugin/coreplugin.pri)

View File

@ -0,0 +1,39 @@
/**
******************************************************************************
*
* @file serial_global.h
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @brief
* @see The GNU Public License (GPL) Version 3
* @defgroup serial_plugin
* @{
*
*****************************************************************************/
/*
* 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 3 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.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef SERIAL_GLOBAL_H
#define SERIAL_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(SERIAL_LIBRARY)
# define SERIAL_EXPORT Q_DECL_EXPORT
#else
# define SERIAL_EXPORT Q_DECL_IMPORT
#endif
#endif // SERIAL_GLOBAL_H

View File

@ -0,0 +1,12 @@
TEMPLATE = lib
TARGET = Serial
include(../../openpilotgcsplugin.pri)
include(serial_dependencies.pri)
INCLUDEPATH += ../../libs\qextserialport\src
HEADERS += serialplugin.h \
serial_global.h
SOURCES += serialplugin.cpp
FORMS +=
RESOURCES +=
DEFINES += SERIAL_LIBRARY
OTHER_FILES += Serial.pluginspec

View File

@ -0,0 +1,122 @@
/**
******************************************************************************
*
* @file serialplugin.cpp
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @brief Register connection object for the core connection manager
* @see The GNU Public License (GPL) Version 3
* @defgroup serial_plugin
* @{
*
*****************************************************************************/
/*
* 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 3 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.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "serialplugin.h"
#include <extensionsystem/pluginmanager.h>
#include <coreplugin/icore.h>
#include <QtCore/QtPlugin>
#include <QtGui/QMainWindow>
#include <qextserialport.h>
#include <qextserialenumerator.h>
#include <QDebug>
SerialConnection::SerialConnection()
{
//I'm cheating a little bit here:
//Knowing if the device enumeration really changed is a bit complicated
//so I just signal it whenever we have a device event...
QMainWindow *mw = Core::ICore::instance()->mainWindow();
QObject::connect(mw, SIGNAL(deviceChange()),
this, SLOT(onEnumerationChanged()));
}
SerialConnection::~SerialConnection()
{
}
void SerialConnection::onEnumerationChanged()
{
emit availableDevChanged(this);
}
QStringList SerialConnection::availableDevices()
{
QStringList list;
QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();
foreach( QextPortInfo port, ports ) {
list.append(port.friendName);
}
return list;
}
QIODevice *SerialConnection::openDevice(const QString &deviceName)
{
QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();
foreach( QextPortInfo port, ports ) {
if(port.friendName == deviceName)
{
//we need to handle port settings here...
return new QextSerialPort(port.portName);
}
}
return NULL;
}
void SerialConnection::closeDevice(const QString &deviceName)
{
//nothing to do here
}
QString SerialConnection::connectionName()
{
return QString("Serial port");
}
QString SerialConnection::shortName()
{
return QString("Serial");
}
SerialPlugin::SerialPlugin()
{
}
SerialPlugin::~SerialPlugin()
{
}
void SerialPlugin::extensionsInitialized()
{
addAutoReleasedObject(new SerialConnection);
}
bool SerialPlugin::initialize(const QStringList &arguments, QString *errorString)
{
Q_UNUSED(arguments);
Q_UNUSED(errorString);
return true;
}
Q_EXPORT_PLUGIN(SerialPlugin)

View File

@ -0,0 +1,78 @@
/**
******************************************************************************
*
* @file serialplugin.h
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @brief
* @see The GNU Public License (GPL) Version 3
* @defgroup serial_plugin
* @{
*
*****************************************************************************/
/*
* 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 3 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.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef SERIALPLUGIN_H
#define SERIALPLUGIN_H
#include "serial_global.h"
#include "coreplugin/iconnection.h"
#include <extensionsystem/iplugin.h>
class IConnection;
class QextSerialEnumerator;
/**
* Define a connection via the IConnection interface
* Plugin will add a instance of this class to the pool,
* so the connection manager can use it.
*/
class SERIAL_EXPORT SerialConnection
: public Core::IConnection
{
Q_OBJECT
public:
SerialConnection();
virtual ~SerialConnection();
virtual QStringList availableDevices();
virtual QIODevice *openDevice(const QString &deviceName);
virtual void closeDevice(const QString &deviceName);
virtual QString connectionName();
virtual QString shortName();
protected slots:
void onEnumerationChanged();
};
class SERIAL_EXPORT SerialPlugin
: public ExtensionSystem::IPlugin
{
Q_OBJECT
public:
SerialPlugin();
~SerialPlugin();
virtual bool initialize(const QStringList &arguments, QString *error_message);
virtual void extensionsInitialized();
};
#endif // SERIALPLUGIN_H

View File

@ -46,9 +46,7 @@ void UAVTalkPlugin::extensionsInitialized()
ExtensionSystem::PluginManager* pm = ExtensionSystem::PluginManager::instance();
objMngr = pm->getObject<UAVObjectManager>();
// TODO: Initialize serial port and USB libraries, get QIODevice from each
// ---Julien: Actually I changed a little bit from the initial plan,
// now you have to connect to signal emited by the connection manager
// Connect to connection manager so we get notified when the user connect to his device
Core::ConnectionManager *cm = Core::ICore::instance()->connectionManager();
QObject::connect(cm, SIGNAL(deviceConnected(QIODevice *)),
this, SLOT(onDeviceConnect(QIODevice *)));