Move packet building and sending logic out of main.cpp
This commit is contained in:
84
lib/packet_builder/PacketBuilder.cpp
Normal file
84
lib/packet_builder/PacketBuilder.cpp
Normal 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;
|
||||
}
|
||||
17
lib/packet_builder/PacketBuilder.h
Normal file
17
lib/packet_builder/PacketBuilder.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef PACKET_BUILDER_H
|
||||
#define PACKET_BUILDER_H
|
||||
|
||||
#include <PacketTypes.h>
|
||||
|
||||
class PacketBuilder {
|
||||
public:
|
||||
PacketBuilder();
|
||||
|
||||
Packet buildOpenPacket(uint16_t destination);
|
||||
Packet buildClosePacket(uint16_t destination);
|
||||
Packet buildStopPacket(uint16_t destination);
|
||||
Packet buildSetPositionPacket(uint16_t destination, float percentage);
|
||||
Packet buildFetchPositionPacket(uint16_t destination);
|
||||
};
|
||||
|
||||
#endif // PACKET_BUILDER_H
|
||||
27
lib/packet_sender/PacketSender.cpp
Normal file
27
lib/packet_sender/PacketSender.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "PacketSender.h"
|
||||
|
||||
PacketSender::PacketSender(RFPowerView& powerView) : powerView(powerView)
|
||||
{
|
||||
}
|
||||
|
||||
bool PacketSender::send(Packet packet)
|
||||
{
|
||||
Serial.println("Attempting to send a packet");
|
||||
packet.rollingCode1 = lastRollingCode1 + 1;
|
||||
packet.rollingCode2 = lastRollingCode2 + 1;
|
||||
|
||||
bool didSend = powerView.sendPacket(&packet);
|
||||
if (!didSend) {
|
||||
Serial.println("Failed to send");
|
||||
return false;
|
||||
} else {
|
||||
lastRollingCode1++;
|
||||
lastRollingCode2++;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void PacketSender::setLastRollingCodes(uint8_t code1, uint8_t code2) {
|
||||
lastRollingCode1 = code1;
|
||||
lastRollingCode2 = code2;
|
||||
}
|
||||
20
lib/packet_sender/PacketSender.h
Normal file
20
lib/packet_sender/PacketSender.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef PACKET_SENDER_H
|
||||
#define PACKET_SENDER_H
|
||||
|
||||
#include <RFPowerView.h>
|
||||
#include <PacketTypes.h>
|
||||
|
||||
class PacketSender {
|
||||
public:
|
||||
PacketSender(RFPowerView& powerView);
|
||||
|
||||
bool send(Packet packet);
|
||||
void setLastRollingCodes(uint8_t code1, uint8_t code2);
|
||||
private:
|
||||
RFPowerView& powerView;
|
||||
|
||||
uint8_t lastRollingCode1 = 0x3D;
|
||||
uint8_t lastRollingCode2 = 0x96;
|
||||
};
|
||||
|
||||
#endif // PACKET_SENDER_H
|
||||
Reference in New Issue
Block a user