Skip to content
This repository was archived by the owner on Oct 6, 2025. It is now read-only.
Merged

Dev #47

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
26 changes: 26 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Remote Debug on Raspberry Pi",
"type": "cppdbg",
"request": "launch",
"program": "/Users/ruipedropires/SEA:ME/QTonRaspberryPi/HelloQt6", // Be sure that this is the path where binary is exist on host
"stopAtEntry": false,
"cwd": "/Users/ruipedropires/SEA:ME/QTonRaspberryPi", // should be host path not target
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb-multiarch", // gdb-multiacrh should be used not gdb
"miDebuggerServerAddress": "192.168.178.21:2000", // Replace with your Raspberry Pi's IP and the gdbserver port
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "", // Optional: Specify a task to run before debugging starts
"postDebugTask": "" // Optional: Specify a task to run after debugging ends
}
]
}
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"editor.defaultFormatter": "xaver.clang-format",
"editor.formatOnSave": true
},
"clang-format.executable": "C:\\Program Files\\LLVM\\bin\\clang-format.exe",
"clang-format.executable": "/opt/homebrew/bin/clang-format",
"C_Cpp.codeAnalysis.clangTidy.enabled": true,
"C_Cpp.codeAnalysis.clangTidy.path": "C:\\Program Files\\LLVM\\bin\\clang-tidy.exe"
}
41 changes: 22 additions & 19 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"label": "Build with Docker",
"type": "shell",
"command": "msbuild",
"args": [
// Ask msbuild to generate full paths for file names.
"/property:GenerateFullPaths=true",
"/t:build",
// Do not generate summary otherwise it leads to duplicate errors in Problems panel
"/consoleloggerparameters:NoSummary"
],
"group": "build",
"presentation": {
// Reveal the output only if unrecognized errors occur.
"reveal": "silent"
},
// Use the standard MS compiler pattern to detect errors, warnings and infos
"problemMatcher": "$msCompile"
}
"command": "./helperTasks.sh build_docker_image",
"group": "build"
},
{
"label": "Copy binary from tmp container",
"type": "shell",
"command": "./helperTasks.sh create_binary_and_copy",
"group": "build"
},
{
"label": "Send binary to Rasp",
"type": "shell",
"command": "./helperTasks.sh send_binary_to_rasp",
"group": "build"
},
{
"label": "Run gdbserver on Rasp",
"type": "shell",
"command": "./helperTasks.sh run_gdb_server_on_rasp",
"group": "build"
}
]
}
74 changes: 27 additions & 47 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,47 +1,27 @@
cmake_minimum_required(VERSION 3.16)

project(Speedometer VERSION 0.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt6 6.5 REQUIRED COMPONENTS Quick)

qt_standard_project_setup(REQUIRES 6.5)

qt_add_executable(appSpeedometer
./src/main.cpp
)

qt_add_qml_module(appSpeedometer
URI Speedometer
VERSION 1.0
QML_FILES
./src/Main.qml
)

# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
set_target_properties(appSpeedometer PROPERTIES
# MACOSX_BUNDLE_GUI_IDENTIFIER com.example.appSpeedometer
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
)

target_link_libraries(appSpeedometer
PRIVATE
Qt6::Quick
Qt6::Core
Qt6::Gui
Qt6::Qml
Qt6::Network
)

include(GNUInstallDirs)
install(TARGETS appSpeedometer
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
cmake_minimum_required(VERSION 3.5)

project(HelloQt6 LANGUAGES CXX)
message("CMAKE_SYSROOT " ${CMAKE_SYSROOT})
message("CMAKE_LIBRARY_ARCHITECTURE " ${CMAKE_LIBRARY_ARCHITECTURE})
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt6 COMPONENTS Core Quick DBus SerialBus REQUIRED)

set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wl,-rpath-link, ${CMAKE_SYSROOT}/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE} -L${CMAKE_SYSROOT}/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Wl,-rpath-link,${CMAKE_SYSROOT}/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE} -L${CMAKE_SYSROOT}/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}")
# Enable automatic MOC, UIC, and RCC processing
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

qt_add_resources(QT_RESOURCES ./src/resources.qrc)

add_executable(speedometer ${QT_RESOURCES}
./src/main.cpp
./src/CANBusHandler.cpp
./include/CANBusHandler.hpp
)

target_link_libraries(speedometer -lm -ldl Qt6::Core Qt6::DBus Qt6::Quick Qt6::SerialBus)

Empty file removed include/.keep
Empty file.
35 changes: 35 additions & 0 deletions include/CANBusHandler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef CANBUSHANDLER_HPP
#define CANBUSHANDLER_HPP

#include <QtCore/QObject>

class CANBusHandler : public QObject
{
Q_OBJECT
Q_PROPERTY(int speed READ getSpeed WRITE setSpeed NOTIFY speedChanged)
Q_PROPERTY(
int battery READ getBattery WRITE setBattery NOTIFY batteryChanged)

public:
explicit CANBusHandler(QObject* parent = nullptr);
~CANBusHandler();

int getSpeed() const;
void setSpeed(int speed);
int getBattery() const;
void setBattery(int battery);

signals:
void speedChanged(int speed);
void batteryChanged(int battery);

private slots:
void readFrames();

private:
int canSocket;
int m_speed;
int m_battery;
};

#endif // CANBUSHANDLER_HPP
112 changes: 112 additions & 0 deletions src/CANBusHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include <QtCore/QDebug>
#include <QtCore/QTimer>
#include <sys/socket.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <arpa/inet.h>
#include "../include/CANBusHandler.hpp"

CANBusHandler::CANBusHandler(QObject* parent)
: QObject(parent), canSocket(-1), m_speed(0), m_battery(0)
{
struct ifreq ifr;
struct sockaddr_can addr;

canSocket = socket(PF_CAN, SOCK_RAW, CAN_RAW);
if (canSocket < 0)
{
qWarning() << "Cannot create CAN socket!";
return;
}

strcpy(ifr.ifr_name, "can0");
ioctl(canSocket, SIOCGIFINDEX, &ifr);

addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
if (bind(canSocket, (struct sockaddr*)&addr, sizeof(addr)) < 0)
{
qWarning() << "Cannot bind CAN socket!";
close(canSocket);
return;
}

qDebug() << "CAN socket bound to can0 interface successfully.";

// Set up a timer to periodically read frames
QTimer* timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, [this]() { readFrames(); });
timer->start(10); // Adjust the interval as needed
}

CANBusHandler::~CANBusHandler()
{
if (canSocket >= 0)
{
close(canSocket); // Close the CAN socket
}
}

int CANBusHandler::getSpeed() const
{
return m_speed;
}

void CANBusHandler::setSpeed(int speed)
{
if (m_speed != speed)
{
m_speed = speed;
emit speedChanged(m_speed);
}
}

int CANBusHandler::getBattery() const
{
return m_battery;
}

void CANBusHandler::setBattery(int battery)
{
if (m_battery != battery)
{
m_battery = battery;
emit batteryChanged(m_battery);
}
}

void CANBusHandler::readFrames()
{
struct can_frame frame;
int nbytes = read(canSocket, &frame, sizeof(struct can_frame));

if (nbytes < 0)
{
qWarning() << "Error reading CAN frame!";
return;
}

if (frame.can_id == 0x01)
{
int speed;
memcpy(&speed, frame.data, sizeof(int));

speed = ntohl(speed);

// qDebug() << "Speed:" << speed; // Print the speed value
setSpeed(speed);
}
else if (frame.can_id == 0x02)
{
int battery;
memcpy(&battery, frame.data, sizeof(int));

battery = ntohl(battery);

// qDebug() << "Battery:" << battery; // Print the speed value
setBattery(battery);
}
}
Loading
Loading