Move packet building and sending logic out of main.cpp

This commit is contained in:
2025-05-24 14:36:32 +10:00
parent b50edfa4aa
commit 32e0462a69
5 changed files with 160 additions and 107 deletions

View File

@@ -0,0 +1,84 @@
#include "PacketBuilder.h"
PacketBuilder::PacketBuilder()
{
}
Packet PacketBuilder::buildOpenPacket(uint16_t destination)
{
Packet packet;
auto header = UnicastHeader{};
header.destination = destination;
header.source = 0x0000;
packet.header = header;
packet.type = PacketType::OPEN;
return packet;
}
Packet PacketBuilder::buildClosePacket(uint16_t destination)
{
Packet packet;
auto header = UnicastHeader{};
header.destination = destination;
header.source = 0x0000;
packet.header = header;
packet.type = PacketType::CLOSE;
return packet;
}
Packet PacketBuilder::buildStopPacket(uint16_t destination)
{
Packet packet;
auto header = UnicastHeader{};
header.destination = destination;
header.source = 0x0000;
packet.header = header;
packet.type = PacketType::STOP;
return packet;
}
Packet PacketBuilder::buildSetPositionPacket(uint16_t destination, float percentage)
{
Packet packet;
auto header = UnicastHeader{};
header.destination = destination;
header.source = 0x0000;
packet.header = header;
packet.type = PacketType::FIELD_COMMAND;
std::vector<Field> fields;
uint8_t identifier = 0x50; // position
FieldType type = FieldType::SET;
uint16_t position = (uint16_t)(0xFFFF * percentage);
fields.push_back(Field{identifier, type, true, position});
packet.parameters = FieldsParameters{fields};
return packet;
}
Packet PacketBuilder::buildFetchPositionPacket(uint16_t destination)
{
Packet packet;
auto header = UnicastHeader{};
header.destination = destination;
header.source = 0x0000;
packet.header = header;
packet.type = PacketType::FIELD_COMMAND;
std::vector<Field> fields;
// position
fields.push_back(Field{0x50, FieldType::FETCH, false, std::monostate{}});
// battery
fields.push_back(Field{0x42, FieldType::FETCH, false, std::monostate{}});
packet.parameters = FieldsParameters{fields};
return packet;
}