diff --git a/CMakeLists.txt b/CMakeLists.txt index 1a3e170..ad9e4c5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,6 +42,7 @@ add_subdirectory(libraries/lvgl) add_executable(${PROJECT_NAME} source/main.c source/lv_ctr_disp.c + source/lv_ctr_indev.c ) target_include_directories(${PROJECT_NAME} PRIVATE diff --git a/include/lv_ctr_indev.h b/include/lv_ctr_indev.h new file mode 100644 index 0000000..d997b3c --- /dev/null +++ b/include/lv_ctr_indev.h @@ -0,0 +1,48 @@ + +/** + * @file lv_ctr_indev.h + * + */ + +#if 1 + +#ifndef LV_CTR_INDEV_H +#define LV_CTR_INDEV_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#if defined(LV_LVGL_H_INCLUDE_SIMPLE) +#include "lvgl.h" +#else +#include "lvgl/lvgl.h" +#endif + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * GLOBAL PROTOTYPES + **********************/ +void lv_ctr_indev_init(void); + +/********************** + * MACROS + **********************/ + +#ifdef __cplusplus +} /*extern "C"*/ +#endif + +#endif /*LV_CTR_INDEV_H*/ + +#endif /*Disable/Enable content*/ diff --git a/source/lv_ctr_indev.c b/source/lv_ctr_indev.c new file mode 100644 index 0000000..9f3791b --- /dev/null +++ b/source/lv_ctr_indev.c @@ -0,0 +1,90 @@ +/** + * @file lv_ctr_indev.c + * + */ + +#if 1 + +/********************* + * INCLUDES + *********************/ +#include "lv_ctr_indev.h" +#include <3ds.h> + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * STATIC PROTOTYPES + **********************/ + +static void touchpad_read(lv_indev_t * indev, lv_indev_data_t * data); + +/********************** + * STATIC VARIABLES + **********************/ +lv_indev_t * indev_touchpad; + +/********************** + * MACROS + **********************/ + +/********************** + * GLOBAL FUNCTIONS + **********************/ + +void lv_ctr_indev_init(void) +{ + /*Register a touch screen input device*/ + indev_touchpad = lv_indev_create(); + lv_indev_set_type(indev_touchpad, LV_INDEV_TYPE_POINTER); + lv_indev_set_read_cb(indev_touchpad, touchpad_read); + + /*Later you should create group(s) with `lv_group_t * group = lv_group_create()`, + *add objects to the group with `lv_group_add_obj(group, obj)` + *and assign this input device to group to navigate in it: + *`lv_indev_set_group(indev_keypad, group);`*/ +} + +/********************** + * STATIC FUNCTIONS + **********************/ + +/*------------------ + * Touchpad + * -----------------*/ + +static void touchpad_read(lv_indev_t * indev_drv, lv_indev_data_t * data) +{ + static int32_t last_x = 0; + static int32_t last_y = 0; + touchPosition touch; + + hidScanInput(); + + /*Save the pressed coordinates and the state*/ + if(hidKeysHeld() & KEY_TOUCH) { + hidTouchRead(&touch); + last_x = touch.px; + last_y = touch.py; + data->state = LV_INDEV_STATE_PRESSED; + } + else { + data->state = LV_INDEV_STATE_RELEASED; + } + + /*Set the last pressed coordinates*/ + data->point.x = last_x; + data->point.y = last_y; +} + +#else /*Enable this file at the top*/ + +/*This dummy typedef exists purely to silence -Wpedantic.*/ +typedef int keep_pedantic_happy; +#endif diff --git a/source/main.c b/source/main.c index 1c2a0e7..682414e 100644 --- a/source/main.c +++ b/source/main.c @@ -4,6 +4,12 @@ #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; long unsigned int sys_tick(void) @@ -23,16 +29,59 @@ void sys_free(void) gfxExit(); } +static void cursor_timer_cb(lv_timer_t * t) +{ + LV_UNUSED(t); + + lv_indev_t * indev = lv_indev_get_next(NULL); + + if (!indev) + return; + + 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); + } +} + int main(int argc, char* argv[]) { sys_init(); lv_init(); lv_ctr_disp_init(); + lv_ctr_indev_init(); lv_tick_set_cb(sys_tick); - lv_obj_t * label = lv_label_create(lv_screen_active()); + label = lv_label_create(lv_screen_active()); lv_label_set_text(label, "Hello world"); lv_obj_center(label); + + /* 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); + + /* Main loop UI update timer */ + lv_timer_create(cursor_timer_cb, 16, NULL); + lv_obj_invalidate(lv_screen_active()); // Main loop