Update RFPowerView to v0.0.3

This commit is contained in:
2024-04-04 21:22:07 +11:00
parent 0a9e3c640c
commit eee6cbef46
2 changed files with 42 additions and 15 deletions

View File

@@ -28,4 +28,4 @@ lib_deps =
ArduinoJson=https://github.com/bblanchon/ArduinoJson#v7.0.0
RFPowerView=https://git.mattway.com.au/matt/RFPowerView.git#v0.0.2
RFPowerView=https://git.mattway.com.au/matt/RFPowerView.git#v0.0.3

View File

@@ -83,7 +83,7 @@ void setup() {
shades.push_back(Shade{0x3EB8, "living_room_door_blind", "Living Room Door Blind", -1, -1, 0, 0, nullptr});
shades.push_back(Shade{0x5463, "living_room_window_blind", "Living Room Window Blind", -1, -1, 0, 0, nullptr});
powerView.setPacketCallback(processPacket);
powerView.setPacketReceivedCallback(processPacket);
if (!powerView.begin()) {
Serial.println("Failed to start RFPowerView");
return;
@@ -107,8 +107,25 @@ void loop() {
void processPacket(const Packet *packet) {
Serial.println("Got a packet");
uint16_t source = -1;
if (std::holds_alternative<BroadcastHeader>(packet->header)) {
auto header = std::get<BroadcastHeader>(packet->header);
source = header.source;
} else if (std::holds_alternative<UnicastHeader>(packet->header)) {
auto header = std::get<UnicastHeader>(packet->header);
source = header.source;
} else if (std::holds_alternative<GroupsHeader>(packet->header)) {
auto header = std::get<GroupsHeader>(packet->header);
source = header.source;
} else {
Serial.print("Cannot process packet, unknown header");
return;
}
// Update last rolling codes each time a packet from a real hub is detected
if (packet->source == 0x0000) {
if (source == 0x0000) {
Serial.println("Updating rolling codes");
lastRollingCode1 = packet->rollingCode1;
lastRollingCode2 = packet->rollingCode2;
}
@@ -119,7 +136,7 @@ void processPacket(const Packet *packet) {
Field field = parameters.fields[i];
if (field.identifier == 0x50) {
for (size_t i = 0; i < shades.size(); i++) {
if (packet->source == shades[i].ID) {
if (source == shades[i].ID) {
uint16_t value = std::get<uint16_t>(field.value);
uint8_t position = (uint8_t)std::round(((float)value / 0xFFFF) * 100);
@@ -135,7 +152,7 @@ void processPacket(const Packet *packet) {
}
} else if (field.identifier == 0x42) {
for (size_t i = 0; i < shades.size(); i++) {
if (packet->source == shades[i].ID) {
if (source == shades[i].ID) {
uint8_t value = std::get<uint8_t>(field.value);
uint8_t battery = uint8_t(((float)value / 200) * 100);
@@ -168,8 +185,10 @@ void onConnectionEstablished() {
bool sendOpenPacket(uint16_t destination) {
Packet packet;
packet.destination = destination;
packet.source = 0x0000;
auto header = UnicastHeader {};
header.destination = destination;
header.source = 0x0000;
packet.header = header;
packet.type = PacketType::OPEN;
return sendPacket(&packet);
@@ -177,8 +196,10 @@ bool sendOpenPacket(uint16_t destination) {
bool sendClosePacket(uint16_t destination) {
Packet packet;
packet.destination = destination;
packet.source = 0x0000;
auto header = UnicastHeader {};
header.destination = destination;
header.source = 0x0000;
packet.header = header;
packet.type = PacketType::CLOSE;
return sendPacket(&packet);
@@ -186,8 +207,10 @@ bool sendClosePacket(uint16_t destination) {
bool sendStopPacket(uint16_t destination) {
Packet packet;
packet.destination = destination;
packet.source = 0x0000;
auto header = UnicastHeader {};
header.destination = destination;
header.source = 0x0000;
packet.header = header;
packet.type = PacketType::STOP;
return sendPacket(&packet);
@@ -195,8 +218,10 @@ bool sendStopPacket(uint16_t destination) {
bool sendSetPosition(uint16_t destination, float percentage) {
Packet packet;
packet.destination = destination;
packet.source = 0x0000;
auto header = UnicastHeader {};
header.destination = destination;
header.source = 0x0000;
packet.header = header;
packet.type = PacketType::FIELD_COMMAND;
std::vector<Field> fields;
@@ -213,8 +238,10 @@ bool sendSetPosition(uint16_t destination, float percentage) {
bool sendFetchPosition(uint16_t destination) {
Packet packet;
packet.destination = destination;
packet.source = 0x0000;
auto header = UnicastHeader {};
header.destination = destination;
header.source = 0x0000;
packet.header = header;
packet.type = PacketType::FIELD_COMMAND;
std::vector<Field> fields;