2018-01-16 22:35:55 +01:00
|
|
|
/****************************************************************************
|
2021-06-16 09:22:16 +02:00
|
|
|
* apps/examples/touchscreen/tp_cal.c
|
2018-01-16 22:35:55 +01:00
|
|
|
*
|
|
|
|
* Copyright (C) 2018 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gábor Kiss-Vámosi <kisvegabor@gmail.com>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2020-07-15 14:33:00 +02:00
|
|
|
#include <lvgl/lvgl.h>
|
2018-01-16 22:35:55 +01:00
|
|
|
#include "tp.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define CIRCLE_SIZE 20
|
|
|
|
#define TP_MAX_VALUE 5000
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Type Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
enum tp_cal_state_e
|
|
|
|
{
|
|
|
|
TP_CAL_STATE_INIT,
|
|
|
|
TP_CAL_STATE_WAIT_TOP_LEFT,
|
|
|
|
TP_CAL_STATE_WAIT_TOP_RIGHT,
|
|
|
|
TP_CAL_STATE_WAIT_BOTTOM_RIGHT,
|
|
|
|
TP_CAL_STATE_WAIT_BOTTOM_LEFT,
|
|
|
|
TP_CAL_STATE_WAIT_LEAVE,
|
|
|
|
TP_CAL_STATE_READY,
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-07 03:30:55 +02:00
|
|
|
static void btn_click_action(FAR lv_obj_t *scr, lv_event_t event);
|
2018-01-16 22:35:55 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static enum tp_cal_state_e state;
|
|
|
|
static lv_point_t p[4];
|
2020-06-17 10:50:05 +02:00
|
|
|
static lv_obj_t *prev_scr;
|
|
|
|
static lv_obj_t *big_btn;
|
|
|
|
static lv_obj_t *label_main;
|
|
|
|
static lv_obj_t *circ_area;
|
|
|
|
static lv_theme_t *prev_theme;
|
2018-01-16 22:35:55 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: btn_click_action
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* scr
|
2020-04-07 03:30:55 +02:00
|
|
|
* event
|
2018-01-16 22:35:55 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* ?
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-07 03:30:55 +02:00
|
|
|
static void btn_click_action(FAR lv_obj_t *scr, lv_event_t event)
|
2018-01-16 22:35:55 +01:00
|
|
|
{
|
2020-04-07 03:30:55 +02:00
|
|
|
if (event == LV_EVENT_CLICKED)
|
2018-01-16 22:35:55 +01:00
|
|
|
{
|
2020-04-07 03:30:55 +02:00
|
|
|
if (state == TP_CAL_STATE_WAIT_TOP_LEFT)
|
|
|
|
{
|
|
|
|
lv_indev_t *indev = lv_indev_get_act();
|
|
|
|
char buf[64];
|
|
|
|
#if LV_USE_ANIMATION
|
|
|
|
lv_anim_t a;
|
2019-04-08 14:57:42 +02:00
|
|
|
#endif
|
2018-01-16 22:35:55 +01:00
|
|
|
|
2020-04-07 03:30:55 +02:00
|
|
|
lv_indev_get_point(indev, &p[0]);
|
|
|
|
|
|
|
|
sprintf(buf, "x: %d\ny: %d", p[0].x, p[0].y);
|
|
|
|
lv_obj_t *label_coord = lv_label_create(lv_scr_act(), NULL);
|
|
|
|
lv_label_set_text(label_coord, buf);
|
|
|
|
|
|
|
|
lv_label_set_text(label_main, "Click the circle in\n"
|
|
|
|
"upper right-hand corner");
|
|
|
|
|
|
|
|
lv_obj_set_pos(label_main,
|
|
|
|
(LV_HOR_RES - lv_obj_get_width(label_main)) / 2,
|
|
|
|
(LV_VER_RES - lv_obj_get_height(label_main)) / 2);
|
|
|
|
|
|
|
|
#if LV_USE_ANIMATION
|
2020-06-16 17:24:56 +02:00
|
|
|
lv_anim_init(&a);
|
|
|
|
lv_anim_set_var(&a, circ_area);
|
|
|
|
lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_x);
|
|
|
|
lv_anim_set_time(&a, 500);
|
|
|
|
lv_anim_set_values(&a, 0, LV_HOR_RES - CIRCLE_SIZE);
|
|
|
|
lv_anim_set_delay(&a, 200);
|
|
|
|
lv_anim_start(&a);
|
2019-04-08 14:57:42 +02:00
|
|
|
#else
|
2020-04-07 03:30:55 +02:00
|
|
|
lv_obj_set_pos(circ_area, LV_HOR_RES - CIRCLE_SIZE, 0);
|
2019-04-08 14:57:42 +02:00
|
|
|
#endif
|
2018-01-16 22:35:55 +01:00
|
|
|
|
2020-04-07 03:30:55 +02:00
|
|
|
state = TP_CAL_STATE_WAIT_TOP_RIGHT;
|
|
|
|
}
|
|
|
|
else if (state == TP_CAL_STATE_WAIT_TOP_RIGHT)
|
|
|
|
{
|
|
|
|
lv_indev_t *indev = lv_indev_get_act();
|
|
|
|
char buf[64];
|
2019-04-08 14:57:42 +02:00
|
|
|
|
2020-04-07 03:30:55 +02:00
|
|
|
#if LV_USE_ANIMATION
|
|
|
|
lv_anim_t a;
|
2019-04-08 14:57:42 +02:00
|
|
|
#endif
|
2018-01-16 22:35:55 +01:00
|
|
|
|
2020-04-07 03:30:55 +02:00
|
|
|
lv_indev_get_point(indev, &p[1]);
|
|
|
|
|
|
|
|
sprintf(buf, "x: %d\ny: %d", p[1].x, p[1].y);
|
|
|
|
lv_obj_t *label_coord = lv_label_create(lv_scr_act(), NULL);
|
|
|
|
lv_label_set_text(label_coord, buf);
|
|
|
|
lv_obj_set_pos(label_coord,
|
|
|
|
LV_HOR_RES - lv_obj_get_width(label_coord),
|
|
|
|
0);
|
|
|
|
|
|
|
|
lv_label_set_text(label_main, "Click the circle in\n"
|
|
|
|
"lower right-hand corner");
|
|
|
|
|
|
|
|
lv_obj_set_pos(label_main,
|
|
|
|
(LV_HOR_RES - lv_obj_get_width(label_main)) / 2,
|
|
|
|
(LV_VER_RES - lv_obj_get_height(label_main)) / 2);
|
|
|
|
|
|
|
|
#if LV_USE_ANIMATION
|
2020-06-16 17:24:56 +02:00
|
|
|
lv_anim_init(&a);
|
|
|
|
lv_anim_set_var(&a, circ_area);
|
|
|
|
lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_y);
|
2020-06-17 10:50:05 +02:00
|
|
|
lv_anim_set_time(&a, 500);
|
2020-06-16 17:24:56 +02:00
|
|
|
lv_anim_set_values(&a, 0, LV_VER_RES - CIRCLE_SIZE);
|
2020-06-17 10:50:05 +02:00
|
|
|
lv_anim_set_delay(&a, 200);
|
2020-06-16 17:24:56 +02:00
|
|
|
lv_anim_start(&a);
|
2019-04-08 14:57:42 +02:00
|
|
|
#else
|
2020-04-07 03:30:55 +02:00
|
|
|
lv_obj_set_pos(circ_area,
|
|
|
|
LV_HOR_RES - CIRCLE_SIZE, LV_VER_RES - CIRCLE_SIZE);
|
2019-04-08 14:57:42 +02:00
|
|
|
#endif
|
2018-01-16 22:35:55 +01:00
|
|
|
|
2020-04-07 03:30:55 +02:00
|
|
|
state = TP_CAL_STATE_WAIT_BOTTOM_RIGHT;
|
|
|
|
}
|
|
|
|
else if (state == TP_CAL_STATE_WAIT_BOTTOM_RIGHT)
|
|
|
|
{
|
|
|
|
lv_indev_t *indev = lv_indev_get_act();
|
|
|
|
char buf[64];
|
|
|
|
#if LV_USE_ANIMATION
|
|
|
|
lv_anim_t a;
|
2019-04-08 14:57:42 +02:00
|
|
|
#endif
|
2020-04-07 03:30:55 +02:00
|
|
|
lv_indev_get_point(indev, &p[2]);
|
|
|
|
|
|
|
|
sprintf(buf, "x: %d\ny: %d", p[2].x, p[2].y);
|
|
|
|
lv_obj_t *label_coord = lv_label_create(lv_scr_act(), NULL);
|
|
|
|
lv_label_set_text(label_coord, buf);
|
|
|
|
lv_obj_set_pos(label_coord,
|
|
|
|
LV_HOR_RES - lv_obj_get_width(label_coord),
|
|
|
|
LV_VER_RES - lv_obj_get_height(label_coord));
|
|
|
|
|
|
|
|
lv_label_set_text(label_main, "Click the circle in\n"
|
|
|
|
"lower left-hand corner");
|
|
|
|
|
|
|
|
lv_obj_set_pos(label_main,
|
|
|
|
(LV_HOR_RES - lv_obj_get_width(label_main)) / 2,
|
|
|
|
(LV_VER_RES - lv_obj_get_height(label_main)) / 2);
|
|
|
|
|
|
|
|
#if LV_USE_ANIMATION
|
2020-06-16 17:24:56 +02:00
|
|
|
lv_anim_init(&a);
|
|
|
|
lv_anim_set_var(&a, circ_area);
|
|
|
|
lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_x);
|
|
|
|
lv_anim_set_time(&a, 500);
|
|
|
|
lv_anim_set_values(&a, LV_HOR_RES - CIRCLE_SIZE, 0);
|
|
|
|
lv_anim_set_delay(&a, 200);
|
|
|
|
lv_anim_start(&a);
|
2019-04-08 14:57:42 +02:00
|
|
|
#else
|
2020-04-07 03:30:55 +02:00
|
|
|
lv_obj_set_pos(circ_area, 0, LV_VER_RES - CIRCLE_SIZE);
|
2019-04-08 14:57:42 +02:00
|
|
|
#endif
|
2018-01-16 22:35:55 +01:00
|
|
|
|
2020-04-07 03:30:55 +02:00
|
|
|
state = TP_CAL_STATE_WAIT_BOTTOM_LEFT;
|
|
|
|
}
|
|
|
|
else if (state == TP_CAL_STATE_WAIT_BOTTOM_LEFT)
|
|
|
|
{
|
|
|
|
lv_indev_t *indev = lv_indev_get_act();
|
|
|
|
char buf[64];
|
|
|
|
|
|
|
|
lv_indev_get_point(indev, &p[3]);
|
|
|
|
|
|
|
|
lv_label_set_text(label_main, "Click the screen\n"
|
|
|
|
"to leave calibration");
|
|
|
|
|
|
|
|
lv_obj_set_pos(label_main,
|
|
|
|
(LV_HOR_RES - lv_obj_get_width(label_main)) / 2,
|
|
|
|
(LV_VER_RES - lv_obj_get_height(label_main)) / 2);
|
|
|
|
|
|
|
|
sprintf(buf, "x: %d\ny: %d", p[3].x, p[3].y);
|
|
|
|
lv_obj_t *label_coord = lv_label_create(lv_scr_act(), NULL);
|
|
|
|
lv_label_set_text(label_coord, buf);
|
|
|
|
lv_obj_set_pos(label_coord, 0,
|
|
|
|
LV_VER_RES - lv_obj_get_height(label_coord));
|
|
|
|
|
|
|
|
lv_obj_del(circ_area);
|
|
|
|
|
|
|
|
state = TP_CAL_STATE_WAIT_LEAVE;
|
|
|
|
}
|
|
|
|
else if (state == TP_CAL_STATE_WAIT_LEAVE)
|
|
|
|
{
|
2020-06-17 10:50:05 +02:00
|
|
|
lv_theme_set_act(prev_theme);
|
2020-04-07 03:30:55 +02:00
|
|
|
lv_scr_load(prev_scr);
|
|
|
|
tp_set_cal_values(&p[0], &p[1], &p[2], &p[3]);
|
|
|
|
state = TP_CAL_STATE_READY;
|
|
|
|
}
|
|
|
|
else if (state == TP_CAL_STATE_READY)
|
|
|
|
{
|
|
|
|
}
|
2018-01-16 22:35:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tp_cal_create
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Create a touchpad calibration screen
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void tp_cal_create(void)
|
|
|
|
{
|
|
|
|
static lv_style_t style_circ;
|
2020-06-16 17:24:56 +02:00
|
|
|
static lv_style_t style_big_btn;
|
2020-04-07 03:30:55 +02:00
|
|
|
#if LV_USE_ANIMATION
|
2018-01-16 22:35:55 +01:00
|
|
|
lv_anim_t a;
|
2019-04-08 14:57:42 +02:00
|
|
|
#endif
|
2018-01-16 22:35:55 +01:00
|
|
|
|
|
|
|
state = TP_CAL_STATE_INIT;
|
|
|
|
|
|
|
|
prev_scr = lv_scr_act();
|
|
|
|
|
2020-06-17 10:50:05 +02:00
|
|
|
lv_theme_t *theme = LV_THEME_DEFAULT_INIT(LV_THEME_DEFAULT_COLOR_PRIMARY,
|
|
|
|
LV_THEME_DEFAULT_COLOR_SECONDARY,
|
|
|
|
LV_THEME_DEFAULT_FLAG,
|
|
|
|
LV_THEME_DEFAULT_FONT_SMALL,
|
|
|
|
LV_THEME_DEFAULT_FONT_NORMAL,
|
|
|
|
LV_THEME_DEFAULT_FONT_SUBTITLE,
|
|
|
|
LV_THEME_DEFAULT_FONT_TITLE);
|
|
|
|
|
|
|
|
prev_theme = lv_theme_get_act();
|
|
|
|
lv_theme_set_act(theme);
|
|
|
|
|
2018-01-16 22:35:55 +01:00
|
|
|
lv_obj_t *scr = lv_obj_create(NULL, NULL);
|
|
|
|
lv_obj_set_size(scr, TP_MAX_VALUE, TP_MAX_VALUE);
|
|
|
|
lv_scr_load(scr);
|
|
|
|
|
|
|
|
/* Create a big transparent button screen to receive clicks */
|
|
|
|
|
|
|
|
big_btn = lv_btn_create(lv_scr_act(), NULL);
|
|
|
|
lv_obj_set_size(big_btn, TP_MAX_VALUE, TP_MAX_VALUE);
|
2020-06-16 17:24:56 +02:00
|
|
|
|
|
|
|
lv_style_init(&style_big_btn);
|
|
|
|
|
|
|
|
lv_style_set_bg_opa(&style_big_btn, LV_STATE_DEFAULT | LV_STATE_PRESSED,
|
|
|
|
LV_OPA_TRANSP);
|
|
|
|
lv_obj_add_style(big_btn, LV_BTN_PART_MAIN, &style_big_btn);
|
|
|
|
|
2020-04-07 03:30:55 +02:00
|
|
|
lv_obj_set_event_cb(big_btn, btn_click_action);
|
2018-01-16 22:35:55 +01:00
|
|
|
lv_btn_set_layout(big_btn, LV_LAYOUT_OFF);
|
|
|
|
|
|
|
|
label_main = lv_label_create(lv_scr_act(), NULL);
|
|
|
|
lv_label_set_text(label_main, "Click the circle in\n"
|
|
|
|
"upper left-hand corner");
|
|
|
|
lv_label_set_align(label_main, LV_LABEL_ALIGN_CENTER);
|
|
|
|
|
|
|
|
lv_obj_set_pos(label_main, (LV_HOR_RES - lv_obj_get_width(label_main)) / 2,
|
|
|
|
(LV_VER_RES - lv_obj_get_height(label_main)) / 2);
|
|
|
|
|
2020-06-16 17:24:56 +02:00
|
|
|
lv_style_init(&style_circ);
|
|
|
|
lv_style_set_radius(&style_circ, LV_STATE_DEFAULT,
|
|
|
|
LV_RADIUS_CIRCLE);
|
|
|
|
lv_style_set_bg_color(&style_circ, LV_STATE_DEFAULT,
|
|
|
|
LV_COLOR_BLUE);
|
2018-01-16 22:35:55 +01:00
|
|
|
circ_area = lv_obj_create(lv_scr_act(), NULL);
|
|
|
|
lv_obj_set_size(circ_area, CIRCLE_SIZE, CIRCLE_SIZE);
|
2020-06-16 17:24:56 +02:00
|
|
|
lv_obj_add_style(circ_area, LV_OBJ_PART_MAIN, &style_circ);
|
2018-01-16 22:35:55 +01:00
|
|
|
lv_obj_set_click(circ_area, false);
|
|
|
|
|
2020-04-07 03:30:55 +02:00
|
|
|
#if LV_USE_ANIMATION
|
2020-06-16 17:24:56 +02:00
|
|
|
lv_anim_init(&a);
|
|
|
|
lv_anim_set_var(&a, circ_area);
|
|
|
|
lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_x);
|
|
|
|
lv_anim_set_time(&a, 200);
|
|
|
|
lv_anim_set_values(&a, LV_HOR_RES / 2, 0);
|
|
|
|
lv_anim_set_delay(&a, 200);
|
|
|
|
lv_anim_start(&a);
|
|
|
|
|
|
|
|
lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_y);
|
|
|
|
lv_anim_set_values(&a, LV_VER_RES / 2, 0);
|
|
|
|
lv_anim_start(&a);
|
2019-04-08 14:57:42 +02:00
|
|
|
#endif
|
2018-01-16 22:35:55 +01:00
|
|
|
state = TP_CAL_STATE_WAIT_TOP_LEFT;
|
|
|
|
}
|