Initial commit - sync AliceRC to Gitea
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
idf_component_register(
|
||||
SRCS "sbus.c"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES driver
|
||||
)
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* @file sbus.c
|
||||
* @brief SBUS decoder implementation
|
||||
*/
|
||||
#include <string.h>
|
||||
#include "sbus.h"
|
||||
|
||||
void sbus_init(void)
|
||||
{
|
||||
/* Nothing to initialize */
|
||||
}
|
||||
|
||||
void sbus_decode(const sbus_frame_t *frame, uint16_t *channels, uint8_t *flags)
|
||||
{
|
||||
if (frame->start_byte != 0x0F) {
|
||||
/* Invalid frame */
|
||||
if (flags) *flags = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
/* Unpack 16 × 11-bit channels from 22 bytes, little-endian */
|
||||
const uint8_t *data = frame->channel_data;
|
||||
for (int i = 0; i < 16; i++) {
|
||||
int byte_idx = (i * 11) / 8;
|
||||
int bit_off = (i * 11) % 8;
|
||||
|
||||
uint16_t ch = data[byte_idx] >> bit_off;
|
||||
ch |= (uint16_t)(data[byte_idx + 1]) << (8 - bit_off);
|
||||
if (bit_off > 5) {
|
||||
ch |= (uint16_t)(data[byte_idx + 2]) << (16 - bit_off);
|
||||
}
|
||||
|
||||
channels[i] = ch & 0x07FF;
|
||||
}
|
||||
|
||||
/* Extract flags */
|
||||
if (flags) {
|
||||
*flags = frame->flags;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* @file sbus.h
|
||||
* @brief SBUS decoder for receiver telemetry input
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* SBUS frame structure (25 bytes) */
|
||||
typedef struct __attribute__((packed)) {
|
||||
uint8_t start_byte; /* 0x0F */
|
||||
uint8_t channel_data[22]; /* 16 channels × 11 bit = 176 bit */
|
||||
uint8_t flags; /* CH17, CH18, failsafe, lost frame */
|
||||
uint8_t end_byte; /* 0x00 */
|
||||
} sbus_frame_t;
|
||||
|
||||
/* SBUS flags */
|
||||
#define SBUS_FLAG_CH17 (1 << 0)
|
||||
#define SBUS_FLAG_CH18 (1 << 1)
|
||||
#define SBUS_FLAG_LOST_FRAME (1 << 2)
|
||||
#define SBUS_FLAG_FAILSAFE (1 << 3)
|
||||
|
||||
/* Initialize SBUS decoder */
|
||||
void sbus_init(void);
|
||||
|
||||
/* Decode SBUS frame into channel values */
|
||||
void sbus_decode(const sbus_frame_t *frame, uint16_t *channels, uint8_t *flags);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user