Skip to content

Commit

Permalink
feat: add id to mq message
Browse files Browse the repository at this point in the history
  • Loading branch information
Rui Rocha authored and rui-rocha-42 committed Feb 6, 2025
1 parent de98e37 commit 6169ce5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
5 changes: 3 additions & 2 deletions com-middleware/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ auto main(int argc, char **argv) -> int {

std::cout << "\n";

std::vector<uint8_t> message(frame.len);
memcpy(message.data(), frame.data, static_cast<size_t>(num_bytes));
std::vector<uint8_t> message(frame.len + 1);
message[0] = static_cast<uint8_t>(frame.can_id);
memcpy(message.data() + 1, frame.data, static_cast<size_t>(num_bytes));

try {
publisher.publish(message);
Expand Down
34 changes: 26 additions & 8 deletions instrument-cluster/mq/ZMQSubscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,34 @@ void ZmqSubscriber::checkForMessages() {
}

void ZmqSubscriber::decodeMessage(const std::vector<uint8_t> &message) {
if (message.size() < 4) {
if (message.empty()) {
std::cerr << "Error: Invalid message - size is too small.\n";
return;
}

uint8_t const speed_value = message[0];
uint8_t const rpm_low_byte = message[2];
uint8_t const rpm_high_byte = message[3];

uint16_t const rpm = (rpm_high_byte << 8) | rpm_low_byte;

emit messageReceived(speed_value, rpm);
switch (message[0]) {
case 1: {
qDebug() << "Speed";
uint8_t const speed_value = message[1];
uint8_t const rpm_low_byte = message[3];
uint8_t const rpm_high_byte = message[4];
uint16_t const rpm = (rpm_high_byte << 8) | rpm_low_byte;
emit messageReceived(speed_value, rpm);
break;
}
case 2: {
qDebug() << "Battery";
uint8_t const battery = message[1];
emit batteryMessageReceived(battery);
break;
}
case 3: {
qDebug() << "Temperature";
uint8_t const temperature = message[1];
emit temperatureMessageReceived(temperature);
break;
}
default:
qDebug() << "No message definition";
}
}
2 changes: 2 additions & 0 deletions instrument-cluster/mq/ZMQSubscriber.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class ZmqSubscriber : public QObject {

signals:
void messageReceived(uint8_t speed, uint16_t rpm);
void batteryMessageReceived(uint8_t battery);
void temperatureMessageReceived(uint8_t temperature);

public slots:
void checkForMessages();
Expand Down

0 comments on commit 6169ce5

Please sign in to comment.