Skip to content

Instantly share code, notes, and snippets.

@maietta
Created May 23, 2025 21:14
Show Gist options
  • Save maietta/213f3f034b809da32abd745e26ea93e0 to your computer and use it in GitHub Desktop.
Save maietta/213f3f034b809da32abd745e26ea93e0 to your computer and use it in GitHub Desktop.
# Stream Deck+ Button Detection Algorithm

Stream Deck+ Button Detection Algorithm

This document describes the algorithm used to detect button presses and releases on the Stream Deck+ device.

Data Format

The Stream Deck+ sends 14-byte packets for button events. The first byte (data[0]) determines the event type:

  • 1: Button or encoder event
  • 2: Touchscreen event
  • 3: Dial event

For button events (data[0] == 1), the data format is as follows:

[1 0 8 0 X Y Z W V U T S R Q]

Where:

  • X (data[4]): Button 0 state (1 = pressed, 0 = released)
  • Y through Q (data[5] through data[12]): Buttons 1-7 states (1 = pressed, 0 = released)

Button Detection Algorithm

  1. Button 0 Detection:

    • Press: Check if data[4] == 1
    • Release: Check if data[4] == 0 and button was previously pressed
  2. Buttons 1-7 Detection:

    • For each position i from 5 to 12:
      • Calculate button number: buttonNum = i - 4
      • Press: Check if data[i] == 1
      • Release: Check if data[i] == 0 and button was previously pressed
  3. State Tracking:

    • Maintain an array lastKeyStates[8] to track the previous state of each button
    • Update state only when a change is detected
    • Use state tracking to prevent duplicate press/release events

Example Data

Button 0 press:

[1 0 8 0 1 0 0 0 0 0 0 0 0 0]

Button 0 release:

[1 0 8 0 0 0 0 0 0 0 0 0 0 0]

Button 1 press:

[1 0 8 0 0 1 0 0 0 0 0 0 0 0]

Button 1 release:

[1 0 8 0 0 0 0 0 0 0 0 0 0 0]

Implementation Notes

  1. The algorithm uses a polling frequency of 20Hz (50ms intervals)
  2. Raw data is only printed when an actual button event is detected
  3. Button numbers are zero-based (0-7)
  4. Array bounds checking is implemented to prevent index out of range errors
  5. The algorithm handles both press and release events for all buttons

Edge Cases

  1. Multiple Button Presses: The algorithm can detect multiple buttons pressed simultaneously
  2. Rapid Presses: State tracking prevents duplicate events during rapid button presses
  3. Out of Range: Button numbers >= 8 are skipped to prevent array index errors
  4. Encoder Events: The algorithm distinguishes between button and encoder events by checking data[1] and data[2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment