Skip to content

Commit

Permalink
msp-vtx: use vtx-actual directly
Browse files Browse the repository at this point in the history
  • Loading branch information
bkleiner committed Oct 22, 2023
1 parent 2e1c9ba commit d25d35c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 42 deletions.
14 changes: 7 additions & 7 deletions src/io/msp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/io/vtx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
56 changes: 22 additions & 34 deletions src/io/vtx_msp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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'};
Expand All @@ -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;
}

Expand All @@ -91,40 +82,37 @@ 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
return VTX_DETECT_WAIT;
}

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);
}

0 comments on commit d25d35c

Please sign in to comment.