esp32-s3 arduino LVGL配置触摸屏输入设备

使用ST7789v(320×240)显示驱动,XPT2046触摸驱动。 

一、lv_indev.cpp代码

/**
 * @file lv_port_indev_templ.c
 *
 */

/*Copy this file as "lv_port_indev.c" and set this value to "1" to enable content*/
#if 1



/*********************
 *      INCLUDES
 *********************/
#include "../porting/lv_port_indev.h"
#include "../lvgl/lvgl.h"
#include "Arduino.h"
#include "../XPT2046_Bitbang_Slim/XPT2046_Bitbang.h"
#include "../touch/touch.h"

static void touchpad_init(void);
static void touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
static bool touchpad_is_pressed(void);
static void touchpad_get_xy(lv_coord_t * x, lv_coord_t * y);

/**********************
 *  STATIC VARIABLES
 **********************/
lv_indev_t * indev_touchpad;

extern TouchPoint touch;

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

void lv_port_indev_init(void)
{


    static lv_indev_drv_t indev_drv;

    /*------------------
     * Touchpad
     * -----------------*/

    /*Initialize your touchpad if you have*/
    touchpad_init();

    /*Register a touchpad input device*/
    lv_indev_drv_init(&indev_drv);
    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.read_cb = touchpad_read;
    indev_touchpad = lv_indev_drv_register(&indev_drv);

}

/**********************
 *   STATIC FUNCTIONS
 **********************/

/*------------------
 * Touchpad
 * -----------------*/

/*Initialize your touchpad*/
static void touchpad_init(void)
{
    /*Your code comes here*/
    touch_init();
}

/*Will be called by the library to read the touchpad*/
static void touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
    static lv_coord_t last_x = 0;
    static lv_coord_t last_y = 0;

    /*Save the pressed coordinates and the state*/
    if(touchpad_is_pressed()) {
        touchpad_get_xy(&last_x, &last_y);
        data->state = LV_INDEV_STATE_PR;
    }
    else {
        data->state = LV_INDEV_STATE_REL;
    }

    /*Set the last pressed coordinates*/
    data->point.x = last_x;
    data->point.y = last_y;
}

/*Return true is the touchpad is pressed*/
static bool touchpad_is_pressed(void)
{
    /*Your code comes here*/
    touch_scan();
    if (touch.zRaw != 0) {
        return true;
    }

    return false;
}

/*Get the x and y coordinates if the touchpad is pressed*/
static void touchpad_get_xy(lv_coord_t * x, lv_coord_t * y)
{
    /*Your code comes here*/

    (*x) = touch.x;
    (*y) = 240-(touch.y);
}


#else /*Enable this file at the top*/

/*This dummy typedef exists purely to silence -Wpedantic.*/
typedef int keep_pedantic_happy;
#endif

二、XPT2046驱动

 

试用了两个,发现这个是最适合的。 

touch.cpp 

#include <Arduino.h>
#include <../XPT2046_Bitbang_Slim/XPT2046_Bitbang.h>

#define MOSI_PIN 42
#define MISO_PIN 41
#define CLK_PIN  1
#define CS_PIN   2

XPT2046_Bitbang touchscreen(MOSI_PIN, MISO_PIN, CLK_PIN, CS_PIN,320,240);
TouchPoint touch;  

void touch_init(void) {
  
  touchscreen.begin();
}

void touch_scan(void) {   
    touch = touchscreen.getTouch();
  
  if (touch.zRaw != 0) {
    // Serial.print("Touch at X: ");
    // Serial.print(touch.x);
    // Serial.print(", Y: ");
    // Serial.println(touch.y);
  }

//   delay(10);  // Adjust delay as needed for responsiveness vs. serial output frequency
}

引脚配置上,我的接法是触摸和屏幕的spi引脚分开接,如果并在一起,我遇到的情况就是显示不出。分开配置引脚时,引脚和显示不会相互影响,后期排查bug方便。 

 具体函数用法可以看readme文件中的github注释

主要用法为:

 1、引脚配置和初始化触摸

#define MOSI_PIN 42
#define MISO_PIN 41
#define CLK_PIN  1
#define CS_PIN   2

XPT2046_Bitbang touchscreen(MOSI_PIN, MISO_PIN, CLK_PIN, CS_PIN,320,240);
TouchPoint touch;  

void touch_init(void) {
  
  touchscreen.begin();
}

2、 获取触摸点

touch = touchscreen.getTouch();
  • getTouch():获取当前触控点。返回具有 和 成员的结构。Pointxy

 3、获取xy坐标

touch.zRaw != 0

这句是判断有无按下,1为按下,这句用作触摸检测 

touch.x
touch.y

 这俩会返回触摸按下时的坐标

三、注册触摸输入设备

1、touchpad_init()中,初始化触摸,所以调用touch_init();函数

2、 touchpad_is_pressed();函数为检测是否按下的状态,所以需要判断touch.zRaw的返回值

    if (touch.zRaw != 0) {
        return true;
    }

3、touchpad_get_xy();传入的是xy的坐标,所以使用touch.x,touch.y,如果坐标希不对,可以另作修改,像我的屏幕就是y轴反了,所以才需要(*y) = 240-(touch.y);

四、调用

主函数中需要初始化时调用lv_port_indev_init();

无需添加组(ground)

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
信息数据从传统到当代,是一直在变革当中,突如其来的互联网让传统的信息管理看到了革命性的曙光,因为传统信息管理从时效性,还是安全性,还是可操作性等各个方面来讲,遇到了互联网时代才发现能补上自古以来的短板,有效的提升管理的效率和业务水平。传统的管理模式,时间越久管理的内容越多,也需要更多的人来对数据进行整理,并且数据的汇总查询方面效率也是极其的低下,并且数据安全方面永远不会保证安全性能。结合数据内容管理的种种缺点,在互联网时代都可以得到有效的补充。结合先进的互联网技术,开发符合需求的软件,让数据内容管理不管是从录入的及时性,查看的及时性还是汇总分析的及时性,都能让正确率达到最高,管理更加的科学和便捷。本次开发的医院后台管理系统实现了病房管理、病例管理、处方管理、字典管理、公告信息管理、患者管理、药品管理、医生管理、预约医生管理、住院管理、管理员管理等功能。系统用到了关系型数据库中王者MySql作为系统的数据库,有效的对数据进行安全的存储,有效的备份,对数据可靠性方面得到了保证。并且程序也具备程序需求的所有功能,使得操作性还是安全性都大大提高,让医院后台管理系统更能从理念走到现实,确确实实的让人们提升信息处理效率。
要基于Arduino使用蓝牙控制另一台ESP32-S3,您可以使用BLE协议进行通信。BLE是一种低功耗的蓝牙协议,可以在ESP32之间直接通信,而无需连接到Wi-Fi网络。以下是基于Arduino使用BLE协议的步骤: 1. 在两个ESP32-S3上安装相同的ESP32开发板支持库,并打开Arduino IDE。 2. 在其中一个ESP32-S3上打开“File” -> “Examples” -> “ESP32 BLE Arduino” -> “BLE_uart”示例程序。 3. 在另一个ESP32-S3上打开“File” -> “Examples” -> “ESP32 BLE Arduino” -> “BLE_uart”示例程序。 4. 在其中一个示例程序中,将设备名称和服务UUID修改为一个固定的值。例如: ``` BLEDevice::init("ESP32-S3-1"); BLEServer *pServer = BLEDevice::createServer(); BLEService *pService = pServer->createService("0000fff0-0000-1000-8000-00805f9b34fb"); ``` 在另一个示例程序中也进行相同的修改,以确保它们能够互相发现和连接。 5. 编译并上传两个示例程序到两个ESP32-S3。 6. 打开两个串口监视器,并分别连接到两个ESP32-S3的串口。 7. 在一个ESP32-S3的串口监视器中,输入“AT+BLESCAN=1”启动BLE扫描。此时,它将开始搜索可用的BLE设备。 8. 在另一个ESP32-S3的串口监视器中,输入“AT+BLEADVERTISE=1”启动BLE广播。此时,它将开始向其他设备广播自己的服务。 9. 在第一个ESP32-S3的串口监视器中,您将看到搜索到的设备列表。找到第二个ESP32-S3并连接到它。 10. 在两个ESP32-S3之间建立连接后,您可以使用串口监视器中的输入框发送命令并控制另一个ESP32-S3。例如,在第一个ESP32-S3的串口监视器中输入“Hello”,在第二个ESP32-S3的串口监视器中将看到“Hello”消息。 请注意,使用BLE协议需要进行额外的配置,例如设置服务UUID、特征UUID和属性等。您可以参考ESP32-S3的官方文档进行详细配置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值