add CANConManager

This commit is contained in:
canpadawan
2016-06-29 11:55:58 +02:00
parent db7ad42199
commit e402ea76fa
5 changed files with 124 additions and 8 deletions
+90 -2
View File
@@ -1,6 +1,94 @@
#include "canconmanager.h"
CANConManager::CANConManager()
{
CANConManager* CANConManager::mInstance = NULL;
CANConManager* CANConManager::getInstance()
{
if(!mInstance)
mInstance = new CANConManager();
return mInstance;
}
CANConManager::CANConManager(QObject *parent): QObject(parent)
{
connect(&mTimer, SIGNAL(timeout()), this, SLOT(refreshCanList()));
mTimer.setInterval(500); /*tick twice a second */
mTimer.setSingleShot(false);
mTimer.start();
}
CANConManager::~CANConManager()
{
mTimer.stop();
mInstance = NULL;
}
void CANConManager::add(CANConnection* pConn_p)
{
connect(pConn_p, SIGNAL(notify()), this, SLOT(refreshCanList()));
mConns.append(pConn_p);
}
void CANConManager::remove(CANConnection* pConn_p)
{
disconnect(pConn_p, 0, this, 0);
mConns.removeOne(pConn_p);
}
void CANConManager::refreshCanList()
{
QObject* sender_p = QObject::sender();
if( sender_p != &mTimer)
{
/* if we are not the sender, the signal is coming from a connection */
/* refresh only the given connection */
if(mConns.contains((CANConnection*) sender_p))
refreshConnection((CANConnection*)sender_p);
}
else
{
foreach (CANConnection* conn_p, mConns)
refreshConnection((CANConnection*)conn_p);
}
}
QList<CANConnection*>& CANConManager::getConnections()
{
return mConns;
}
CANConnection* CANConManager::getByName(const QString& pName) const
{
foreach(CANConnection* conn_p, mConns)
{
if(conn_p->getPort() == pName)
return conn_p;
}
return NULL;
}
void CANConManager::refreshConnection(CANConnection* pConn_p)
{
CANFrame* frame_p = NULL;
QVector<CANFrame> frames;
while( (frame_p = pConn_p->getQueue().peek() ) ) {
frames.append(*frame_p);
pConn_p->getQueue().dequeue();
}
if(frames.size())
emit framesReceived(pConn_p, frames);
}
+31 -3
View File
@@ -1,11 +1,39 @@
#ifndef CANCONMANAGER_H
#define CANCONMANAGER_H
#include <QObject>
#include <QTimer>
class CANConManager
#include "canconnection.h"
class CANConManager : public QObject
{
Q_OBJECT
public:
CANConManager();
static CANConManager* getInstance();
virtual ~CANConManager();
void add(CANConnection* pConn_p);
void remove(CANConnection* pConn_p);
QList<CANConnection*>& getConnections();
CANConnection* getByName(const QString& pName) const;
signals:
void framesReceived(CANConnection* pConn_p, QVector<CANFrame>& pFrames);
private slots:
void refreshCanList();
private:
explicit CANConManager(QObject *parent = 0);
void refreshConnection(CANConnection* pConn_p);
static CANConManager* mInstance;
QList<CANConnection*> mConns;
QTimer mTimer;
};
#endif // CANCONMANAGER_H
#endif // CANCONNECTIONMODEL_H
+1 -1
View File
@@ -191,7 +191,7 @@ bool CANConnection::sendFrames(const QList<CANFrame>& pFrames)
}
int CANConnection::getNumBuses() {
int CANConnection::getNumBuses() const{
return mNumBuses;
}
+1 -1
View File
@@ -42,7 +42,7 @@ public:
* @brief getNumBuses
* @return returns the number of buses of the device
*/
int getNumBuses();
int getNumBuses() const;
/**
* @brief getPort
+1 -1
View File
@@ -147,7 +147,7 @@ void SocketCan::errorReceived(QCanBusDevice::CanBusError error) const
void SocketCan::framesWritten(qint64 count)
{
qDebug() << "Number of frames written:" << count;
//qDebug() << "Number of frames written:" << count;
}
void SocketCan::framesReceived()