Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remember busspeed #457

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions connections/canconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,24 @@ void CANConnection::setBusSettings(int pBusIdx, CANBus pBus)
}


void CANConnection::updateBusSpeed(int pBusIdx, int speed)
{
/* make sure we execute in mThread context */
if( mThread_p && (mThread_p != QThread::currentThread()) ) {
QMetaObject::invokeMethod(this, "updateBusSpeed",
Qt::BlockingQueuedConnection,
Q_ARG(int, pBusIdx),
Q_ARG(int, speed));
return;
}


if (pBusIdx >= mNumBuses || pBusIdx >= mBusData.length()) return;

mBusData[pBusIdx].mBus.setSpeed(speed);
}


bool CANConnection::sendFrame(const CANFrame& pFrame)
{
/* make sure we execute in mThread context */
Expand Down
5 changes: 5 additions & 0 deletions connections/canconnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ public slots:
*/
bool getBusSettings(int pBusIdx, CANBus& pBus);

/**
* updateBusSpeed
*/
void updateBusSpeed(int pBusIdx, int speed);

/**
* @brief suspends/restarts data capture
* @param pSuspend: suspends capture if true else restarts it
Expand Down
66 changes: 60 additions & 6 deletions connections/connectionwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ void ConnectionWindow::handleNewConn()
newType = thisDialog->getConnectionType();
newPort = thisDialog->getPortName();
newDriver = thisDialog->getDriverName();
conn = create(newType, newPort, newDriver);
QVector<int> newBusSpeed;
conn = create(newType, newPort, newDriver, 0, newBusSpeed);
if (conn)
{
connModel->add(conn);
Expand Down Expand Up @@ -293,6 +294,8 @@ void ConnectionWindow::handleResetConn()
{
QString port, driver;
CANCon::type type;
int busnums;
QVector<int> busSpeeds;

int selIdx = ui->tableConnections->selectionModel()->currentIndex().row();
if (selIdx <0) return;
Expand All @@ -305,13 +308,19 @@ void ConnectionWindow::handleResetConn()
type = conn_p->getType();
port = conn_p->getPort();
driver = conn_p->getDriver();
busnums = conn_p->getNumBuses();
for (int i = 0; i < busnums; i++) {
CANBus bus;
conn_p->getBusSettings(i, bus);
busSpeeds.append(bus.getSpeed());
}

/* stop and delete connection */
conn_p->stop();

conn_p = nullptr;

conn_p = create(type, port, driver);
conn_p = create(type, port, driver, busnums, busSpeeds);
if (conn_p) connModel->replace(selIdx, conn_p);
}

Expand Down Expand Up @@ -476,7 +485,7 @@ void ConnectionWindow::handleSendText() {
emit sendDebugData(bytes);
}

CANConnection* ConnectionWindow::create(CANCon::type pTye, QString pPortName, QString pDriver)
CANConnection* ConnectionWindow::create(CANCon::type pTye, QString pPortName, QString pDriver, int busNum, QVector<int> busSpeeds)
{
CANConnection* conn_p;

Expand All @@ -492,8 +501,18 @@ CANConnection* ConnectionWindow::create(CANCon::type pTye, QString pPortName, QS
//set up the debug console to operate if we've selected it. Doing so here allows debugging right away during set up
connect(conn_p, SIGNAL(debugOutput(QString)), this, SLOT(getDebugText(QString)));
}
/*TODO add return value and checks */

conn_p->start();

connect(this, SIGNAL(updateBusSpeed(int, int)), conn_p, SLOT(updateBusSpeed(int, int)));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

connect(this, &ConnectionWindow::updateBusSpeed, conn_p, &CANConnection::updateBusSpeed);

Gives errors during compile time instead of runtime.

// if portNum == 0 will skip
for (int i = 0; i < busNum && i < busSpeeds.count(); i++) {
CANBus bus;
bus.setSpeed(busSpeeds[i]);
emit updateBusSpeed(i, busSpeeds[i]);
}
disconnect(this, SIGNAL(updateBusSpeed(int, int)), conn_p, SLOT(updateBusSpeed(int, int)));
/*TODO add return value and checks */
}
return conn_p;
}
Expand All @@ -510,13 +529,37 @@ void ConnectionWindow::loadConnections()
QVector<QString> portNames = settings.value("connections/portNames").value<QVector<QString>>();
QVector<QString> driverNames = settings.value("connections/driverNames").value<QVector<QString>>();
QVector<int> devTypes = settings.value("connections/types").value<QVector<int>>();
QVector<int> busNums = settings.value("connections/busNums").value<QVector<int>>();
QVector<int> busSpeedsT = settings.value("connections/busSpeeds").value<QVector<int>>();
QVector<QVector<int>> busSpeeds;

// if values not present (e.g. when you update the program)
if (busNums.count() != driverNames.count()) {
busNums = QVector<int>(driverNames.count());
busSpeedsT = QVector<int>(0);
}

//don't load the connections if the three setting arrays above aren't all the same size.
if (portNames.count() != driverNames.count() || devTypes.count() != driverNames.count()) return;
if (portNames.count() != driverNames.count() || devTypes.count() != driverNames.count() || driverNames.count() != busNums.count()) return;

// connect tthe bus get/set
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: tthe -> the


qDebug() << "Loading connections";
// demangle bus speeds
for (int i = 0, j = 0; i < busNums.count(); i++) {
QVector<int> busSpeed;
for (int k = 0; k < busNums[i] && j+k < busSpeedsT.count(); k++) {
busSpeed.append(busSpeedsT[j+k]);
qDebug() << "Speed " << busSpeedsT[j+k];
}
busSpeeds.append(busSpeed);
qDebug() << "Load speed" << busNums[i];
j += busNums[i];
}

for(int i = 0 ; i < portNames.count() ; i++)
{
CANConnection* conn_p = create((CANCon::type)devTypes[i], portNames[i], driverNames[i]);
CANConnection* conn_p = create((CANCon::type)devTypes[i], portNames[i], driverNames[i], busNums[i], busSpeeds[i]);
/* add connection to model */
connModel->add(conn_p);
}
Expand All @@ -534,18 +577,29 @@ void ConnectionWindow::saveConnections()
QVector<QString> portNames;
QVector<int> devTypes;
QVector<QString> driverNames;
QVector<int> busNums;
QVector<int> busSpeeds;

/* save connections */
foreach(CANConnection* conn_p, conns)
{
portNames.append(conn_p->getPort());
devTypes.append(conn_p->getType());
driverNames.append(conn_p->getDriver());
busNums.append(conn_p->getNumBuses());
for (int i = 0; i < conn_p->getNumBuses(); i++) {
CANBus bus;
conn_p->getBusSettings(i, bus);
busSpeeds.append(bus.getSpeed());
qDebug() << "Bus speed " << bus.getSpeed();
}
}

settings.setValue("connections/portNames", QVariant::fromValue(portNames));
settings.setValue("connections/types", QVariant::fromValue(devTypes));
settings.setValue("connections/driverNames", QVariant::fromValue(driverNames));
settings.setValue("connections/busNums", QVariant::fromValue(busNums));
settings.setValue("connections/busSpeeds", QVariant::fromValue(busSpeeds));
}

void ConnectionWindow::moveConnUp()
Expand Down
3 changes: 2 additions & 1 deletion connections/connectionwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class ConnectionWindow : public QDialog
void updateBusSettings(CANBus *bus);
void updatePortName(QString port);
void sendDebugData(QByteArray bytes);
void updateBusSpeed(int, int);

public slots:
void getDebugText(QString debugText);
Expand Down Expand Up @@ -65,7 +66,7 @@ private slots:
QVector<QString> remoteDeviceIPGVRET;
QVector<QString> remoteDeviceKayak;

CANConnection* create(CANCon::type pTye, QString pPortName, QString pDriver);
CANConnection* create(CANCon::type pTye, QString pPortName, QString pDriver, int portNum, QVector<int> portSpeeds);
void populateBusDetails(int offset);
void loadConnections();
void saveConnections();
Expand Down