233 lines
5.6 KiB
C
233 lines
5.6 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <3ds.h>
|
|
#include "lvgl.h"
|
|
#include "lv_ctr_disp.h"
|
|
#include "lv_ctr_indev.h"
|
|
|
|
|
|
static lv_obj_t * cursor;
|
|
static lv_obj_t * label;
|
|
static lv_obj_t * label2;
|
|
static lv_obj_t * fps_label;
|
|
static lv_obj_t * keypad_label;
|
|
|
|
|
|
long unsigned int sys_tick(void)
|
|
{
|
|
long unsigned int tick = (long unsigned int)osGetTime();
|
|
return tick;
|
|
}
|
|
|
|
void sys_init(void)
|
|
{
|
|
gfxInitDefault();
|
|
}
|
|
|
|
void sys_free(void)
|
|
{
|
|
gfxExit();
|
|
}
|
|
|
|
void sys_log(lv_log_level_t level, const char * buf)
|
|
{
|
|
(level);
|
|
svcOutputDebugString(buf, strlen(buf)-1);
|
|
}
|
|
|
|
static void ui_timer_cb(lv_timer_t * t)
|
|
{
|
|
LV_UNUSED(t);
|
|
|
|
lv_indev_t * indev = lv_indev_get_next(NULL);
|
|
|
|
while (indev) {
|
|
if (lv_indev_get_type(indev) == LV_INDEV_TYPE_POINTER) {
|
|
lv_point_t p;
|
|
lv_indev_get_point(indev, &p);
|
|
|
|
if (lv_indev_get_state(indev) == LV_INDEV_STATE_PRESSED) {
|
|
lv_obj_set_pos(cursor, p.x - 5, p.y - 5);
|
|
lv_obj_clear_flag(cursor, LV_OBJ_FLAG_HIDDEN);
|
|
|
|
lv_label_set_text_fmt(label2,
|
|
"x: %d\ny: %d",
|
|
p.x,
|
|
p.y);
|
|
}
|
|
else {
|
|
lv_obj_add_flag(cursor, LV_OBJ_FLAG_HIDDEN);
|
|
}
|
|
}
|
|
if (lv_indev_get_type(indev) == LV_INDEV_TYPE_KEYPAD) {
|
|
uint32_t key = lv_indev_get_key(indev);
|
|
uint32_t state = lv_indev_get_state(indev);
|
|
const char * txt = "None";
|
|
|
|
switch(key) {
|
|
case LV_KEY_UP:
|
|
txt = "UP";
|
|
break;
|
|
case LV_KEY_DOWN:
|
|
txt = "DOWN";
|
|
break;
|
|
case LV_KEY_LEFT:
|
|
txt = "LEFT";
|
|
break;
|
|
case LV_KEY_RIGHT:
|
|
txt = "RIGHT";
|
|
break;
|
|
case LV_KEY_ENTER:
|
|
txt = "A";
|
|
break;
|
|
case LV_KEY_ESC:
|
|
txt = "B";
|
|
break;
|
|
case LV_KEY_PREV:
|
|
txt = "L";
|
|
break;
|
|
case LV_KEY_NEXT:
|
|
txt = "R";
|
|
break;
|
|
default:
|
|
txt = "None";
|
|
break;
|
|
}
|
|
|
|
if (state == LV_INDEV_STATE_PRESSED)
|
|
lv_label_set_text_fmt(keypad_label, "Pressed: %s", txt);
|
|
else
|
|
lv_label_set_text_fmt(keypad_label, "Released", txt);
|
|
}
|
|
indev = lv_indev_get_next(indev);
|
|
}
|
|
}
|
|
|
|
static void switch_event_cb(lv_event_t * e)
|
|
{
|
|
lv_obj_t * sw = lv_event_get_target(e);
|
|
|
|
if (lv_obj_has_state(sw, LV_STATE_CHECKED))
|
|
lv_label_set_text_fmt(label, "ON");
|
|
else
|
|
lv_label_set_text_fmt(label, "OFF");
|
|
}
|
|
|
|
static void fps_tick(void)
|
|
{
|
|
static u32 frames = 0;
|
|
static u64 last = 0;
|
|
|
|
u64 now = osGetTime();
|
|
|
|
frames++;
|
|
|
|
if (now - last >= 1000) {
|
|
|
|
lv_label_set_text_fmt(fps_label,
|
|
"FPS: %lu",
|
|
frames);
|
|
|
|
frames = 0;
|
|
last = now;
|
|
}
|
|
}
|
|
|
|
static void ui_prepare(void)
|
|
{
|
|
/* Container START */
|
|
lv_obj_t * cont = lv_obj_create(lv_screen_active());
|
|
|
|
/* Container layout */
|
|
lv_obj_set_size(cont, 220, 120);
|
|
lv_obj_center(cont);
|
|
|
|
lv_obj_set_layout(cont, LV_LAYOUT_FLEX);
|
|
lv_obj_set_flex_flow(cont, LV_FLEX_FLOW_COLUMN);
|
|
lv_obj_set_flex_align(cont,
|
|
LV_FLEX_ALIGN_CENTER,
|
|
LV_FLEX_ALIGN_CENTER,
|
|
LV_FLEX_ALIGN_CENTER);
|
|
|
|
/* Optional cleaner look */
|
|
lv_obj_set_style_pad_all(cont, 16, 0);
|
|
lv_obj_set_style_pad_row(cont, 12, 0);
|
|
|
|
/* Label */
|
|
label = lv_label_create(cont);
|
|
lv_label_set_text(label, "Hello world");
|
|
|
|
/* Switch */
|
|
lv_obj_t * sw = lv_switch_create(cont);
|
|
lv_obj_center(sw);
|
|
lv_obj_add_event_cb(sw,
|
|
switch_event_cb,
|
|
LV_EVENT_VALUE_CHANGED,
|
|
NULL);
|
|
/* Container END */
|
|
|
|
/* Cursor */
|
|
cursor = lv_obj_create(lv_screen_active());
|
|
lv_obj_set_size(cursor, 10, 10);
|
|
lv_obj_set_style_radius(cursor, LV_RADIUS_CIRCLE, 0);
|
|
lv_obj_set_style_bg_color(cursor, lv_palette_main(LV_PALETTE_RED), 0);
|
|
lv_obj_set_style_border_width(cursor, 0, 0);
|
|
|
|
/* Coord label */
|
|
label2 = lv_label_create(lv_screen_active());
|
|
lv_obj_align(label2, LV_ALIGN_TOP_LEFT, 4, 4);
|
|
|
|
/* Keypad label */
|
|
keypad_label = lv_label_create(lv_screen_active());
|
|
lv_obj_align(keypad_label, LV_ALIGN_TOP_LEFT, 4, 36);
|
|
|
|
/* FPS label */
|
|
fps_label = lv_label_create(lv_screen_active());
|
|
lv_obj_align(fps_label,
|
|
LV_ALIGN_BOTTOM_LEFT,
|
|
4,
|
|
-4);
|
|
|
|
/* Main loop UI update timer */
|
|
lv_timer_create(ui_timer_cb, 16, NULL);
|
|
|
|
/* Dummy group to make indev happy */
|
|
lv_group_t * group = lv_group_create();
|
|
lv_indev_set_group(lv_ctr_indev_get_keypad(), group);
|
|
|
|
lv_obj_invalidate(lv_screen_active());
|
|
}
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
sys_init();
|
|
lv_init();
|
|
lv_log_register_print_cb(sys_log);
|
|
lv_tick_set_cb(sys_tick);
|
|
lv_ctr_disp_init();
|
|
lv_ctr_indev_init();
|
|
|
|
ui_prepare();
|
|
|
|
// Main loop
|
|
while (aptMainLoop())
|
|
{
|
|
// scan HID inputs
|
|
hidScanInput();
|
|
|
|
// call LVGL timer handler
|
|
u64 start = osGetTime();
|
|
lv_timer_handler();
|
|
u64 end = osGetTime();
|
|
lv_label_set_text_fmt(fps_label, "render: %llums", end - start);
|
|
|
|
// evacuate the dance floor!
|
|
u32 kDown = hidKeysDown();
|
|
if (kDown & KEY_START)
|
|
break; // break in order to return to hbmenu
|
|
}
|
|
|
|
sys_free();
|
|
return 0;
|
|
}
|