fix: update 0x02 protocol to use byte offset instead of LED position offset

This commit is contained in:
2025-07-09 14:26:25 +08:00
parent 5f5824721e
commit 142332730f
3 changed files with 40 additions and 17 deletions

View File

@ -64,8 +64,8 @@ Byte 0: Header (0x01)
```text ```text
Byte 0: Header (0x02) Byte 0: Header (0x02)
Byte 1: Offset High (upper 8 bits of LED start position) Byte 1: Offset High (upper 8 bits of data byte offset)
Byte 2: Offset Low (lower 8 bits of LED start position) Byte 2: Offset Low (lower 8 bits of data byte offset)
Byte 3+: LED Color Data (variable length) Byte 3+: LED Color Data (variable length)
``` ```
@ -85,6 +85,26 @@ Byte 3+: LED Color Data (variable length)
All values are 0-255. All values are 0-255.
### Offset Calculation
The offset field specifies the starting byte position in the LED data buffer:
- **16-bit value**: Combines Offset High and Offset Low bytes (big-endian)
- **Range**: 0-65535 bytes supported
- **Purpose**: Allows partial updates of LED strip data at any byte position
**Example Calculations:**
- Byte position 0: `Offset High = 0x00, Offset Low = 0x00`
- Byte position 30: `Offset High = 0x00, Offset Low = 0x1E` (10 RGB LEDs × 3 bytes)
- Byte position 256: `Offset High = 0x01, Offset Low = 0x00`
- Byte position 1000: `Offset High = 0x03, Offset Low = 0xE8`
**LED Position to Byte Offset Conversion:**
- **RGB LEDs**: `byte_offset = led_position × 3`
- **RGBW LEDs**: `byte_offset = led_position × 4`
## LED Chip Specifications ## LED Chip Specifications
### WS2812B (RGB) ### WS2812B (RGB)
@ -206,24 +226,24 @@ Unknown → Connecting(1) → Connected
### RGB Example ### RGB Example
3 RGB LEDs starting at position 0: Red, Green, Blue 3 RGB LEDs starting at byte offset 0: Red, Green, Blue
```text ```text
02 00 00 FF 00 00 00 FF 00 00 00 FF 02 00 00 FF 00 00 00 FF 00 00 00 FF
│ │ │ └─────────────────────────── 9 bytes color data │ │ │ └─────────────────────────── 9 bytes color data
│ │ └─ Offset Low (0) │ │ └─ Offset Low (0 bytes)
│ └─ Offset High (0) │ └─ Offset High (0)
└─ Header (0x02) └─ Header (0x02)
``` ```
### RGBW Example ### RGBW Example
2 RGBW LEDs starting at position 10: White, Warm White 2 RGBW LEDs starting at byte offset 40 (LED position 10): White, Warm White
```text ```text
02 00 0A FF FF FF FF FF C8 96 C8 02 00 28 FF FF FF FF FF C8 96 C8
│ │ │ └─────────────────────── 8 bytes color data │ │ │ └─────────────────────── 8 bytes color data
│ │ └─ Offset Low (10) │ │ └─ Offset Low (40 bytes = 0x28)
│ └─ Offset High (0) │ └─ Offset High (0)
└─ Header (0x02) └─ Header (0x02)
``` ```
@ -284,13 +304,14 @@ void handle_ping(uint8_t* data, size_t len) {
void handle_led_data(uint8_t* data, size_t len) { void handle_led_data(uint8_t* data, size_t len) {
if (len < 3) return; if (len < 3) return;
uint16_t offset = (data[1] << 8) | data[2]; uint16_t byte_offset = (data[1] << 8) | data[2];
uint8_t* color_data = &data[3]; uint8_t* color_data = &data[3];
size_t color_len = len - 3; size_t color_len = len - 3;
// Direct forward to LED strip - supports WS2812B, SK6812, SK6812-RGBW // Direct forward to LED strip - supports WS2812B, SK6812, SK6812-RGBW
// byte_offset specifies the starting byte position in LED data buffer
// Color order conversion handled by LED driver library // Color order conversion handled by LED driver library
led_strip_update(offset, color_data, color_len); led_strip_update(byte_offset, color_data, color_len);
} }
``` ```
@ -379,7 +400,7 @@ void led_strip_update(uint16_t offset, uint8_t* data, size_t len) {
- Verify hardware processes 0x02 packets correctly - Verify hardware processes 0x02 packets correctly
- Check WS2812 wiring and power supply - Check WS2812 wiring and power supply
- Monitor packet reception on hardware side - Monitor packet reception on hardware side
- Verify offset calculations and LED strip configuration - Verify byte offset calculations and LED strip configuration
**Wrong Colors**: **Wrong Colors**:

View File

@ -430,10 +430,12 @@ impl LedColorsPublisher {
} }
} }
let offset = group.start.min(group.end); // Calculate byte offset based on LED position and LED type
let led_offset = group.start.min(group.end);
let byte_offset = led_offset * bytes_per_led;
let mut tx_buffer = vec![2]; let mut tx_buffer = vec![2];
tx_buffer.push((offset >> 8) as u8); tx_buffer.push((byte_offset >> 8) as u8);
tx_buffer.push((offset & 0xff) as u8); tx_buffer.push((byte_offset & 0xff) as u8);
tx_buffer.append(&mut buffer); tx_buffer.append(&mut buffer);
udp_rpc.send_to_all(&tx_buffer).await?; udp_rpc.send_to_all(&tx_buffer).await?;

View File

@ -182,8 +182,8 @@ async fn send_test_colors_to_board(board_address: String, offset: u16, buffer: V
})?; })?;
let mut packet = vec![0x02]; // Header let mut packet = vec![0x02]; // Header
packet.push((offset >> 8) as u8); // Offset high packet.push((offset >> 8) as u8); // Byte offset high
packet.push((offset & 0xff) as u8); // Offset low packet.push((offset & 0xff) as u8); // Byte offset low
packet.extend_from_slice(&buffer); // Color data packet.extend_from_slice(&buffer); // Color data
socket.send_to(&packet, &board_address).await.map_err(|e| { socket.send_to(&packet, &board_address).await.map_err(|e| {
@ -351,8 +351,8 @@ async fn send_test_colors_to_board_internal(board_address: &str, offset: u16, bu
let socket = UdpSocket::bind("0.0.0.0:0").await?; let socket = UdpSocket::bind("0.0.0.0:0").await?;
let mut packet = vec![0x02]; // Header let mut packet = vec![0x02]; // Header
packet.push((offset >> 8) as u8); // Offset high packet.push((offset >> 8) as u8); // Byte offset high
packet.push((offset & 0xff) as u8); // Offset low packet.push((offset & 0xff) as u8); // Byte offset low
packet.extend_from_slice(&buffer); // Color data packet.extend_from_slice(&buffer); // Color data
socket.send_to(&packet, board_address).await?; socket.send_to(&packet, board_address).await?;