Skip to content

Commit

Permalink
Full touch support
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillem96 committed Jan 23, 2019
1 parent c250cfb commit dc21a8a
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 83 deletions.
2 changes: 1 addition & 1 deletion include/menu/gui/gui_menu_entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ gui_menu_entry_t *gui_create_menu_entry_no_bitmap(const char *text,
u32 width, u32 height,
int (*handler)(void *), void *param);
/* Renders a gfx menu entry */
void gui_menu_render_entry(gui_menu_entry_t*, bool);
void gui_menu_render_entry(gui_menu_entry_t*);

/* Destroy menu entry */
void gui_menu_entry_destroy(gui_menu_entry_t*);
Expand Down
3 changes: 0 additions & 3 deletions include/utils/touch.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,11 @@ typedef struct {
u16 y;
} touch_event_t;

/* Set to true or create an empty file called "touch" inside "argon" dir to enable touch support */
bool g_touch_enabled;

/* Init touch support */
int touch_power_on();

/* Wait for touch input */
/* No safe to call it when g_touch_enabled = false */
touch_event_t touch_wait();

/**
Expand Down
2 changes: 1 addition & 1 deletion src/core/custom-gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,6 @@ int screenshot(void* params)

g_gfx_con.scale = 2;
gfx_con_setpos(&g_gfx_con, 0, 665);
gfx_printf(&g_gfx_con, " Screenshot saved!\n Found it at argon/screenshot.bmp");
gfx_printf(&g_gfx_con, " Screenshot saved!\n Find it at argon/screenshot.bmp");
return 0;
}
2 changes: 0 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ void ipl_main()
g_gfx_con.mute = 0;

/* Cofigure touch input */
/* If touch file exists enable touch support*/
g_touch_enabled = true;
touch_power_on();

/* Mount Sd card and launch payload */
Expand Down
70 changes: 18 additions & 52 deletions src/menu/gui/gui_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,17 @@
#define MINOR_VERSION 2
#define MAJOR_VERSION 0

bool g_first_render = true;

/* Render the menu */
static void gui_menu_render_menu(gui_menu_t*);
static void gui_menu_draw_background(gui_menu_t*);
static void gui_menu_draw_entries(gui_menu_t*);

/* Update menu after an input */
static int gui_menu_update(gui_menu_t*);

/* Handle inputs input */
/* Handle input */
static int handle_touch_input(gui_menu_t*);
static int handle_btn_input(gui_menu_t*);

gui_menu_t *gui_menu_create(const char *title)
{
Expand Down Expand Up @@ -80,38 +79,29 @@ static void gui_menu_draw_background(gui_menu_t* menu)
}
}

static void gui_menu_render_menu(gui_menu_t* menu)
{
gui_menu_draw_background(menu);
gui_menu_draw_entries(menu);
gfx_swap_buffer(&g_gfx_ctxt);
}

static void gui_menu_draw_entries(gui_menu_t *menu)
{
for (s16 i = 0; i < menu->next_entry; i++)
gui_menu_render_entry(menu->entries[i], i == menu->selected_index);
gui_menu_render_entry(menu->entries[i]);
}

static bool first_render = true;

static int gui_menu_update(gui_menu_t *menu)
{
u32 res = 0;

gui_menu_draw_background(menu);

if (first_render)
{
/* When first render, we must render all because input is blocking */
gui_menu_draw_entries(menu);
gfx_swap_buffer(&g_gfx_ctxt);
}
gui_menu_draw_entries(menu);

if (!g_touch_enabled)
res = handle_btn_input(menu);
else
res = handle_touch_input(menu);
res = handle_touch_input(menu);

if (first_render) /* Force render background because on first render we flushed buffers before input */
{
gui_menu_draw_background(menu);
first_render = false;
}
/* Draw entries after handling input */
gui_menu_draw_entries(menu);
gfx_swap_buffer(&g_gfx_ctxt);

return res;
Expand All @@ -120,6 +110,11 @@ static int gui_menu_update(gui_menu_t *menu)
int gui_menu_open(gui_menu_t *menu)
{
gfx_con_setcol(&g_gfx_con, 0xFFF9F9F9, 0, 0xFF191414);
/*
* Render and flush at first render because blocking input won't allow us
* flush buffers
*/
gui_menu_render_menu(menu);

while (gui_menu_update(menu))
;
Expand Down Expand Up @@ -157,32 +152,3 @@ static int handle_touch_input(gui_menu_t *menu)

return 1;
}


static int handle_btn_input(gui_menu_t *menu)
{
gui_menu_entry_t *entry = NULL;
u32 input;

input = btn_wait();

if ((input & BTN_VOL_DOWN) && menu->selected_index > 0)
{
menu->selected_index--;
}
else if ((input & BTN_VOL_UP) && menu->selected_index < menu->next_entry - 1)
{
menu->selected_index++;
}
else if (input & BTN_POWER)
{
entry = menu->entries[menu->selected_index];
if (entry->handler != NULL)
{
if (entry->handler(entry->param) != 0)
return 0;
}
}

return 1;
}
18 changes: 7 additions & 11 deletions src/menu/gui/gui_menu_entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ gui_menu_entry_t *gui_create_menu_entry(const char *text,

if (bitmap != NULL)
{
/* When not using the default icon set the text empty */
/* User knows which icon he uses for each payload */
menu_entry->bitmap = bitmap;
if (g_touch_enabled)
strcpy(menu_entry->text, ""); // If not default icon, text is not needed on touch input
strcpy(menu_entry->text, ""); // If not default icon, text is not needed on touch input
}
else
menu_entry->bitmap = sd_file_read(DEFAULT_LOGO);
Expand Down Expand Up @@ -80,33 +81,28 @@ static u32 get_text_width(char *text)
return lenght * g_gfx_con.scale * (u32)CHAR_WIDTH;
}

static void render_text_centered(gui_menu_entry_t *entry, char *text, bool selected)
static void render_text_centered(gui_menu_entry_t *entry, char *text)
{
g_gfx_con.scale = 2;

/* Set text below the logo and centered */
s32 x_offset = -(get_text_width(text) - entry->width) / 2;
u32 y_offset = entry->bitmap != NULL ? entry->height + 20 : 0;

u32 prevColor = g_gfx_con.fgcol;

g_gfx_con.scale = 2;
gfx_con_setpos(&g_gfx_con, entry->x + x_offset, entry->y + y_offset);

if (selected && !g_touch_enabled) /* If touch is enabled there s no selected text */
gfx_printf(&g_gfx_con, "%k%s%k", 0xFF1971FF, entry->text, prevColor);
else
gfx_printf(&g_gfx_con, "%s", entry->text);
gfx_printf(&g_gfx_con, "%s", entry->text);
}

/* Renders a gfx menu entry */
void gui_menu_render_entry(gui_menu_entry_t* entry, bool selected)
void gui_menu_render_entry(gui_menu_entry_t* entry)
{
gfx_render_bmp_arg_bitmap(&g_gfx_ctxt, entry->bitmap,
entry->x, entry->y,
entry->width, entry->height);

render_text_centered(entry, entry->text, selected);
render_text_centered(entry, entry->text);
}

void gui_menu_entry_destroy(gui_menu_entry_t *entry)
Expand Down
20 changes: 7 additions & 13 deletions src/utils/touch.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,19 @@ static void touch_poll(touch_event_t *event)
touch_event_t touch_wait()
{
touch_event_t event;
if (g_touch_enabled)
{
do
{
touch_poll(&event);
} while(event.type != STMFTS_EV_MULTI_TOUCH_ENTER);
}

do
{
touch_poll(&event);
} while(event.type != STMFTS_EV_MULTI_TOUCH_ENTER);

return event;
}

int touch_power_on()
{
int err;

/* Avoid switch freeze if touch support is not enabled */
if (!g_touch_enabled)
{
return -1;
}
/*
* The datasheet does not specify the power on time, but considering
* that the reset time is < 10ms, I sleep 20ms to be sure
Expand Down Expand Up @@ -118,7 +112,7 @@ int touch_power_on()

bool is_rect_touched(touch_event_t* event, u32 x, u32 y, u32 width, u32 height)
{
if (!g_touch_enabled || event == NULL)
if (event == NULL)
return false;

u32 event_x = event->y;
Expand Down

0 comments on commit dc21a8a

Please sign in to comment.