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 1f60ad876..c47ea2a76 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 (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); + 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