From d25d35c5cea9bc6b2ea4544c4a687017125a6cbd Mon Sep 17 00:00:00 2001 From: bkleiner Date: Sun, 22 Oct 2023 20:06:38 +0200 Subject: [PATCH] msp-vtx: use vtx-actual directly --- src/io/msp.c | 14 ++++++------ src/io/vtx.c | 2 +- src/io/vtx_msp.c | 56 +++++++++++++++++++----------------------------- 3 files changed, 30 insertions(+), 42 deletions(-) diff --git a/src/io/msp.c b/src/io/msp.c index 481aec6f5..26430dc29 100644 --- a/src/io/msp.c +++ b/src/io/msp.c @@ -27,7 +27,7 @@ enum { }; extern uint8_t msp_vtx_detected; -extern vtx_settings_t msp_vtx_settings; +extern vtx_settings_t vtx_actual; extern const uint16_t frequency_table[VTX_BAND_MAX][VTX_CHANNEL_MAX]; extern char msp_vtx_band_letters[VTX_BAND_MAX]; extern char msp_vtx_band_labels[VTX_BAND_MAX][8]; @@ -324,7 +324,7 @@ static void msp_process_serial_cmd(msp_t *msp, msp_magic_t magic, uint16_t cmd, } case MSP_SET_VTX_CONFIG: { - vtx_settings_t *settings = &msp_vtx_settings; + vtx_settings_t *settings = &vtx_actual; if (msp->device != MSP_DEVICE_VTX) { // store non-msp settings in temporary; static vtx_settings_t _vtx_settings; @@ -451,14 +451,14 @@ static void msp_process_serial_cmd(msp_t *msp, msp_magic_t magic, uint16_t cmd, break; } - const uint16_t power = vtx_settings.power_table.values[level - 1]; + const uint16_t power = vtx_actual.power_table.values[level - 1]; uint8_t buf[7]; buf[0] = level; buf[1] = power & 0xFF; buf[2] = power >> 8; buf[3] = 3; - memcpy(buf + 4, vtx_settings.power_table.labels[level - 1], 3); + memcpy(buf + 4, vtx_actual.power_table.labels[level - 1], 3); msp_send_reply(msp, magic, cmd, buf, 7); break; @@ -471,12 +471,12 @@ static void msp_process_serial_cmd(msp_t *msp, msp_magic_t magic, uint16_t cmd, break; } - vtx_settings.power_table.levels = max(level, vtx_settings.power_table.levels); - vtx_settings.power_table.values[level - 1] = payload[2] << 8 | payload[1]; + vtx_actual.power_table.levels = max(level, vtx_actual.power_table.levels); + vtx_actual.power_table.values[level - 1] = payload[2] << 8 | payload[1]; const uint8_t label_len = payload[3]; for (uint8_t i = 0; i < 3; i++) { - vtx_settings.power_table.labels[level - 1][i] = i >= label_len ? 0 : payload[4 + i]; + vtx_actual.power_table.labels[level - 1][i] = i >= label_len ? 0 : payload[4 + i]; } msp_send_reply(msp, magic, cmd, NULL, 0); break; diff --git a/src/io/vtx.c b/src/io/vtx.c index ded78dd8e..55ad4c111 100644 --- a/src/io/vtx.c +++ b/src/io/vtx.c @@ -24,7 +24,7 @@ vtx_settings_t vtx_settings = { }; uint8_t vtx_connect_tries = 0; -static vtx_settings_t vtx_actual; +vtx_settings_t vtx_actual; static uint8_t apply_tries = 0; static uint32_t vtx_delay_start = 0; diff --git a/src/io/vtx_msp.c b/src/io/vtx_msp.c index 592f533b9..84f9736b2 100644 --- a/src/io/vtx_msp.c +++ b/src/io/vtx_msp.c @@ -28,10 +28,9 @@ typedef struct { } msp_vtx_config_t; extern uint8_t vtx_connect_tries; +extern vtx_settings_t vtx_actual; uint8_t msp_vtx_detected = 0; -vtx_settings_t msp_vtx_settings; - msp_t *msp_vtx; char msp_vtx_band_letters[VTX_BAND_MAX] = {'A', 'B', 'E', 'F', 'R', 'L'}; @@ -47,38 +46,30 @@ char msp_vtx_band_labels[VTX_BAND_MAX][8] = { extern void msp_send_reply(msp_t *msp, msp_magic_t magic, uint16_t cmd, uint8_t *data, uint32_t len); void msp_vtx_send_config_reply(msp_t *msp, msp_magic_t magic) { - const uint16_t freq = vtx_frequency_from_channel(msp_vtx_settings.band, msp_vtx_settings.channel); + const uint16_t freq = vtx_frequency_from_channel(vtx_actual.band, vtx_actual.channel); msp_vtx_config_t config = { .vtx_type = 0, - .band = msp_vtx_settings.band + 1, - .channel = msp_vtx_settings.channel + 1, - .power = msp_vtx_settings.power_level + 1, - .pitmode = msp_vtx_settings.pit_mode == VTX_PIT_MODE_ON ? 1 : 0, + .band = vtx_actual.band + 1, + .channel = vtx_actual.channel + 1, + .power = vtx_actual.power_level + 1, + .pitmode = vtx_actual.pit_mode == VTX_PIT_MODE_ON ? 1 : 0, .freq_lsb = freq & 0xFF, .freq_msb = (freq >> 8), - .device_is_ready = msp_vtx_settings.detected, + .device_is_ready = vtx_actual.detected, .low_power_disarm = 0, .pit_mode_freq_lsb = freq & 0xFF, .pit_mode_freq_msb = (freq >> 8), .vtx_table_available = 1, .bands = VTX_BAND_MAX, .channels = VTX_CHANNEL_MAX, - .power_levels = vtx_settings.power_table.levels, + .power_levels = vtx_actual.power_table.levels, }; msp_send_reply(msp, magic, MSP_VTX_CONFIG, (uint8_t *)&config, sizeof(msp_vtx_config_t)); } vtx_detect_status_t vtx_msp_update(vtx_settings_t *actual) { - static bool settings_init = false; - if (!settings_init) { - msp_vtx_settings = vtx_settings; - settings_init = true; - return VTX_DETECT_WAIT; - } - if (vtx_connect_tries > MSP_VTX_DETECT_TRIES) { - settings_init = false; return VTX_DETECT_ERROR; } @@ -91,22 +82,19 @@ vtx_detect_status_t vtx_msp_update(vtx_settings_t *actual) { case VTX_SUCCESS: case VTX_IDLE: - if (msp_vtx_detected) { - *actual = msp_vtx_settings; + if (!msp_vtx_detected) { + return VTX_DETECT_WAIT; + } - if (vtx_settings.detected == VTX_PROTOCOL_MSP_VTX) { - return VTX_DETECT_UPDATE; - } + if (vtx_settings.magic != VTX_SETTINGS_MAGIC) { + vtx_set(actual); + } - if (vtx_settings.magic != VTX_SETTINGS_MAGIC) { - vtx_set(actual); - } + memcpy(&vtx_settings.power_table, &actual->power_table, sizeof(vtx_power_table_t)); - vtx_settings.detected = VTX_PROTOCOL_MSP_VTX; - vtx_connect_tries = 0; - return VTX_DETECT_SUCCESS; - } - return VTX_DETECT_WAIT; + vtx_settings.detected = VTX_PROTOCOL_MSP_VTX; + vtx_connect_tries = 0; + return VTX_DETECT_SUCCESS; } // wait otherwise @@ -114,17 +102,17 @@ vtx_detect_status_t vtx_msp_update(vtx_settings_t *actual) { } void msp_vtx_set_frequency(vtx_band_t band, vtx_channel_t channel) { - msp_vtx_settings.band = band; - msp_vtx_settings.channel = channel; + vtx_actual.band = band; + vtx_actual.channel = channel; msp_vtx_send_config_reply(msp_vtx, MSP2_MAGIC); } void msp_vtx_set_power_level(vtx_power_level_t power) { - msp_vtx_settings.power_level = power; + vtx_actual.power_level = power; msp_vtx_send_config_reply(msp_vtx, MSP2_MAGIC); } void msp_vtx_set_pit_mode(vtx_pit_mode_t pit_mode) { - msp_vtx_settings.pit_mode = pit_mode; + vtx_actual.pit_mode = pit_mode; msp_vtx_send_config_reply(msp_vtx, MSP2_MAGIC); } \ No newline at end of file