Skip to content

Commit

Permalink
Allow server to set blink offset with new packet type
Browse files Browse the repository at this point in the history
  • Loading branch information
nullstalgia committed Aug 9, 2023
1 parent 5fe2444 commit 9a7aec2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
24 changes: 23 additions & 1 deletion src/LEDManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
7 changes: 7 additions & 0 deletions src/LEDManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,20 @@ 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:
uint8_t m_CurrentCount = 0;
unsigned long m_Timer = 0;
LEDStage m_CurrentStage = OFF;
unsigned long m_LastUpdate = millis();
unsigned long m_Offset = 0;

uint8_t m_Pin;

Expand Down
16 changes: 15 additions & 1 deletion src/network/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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<uint32_t>(&m_Packet[12]);
//ledManager.resetPatternWithOffset(offset);
break;
}

}
}

Expand Down
1 change: 1 addition & 0 deletions src/network/packets.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9a7aec2

Please sign in to comment.