Skip to content

Commit

Permalink
feat: add format quality factor
Browse files Browse the repository at this point in the history
  • Loading branch information
edy555 committed Jun 19, 2020
1 parent 64de4d5 commit bc1b57c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
7 changes: 4 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1550,7 +1550,8 @@ static const struct {
{ "REAL", NGRIDY/2, 0.25 },
{ "IMAG", NGRIDY/2, 0.25 },
{ "R", NGRIDY/2, 100.0 },
{ "X", NGRIDY/2, 100.0 }
{ "X", NGRIDY/2, 100.0 },
{ "Q", 0, 10.0 }
};

static const char * const trc_channel_name[] = {
Expand Down Expand Up @@ -1654,8 +1655,8 @@ VNA_SHELL_FUNCTION(cmd_trace)
#if MAX_TRACE_TYPE != 12

This comment has been minimized.

Copy link
@DiSlord

DiSlord Jun 20, 2020

Contributor

Need also set MAX_TRACE_TYPE = 13 in nanovna.h and fix this for correct work command

#error "Trace type enum possibly changed, check cmd_trace function"
#endif
// enum TRC_LOGMAG, TRC_PHASE, TRC_DELAY, TRC_SMITH, TRC_POLAR, TRC_LINEAR, TRC_SWR, TRC_REAL, TRC_IMAG, TRC_R, TRC_X, TRC_OFF
static const char cmd_type_list[] = "logmag|phase|delay|smith|polar|linear|swr|real|imag|r|x|off";
// enum TRC_LOGMAG, TRC_PHASE, TRC_DELAY, TRC_SMITH, TRC_POLAR, TRC_LINEAR, TRC_SWR, TRC_REAL, TRC_IMAG, TRC_R, TRC_X, TRC_Q, TRC_OFF
static const char cmd_type_list[] = "logmag|phase|delay|smith|polar|linear|swr|real|imag|r|x|q|off";
int type = get_str_index(argv[1], cmd_type_list);
if (type >= 0) {
set_trace_type(t, type);
Expand Down
4 changes: 2 additions & 2 deletions nanovna.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ extern const uint16_t numfont16x22[];

#define MAX_TRACE_TYPE 12
enum trace_type {
TRC_LOGMAG=0, TRC_PHASE, TRC_DELAY, TRC_SMITH, TRC_POLAR, TRC_LINEAR, TRC_SWR, TRC_REAL, TRC_IMAG, TRC_R, TRC_X, TRC_OFF
TRC_LOGMAG=0, TRC_PHASE, TRC_DELAY, TRC_SMITH, TRC_POLAR, TRC_LINEAR, TRC_SWR, TRC_REAL, TRC_IMAG, TRC_R, TRC_X, TRC_Q, TRC_OFF
};
// Mask for define rectangular plot
#define RECTANGULAR_GRID_MASK ((1<<TRC_LOGMAG)|(1<<TRC_PHASE)|(1<<TRC_DELAY)|(1<<TRC_LINEAR)|(1<<TRC_SWR)|(1<<TRC_REAL)|(1<<TRC_IMAG)|(1<<TRC_R)|(1<<TRC_X))
#define RECTANGULAR_GRID_MASK ((1<<TRC_LOGMAG)|(1<<TRC_PHASE)|(1<<TRC_DELAY)|(1<<TRC_LINEAR)|(1<<TRC_SWR)|(1<<TRC_REAL)|(1<<TRC_IMAG)|(1<<TRC_R)|(1<<TRC_X)|(1<<TRC_Q))

// LOGMAG: SCALE, REFPOS, REFVAL
// PHASE: SCALE, REFPOS, REFVAL
Expand Down
19 changes: 19 additions & 0 deletions plot.c
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,14 @@ reactance(const float *v)
return zi;
}

static float
qualityfactor(const float *v)
{
float i = 2*v[1];
float r = (1+v[0])*(1-v[0]) - v[1]*v[1];
return fabs(i / r);
}

static void
cartesian_scale(float re, float im, int *xp, int *yp, float scale)
{
Expand Down Expand Up @@ -567,6 +575,9 @@ trace_into_index(int t, int i, float array[POINTS_COUNT][2])
case TRC_X:
v-= reactance(coeff) * scale;
break;
case TRC_Q:
v-= qualityfactor(coeff) * scale;
break;
case TRC_SMITH:
//case TRC_ADMIT:
case TRC_POLAR:
Expand Down Expand Up @@ -669,6 +680,10 @@ trace_get_value_string(int t, char *buf, int len, float array[POINTS_COUNT][2],
format = "%.2F"S_OHM;
v = gamma2reactance(coeff);
break;
case TRC_Q:
format = "%.1f";
v = qualityfactor(coeff);
break;
case TRC_SMITH:
format_smith_value(buf, len, coeff, frequencies[i]);
return;
Expand Down Expand Up @@ -729,6 +744,10 @@ trace_get_value_string_delta(int t, char *buf, int len, float array[POINTS_COUNT
format = "%.2F"S_OHM;
v = gamma2reactance(coeff);
break;
case TRC_Q:
format = "%.1f";
v = qualityfactor(coeff);
break;
//case TRC_ADMIT:
case TRC_POLAR:
plot_printf(buf, len, "%.2f%+.2fj", coeff[0], coeff[1]);
Expand Down
10 changes: 6 additions & 4 deletions ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,7 @@ const menuitem_t menu_format2[] = {
{ MT_CALLBACK, TRC_IMAG, "IMAG", menu_format_cb },
{ MT_CALLBACK, TRC_R, "RESISTANCE", menu_format_cb },
{ MT_CALLBACK, TRC_X, "REACTANCE", menu_format_cb },
{ MT_CALLBACK, TRC_Q, "Q FACTOR", menu_format_cb },
{ MT_CANCEL, 0, S_LARROW" BACK", NULL },
{ MT_NONE, 0, NULL, NULL } // sentinel
};
Expand Down Expand Up @@ -1140,6 +1141,7 @@ menu_invoke(int item)

#define MENU_BUTTON_WIDTH 60
#define MENU_BUTTON_HEIGHT 30
#define MENU_BUTTON_MAX 8
#define NUM_INPUT_HEIGHT 30

#define KP_WIDTH 48
Expand Down Expand Up @@ -1414,7 +1416,7 @@ static void
draw_menu_buttons(const menuitem_t *menu)
{
int i = 0;
for (i = 0; i < 7; i++) {
for (i = 0; i < MENU_BUTTON_MAX; i++) {
const char *l1, *l2;
if (menu[i].type == MT_NONE)
break;
Expand Down Expand Up @@ -1460,7 +1462,7 @@ menu_apply_touch(void)
int i;

touch_position(&touch_x, &touch_y);
for (i = 0; i < 7; i++) {
for (i = 0; i < MENU_BUTTON_MAX; i++) {
if (menu[i].type == MT_NONE)
break;
if (menu[i].type == MT_BLANK)
Expand All @@ -1485,7 +1487,7 @@ draw_menu(void)
static void
erase_menu_buttons(void)
{
ili9341_fill(320-MENU_BUTTON_WIDTH, 0, MENU_BUTTON_WIDTH, MENU_BUTTON_HEIGHT*7, DEFAULT_BG_COLOR);
ili9341_fill(320-MENU_BUTTON_WIDTH, 0, MENU_BUTTON_WIDTH, MENU_BUTTON_HEIGHT * MENU_BUTTON_MAX, DEFAULT_BG_COLOR);
}

static void
Expand All @@ -1503,8 +1505,8 @@ leave_ui_mode()
} else if (ui_mode == UI_NUMERIC) {
request_to_draw_cells_behind_numeric_input();
erase_numeric_input();
draw_frequencies();
}
draw_frequencies();
}

static void
Expand Down

0 comments on commit bc1b57c

Please sign in to comment.