-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
109 lines (91 loc) · 4.27 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* Copyright (c) 2016 Erik Botö ([email protected])
*
* 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 2 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., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <QCoreApplication>
#include "LibUsb.h"
#include "ant.h"
#include "MonarkConnection.h"
#include "bledatabroadcaster.h"
#include "ftmsdevice.h"
#include <QDebug>
#include <QNetworkInterface>
#include <QDBusConnection>
#include "dbusadaptor.h"
#include "gearsimulator.h"
#include "mqttconnection.h"
#include <QFile>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// Use MAC-address of network interface to select a device ID for ANT+
QString hwaddr;
for (QNetworkInterface iface: QNetworkInterface::allInterfaces())
{
if (!(iface.flags() & QNetworkInterface::IsLoopBack))
{
hwaddr = iface.hardwareAddress();
break;
}
}
hwaddr = hwaddr.remove(":");
bool ok = false;
unsigned short devId = hwaddr.right(4).toInt(&ok, 16);
if (!ok)
devId = 1137;
qDebug() << "Using ANT+ device ID: " << devId;
MonarkConnection *monark = new MonarkConnection();
ANT * ant = new ANT(devId);
GearSimulator *gearSimu = new GearSimulator(monark);
DBusAdaptor *dbusAdaptor = new DBusAdaptor(monark, gearSimu);
MqttConnection *mqttConnection = new MqttConnection(QString("Monark %1").arg(devId),
monark,
dbusAdaptor);
Q_UNUSED(dbusAdaptor)
QDBusConnection::sessionBus().registerObject("/Monark", monark);
QDBusConnection::sessionBus().registerService("se.unixshell");
//BLEDataBroadcaster *btpower = new BLEDataBroadcaster(devId);
FTMSDevice *ftmsDevice = new FTMSDevice(devId);
QObject::connect(monark, &MonarkConnection::typeIdentified, ftmsDevice, [ftmsDevice, monark](){
ftmsDevice->setControllable(monark->canDoLoad());
ftmsDevice->initialize();
qDebug() << "canDoLoad: " << monark->canDoLoad() << " initializing";
}, Qt::QueuedConnection);
QObject::connect(monark, &MonarkConnection::power, ant, &ANT::setCurrentPower);
QObject::connect(monark, &MonarkConnection::cadence, ant, &ANT::setCurrentCadence);
QObject::connect(ant, &ANT::newTargetPower, monark, &MonarkConnection::setLoad);
QObject::connect(ant, &ANT::gradeChanged, gearSimu, &GearSimulator::onGradeChanged);
QObject::connect(ant, &ANT::fecModeChanged, monark, &MonarkConnection::setFecMode);
QObject::connect(monark, &MonarkConnection::power, ftmsDevice, &FTMSDevice::setCurrentPower);
QObject::connect(monark, &MonarkConnection::cadence, ftmsDevice, &FTMSDevice::setCurrentCadence);
QObject::connect(ftmsDevice, &FTMSDevice::newTargetKp, monark, &MonarkConnection::setKp);
QObject::connect(ftmsDevice, &FTMSDevice::newTargetPower, monark, &MonarkConnection::setLoad);
QObject::connect(ftmsDevice, &FTMSDevice::newGrade, gearSimu, &GearSimulator::onGradeChanged);
QObject::connect(ftmsDevice, &FTMSDevice::simulationModeChanged, monark, &MonarkConnection::setFecMode);
QObject::connect(monark, &MonarkConnection::targetKpChanged, mqttConnection, &MqttConnection::onTargetKpChanged);
QObject::connect(monark, &MonarkConnection::targetPowerChanged, mqttConnection, &MqttConnection::onTargetPowerChanged);
// Control the status led on Raspberry Pi
QObject::connect(monark, &MonarkConnection::connectionStatus, [](bool connected){
QFile led("/sys/class/leds/led0/brightness");
if ( led.open(QIODevice::WriteOnly) )
{
led.write(connected ? "1" : "0");
}
});
monark->start();
ant->start();
return a.exec();
}