Skip to content

Commit

Permalink
target: use more compact cbor definition
Browse files Browse the repository at this point in the history
  • Loading branch information
bkleiner committed Nov 5, 2023
1 parent 74d48b6 commit 915f504
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 84 deletions.
57 changes: 8 additions & 49 deletions src/core/target.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,95 +31,54 @@ target_info_t target_info = {
.gyro_id = 0x0,
};

#define START_STRUCT CBOR_START_STRUCT_ENCODER
#define END_STRUCT CBOR_END_STRUCT_ENCODER
#define MEMBER CBOR_ENCODE_MEMBER
#define STR_MEMBER CBOR_ENCODE_STR_MEMBER
#define TSTR_MEMBER CBOR_ENCODE_TSTR_MEMBER
#define ARRAY_MEMBER CBOR_ENCODE_ARRAY_MEMBER
#define INDEX_ARRAY_MEMBER CBOR_ENCODE_INDEX_ARRAY_MEMBER
#define STR_ARRAY_MEMBER CBOR_ENCODE_STR_ARRAY_MEMBER

CBOR_START_STRUCT_ENCODER(target_led_t)
TARGET_LED_MEMBERS
CBOR_END_STRUCT_ENCODER()

CBOR_START_STRUCT_ENCODER(target_invert_pin_t)
TARGET_BUZZER_MEMBERS
CBOR_END_STRUCT_ENCODER()

CBOR_START_STRUCT_ENCODER(target_serial_port_t)
TARGET_SERIAL_MEMBERS
CBOR_END_STRUCT_ENCODER()

CBOR_START_STRUCT_ENCODER(target_spi_port_t)
TARGET_SPI_MEMBERS
CBOR_END_STRUCT_ENCODER()

CBOR_START_STRUCT_ENCODER(target_spi_device_t)
TARGET_SPI_DEVICE_MEMBERS
CBOR_END_STRUCT_ENCODER()

CBOR_START_STRUCT_ENCODER(target_gyro_spi_device_t)
TARGET_GYRO_SPI_DEVICE_MEMBERS
CBOR_END_STRUCT_ENCODER()

CBOR_START_STRUCT_ENCODER(target_rx_spi_device_t)
TARGET_RX_SPI_DEVICE_MEMBERS
CBOR_END_STRUCT_ENCODER()

CBOR_START_STRUCT_ENCODER(target_t)
TARGET_MEMBERS
CBOR_END_STRUCT_ENCODER()

CBOR_START_STRUCT_ENCODER(target_info_t)
TARGET_INFO_MEMBERS
CBOR_END_STRUCT_ENCODER()

#undef START_STRUCT
#undef END_STRUCT
#undef MEMBER
#undef STR_MEMBER
#undef TSTR_MEMBER
#undef ARRAY_MEMBER
#undef INDEX_ARRAY_MEMBER
#undef STR_ARRAY_MEMBER

#define START_STRUCT CBOR_START_STRUCT_DECODER
#define END_STRUCT CBOR_END_STRUCT_DECODER
#define MEMBER CBOR_DECODE_MEMBER
#define STR_MEMBER CBOR_DECODE_STR_MEMBER
#define TSTR_MEMBER CBOR_DECODE_TSTR_MEMBER
#define ARRAY_MEMBER CBOR_DECODE_ARRAY_MEMBER
#define INDEX_ARRAY_MEMBER CBOR_DECODE_INDEX_ARRAY_MEMBER
#define STR_ARRAY_MEMBER CBOR_DECODE_STR_ARRAY_MEMBER

CBOR_START_STRUCT_DECODER(target_led_t)
TARGET_LED_MEMBERS
CBOR_END_STRUCT_DECODER()

CBOR_START_STRUCT_DECODER(target_invert_pin_t)
TARGET_BUZZER_MEMBERS
CBOR_END_STRUCT_DECODER()

CBOR_START_STRUCT_DECODER(target_serial_port_t)
TARGET_SERIAL_MEMBERS
CBOR_END_STRUCT_DECODER()

CBOR_START_STRUCT_DECODER(target_spi_port_t)
TARGET_SPI_MEMBERS
CBOR_END_STRUCT_DECODER()

CBOR_START_STRUCT_DECODER(target_gyro_spi_device_t)
TARGET_GYRO_SPI_DEVICE_MEMBERS
CBOR_END_STRUCT_DECODER()

CBOR_START_STRUCT_DECODER(target_spi_device_t)
TARGET_SPI_DEVICE_MEMBERS
CBOR_END_STRUCT_DECODER()

CBOR_START_STRUCT_DECODER(target_rx_spi_device_t)
TARGET_RX_SPI_DEVICE_MEMBERS
CBOR_END_STRUCT_DECODER()

CBOR_START_STRUCT_DECODER(target_t)
TARGET_MEMBERS
CBOR_END_STRUCT_DECODER()

#undef START_STRUCT
#undef END_STRUCT
#undef MEMBER
#undef STR_MEMBER
#undef TSTR_MEMBER
Expand Down
88 changes: 53 additions & 35 deletions src/core/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,22 @@ typedef struct {
bool invert;
} target_led_t;

#define TARGET_LED_MEMBERS \
MEMBER(pin, gpio_pins_t) \
MEMBER(invert, bool)
#define TARGET_LED_MEMBERS \
START_STRUCT(target_led_t) \
MEMBER(pin, gpio_pins_t) \
MEMBER(invert, bool) \
END_STRUCT()

typedef struct {
gpio_pins_t pin;
bool invert;
} target_invert_pin_t;

#define TARGET_BUZZER_MEMBERS \
MEMBER(pin, gpio_pins_t) \
MEMBER(invert, bool)
#define TARGET_BUZZER_MEMBERS \
START_STRUCT(target_invert_pin_t) \
MEMBER(pin, gpio_pins_t) \
MEMBER(invert, bool) \
END_STRUCT()

typedef struct {
uint8_t index;
Expand All @@ -87,11 +91,13 @@ typedef struct {
gpio_pins_t inverter;
} target_serial_port_t;

#define TARGET_SERIAL_MEMBERS \
MEMBER(index, uint8_t) \
MEMBER(rx, gpio_pins_t) \
MEMBER(tx, gpio_pins_t) \
MEMBER(inverter, gpio_pins_t)
#define TARGET_SERIAL_MEMBERS \
START_STRUCT(target_serial_port_t) \
MEMBER(index, uint8_t) \
MEMBER(rx, gpio_pins_t) \
MEMBER(tx, gpio_pins_t) \
MEMBER(inverter, gpio_pins_t) \
END_STRUCT()

typedef struct {
uint8_t index;
Expand All @@ -100,31 +106,37 @@ typedef struct {
gpio_pins_t sck;
} target_spi_port_t;

#define TARGET_SPI_MEMBERS \
MEMBER(index, uint8_t) \
MEMBER(miso, gpio_pins_t) \
MEMBER(mosi, gpio_pins_t) \
MEMBER(sck, gpio_pins_t)
#define TARGET_SPI_MEMBERS \
START_STRUCT(target_spi_port_t) \
MEMBER(index, uint8_t) \
MEMBER(miso, gpio_pins_t) \
MEMBER(mosi, gpio_pins_t) \
MEMBER(sck, gpio_pins_t) \
END_STRUCT()

typedef struct {
spi_ports_t port;
gpio_pins_t nss;
} target_spi_device_t;

#define TARGET_SPI_DEVICE_MEMBERS \
MEMBER(port, uint8_t) \
MEMBER(nss, gpio_pins_t)
#define TARGET_SPI_DEVICE_MEMBERS \
START_STRUCT(target_spi_device_t) \
MEMBER(port, uint8_t) \
MEMBER(nss, gpio_pins_t) \
END_STRUCT()

typedef struct {
spi_ports_t port;
gpio_pins_t nss;
gpio_pins_t exti;
} target_gyro_spi_device_t;

#define TARGET_GYRO_SPI_DEVICE_MEMBERS \
MEMBER(port, uint8_t) \
MEMBER(nss, gpio_pins_t) \
MEMBER(exti, gpio_pins_t)
#define TARGET_GYRO_SPI_DEVICE_MEMBERS \
START_STRUCT(target_gyro_spi_device_t) \
MEMBER(port, uint8_t) \
MEMBER(nss, gpio_pins_t) \
MEMBER(exti, gpio_pins_t) \
END_STRUCT()

typedef struct {
spi_ports_t port;
Expand All @@ -138,16 +150,18 @@ typedef struct {
gpio_pins_t reset;
} target_rx_spi_device_t;

#define TARGET_RX_SPI_DEVICE_MEMBERS \
MEMBER(port, uint8_t) \
MEMBER(nss, gpio_pins_t) \
MEMBER(exti, gpio_pins_t) \
MEMBER(ant_sel, gpio_pins_t) \
MEMBER(lna_en, gpio_pins_t) \
MEMBER(tx_en, gpio_pins_t) \
MEMBER(busy, gpio_pins_t) \
MEMBER(busy_exti, bool) \
MEMBER(reset, gpio_pins_t)
#define TARGET_RX_SPI_DEVICE_MEMBERS \
START_STRUCT(target_rx_spi_device_t) \
MEMBER(port, uint8_t) \
MEMBER(nss, gpio_pins_t) \
MEMBER(exti, gpio_pins_t) \
MEMBER(ant_sel, gpio_pins_t) \
MEMBER(lna_en, gpio_pins_t) \
MEMBER(tx_en, gpio_pins_t) \
MEMBER(busy, gpio_pins_t) \
MEMBER(busy_exti, bool) \
MEMBER(reset, gpio_pins_t) \
END_STRUCT()

typedef struct {
uint8_t name[32];
Expand Down Expand Up @@ -180,6 +194,7 @@ typedef struct {
} target_t;

#define TARGET_MEMBERS \
START_STRUCT(target_t) \
TSTR_MEMBER(name, 32) \
MEMBER(brushless, bool) \
ARRAY_MEMBER(leds, LED_MAX, target_led_t) \
Expand All @@ -200,7 +215,8 @@ typedef struct {
MEMBER(buzzer, target_invert_pin_t) \
ARRAY_MEMBER(motor_pins, MOTOR_PIN_MAX, gpio_pins_t) \
MEMBER(vbat_scale, uint16_t) \
MEMBER(ibat_scale, uint16_t)
MEMBER(ibat_scale, uint16_t) \
END_STRUCT()

typedef enum {
FEATURE_BRUSHLESS = (1 << 1),
Expand All @@ -221,12 +237,14 @@ typedef struct {
} target_info_t;

#define TARGET_INFO_MEMBERS \
START_STRUCT(target_info_t) \
STR_MEMBER(mcu) \
STR_MEMBER(git_version) \
MEMBER(quic_protocol_version, uint32_t) \
MEMBER(features, uint32_t) \
ARRAY_MEMBER(rx_protocols, RX_PROTOCOL_MAX, uint8_t) \
MEMBER(gyro_id, uint8_t)
MEMBER(gyro_id, uint8_t) \
END_STRUCT()

extern target_t target;
extern target_info_t target_info;
Expand Down

0 comments on commit 915f504

Please sign in to comment.