Skip to content

Commit

Permalink
feat: add lever operations (center, span, search)
Browse files Browse the repository at this point in the history
  • Loading branch information
edy555 committed Nov 17, 2019
1 parent e6035a5 commit b909ccb
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 17 deletions.
3 changes: 3 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,9 @@ void
set_sweep_frequency(int type, int32_t freq)
{
int cal_applied = cal_status & CALSTAT_APPLY;
// negative value indicate overflow, do nothing
if (freq < 0)
return;
switch (type) {
case ST_START:
freq_mode_startstop();
Expand Down
118 changes: 101 additions & 17 deletions ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,14 @@ enum {
KM_START, KM_STOP, KM_CENTER, KM_SPAN, KM_CW, KM_SCALE, KM_REFPOS, KM_EDELAY, KM_VELOCITY_FACTOR, KM_SCALEDELAY
};

enum {
LM_MARKER, LM_SEARCH, LM_CENTER, LM_SPAN
};

uint8_t ui_mode = UI_NORMAL;
uint8_t keypad_mode;
int8_t selection = 0;
uint8_t lever_mode = LM_MARKER;

typedef struct {
uint8_t type;
Expand Down Expand Up @@ -774,6 +779,7 @@ menu_stimulus_cb(int item)
ui_mode_keypad(item);
ui_process_keypad();
}
lever_mode = item == 3 ? LM_SPAN : LM_CENTER;
break;
case 5: /* PAUSE */
toggle_sweep();
Expand Down Expand Up @@ -867,6 +873,7 @@ menu_marker_search_cb(int item)
break;
}
redraw_marker(active_marker, TRUE);
lever_mode = LM_SEARCH;
}

void
Expand Down Expand Up @@ -911,6 +918,7 @@ menu_marker_sel_cb(int item)
}
redraw_marker(active_marker, TRUE);
draw_menu();
lever_mode = LM_MARKER;
}

const menuitem_t menu_calop[] = {
Expand Down Expand Up @@ -1665,6 +1673,93 @@ ui_mode_normal(void)
ui_mode = UI_NORMAL;
}

static void
lever_move_marker(int status)
{
do {
if (active_marker >= 0 && markers[active_marker].enabled) {
if ((status & EVT_DOWN) && markers[active_marker].index > 0) {
markers[active_marker].index--;
markers[active_marker].frequency = frequencies[markers[active_marker].index];
redraw_marker(active_marker, FALSE);
}
if ((status & EVT_UP) && markers[active_marker].index < 100) {
markers[active_marker].index++;
markers[active_marker].frequency = frequencies[markers[active_marker].index];
redraw_marker(active_marker, FALSE);
}
}
status = btn_wait_release();
} while (status != 0);
if (active_marker >= 0)
redraw_marker(active_marker, TRUE);
}

static void
lever_search_marker(int status)
{
if (active_marker >= 0) {
if (status & EVT_DOWN) {
int i = marker_search_left(markers[active_marker].index);
if (i != -1)
markers[active_marker].index = i;
} else if (status & EVT_UP) {
int i = marker_search_right(markers[active_marker].index);
if (i != -1)
markers[active_marker].index = i;
}
redraw_marker(active_marker, TRUE);
}
}

// ex. 10942 -> 10000
// 6791 -> 5000
// 341 -> 200
static uint32_t
step_round(uint32_t v)
{
// decade step
uint32_t x = 1;
for (x = 1; x * 10 < v; x *= 10)
;

// 1-2-5 step
if (x * 2 > v)
return x;
else if (x * 5 > v)
return x * 2;
else
return x * 5;
}

static void
lever_zoom_span(int status)
{
if (status & EVT_UP) {
uint32_t span = get_sweep_frequency(ST_SPAN);
span = step_round(span - 1);
set_sweep_frequency(ST_SPAN, span);
} else if (status & EVT_DOWN) {
uint32_t span = get_sweep_frequency(ST_SPAN);
span = step_round(span);
span = step_round(span * 3);
set_sweep_frequency(ST_SPAN, span);
}
}

static void
lever_move_center(int status)
{
uint32_t center = get_sweep_frequency(ST_CENTER);
uint32_t span = get_sweep_frequency(ST_SPAN);
span = step_round(span / 3);
if (status & EVT_UP) {
set_sweep_frequency(ST_CENTER, center + span);
} else if (status & EVT_DOWN) {
set_sweep_frequency(ST_CENTER, center - span);
}
}

static void
ui_process_normal(void)
{
Expand All @@ -1673,23 +1768,12 @@ ui_process_normal(void)
if (status & EVT_BUTTON_SINGLE_CLICK) {
ui_mode_menu();
} else {
do {
if (active_marker >= 0 && markers[active_marker].enabled) {
if ((status & EVT_DOWN) && markers[active_marker].index > 0) {
markers[active_marker].index--;
markers[active_marker].frequency = frequencies[markers[active_marker].index];
redraw_marker(active_marker, FALSE);
}
if ((status & EVT_UP) && markers[active_marker].index < 100) {
markers[active_marker].index++;
markers[active_marker].frequency = frequencies[markers[active_marker].index];
redraw_marker(active_marker, FALSE);
}
}
status = btn_wait_release();
} while (status != 0);
if (active_marker >= 0)
redraw_marker(active_marker, TRUE);
switch (lever_mode) {
case LM_MARKER: lever_move_marker(status); break;
case LM_SEARCH: lever_search_marker(status); break;
case LM_CENTER: lever_move_center(status); break;
case LM_SPAN: lever_zoom_span(status); break;
}
}
}
}
Expand Down

0 comments on commit b909ccb

Please sign in to comment.