Skip to content

Commit

Permalink
Fix applying filters while changing mode in CAT
Browse files Browse the repository at this point in the history
Closes: strijar#4
  • Loading branch information
gdyuldin committed Oct 1, 2024
1 parent 2915201 commit b63b537
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ target_sources(${PROJECT_NAME} PUBLIC
dialog_ft8.c dialog_freq.c dialog_gps.c dialog_msg_cw.c
dialog_msg_voice.c dialog_recorder.c dialog_qth.c dialog_callsign.c
textarea_window.c cw_encoder.c buttons.c vol.c recorder.c
qth.c voice.cpp gfsk.c cw_tune_ui.c adif.c qso_log.c
qth.c voice.cpp gfsk.c cw_tune_ui.c adif.c qso_log.c scheduler.c
)

add_subdirectory(fonts)
Expand Down
55 changes: 41 additions & 14 deletions src/cat.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include "events.h"
#include "waterfall.h"
#include "spectrum.h"
#include "scheduler.h"
#include "main_screen.h"

#include <aether_radio/x6100_control/low/gpio.h>
#include "lvgl/lvgl.h"
Expand Down Expand Up @@ -108,6 +110,16 @@ static int fd;

static uint8_t frame[256];

static void log_msg(const char * msg, uint16_t len) {
char buf[512];
char *buf_ptr = buf;
for (size_t i = 0; i < len; i++) {
buf_ptr += sprintf(buf_ptr, "%02X:", msg[i]);
}
*(buf_ptr - 1) = '\0';
LV_LOG_USER("Cmd %s (Len %i)", buf, len);
}

static uint16_t frame_get() {
uint16_t len = 0;
uint8_t c;
Expand Down Expand Up @@ -151,13 +163,26 @@ static void send_code(uint8_t code) {
send_frame(6);
}

static void set_freq(uint64_t freq) {
static void set_freq(void * arg) {
if (!arg) {
LV_LOG_ERROR("arg is NULL");
}
uint64_t freq = * (uint64_t*) arg;
if (params_bands_find(freq, &params.freq_band)) {
bands_activate(&params.freq_band, NULL);
}

radio_set_freq(freq);
event_send(lv_scr_act(), EVENT_SCREEN_UPDATE, NULL);
lv_event_send(lv_scr_act(), EVENT_SCREEN_UPDATE, NULL);
}

static void set_vfo(void * arg) {
if (!arg) {
LV_LOG_ERROR("arg is NULL");
}
x6100_vfo_t vfo = * (x6100_vfo_t*) arg;
radio_set_vfo(vfo);
lv_event_send(lv_scr_act(), EVENT_SCREEN_UPDATE, NULL);
}


Expand Down Expand Up @@ -258,7 +283,7 @@ static void frame_parse(uint16_t len) {
x6100_vfo_t target_vfo = cur_vfo;

#if 0
LV_LOG_WARN("Cmd %02X:%02X (Len %i)", frame[4], frame[5], len);
log_msg(frame, len);
#endif

// echo input frame
Expand All @@ -282,7 +307,7 @@ static void frame_parse(uint16_t len) {
new_freq = from_bcd(&frame[5], 10);
if (new_freq != cur_freq)
{
set_freq(new_freq);
scheduler_put(set_freq, &new_freq, sizeof(new_freq));
}

send_code(CODE_OK);
Expand All @@ -291,8 +316,7 @@ static void frame_parse(uint16_t len) {
case C_SET_MODE: ;
x6100_mode_t new_mode = ci_mode_2_x_mode(frame[5], NULL);
if (new_mode != cur_mode) {
radio_set_mode(cur_vfo, new_mode);
event_send(lv_scr_act(), EVENT_SCREEN_UPDATE, NULL);
scheduler_put(main_screen_set_mode, &new_mode, sizeof(new_mode));
}

send_code(CODE_OK);
Expand All @@ -319,26 +343,28 @@ static void frame_parse(uint16_t len) {
}
break;

case C_SET_VFO:
case C_SET_VFO:;
x6100_vfo_t new_vfo;
switch (frame[5]) {
case S_VFOA:
if (cur_vfo != X6100_VFO_A) {
radio_set_vfo(X6100_VFO_A);
event_send(lv_scr_act(), EVENT_SCREEN_UPDATE, NULL);
new_vfo = X6100_VFO_A;
scheduler_put(set_vfo, &new_vfo, sizeof(new_vfo));
}

send_code(CODE_OK);
break;

case S_VFOB:
if (cur_vfo != X6100_VFO_B) {
radio_set_vfo(X6100_VFO_B);
event_send(lv_scr_act(), EVENT_SCREEN_UPDATE, NULL);
new_vfo = X6100_VFO_A;
scheduler_put(set_vfo, &new_vfo, sizeof(new_vfo));
}
send_code(CODE_OK);
break;

default:
LV_LOG_WARN("Unsupported %02X:%02X (Len %i)", frame[4], frame[5], len);
send_code(CODE_NG);
break;
}
Expand All @@ -358,7 +384,7 @@ static void frame_parse(uint16_t len) {
params_band_vfo_freq_set(target_vfo, freq);

if (cur_vfo == target_vfo) {
set_freq(freq);
scheduler_put(set_freq, &freq, sizeof(freq));
}
}
send_code(CODE_OK);
Expand All @@ -377,8 +403,8 @@ static void frame_parse(uint16_t len) {
send_frame(10);
} else {
// TODO: Add filters applying
radio_set_mode(target_vfo, ci_mode_2_x_mode(frame[6], &frame[7]));
event_send(lv_scr_act(), EVENT_SCREEN_UPDATE, NULL);
x6100_mode_t new_mode = ci_mode_2_x_mode(frame[6], &frame[7]);
scheduler_put(main_screen_set_mode, &new_mode, sizeof(new_mode));
send_code(CODE_OK);
}
break;
Expand All @@ -394,6 +420,7 @@ static void frame_parse(uint16_t len) {
break;

default:
LV_LOG_WARN("Unsupported %02X:%02X (Len %i)", frame[4], frame[5], len);
send_code(CODE_NG);
break;
}
Expand Down
2 changes: 2 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "mfk.h"
#include "vol.h"
#include "qso_log.h"
#include "scheduler.h"

#define DISP_BUF_SIZE (800 * 480 * 4)

Expand Down Expand Up @@ -130,6 +131,7 @@ int main(void) {
while (1) {
next_loop_time = get_time() + lv_timer_handler();
event_obj_check();
scheduler_work();
sleep_time = next_loop_time - get_time();
if (sleep_time > 0) {
usleep(sleep_time * 1000);
Expand Down
29 changes: 19 additions & 10 deletions src/main_screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,16 +440,7 @@ static void change_mode(keypad_key_t key, keypad_state_t state) {
}
}

radio_set_mode(params_band_vfo_get(), next_mode);
radio_filters_setup();
uint16_t zoom_factor = params_current_mode_spectrum_factor_get();
lv_msg_send(MSG_SPECTRUM_ZOOM_CHANGED, &zoom_factor);
info_params_set();
pannel_visible();

if (params.mag_info.x) {
msg_tiny_set_text_fmt("%s", info_params_mode());
}
main_screen_set_mode(&next_mode);
}

static void main_screen_keypad_cb(lv_event_t * e) {
Expand Down Expand Up @@ -1200,3 +1191,21 @@ void main_screen_band_set()
{
freq_update();
}


void main_screen_set_mode(void * arg) {
if (!arg) {
LV_LOG_WARN("arg is NULL");
}
x6100_mode_t next_mode = * (x6100_mode_t *) arg;
radio_set_mode(params_band_vfo_get(), next_mode);
radio_filters_setup();
uint16_t zoom_factor = params_current_mode_spectrum_factor_get();
lv_msg_send(MSG_SPECTRUM_ZOOM_CHANGED, &zoom_factor);
info_params_set();
pannel_visible();

if (params.mag_info.x) {
msg_tiny_set_text_fmt("%s", info_params_mode());
}
}
3 changes: 3 additions & 0 deletions src/main_screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ void main_screen_set_freq(uint64_t f);

void mem_load(uint16_t id);
void mem_save(uint16_t id);

// Scheduler functions
void main_screen_set_mode(void *arg);
64 changes: 64 additions & 0 deletions src/scheduler.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* Xiegu X6100 LVGL GUI
*
* Copyright (c) 2024 Georgy Dyuldin aka R2RFE
*/

#include "scheduler.h"

#include "lvgl/lvgl.h"

#include <pthread.h>
#include <stdlib.h>


#define QUEUE_SIZE 64

typedef struct {
scheduler_fn_t fn;
void *arg;
} item_t;

static item_t queue[QUEUE_SIZE];
static uint8_t queue_read = 0;
static uint8_t queue_write = 0;

static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;


void scheduler_put(scheduler_fn_t fn, void * arg, size_t arg_size) {
pthread_mutex_lock(&mutex);
uint8_t next = (queue_write + 1) % QUEUE_SIZE;

if (next == queue_read) {
pthread_mutex_unlock(&mutex);
LV_LOG_ERROR("Scheduler queue overflow");
return;
}

queue[next].fn = fn;
if (arg_size) {
queue[next].arg = malloc(arg_size);
memcpy(queue[next].arg, arg, arg_size);
} else {
queue[next].arg = NULL;
}
queue_write = next;
pthread_mutex_unlock(&mutex);
}

void scheduler_work() {
while (queue_read != queue_write) {
pthread_mutex_lock(&mutex);
queue_read = (queue_read + 1) % QUEUE_SIZE;

queue[queue_read].fn(queue[queue_read].arg);

if (queue[queue_read].arg) {
free(queue[queue_read].arg);
}
pthread_mutex_unlock(&mutex);
}
}
24 changes: 24 additions & 0 deletions src/scheduler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* Xiegu X6100 LVGL GUI
*
* Copyright (c) 2024 Georgy Dyuldin aka R2RFE
*/

#pragma once

#include <stddef.h>

typedef void (* scheduler_fn_t)(void *);

/**
* Schedule execution function in main thread
*/
void scheduler_put(scheduler_fn_t fn, void *arg, size_t arg_size);


/**
* Execute scheduled functions
*/
void scheduler_work();

0 comments on commit b63b537

Please sign in to comment.