Refactor app to use 'screen with widgets' rather than 'widgets'

This commit is contained in:
Pim van Pelt
2017-11-26 15:01:59 +01:00
parent 3e2e9d4e5b
commit fd68b1ec03
11 changed files with 134 additions and 104 deletions

28
fs/screen_main.json Normal file
View File

@ -0,0 +1,28 @@
{
"name": "Main",
"x": 20,
"y": 0,
"w": 320,
"h": 220,
"widgets": [
{
"name": "one",
"x": 16,
"y": 16,
"w": 48,
"h": 48,
"label": "One",
"type": 0
},
{
"name": "two",
"x": 256,
"y": 16,
"w": 48,
"h": 48,
"label": "Two",
"type": 0
}
]
}

View File

@ -2,6 +2,7 @@
#define __MONGOOSE_TOUCH_H #define __MONGOOSE_TOUCH_H
#include "widget.h" #include "widget.h"
#include "screen.h"
void widget_time_ev(int ev, struct widget_t *w, void *ev_data); void widget_time_ev(int ev, struct widget_t *w, void *ev_data);

View File

@ -24,5 +24,6 @@ struct widget_t *screen_widget_add_from_file(struct screen_t *s, char *fn);
bool screen_widget_destroy(struct screen_t *s, struct widget_t **w); bool screen_widget_destroy(struct screen_t *s, struct widget_t **w);
uint16_t screen_get_num_widgets(struct screen_t *s); uint16_t screen_get_num_widgets(struct screen_t *s);
struct widget_t *screen_widget_find_by_xy(struct screen_t *s, uint16_t x, uint16_t y);
#endif //__SCREEN_H #endif //__SCREEN_H

View File

@ -17,6 +17,7 @@ struct widget_t;
typedef void (*widget_event_fn)(int ev, struct widget_t *w, void *ev_data); typedef void (*widget_event_fn)(int ev, struct widget_t *w, void *ev_data);
struct widget_t { struct widget_t {
char *name;
uint16_t x, y, w, h; uint16_t x, y, w, h;
uint32_t timer_msec; // 0 to disable uint32_t timer_msec; // 0 to disable
@ -32,14 +33,14 @@ struct widget_list_t {
SLIST_ENTRY(widget_list_t) entries; SLIST_ENTRY(widget_list_t) entries;
}; };
struct widget_t *widget_add(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint32_t timer_msec, widget_event_fn handler, void *user_data); /*
struct widget_t *widget_add_from_file(const char *fn, uint32_t timer_msec, widget_event_fn handler, void *user_data);
struct widget_t *widget_find(uint16_t x, uint16_t y); struct widget_t *widget_find(uint16_t x, uint16_t y);
void widget_remove(struct widget_t *widget); void widget_remove(struct widget_t *widget);
*/
struct widget_t *widget_create(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint32_t timer_msec, widget_event_fn handler, void *user_data); struct widget_t *widget_create(char *name, uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint32_t timer_msec, widget_event_fn handler, void *user_data);
struct widget_t *widget_create_from_file(const char *fn);
struct widget_t *widget_create_from_json(const char *json); struct widget_t *widget_create_from_json(const char *json);
struct widget_t *widget_create_from_file(const char *fn);
void widget_destroy(struct widget_t **widget); void widget_destroy(struct widget_t **widget);

View File

@ -6,6 +6,8 @@
#include "stmpe610.h" #include "stmpe610.h"
#include "mongoose-touch.h" #include "mongoose-touch.h"
static struct screen_t *screen = NULL;
static long map(long x, long in_min, long in_max, long out_min, long out_max) { static long map(long x, long in_min, long in_max, long out_min, long out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
} }
@ -19,7 +21,11 @@ static void touch_handler(struct mgos_stmpe610_event_data *ed) {
x = map(ed->x, 0, 4095, 0, _width-1); x = map(ed->x, 0, 4095, 0, _width-1);
y = map(ed->y, 0, 4095, 0, _height-1); y = map(ed->y, 0, 4095, 0, _height-1);
LOG(LL_INFO, ("Touch %s at (%d,%d) pressure=%d, length=%d", ed->direction==TOUCH_UP?"UP":"DOWN", x, y, ed->z, ed->length)); LOG(LL_INFO, ("Touch %s at (%d,%d) pressure=%d, length=%d", ed->direction==TOUCH_UP?"UP":"DOWN", x, y, ed->z, ed->length));
widget = widget_find(x, y);
widget = screen_widget_find_by_xy(screen, x, y);
if (widget) {
LOG(LL_INFO, ("Widget '%s' found", widget->name));
}
if (ed->direction==TOUCH_DOWN) { if (ed->direction==TOUCH_DOWN) {
// mgos_ili9341_drawCircle(x, y, ed->length, ILI9341_YELLOW); // mgos_ili9341_drawCircle(x, y, ed->length, ILI9341_YELLOW);
@ -36,6 +42,8 @@ static void touch_handler(struct mgos_stmpe610_event_data *ed) {
void tft_demo(void) void tft_demo(void)
{ {
struct widget_t *w;
mgos_ili9341_setRotation(mgos_sys_config_get_tft_orientation()); mgos_ili9341_setRotation(mgos_sys_config_get_tft_orientation());
mgos_stmpe610_set_rotation(mgos_sys_config_get_tft_orientation()); mgos_stmpe610_set_rotation(mgos_sys_config_get_tft_orientation());
mgos_ili9341_setGammaCurve(DEFAULT_GAMMA_CURVE); mgos_ili9341_setGammaCurve(DEFAULT_GAMMA_CURVE);
@ -44,11 +52,21 @@ void tft_demo(void)
mgos_ili9341_jpg_image(CENTER, CENTER, 1, "mongoose-os.jpg", NULL, 0); mgos_ili9341_jpg_image(CENTER, CENTER, 1, "mongoose-os.jpg", NULL, 0);
// mgos_ili9341_jpg_image(200, 150, 2, "flower.jpg", NULL, 0); // mgos_ili9341_jpg_image(200, 150, 2, "flower.jpg", NULL, 0);
widget_add(0, 0, 198, 20, 0, widget_name_ev, NULL); screen = screen_create_from_file("fs/screen_main.json");
widget_add(198, 0, 22, 20, 0, widget_network_ev, NULL); w = widget_create("name", 0, 0, 198, 20, 0, widget_name_ev, NULL);
widget_add(220, 0, 20, 20, 5000, widget_wifi_ev, NULL); screen_widget_add(screen, w);
widget_add(240, 0, 80, 20, 1000, widget_time_ev, NULL);
widget_add(0, 21, 320, 2, 0, widget_topbar_ev, NULL); w = widget_create("network", 198, 0, 22, 20, 0, widget_network_ev, NULL);
screen_widget_add(screen, w);
w = widget_create("wifi", 220, 0, 20, 20, 5000, widget_wifi_ev, NULL);
screen_widget_add(screen, w);
w = widget_create("time", 240, 0, 80, 20, 1000, widget_time_ev, NULL);
screen_widget_add(screen, w);
w = widget_create("topbar", 0, 21, 320, 2, 0, widget_topbar_ev, NULL);
screen_widget_add(screen, w);
} }
enum mgos_app_init_result mgos_app_init(void) enum mgos_app_init_result mgos_app_init(void)

View File

@ -75,6 +75,9 @@ void screen_destroy(struct screen_t **s) {
bool screen_widget_add(struct screen_t *s, struct widget_t *w) { bool screen_widget_add(struct screen_t *s, struct widget_t *w) {
struct widget_list_t *wl; struct widget_list_t *wl;
if (!s || !w)
return false;
wl = (struct widget_list_t *) calloc(1, sizeof(*wl)); wl = (struct widget_list_t *) calloc(1, sizeof(*wl));
if (!wl) { if (!wl) {
return false; return false;
@ -119,3 +122,20 @@ uint16_t screen_get_num_widgets(struct screen_t *s) {
return num; return num;
} }
struct widget_t *screen_widget_find_by_xy(struct screen_t *s, uint16_t x, uint16_t y) {
struct widget_list_t *wl;
if (!s)
return NULL;
SLIST_FOREACH(wl, &s->widget_entries, entries) {
LOG(LL_INFO, ("Inspecing widget '%s' (x=%d,y=%d,w=%d,h=%d)", wl->widget->name, wl->widget->x, wl->widget->y, wl->widget->w, wl->widget->h));
if (wl->widget->x <= x &&
x < (wl->widget->x+wl->widget->w) &&
wl->widget->y <= y &&
y < (wl->widget->y+wl->widget->h))
return wl->widget;
}
return NULL;
}

View File

@ -3,8 +3,6 @@
#include "frozen/frozen.h" #include "frozen/frozen.h"
#include "common/queue.h" #include "common/queue.h"
SLIST_HEAD(widget_list, widget_list_t) s_widgets;
static void widget_event_timer(void *arg) { static void widget_event_timer(void *arg) {
struct widget_t *widget = (struct widget_t *) arg; struct widget_t *widget = (struct widget_t *) arg;
if (!widget) if (!widget)
@ -27,92 +25,14 @@ void widget_destroy(struct widget_t **widget) {
*widget=NULL; *widget=NULL;
} }
struct widget_t *widget_add(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint32_t timer_msec, widget_event_fn handler, void *user_data) { struct widget_t *widget_create(char *name, uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint32_t timer_msec, widget_event_fn handler, void *user_data) {
struct widget_t *widget;
struct widget_list_t *wl;
wl = (struct widget_list_t *) calloc(1, sizeof(*wl));
if (!wl) {
return NULL;
}
widget = widget_create(x, y, w, h, timer_msec, handler, user_data);
if (!widget) {
free(wl);
return NULL;
}
wl->widget = widget;
SLIST_INSERT_HEAD(&s_widgets, wl, entries);
return widget;
}
struct widget_t *widget_find(uint16_t x, uint16_t y) {
struct widget_list_t *wl;
SLIST_FOREACH(wl, &s_widgets, entries) {
if (wl->widget->x < x &&
x < (wl->widget->x+wl->widget->w) &&
wl->widget->y < y &&
y < (wl->widget->y+wl->widget->h))
return wl->widget;
}
return NULL;
}
void widget_remove(struct widget_t *widget) {
struct widget_list_t *wl;
if (!widget)
return;
SLIST_FOREACH(wl, &s_widgets, entries) {
if (wl->widget == widget) {
SLIST_REMOVE(&s_widgets, wl, widget_list_t, entries);
widget_destroy(&widget);
}
}
}
struct widget_t *widget_add_from_file(const char *fn, uint32_t timer_msec, widget_event_fn handler, void *user_data) {
char *json;
int x, y, w, h;
int type = 0;
char *label = NULL;
char *icon = NULL;
struct widget_t *widget=NULL;
json = json_fread(fn);
if (!json) {
LOG(LL_ERROR, ("%s: Could not json_fread()", fn));
widget=NULL; goto exit;
}
if (json_scanf(json, strlen(json), "{x:%d,y:%d,w:%d,h:%d}", &x, &y, &w, &h) != 4) {
LOG(LL_ERROR, ("%s: Incomplete JSON: require 'x', 'y', 'w', 'h' fields", fn));
widget=NULL; goto exit;
}
json_scanf(json, strlen(json), "{type:%d,label:%Q,icon:%Q}", &type, &label, &icon);
free(json);
LOG(LL_INFO, ("%s: x=%d y=%d w=%d h=%d type=%d label='%s' icon='%s'", fn, x, y, w, h, type, label?label:"(empty)",icon?icon:"(empty)"));
widget = widget_add(x, y, w, h, 0, NULL, NULL);
exit:
if (label) free(label);
if (icon) free(icon);
return widget;
(void) timer_msec;
(void) handler;
(void) user_data;
}
struct widget_t *widget_create(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint32_t timer_msec, widget_event_fn handler, void *user_data) {
struct widget_t *widget; struct widget_t *widget;
widget = (struct widget_t *) calloc(1, sizeof(*widget)); widget = (struct widget_t *) calloc(1, sizeof(*widget));
if (!widget) if (!widget)
return NULL; return NULL;
if (name)
widget->name=strdup(name);
widget->x=x; widget->x=x;
widget->y=y; widget->y=y;
widget->w=w; widget->w=w;
@ -133,15 +53,17 @@ struct widget_t *widget_create_from_json(const char *json) {
struct widget_t *widget=NULL; struct widget_t *widget=NULL;
int x, y, w, h; int x, y, w, h;
int type = 0; int type = 0;
char *name = NULL;
char *label = NULL; char *label = NULL;
char *icon = NULL; char *icon = NULL;
if (json_scanf(json, strlen(json), "{x:%d,y:%d,w:%d,h:%d}", &x, &y, &w, &h) != 4) { if (json_scanf(json, strlen(json), "{name:%Q,x:%d,y:%d,w:%d,h:%d}", &name, &x, &y, &w, &h) != 5) {
LOG(LL_ERROR, ("Incomplete JSON: require 'x', 'y', 'w', 'h' fields")); LOG(LL_ERROR, ("Incomplete JSON: require 'x', 'y', 'w', 'h' and 'name' fields"));
return NULL; return NULL;
} }
json_scanf(json, strlen(json), "{type:%d,label:%Q,icon:%Q}", &type, &label, &icon); json_scanf(json, strlen(json), "{type:%d,label:%Q,icon:%Q}", &type, &label, &icon);
widget = widget_create(x, y, w, h, 0, NULL, NULL); widget = widget_create(name, x, y, w, h, 0, NULL, NULL);
if (name) free(name);
return widget; return widget;
} }

View File

@ -7,6 +7,7 @@
"widgets": [ "widgets": [
{ {
"name": "one",
"x": 16, "x": 16,
"y": 16, "y": 16,
"w": 48, "w": 48,
@ -15,6 +16,7 @@
"type": 0 "type": 0
}, },
{ {
"name": "two",
"x": 256, "x": 256,
"y": 16, "y": 16,
"w": 48, "w": 48,

View File

@ -1,4 +1,5 @@
{ {
"name": "one",
"x": 16, "x": 16,
"y": 16, "y": 16,
"w": 48, "w": 48,

View File

@ -25,7 +25,7 @@ int log_print_prefix(enum cs_log_level l, const char *func, const char *file) {
default: // LL_NONE default: // LL_NONE
return 0; return 0;
} }
printf ("%-5s %-15s %-15s| ", ll_str, file, func); printf ("%-5s %-15s %-40s| ", ll_str, file, func);
return 1; return 1;
} }

View File

@ -3,7 +3,7 @@
#include "widget.h" #include "widget.h"
static void test_widget_add_and_remove(struct screen_t *s, const char *fn) { static void test_screen_widget_add_and_remove(struct screen_t *s, const char *fn) {
struct widget_t *w1; struct widget_t *w1;
int num_widgets_before, num_widgets; int num_widgets_before, num_widgets;
bool ret; bool ret;
@ -27,11 +27,45 @@ static void test_widget_add_and_remove(struct screen_t *s, const char *fn) {
ASSERT(num_widgets == num_widgets_before, "Too many widgets in screen"); ASSERT(num_widgets == num_widgets_before, "Too many widgets in screen");
return; return;
(void) s;
(void) fn;
} }
static void test_screen_widget_find(struct screen_t *s) {
struct widget_t *w;
uint16_t x, y;
x=16; y=16;
LOG(LL_INFO, ("want to find widget at (%d,%d)", x, y));
w = screen_widget_find_by_xy(s, x, y);
ASSERT(w, "widget not found");
x=256; y=16;
LOG(LL_INFO, ("want to find widget at (%d,%d)", x, y));
w = screen_widget_find_by_xy(s, x, y);
ASSERT(w, "widget not found");
x=256+47; y=16+47;
LOG(LL_INFO, ("want to find widget at (%d,%d)", x, y));
w = screen_widget_find_by_xy(s, x, y);
ASSERT(w, "widget not found");
x=16+47; y=16+47;
LOG(LL_INFO, ("want to find widget at (%d,%d)", x, y));
w = screen_widget_find_by_xy(s, x, y);
ASSERT(w, "widget not found");
x=15; y=16;
LOG(LL_INFO, ("do not want to find widget at (%d,%d)", x, y));
w = screen_widget_find_by_xy(s, x, y);
ASSERT(!w, "widget found");
x=256+48; y=16+48;
LOG(LL_INFO, ("do not want to find widget at (%d,%d)", x, y));
w = screen_widget_find_by_xy(s, x, y);
ASSERT(!w, "widget found");
(void) s;
}
int test_screen() { int test_screen() {
struct screen_t *s = NULL; struct screen_t *s = NULL;
@ -46,8 +80,8 @@ int test_screen() {
LOG(LL_INFO, ("Screen has %d widget(s)", num_widgets)); LOG(LL_INFO, ("Screen has %d widget(s)", num_widgets));
ASSERT(num_widgets == 2, "Expecting 2 widgets in screen"); ASSERT(num_widgets == 2, "Expecting 2 widgets in screen");
LOG(LL_INFO, ("test_widget_add_and_remove(data/TestWidget.json)")); LOG(LL_INFO, ("test_screen_widget_add_and_remove(data/TestWidget.json)"));
test_widget_add_and_remove(s, "data/TestWidget.json"); test_screen_widget_add_and_remove(s, "data/TestWidget.json");
num_widgets = screen_get_num_widgets(s); num_widgets = screen_get_num_widgets(s);
LOG(LL_INFO, ("Screen has %d widget(s)", num_widgets)); LOG(LL_INFO, ("Screen has %d widget(s)", num_widgets));
@ -69,6 +103,8 @@ int test_screen() {
LOG(LL_INFO, ("Screen has %d widget(s)", num_widgets)); LOG(LL_INFO, ("Screen has %d widget(s)", num_widgets));
ASSERT(num_widgets == 2, "Expecting 2 widgets in screen"); ASSERT(num_widgets == 2, "Expecting 2 widgets in screen");
LOG(LL_INFO, ("test_screen_widget_find()"));
test_screen_widget_find(s);
LOG(LL_INFO, ("screen_destroy()")); LOG(LL_INFO, ("screen_destroy()"));
screen_destroy(&s); screen_destroy(&s);