Use better button input debounce and input process (this used in last NanoVNA firmwares) more compact and simple

Replace POINTS_COUNT to sweep_points this more correct
pull/4/head
DiSlord 6 years ago
parent 1c1491fd2a
commit 425221aa10

@ -863,7 +863,7 @@ static void trace_get_value_string(
if (FREQ_IS_CW()) { if (FREQ_IS_CW()) {
float t = ii*(setting.actual_sweep_time)*1000.0/290.0; float t = ii*(setting.actual_sweep_time)*1000.0/290.0;
#if 1 #if 1
plot_printf(&buf2[1], sizeof(buf2) -1, "%.3FS" , t/1000000.0); plot_printf(&buf2[1], sizeof(buf2) -1, "%.3Fs" , t/1000000.0);
#else #else
if (t>1000000.0){ if (t>1000000.0){
plot_printf(&buf2[1], sizeof(buf2) -1, "%4f" , t/1000000.0); plot_printf(&buf2[1], sizeof(buf2) -1, "%4f" , t/1000000.0);
@ -884,22 +884,17 @@ static void trace_get_value_string(
} }
#endif #endif
} else { } else {
uint32_t resolution = get_sweep_frequency(ST_SPAN)/290; #if 1
#if 0 uint32_t resolution = get_sweep_frequency(ST_SPAN);
if (resolution <= 2000) if (resolution <= 2000*290)
plot_printf(&buf2[1], sizeof(buf2) -1, "%3.3f" , (dfreq + 500) / 1000000.0); plot_printf(&buf2[1], sizeof(buf2) -1, "%3.3f" , (dfreq + 500) / 1000000.0);
else if (resolution <= 20000) else if (resolution <= 20000*290)
plot_printf(&buf2[1], sizeof(buf2) -1, "%3.2f" , (dfreq + 5000) / 1000000.0); plot_printf(&buf2[1], sizeof(buf2) -1, "%3.2f" , (dfreq + 5000) / 1000000.0);
else else
plot_printf(&buf2[1], sizeof(buf2) -1, "%3.1f" , (dfreq + 50000) / 1000000.0); plot_printf(&buf2[1], sizeof(buf2) -1, "%3.1f" , (dfreq + 50000) / 1000000.0);
} }
#else #else
int digits = 1; plot_printf(&buf2[1], sizeof(buf2) -1, "%.8qHz" , dfreq);
if (resolution <= 2000)
digits = 3;
else if (resolution <= 20000)
digits = 2;
plot_printf(&buf2[1], sizeof(buf2) -1, "%3.*f" , digits, (dfreq + 50000) / 1000000.0);
} }
#endif #endif
// frequency_string(&buf2[1], sizeof(buf2) -1, dfreq); // frequency_string(&buf2[1], sizeof(buf2) -1, dfreq);
@ -1906,7 +1901,7 @@ cell_draw_marker_info(int x0, int y0)
#endif #endif
static void cell_draw_marker_info(int x0, int y0) static void cell_draw_marker_info(int x0, int y0)
{ {
char buf[25]; char buf[32];
int t; int t;
int ref_marker = 0; int ref_marker = 0;
int j = 0; int j = 0;
@ -1991,16 +1986,15 @@ static void cell_draw_marker_info(int x0, int y0)
if (markers[i].mtype & M_NOISE) if (markers[i].mtype & M_NOISE)
buf[k++] = 'N'; buf[k++] = 'N';
buf[k++] = ' '; buf[k++] = ' ';
buf[k++] = 0; // buf[k++] = 0;
ili9341_set_background(DEFAULT_BG_COLOR); ili9341_set_background(DEFAULT_BG_COLOR);
ili9341_set_foreground(marker_color(markers[i].mtype)); ili9341_set_foreground(marker_color(markers[i].mtype));
// if (setting.unit) // if (setting.unit)
// cell_drawstring(buf, xpos, ypos); // cell_drawstring(buf, xpos, ypos);
// else // else
// cell_drawstring_7x13(buf, xpos, ypos); // cell_drawstring_7x13(buf, xpos, ypos);
int offs = strlen(buf);
trace_get_value_string( trace_get_value_string(
t, &buf[offs], (sizeof buf) - offs, t, &buf[k], (sizeof buf) - k,
idx, measured[trace[t].channel], frequencies, sweep_points, ridx, markers[i].mtype); idx, measured[trace[t].channel], frequencies, sweep_points, ridx, markers[i].mtype);
if (/* strlen(buf)*7> WIDTH/2 && */active > 1) if (/* strlen(buf)*7> WIDTH/2 && */active > 1)
cell_drawstring(buf, xpos, ypos); cell_drawstring(buf, xpos, ypos);

@ -327,7 +327,7 @@ void SI4432_Fill(int s, int start)
if (t < 0) if (t < 0)
t = 0; t = 0;
int ti = t * 1000 / 290.0; // Now in uS per point if (t < 30000) int ti = t * 1000 / 290.0; // Now in uS per point if (t < 30000)
for (int i=start; i<POINTS_COUNT; ) { for (int i=start; i<sweep_points; ) {
SPI2_CLK_LOW; SPI2_CLK_LOW;
palClearPad(GPIOC, sel); palClearPad(GPIOC, sel);
shiftOut( 0x26 ); shiftOut( 0x26 );
@ -356,7 +356,7 @@ float SI4432_RSSI(uint32_t i, int s)
#ifdef __FAST_SWEEP__ #ifdef __FAST_SWEEP__
if (buf_read) { if (buf_read) {
float dBm = ((float)((unsigned char)age[buf_index++]))/2 + SI4432_RSSI_correction; float dBm = ((float)((unsigned char)age[buf_index++]))/2 + SI4432_RSSI_correction;
if (buf_index ==POINTS_COUNT) { if (buf_index == sweep_points) {
buf_read = false; buf_read = false;
} }
return dBm; return dBm;

88
ui.c

@ -42,9 +42,9 @@ uistat_t uistat = {
#define EVT_DOWN 0x20 #define EVT_DOWN 0x20
#define EVT_REPEAT 0x40 #define EVT_REPEAT 0x40
#define BUTTON_DOWN_LONG_TICKS 5000 /* 1sec */ #define BUTTON_DOWN_LONG_TICKS 5000 /* 500ms */
#define BUTTON_DOUBLE_TICKS 2500 /* 500ms */ #define BUTTON_DOUBLE_TICKS 2500 /* 250ms */
#define BUTTON_REPEAT_TICKS 625 /* 125ms */ #define BUTTON_REPEAT_TICKS 400 /* 40ms */
#define BUTTON_DEBOUNCE_TICKS 200 #define BUTTON_DEBOUNCE_TICKS 200
/* lever switch assignment */ /* lever switch assignment */
@ -62,7 +62,6 @@ uistat_t uistat = {
static uint16_t last_button = 0b0000; static uint16_t last_button = 0b0000;
static uint32_t last_button_down_ticks; static uint32_t last_button_down_ticks;
static uint32_t last_button_repeat_ticks; static uint32_t last_button_repeat_ticks;
static int8_t inhibit_until_release = FALSE;
volatile uint8_t operation_requested = OP_NONE; volatile uint8_t operation_requested = OP_NONE;
@ -135,73 +134,62 @@ static void menu_push_submenu(const menuitem_t *submenu);
static int btn_check(void) static int btn_check(void)
{ {
int cur_button = READ_PORT() & BUTTON_MASK; systime_t ticks;
int changed = last_button ^ cur_button; // Debounce input
int status = 0; while(TRUE){
uint32_t ticks = chVTGetSystemTime(); ticks = chVTGetSystemTimeX();
if (changed & (1<<BIT_PUSH)) { if(ticks - last_button_down_ticks > BUTTON_DEBOUNCE_TICKS)
if ((cur_button & (1<<BIT_PUSH)) break;
&& ticks >= last_button_down_ticks + BUTTON_DEBOUNCE_TICKS) { chThdSleepMilliseconds(10);
// button released
status |= EVT_BUTTON_SINGLE_CLICK;
if (inhibit_until_release) {
status = 0;
inhibit_until_release = FALSE;
}
}
}
if (changed & (1<<BIT_UP1)) {
if ((cur_button & (1<<BIT_UP1))
&& (ticks >= last_button_down_ticks + BUTTON_DEBOUNCE_TICKS)) {
status |= EVT_UP;
}
}
if (changed & (1<<BIT_DOWN1)) {
if ((cur_button & (1<<BIT_DOWN1))
&& (ticks >= last_button_down_ticks + BUTTON_DEBOUNCE_TICKS)) {
status |= EVT_DOWN;
}
} }
int status = 0;
uint16_t cur_button = READ_PORT() & BUTTON_MASK;
// Detect only changed and pressed buttons
uint16_t button_set = (last_button ^ cur_button) & cur_button;
last_button_down_ticks = ticks; last_button_down_ticks = ticks;
last_button = cur_button; last_button = cur_button;
if (button_set & (1<<BIT_PUSH))
status |= EVT_BUTTON_SINGLE_CLICK;
if (button_set & (1<<BIT_UP1))
status |= EVT_UP;
if (button_set & (1<<BIT_DOWN1))
status |= EVT_DOWN;
return status; return status;
} }
static int btn_wait_release(void) static int btn_wait_release(void)
{ {
while (TRUE) { while (TRUE) {
int cur_button = READ_PORT() & BUTTON_MASK; systime_t ticks = chVTGetSystemTimeX();
int changed = last_button ^ cur_button; systime_t dt = ticks - last_button_down_ticks;
uint32_t ticks = chVTGetSystemTime(); // Debounce input
int status = 0; // if (dt < BUTTON_DEBOUNCE_TICKS){
if (!inhibit_until_release) { // chThdSleepMilliseconds(10);
if ((cur_button & (1<<BIT_PUSH)) // continue;
&& ticks >= last_button_down_ticks + BUTTON_DOWN_LONG_TICKS) { // }
inhibit_until_release = TRUE; chThdSleepMilliseconds(1);
uint16_t cur_button = READ_PORT() & BUTTON_MASK;
uint16_t changed = last_button ^ cur_button;
if (dt >= BUTTON_DOWN_LONG_TICKS && (cur_button & (1<<BIT_PUSH)))
return EVT_BUTTON_DOWN_LONG; return EVT_BUTTON_DOWN_LONG;
} else if (changed & (1<<BIT_PUSH)) // release
if ((changed & (1<<BIT_PUSH))
&& ticks < last_button_down_ticks + BUTTON_DOWN_LONG_TICKS) {
return EVT_BUTTON_SINGLE_CLICK; return EVT_BUTTON_SINGLE_CLICK;
}
}
if (changed) { if (changed) {
// finished // finished
last_button = cur_button; last_button = cur_button;
last_button_down_ticks = ticks; last_button_down_ticks = ticks;
inhibit_until_release = FALSE;
return 0; return 0;
} }
if (ticks >= last_button_down_ticks + BUTTON_DOWN_LONG_TICKS if (dt > BUTTON_DOWN_LONG_TICKS &&
&& ticks >= last_button_repeat_ticks + BUTTON_REPEAT_TICKS) { ticks > last_button_repeat_ticks) {
int status = 0;
if (cur_button & (1<<BIT_DOWN1)) if (cur_button & (1<<BIT_DOWN1))
status |= EVT_DOWN | EVT_REPEAT; status |= EVT_DOWN | EVT_REPEAT;
if (cur_button & (1<<BIT_UP1)) if (cur_button & (1<<BIT_UP1))
status |= EVT_UP | EVT_REPEAT; status |= EVT_UP | EVT_REPEAT;
last_button_repeat_ticks = ticks; last_button_repeat_ticks = ticks + BUTTON_REPEAT_TICKS;
return status; return status;
} }
} }
@ -1926,8 +1914,8 @@ lever_move_marker(int status)
} }
if ((status & EVT_UP) && markers[active_marker].index < sweep_points-1) { if ((status & EVT_UP) && markers[active_marker].index < sweep_points-1) {
markers[active_marker].index += step; markers[active_marker].index += step;
if (markers[active_marker].index > POINTS_COUNT-1) if (markers[active_marker].index > sweep_points-1)
markers[active_marker].index = POINTS_COUNT-1 ; markers[active_marker].index = sweep_points-1 ;
markers[active_marker].frequency = frequencies[markers[active_marker].index]; markers[active_marker].frequency = frequencies[markers[active_marker].index];
redraw_marker(active_marker); redraw_marker(active_marker);
} }

Loading…
Cancel
Save

Powered by TurnKey Linux.