From 9a7aec243a12972fd15976672ac4cfee6da60747 Mon Sep 17 00:00:00 2001 From: nullstalgia Date: Tue, 8 Aug 2023 21:42:05 -0700 Subject: [PATCH 1/2] Allow server to set blink offset with new packet type --- src/LEDManager.cpp | 24 +++++++++++++++++++++++- src/LEDManager.h | 7 +++++++ src/network/connection.cpp | 16 +++++++++++++++- src/network/packets.h | 1 + 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/LEDManager.cpp b/src/LEDManager.cpp index f184d55b3..18948588f 100644 --- a/src/LEDManager.cpp +++ b/src/LEDManager.cpp @@ -72,8 +72,18 @@ namespace SlimeVR unsigned long time = millis(); unsigned long diff = time - m_LastUpdate; + // Consider the offset if it is set + if (m_Offset > 0 && m_CurrentStage == OFF) + { + if (diff < m_Offset) { + // Wait until the offset has passed + return; + } + + m_Offset = 0; + } // Don't tick the LEDManager *too* often - if (diff < 10) + else if (diff < 10) { return; } @@ -209,4 +219,16 @@ namespace SlimeVR m_Timer += diff; } } + + void LEDManager::resetPatternWithOffset(unsigned long offset) + { + // Turn off the LED + off(); + // Reset the pattern state + m_CurrentStage = OFF; + // Reset the timer + m_Timer = 0; + // Set the offset + m_Offset = offset; + } } diff --git a/src/LEDManager.h b/src/LEDManager.h index 696caa746..e75688bc5 100644 --- a/src/LEDManager.h +++ b/src/LEDManager.h @@ -86,6 +86,12 @@ namespace SlimeVR */ void pattern(unsigned long timeon, unsigned long timeoff, int times); + /*! + * @brief Resets the timer used for the blinking pattern and adds an offset to when the pattern starts + * @param offset Offset in milliseconds + */ + void resetPatternWithOffset(unsigned long offset); + void update(); private: @@ -93,6 +99,7 @@ namespace SlimeVR unsigned long m_Timer = 0; LEDStage m_CurrentStage = OFF; unsigned long m_LastUpdate = millis(); + unsigned long m_Offset = 0; uint8_t m_Pin; diff --git a/src/network/connection.cpp b/src/network/connection.cpp index 907427365..a313407f2 100644 --- a/src/network/connection.cpp +++ b/src/network/connection.cpp @@ -689,7 +689,7 @@ void Connection::update() { break; - case PACKET_FEATURE_FLAGS: + case PACKET_FEATURE_FLAGS: { // Packet type (4) + Packet number (8) + flags (len - 12) if (len < 13) { m_Logger.warn("Invalid feature flags packet: too short"); @@ -710,6 +710,20 @@ void Connection::update() { } break; + } + + case PACKET_RECEIVE_LED_OFFSET: { + // Packet type (4) + Packet number (8) + offset in ms (32) + if (len < 44) { + m_Logger.warn("Invalid LED offset packet: too short"); + break; + } + + //uint32_t offset = convert_chars(&m_Packet[12]); + //ledManager.resetPatternWithOffset(offset); + break; + } + } } diff --git a/src/network/packets.h b/src/network/packets.h index e962a6af0..6e9820eed 100644 --- a/src/network/packets.h +++ b/src/network/packets.h @@ -56,6 +56,7 @@ #define PACKET_RECEIVE_VIBRATE 2 #define PACKET_RECEIVE_HANDSHAKE 3 #define PACKET_RECEIVE_COMMAND 4 +#define PACKET_RECEIVE_LED_OFFSET 23 #define PACKET_INSPECTION_PACKETTYPE_RAW_IMU_DATA 1 #define PACKET_INSPECTION_PACKETTYPE_FUSED_IMU_DATA 2 From fcbfe3b57b8a8d107551fa194c33d62d23ed42a4 Mon Sep 17 00:00:00 2001 From: nullstalgia Date: Wed, 9 Aug 2023 19:34:21 -0700 Subject: [PATCH 2/2] Correct issues stopping ESP from hearing packet --- src/network/connection.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/network/connection.cpp b/src/network/connection.cpp index a313407f2..d61d2bcf1 100644 --- a/src/network/connection.cpp +++ b/src/network/connection.cpp @@ -713,14 +713,14 @@ void Connection::update() { } case PACKET_RECEIVE_LED_OFFSET: { - // Packet type (4) + Packet number (8) + offset in ms (32) - if (len < 44) { + // Packet type (4) + Packet number (8) + offset in ms (4) + if (len < 16) { m_Logger.warn("Invalid LED offset packet: too short"); break; } - //uint32_t offset = convert_chars(&m_Packet[12]); - //ledManager.resetPatternWithOffset(offset); + uint32_t offset = convert_chars(&m_Packet[12]); + ledManager.resetPatternWithOffset(offset); break; }