Skip to content

Commit

Permalink
- Battery percentage works
Browse files Browse the repository at this point in the history
- Basically driveable
- Added turbo but steering broken
  • Loading branch information
martonmiklos committed Dec 5, 2021
1 parent 7e4ff31 commit 1e5de50
Show file tree
Hide file tree
Showing 19 changed files with 203 additions and 50 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "QMLVirtualJoystick"]
path = qml/QMLVirtualJoystick
url = [email protected]:martonmiklos/QMLVirtualJoystick.git
[submodule "Qt-AES"]
path = Qt-AES
url = [email protected]:bricke/Qt-AES.git
1 change: 1 addition & 0 deletions Qt-AES
Submodule Qt-AES added at 845e3b
2 changes: 1 addition & 1 deletion abstract_rc_car.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ AbstractRC_Car::AbstractRC_Car(QObject *parent) : QObject(parent)

}

qreal AbstractRC_Car::batteryVoltage()
qreal AbstractRC_Car::batteryPercentage()
{
return 0.0;
}
Expand Down
17 changes: 9 additions & 8 deletions abstract_rc_car.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef ABSTRACTRC_CAR_H
#define ABSTRACTRC_CAR_H
#pragma once

#include <QObject>
#include <QVariant>

class AbstractRC_Car : public QObject
{
Expand Down Expand Up @@ -39,23 +39,24 @@ class AbstractRC_Car : public QObject
* @return If battery voltage query supported it returns
* the battery voltage in Volts unit. If not supported it returns 0.
*/
virtual qreal batteryVoltage();
Q_INVOKABLE virtual qreal batteryPercentage();

virtual bool isFeatureSupported(Feature feature) const = 0;
virtual bool setFeature(Feature feature, const QVariant &value) = 0;
Q_INVOKABLE virtual bool isFeatureSupported(Feature feature) const = 0;
Q_INVOKABLE virtual bool setFeature(Feature feature, const QVariant &value) = 0;
Q_INVOKABLE virtual QVariant featureValue(Feature feature) const = 0;

virtual QString name() const = 0;
Q_INVOKABLE virtual QString name() const = 0;
virtual QString connectionStateString() const = 0;

virtual bool connectToDevice() = 0;
virtual void disconnectFromDevice() = 0;
signals:
void batteryVoltageUpdated();
void batteryPercentageUpdated();
void connectionStateChanged(ConnectionState oldState, ConnectionState newState);
void connectionStateStringChanged();

protected:
ConnectionState m_connectionState = Disconnected;
void setConnectionState(ConnectionState state);
};

#endif // ABSTRACTRC_CAR_H
8 changes: 8 additions & 0 deletions availabledevicesmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ void AvailableDevicesModel::currentDeviceConnectionStateChangedSlot(AbstractRC_C
}
}

void AvailableDevicesModel::currentDeviceConnectionStateStringChangedSlot()
{
emit currentDeviceConnectionStateStringChanged(m_currentDevice->connectionStateString());
}

void AvailableDevicesModel::setScanInProgress(bool scanInProgress)
{
if (m_scanInProgress != scanInProgress) {
Expand Down Expand Up @@ -183,6 +188,7 @@ void AvailableDevicesModel::connectToDevice(int deviceIndex)
if (deviceIndex < m_devices.count()) {
if (m_currentDevice) {
delete m_currentDevice;
m_currentDevice = nullptr;
}
switch (m_devices.at(deviceIndex).type) {
case Shell:
Expand All @@ -192,6 +198,8 @@ void AvailableDevicesModel::connectToDevice(int deviceIndex)
m_currentDevice = shell;
connect(m_currentDevice, &AbstractRC_Car::connectionStateChanged,
this, &AvailableDevicesModel::currentDeviceConnectionStateChangedSlot);
connect(m_currentDevice, &AbstractRC_Car::connectionStateStringChanged,
this, &AvailableDevicesModel::currentDeviceConnectionStateStringChangedSlot);
break;
}
}
Expand Down
2 changes: 2 additions & 0 deletions availabledevicesmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ private slots:
void deviceScanFinished();
void deviceScanError(QBluetoothDeviceDiscoveryAgent::Error error);
void currentDeviceConnectionStateChangedSlot(AbstractRC_Car::ConnectionState oldState, AbstractRC_Car::ConnectionState newState);
void currentDeviceConnectionStateStringChangedSlot();

Q_SIGNALS:
void scanStateChanged();
void statusStringChanged();
void currentDeviceConnectionStateChanged(int oldState, int newState);
void currentDeviceConnectionStateStringChanged(const QString &stateString);

private:
void setScanInProgress(bool scanInProgress);
Expand Down
31 changes: 26 additions & 5 deletions ble_rc_car.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ bool BLE_RC_Car::connectToDevice()
}
m_controller->connectToDevice();
setConnectionState(Connecting);
setConnectionStateString(tr("Connecting to device"));
}
return true;
}
Expand All @@ -36,11 +37,6 @@ void BLE_RC_Car::disconnectFromDevice()
m_controller->disconnectFromDevice();
}

void BLE_RC_Car::deviceConnected()
{
m_controller->discoverServices();
}

void BLE_RC_Car::deviceDisconnected()
{

Expand All @@ -52,21 +48,46 @@ void BLE_RC_Car::deviceDisconnected()
}
}

void BLE_RC_Car::deviceConnected()
{
m_controller->discoverServices();
setConnectionStateString(tr("Discovering services"));
}



void BLE_RC_Car::serviceScanDone()
{
setConnectionState(Connected);
setConnectionStateString(tr("Discovering services done, connected"));
}

void BLE_RC_Car::errorReceived(QLowEnergyController::Error error)
{
qWarning() << error;
if (m_connectionState != Connected) {
setConnectionStateString(tr("Discovery error: %1").arg(error));
}
}

void BLE_RC_Car::setConnectionStateString(const QString &newConnectionStateString)
{
if (m_connectionStateString != newConnectionStateString) {
m_connectionStateString = newConnectionStateString;
emit connectionStateStringChanged();
}
}

void BLE_RC_Car::setDevInfo(const QBluetoothDeviceInfo &newDevInfo)
{
m_devInfo = newDevInfo;
}

QString BLE_RC_Car::connectionStateString() const
{
return m_connectionStateString;
}

QString BLE_RC_Car::errorString() const
{
return m_errorString;
Expand Down
5 changes: 4 additions & 1 deletion ble_rc_car.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class BLE_RC_Car : public AbstractRC_Car
QString errorString() const;

void setDevInfo(const QBluetoothDeviceInfo &newDevInfo);
QString connectionStateString() const override;

protected slots:
virtual void send() = 0;
Expand All @@ -32,7 +33,9 @@ protected slots:
protected:
QBluetoothDeviceInfo m_devInfo;
QLowEnergyController *m_controller = nullptr;
QString m_errorString;
QString m_errorString, m_connectionStateString;
void setConnectionStateString(const QString &newConnectionStateString);

bool m_reconnecting = false;
};

18 changes: 16 additions & 2 deletions qml/pages/ConnectingPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Page {
id: connectingPage

Text {
id: connecingText
text: qsTr("Connecting...")
font.pixelSize: Theme.fontSizeLarge
color: Theme.primaryColor
Expand All @@ -14,6 +15,15 @@ Page {
anchors.topMargin: Theme.paddingLarge
}

Text {
id: connectionStateText
font.pixelSize: Theme.fontSizeSmall
color: Theme.primaryColor
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: connecingText.bottom
anchors.topMargin: Theme.paddingSmall
}

BusyIndicator {
id: busyIndicator
size: BusyIndicatorSize.Large
Expand All @@ -35,10 +45,14 @@ Page {
Connections {
target: AvailableDevicesModel
onCurrentDeviceConnectionStateChanged: {
if (newState === AbstractRC_Car.Disconnected)
if (newState == AbstractRC_Car.Disconnected)
pageStack.push(Qt.resolvedUrl("SearchPage.qml"));
else if (newState === AbstractRC_Car.Connected)
else if (newState == AbstractRC_Car.Connected)
pageStack.push(Qt.resolvedUrl("DrivePage.qml"));
}
onCurrentDeviceConnectionStateStringChanged: {
console.log(stateString)
connectionStateText.text = stateString
}
}
}
2 changes: 2 additions & 0 deletions qml/pages/DiscoveredRCItem.qml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ ListItem {

Row {
id: row
spacing: Theme.paddingSmall
anchors.leftMargin: Theme.paddingMedium
Column {
Text {
id: nameText
Expand Down
83 changes: 64 additions & 19 deletions qml/pages/DrivePage.qml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import QtQuick 2.0
import QtQuick 2.6
import Sailfish.Silica 1.0
import "../QMLVirtualJoystick"
import hu.mm.sailfish_ble_rc 1.0
Expand All @@ -15,7 +15,7 @@ Page {
VirtualJoystick {
id: throttle
verticalOnly: true
height: page.height / 3
height: page.height / 2
anchors.left: parent.left
anchors.leftMargin: Theme.paddingLarge + (parent.width / 10.0)
anchors.verticalCenter: parent.verticalCenter
Expand All @@ -24,31 +24,76 @@ Page {
}
}


Column {
anchors.horizontalCenter: parent.horizontalCenter
spacing: Theme.paddingMedium
topPadding: Theme.paddingLarge

Text {
id: batteryLevel
text: "?"
font.pixelSize: Theme.fontSizeMedium
color: Theme.primaryColor
anchors.horizontalCenter: parent.horizontalCenter
Connections {
target: AvailableDevicesModel.currentDevice()
onBatteryPercentageUpdated: {
batteryLevel.text = (AvailableDevicesModel.currentDevice().batteryPercentage().toString() + "%")
}
}
}

Button {
id: backButton
anchors.horizontalCenter: parent.horizontalCenter
width: height
Image {
anchors.fill: parent
source: "qrc:/res/go-previous.svg"
fillMode: Image.Stretch
}

onClicked: {
AvailableDevicesModel.disconnectFromCurrentDevice()
pageStack.push(Qt.resolvedUrl("SearchPage.qml"));
}
}

Button {
id: turboButton
width: backButton.width
anchors.horizontalCenter: parent.horizontalCenter
visible: AvailableDevicesModel.currentDevice().isFeatureSupported(AbstractRC_Car.TurboMode)
onClicked: {
AvailableDevicesModel.currentDevice().setFeature(
AbstractRC_Car.TurboMode,
!AvailableDevicesModel.currentDevice().featureValue(AbstractRC_Car.TurboMode))
turboImage.source = AvailableDevicesModel.currentDevice().featureValue(AbstractRC_Car.TurboMode)
? "qrc:/res/turbo_active.png"
: "qrc:/res/turbo.png"
}

Image {
id: turboImage
anchors.fill: parent
source: AvailableDevicesModel.currentDevice().featureValue(AbstractRC_Car.TurboMode)
? "qrc:/res/turbo_active.png"
: "qrc:/res/turbo.png"
fillMode: Image.Stretch
}
}
}

VirtualJoystick {
id: steer
horizontalOnly: true
height: page.height / 3
height: page.height / 2
anchors.right: parent.right
anchors.rightMargin: Theme.paddingLarge + (parent.width / 10.0)
anchors.verticalCenter: parent.verticalCenter
onJoystick_moved: {
AvailableDevicesModel.currentDevice().setSteer(x)
}
}

Button {
anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter
anchors.centerIn: parent
Image {
anchors.fill: parent
source: ":/res/go-previous.svg"
fillMode: Image.Tile
}

onClicked: {
AvailableDevicesModel.disconnectFromCurrentDevice()
pageStack.push(Qt.resolvedUrl("SearchPage.qml"));
}
}
}
6 changes: 4 additions & 2 deletions qml/pages/SearchPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Page {
// The effective value will be restricted by ApplicationWindow.allowedOrientations
allowedOrientations: Orientation.All

Component.onCompleted: {
AvailableDevicesModel.disconnectFromCurrentDevice()
}

Component {
id: detectedDeviceDelegate
DiscoveredRCItem {
Expand All @@ -17,8 +21,6 @@ Page {
image: ImagePath
index: Index
}


}

SilicaListView {
Expand Down
Binary file added res/turbo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/turbo_active.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions resources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
<qresource prefix="/">
<file>res/deviceIcons/shell.png</file>
<file>res/go-previous.svg</file>
<file>res/turbo_active.png</file>
<file>res/turbo.png</file>
</qresource>
</RCC>
2 changes: 2 additions & 0 deletions sailfish-ble-rc.pro
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ CONFIG += sailfishapp
QT += bluetooth

SOURCES += src/sailfish-ble-rc.cpp \
Qt-AES/qaesencryption.cpp \
abstract_rc_car.cpp \
availabledevicesmodel.cpp \
ble_devicescanner.cpp \
Expand Down Expand Up @@ -54,6 +55,7 @@ CONFIG += qml_debug
#TRANSLATIONS += translations/sailfish-ble-rc-de.ts

HEADERS += \
Qt-AES/qaesencryption.h \
abstract_rc_car.h \
availabledevicesmodel.h \
ble_devicescanner.h \
Expand Down
Loading

0 comments on commit 1e5de50

Please sign in to comment.