From 915f504a450b0bb66cd2c4806ee9496ec7e93e29 Mon Sep 17 00:00:00 2001 From: bkleiner Date: Sat, 4 Nov 2023 15:21:26 +0100 Subject: [PATCH] target: use more compact cbor definition --- src/core/target.c | 57 +++++------------------------- src/core/target.h | 88 ++++++++++++++++++++++++++++------------------- 2 files changed, 61 insertions(+), 84 deletions(-) diff --git a/src/core/target.c b/src/core/target.c index 015675b65..1d94d1a11 100644 --- a/src/core/target.c +++ b/src/core/target.c @@ -31,6 +31,8 @@ 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 @@ -38,42 +40,18 @@ target_info_t target_info = { #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 @@ -81,6 +59,8 @@ CBOR_END_STRUCT_ENCODER() #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 @@ -88,38 +68,17 @@ CBOR_END_STRUCT_ENCODER() #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 diff --git a/src/core/target.h b/src/core/target.h index 16447bd52..8aacbf9dd 100644 --- a/src/core/target.h +++ b/src/core/target.h @@ -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; @@ -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; @@ -100,20 +106,24 @@ 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; @@ -121,10 +131,12 @@ typedef struct { 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; @@ -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]; @@ -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) \ @@ -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), @@ -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;