diff --git a/platformio.ini b/platformio.ini index 344fe98..8c1375c 100644 --- a/platformio.ini +++ b/platformio.ini @@ -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 diff --git a/src/main.cpp b/src/main.cpp index 94140fa..37d21f7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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(packet->header)) { + auto header = std::get(packet->header); + source = header.source; + } else if (std::holds_alternative(packet->header)) { + auto header = std::get(packet->header); + source = header.source; + } else if (std::holds_alternative(packet->header)) { + auto header = std::get(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(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(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 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 fields;