From 7943a3fe96f7297779a6b823abb99a6b5c2ab94a Mon Sep 17 00:00:00 2001 From: TT Date: Tue, 3 Jan 2017 16:39:00 +0900 Subject: [PATCH] add dragging marker --- flash.c | 6 ++--- nanovna.h | 2 ++ plot.c | 31 ++++++++++++++++++++++++++ ui.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 3 deletions(-) diff --git a/flash.c b/flash.c index 7e67a48..e6c876a 100644 --- a/flash.c +++ b/flash.c @@ -66,7 +66,7 @@ void flash_unlock(void) static uint32_t -checksum(void *start, size_t len) +checksum(const void *start, size_t len) { uint32_t *p = (uint32_t*)start; uint32_t *tail = (uint32_t*)(start + len); @@ -85,7 +85,7 @@ int config_save(void) { uint16_t *src = (uint16_t*)&config; - uint16_t *dst = save_config_area; + uint16_t *dst = (uint16_t*)save_config_area; int count = sizeof(config_t) / sizeof(uint16_t); config.magic = CONFIG_MAGIC; @@ -109,7 +109,7 @@ config_save(void) int config_recall(void) { - config_t *src = save_config_area; + const config_t *src = (const config_t*)save_config_area; void *dst = &config; if (src->magic != CONFIG_MAGIC) diff --git a/nanovna.h b/nanovna.h index 4b1206b..654c0eb 100644 --- a/nanovna.h +++ b/nanovna.h @@ -206,6 +206,8 @@ void draw_cal_status(void); void markmap_all_markers(void); +void marker_position(int m, int t, int *x, int *y); +int search_nearest_index(int x, int y, int t); /* * ili9341.c diff --git a/plot.c b/plot.c index 6de1c23..129ff25 100644 --- a/plot.c +++ b/plot.c @@ -885,6 +885,37 @@ draw_marker(int w, int h, int x, int y, int c, int ch) } } +void +marker_position(int m, int t, int *x, int *y) +{ + uint32_t index = trace_index[t][markers[m].index]; + *x = CELL_X(index); + *y = CELL_Y(index); +} + +int +search_nearest_index(int x, int y, int t) +{ + uint32_t *index = trace_index[t]; + int min_i = -1; + int min_d = 1000; + int i; + for (i = 0; i < 101; i++) { + int dx = x - CELL_X(index[i]) - OFFSETX; + int dy = y - CELL_Y(index[i]) - OFFSETY; + if (dx < 0) dx = -dx; + if (dy < 0) dy = -dy; + if (dx > 20 && dy > 20) + continue; + int d = dx*dx + dy*dy; + if (d < min_d) { + min_i = i; + } + } + + return min_i; +} + void cell_draw_markers(int m, int n, int w, int h) { diff --git a/ui.c b/ui.c index 7a15619..0660cf5 100644 --- a/ui.c +++ b/ui.c @@ -1152,6 +1152,64 @@ ui_process_lever(void) } } + +void drag_marker(int t, int m) +{ + int status; + /* wait touch release */ + do { + int touch_x, touch_y; + int index; + touch_position(&touch_x, &touch_y); + index = search_nearest_index(touch_x, touch_y, t); + if (index >= 0) { + markers[m].index = index; + redraw_marker(m, TRUE); + } + + status = touch_check(); + } while(status != EVT_TOUCH_RELEASED); +} + +static int +sq_distance(int x0, int y0) +{ + return x0*x0 + y0*y0; +} + +int +touch_pickup_marker(void) +{ + int touch_x, touch_y; + int m, t; + touch_position(&touch_x, &touch_y); + + for (m = 0; m < 4; m++) { + if (!markers[m].enabled) + continue; + + for (t = 0; t < 4; t++) { + int x, y; + if (!trace[t].enabled) + continue; + + marker_position(m, t, &x, &y); + + if (sq_distance(x - touch_x, y - touch_y) < 400) { + active_marker = m; + redraw_marker(active_marker, TRUE); + + drag_marker(t, m); + return TRUE; + } + } + } + + return FALSE; +} + + + void ui_process_touch(void) { awd_count++; @@ -1161,7 +1219,14 @@ void ui_process_touch(void) if (status == EVT_TOUCH_PRESSED) { switch (ui_mode) { case UI_NORMAL: + + if (touch_pickup_marker()) { + break; + } + touch_wait_release(); + + // switch menu mode selection = -1; ui_mode_menu(); break;