Hotwheels-Cluster 1.2
Creation of Cluster APP for SEA:ME project.
 
Loading...
Searching...
No Matches
MCP2515Controller.cpp
Go to the documentation of this file.
1
24
25#include "MCP2515Controller.hpp"
26#include "SPIController.hpp"
27#include <QDebug>
28#include <QThread>
29#include <cstring>
30#include <stdexcept>
31
39MCP2515Controller::MCP2515Controller(const std::string &spiDevice)
42 if (!spiController->openDevice(spiDevice)) {
43 throw std::runtime_error("Failed to open SPI device : " + spiDevice);
44 }
46}
47
56MCP2515Controller::MCP2515Controller(const std::string &spiDevice,
60 if (!spiController.openDevice(spiDevice)) {
61 throw std::runtime_error("Failed to open SPI device : " + spiDevice);
62 }
64}
65
72 spiController->closeDevice();
73 if (this->ownsSPIController) {
74 delete this->spiController;
75 }
76}
77
86 if (!configurator.resetChip()) {
87 throw std::runtime_error("Failed to reset MCP2515");
88 }
89
90 configurator.configureBaudRate();
91 configurator.configureTXBuffer();
92 configurator.configureRXBuffer();
93 configurator.configureFiltersAndMasks();
94 configurator.configureInterrupts();
95 configurator.setMode(0x00);
96
97 if (!configurator.verifyMode(0x00)) {
98 throw std::runtime_error("Failed to set MCP2515 to normal mode");
99 }
100
101 return true;
102}
103
109 while (!stopReadingFlag) {
110 uint16_t frameID;
111 std::vector<uint8_t> data;
112 // qDebug() << "In loop" << stopReadingFlag;
113 try {
114 data = configurator.readCANMessage(frameID);
115 if (!data.empty()) {
116 messageProcessor.processMessage(frameID, data);
117 }
118 } catch (const std::exception &e) {
119 qDebug() << "Error while processing CAN message:" << e.what();
120 }
121
122 if (stopReadingFlag) {
123 break;
124 }
125
126 QThread::msleep(10);
127 }
128}
129
135
144 messageProcessor.registerHandler(
145 0x100, [this](const std::vector<uint8_t> &data) {
146 if (data.size() == sizeof(float)) {
147 float speed;
148 memcpy(&speed, data.data(), sizeof(float));
149 emit speedUpdated(speed / 10.0F);
150 }
151 });
152
153 messageProcessor.registerHandler(0x200,
154 [this](const std::vector<uint8_t> &data) {
155 if (data.size() == 2) {
156 uint16_t rpm = (data[0] << 8) | data[1];
157 emit rpmUpdated(rpm);
158 }
159 });
160}
161
168 return this->stopReadingFlag;
169}
Definition of the MCP2515Controller class.
Definition of the SPIController class.
void rpmUpdated(int newRpm)
Signal emitted when the RPM is updated.
void speedUpdated(float newSpeed)
Signal emitted when the speed is updated.
Interface for the SPI controller.
bool isStopReadingFlagSet() const override
Check if the stop reading flag is set.
void processReading() override
Start reading CAN messages.
~MCP2515Controller() override
Destroy the MCP2515Controller::MCP2515Controller object.
bool stopReadingFlag
Flag to indicate if the reading process should stop.
bool init() override
Initialize the MCP2515 controller.
bool ownsSPIController
Flag to indicate if the SPI controller is owned by the MCP2515Controller.
void stopReading() override
Stop reading CAN messages.
MCP2515Configurator configurator
MCP2515Configurator object.
CANMessageProcessor messageProcessor
CANMessageProcessor object.
void setupHandlers()
Send a CAN message.
MCP2515Controller(const std::string &spiDevice)
Construct a new MCP2515Controller::MCP2515Controller object.
ISPIController * spiController
Pointer to the ISPIController object.
Class that controls the SPI communication. ISPIController.