Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new packet recieve type: LED Blink Offset #274

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (4)
if (len < 16) {
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
Loading