您尚未登录。

#1 全志 SOC » esp32芯片基于arduino搭配lvgl 触摸屏点击按钮不会调用回调函数,但是会返回点击信息 » 2022-05-23 23:36:28

WenWu
回复: 0
#include <TFT_eSPI.h>
#include <lvgl.h>

static const uint16_t screenWidth = 480;
static const uint16_t screenHeight = 320;

static lv_disp_draw_buf_t draw_buf;
static lv_color_t buf[screenWidth * 10];

TFT_eSPI tft = TFT_eSPI(screenWidth, screenHeight); /* TFT instance */

lv_obj_t *label;
int hour = 8;
int minute = 0;
int second = 0;
lv_timer_t *timer;
uint16_t x, y;
/*Read the touchpad*/
void my_touchpad_read(lv_indev_drv_t *indev_driver, lv_indev_data_t *data)
{
    uint16_t touchX, touchY;

    bool touched = tft.getTouch(&touchX, &touchY, 600);
    if (!touched)
    {
        data->state = LV_INDEV_STATE_REL;
    }
    else
    {
        data->state = LV_INDEV_STATE_PR;

        /*Set the coordinates*/
        data->point.x = touchX;
        data->point.y = touchY;

        Serial.print("Data x ");
        Serial.println(touchX);

        Serial.print("Data y ");
        Serial.println(touchY);
    }
}

void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p)
{
    uint32_t w = (area->x2 - area->x1 + 1);
    uint32_t h = (area->y2 - area->y1 + 1);

    tft.startWrite();
    tft.setAddrWindow(area->x1, area->y1, w, h);
    tft.pushColors((uint16_t *)&color_p->full, w * h, true);
    tft.endWrite();

    lv_disp_flush_ready(disp);
}

void my_timer(lv_timer_t *timer)
{
    minute += ++second / 60;
    hour += minute / 60;
    second %= 60;
    minute %= 60;
    hour %= 24;
    String Ssecond = second >= 10 ? String(second) : "0" + String(second);
    String Sminute = minute >= 10 ? String(minute) : "0" + String(minute);
    String Shour = hour >= 10 ? String(hour) : "0" + String(hour);
    String time = Shour + "-" + Sminute + "-" + Ssecond;
    lv_label_set_text_fmt(label, "%s", time.c_str());
    Serial.println(time);
}

static void my_event_cb(lv_event_t *e)
{
    lv_obj_t *target = lv_event_get_target(e);
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_CLICKED)
        Serial.println("Clicked");
    else
        Serial.println("Success");
    lv_obj_set_style_bg_color(target, lv_color_hex(0x000000), 0);
}

void setup()
{
    Serial.begin(115200);
    lv_init();   // lvgl init
    tft.begin(); /* TFT init */
    tft.fillScreen(TFT_WHITE);
    tft.setRotation(3);

    uint16_t calData[5] = {261, 3647, 207, 3656, 7};
    tft.setTouch(calData);
    lv_disp_draw_buf_init(&draw_buf, buf, NULL, screenWidth * 10);
    /*Initialize the display*/
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    /*Change the following line to your display resolution*/
    disp_drv.hor_res = screenWidth;
    disp_drv.ver_res = screenHeight;
    disp_drv.flush_cb = my_disp_flush;
    disp_drv.draw_buf = &draw_buf;
    lv_disp_drv_register(&disp_drv);

    /*Initialize the (dummy) input device driver*/
    static lv_indev_drv_t indev_drv;
    lv_indev_drv_init(&indev_drv);
    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.read_cb = my_touchpad_read;
    lv_indev_drv_register(&indev_drv);

    static lv_obj_t *obj = lv_obj_create(lv_scr_act());
    label = lv_label_create(obj);
    lv_obj_center(obj);
    lv_obj_center(label);
    lv_label_set_text(label, "0");

    static lv_obj_t *btn = lv_btn_create(obj);
    lv_obj_add_event_cb(btn, my_event_cb, LV_EVENT_CLICKED, NULL);

    timer = lv_timer_create(my_timer, 1000, NULL);
    lv_timer_set_repeat_count(timer, -1);
    lv_timer_ready(timer);
}

void loop()
{
    long last_time = millis();
    lv_timer_handler(); /* let the GUI do its work */
    bool touched = tft.getTouch(&x, &y, 600);
    Serial.println(touched);
    delay(1000);
    lv_tick_inc(int(millis() - last_time));
}

串口会打印出触屏点信息
uTools_1653301942512.png

#2 ESP32/ESP8266 » ILI9486触摸屏 LVGL中按钮点击后不触发回调函数 » 2022-05-23 18:33:23

WenWu
回复: 0
#include <TFT_eSPI.h>
#include <lvgl.h>

static const uint16_t screenWidth = 480;
static const uint16_t screenHeight = 320;

static lv_disp_draw_buf_t draw_buf;
static lv_color_t buf[screenWidth * 10];

TFT_eSPI tft = TFT_eSPI(screenWidth, screenHeight); /* TFT instance */

lv_obj_t *label;
int hour = 8;
int minute = 0;
int second = 0;
lv_timer_t *timer;
uint16_t x, y;
/*Read the touchpad*/
void my_touchpad_read(lv_indev_drv_t *indev_driver, lv_indev_data_t *data)
{
    uint16_t touchX, touchY;

    bool touched = tft.getTouch(&touchX, &touchY, 600);
    if (!touched)
    {
        data->state = LV_INDEV_STATE_REL;
    }
    else
    {
        data->state = LV_INDEV_STATE_PR;

        /*Set the coordinates*/
        data->point.x = touchX;
        data->point.y = touchY;

        Serial.print("Data x ");
        Serial.println(touchX);

        Serial.print("Data y ");
        Serial.println(touchY);
    }
}

void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p)
{
    uint32_t w = (area->x2 - area->x1 + 1);
    uint32_t h = (area->y2 - area->y1 + 1);

    tft.startWrite();
    tft.setAddrWindow(area->x1, area->y1, w, h);
    tft.pushColors((uint16_t *)&color_p->full, w * h, true);
    tft.endWrite();

    lv_disp_flush_ready(disp);
}

void my_timer(lv_timer_t *timer)
{
    minute += ++second / 60;
    hour += minute / 60;
    second %= 60;
    minute %= 60;
    hour %= 24;
    String Ssecond = second >= 10 ? String(second) : "0" + String(second);
    String Sminute = minute >= 10 ? String(minute) : "0" + String(minute);
    String Shour = hour >= 10 ? String(hour) : "0" + String(hour);
    String time = Shour + "-" + Sminute + "-" + Ssecond;
    lv_label_set_text_fmt(label, "%s", time.c_str());
    Serial.println(time);
}

static void my_event_cb(lv_event_t *e)
{
    lv_obj_t *target = lv_event_get_target(e);
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_CLICKED)
        Serial.println("Clicked");
    else
        Serial.println("Success");
    lv_obj_set_style_bg_color(target, lv_color_hex(0x000000), 0);
}

void setup()
{
    Serial.begin(115200);
    lv_init();   // lvgl init
    tft.begin(); /* TFT init */
    tft.fillScreen(TFT_WHITE);
    tft.setRotation(3);

    uint16_t calData[5] = {261, 3647, 207, 3656, 7};
    tft.setTouch(calData);
    lv_disp_draw_buf_init(&draw_buf, buf, NULL, screenWidth * 10);
    /*Initialize the display*/
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    /*Change the following line to your display resolution*/
    disp_drv.hor_res = screenWidth;
    disp_drv.ver_res = screenHeight;
    disp_drv.flush_cb = my_disp_flush;
    disp_drv.draw_buf = &draw_buf;
    lv_disp_drv_register(&disp_drv);

    /*Initialize the (dummy) input device driver*/
    static lv_indev_drv_t indev_drv;
    lv_indev_drv_init(&indev_drv);
    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.read_cb = my_touchpad_read;
    lv_indev_drv_register(&indev_drv);

    static lv_obj_t *obj = lv_obj_create(lv_scr_act());
    label = lv_label_create(obj);
    lv_obj_center(obj);
    lv_obj_center(label);
    lv_label_set_text(label, "0");

    static lv_obj_t *btn = lv_btn_create(obj);
    lv_obj_add_event_cb(btn, my_event_cb, LV_EVENT_CLICKED, NULL);

    timer = lv_timer_create(my_timer, 1000, NULL);
    lv_timer_set_repeat_count(timer, -1);
    lv_timer_ready(timer);
}

void loop()
{
    long last_time = millis();
    lv_timer_handler(); /* let the GUI do its work */
    bool touched = tft.getTouch(&x, &y, 600);
    Serial.println(touched);
    delay(1000);
    lv_tick_inc(int(millis() - last_time));
}

串口会打印出触屏点信息
uTools_1653301942512.png

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn