esp32-s3 arduino LVGL使用EC11编码器作为输入设备

一、配置ec11驱动

我之前写的如何调用ec11库的文章

二、lv_port_indev.cpp配置

因为用的是arduino环境,所以我把.c改为.cpp了,以确保被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 "lv_port_indev.h"
#include "..\lvgl\lvgl.h"
#include "Arduino.h"
#include "..\..\lib\five_way_switch\five_way_switch.h"

#include <Encoder.h>

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

static void keypad_init(void);
static void keypad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
static uint32_t keypad_get_key(void);

static void encoder_init(void);
static void encoder_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
static void encoder_handler(void);
int encoder_key=0;

/**********************
 *  STATIC VARIABLES
 **********************/

lv_indev_t * indev_keypad;
lv_indev_t * indev_encoder;

static int32_t encoder_diff;
static lv_indev_state_t encoder_state;

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

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

void lv_port_indev_init(void)
{
    static lv_indev_drv_t indev_drv;
    static lv_indev_drv_t indev_drv_encoder;

   
    /*------------------
     * Keypad
     * -----------------*/

    /*Initialize your keypad or keyboard if you have*/
    keypad_init();

    /*Register a keypad input device*/
    lv_indev_drv_init(&indev_drv);
    indev_drv.type = LV_INDEV_TYPE_KEYPAD;
    indev_drv.read_cb = keypad_read;
    indev_keypad = lv_indev_drv_register(&indev_drv);


    /*------------------
     * Encoder
     * -----------------*/

    /*Initialize your encoder if you have*/
    encoder_init();

    /*Register a encoder input device*/
    lv_indev_drv_init(&indev_drv_encoder);
    indev_drv_encoder.type = LV_INDEV_TYPE_ENCODER;
    indev_drv_encoder.read_cb = encoder_read;
    indev_encoder = lv_indev_drv_register(&indev_drv_encoder);

    /*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_encoder, group);`*/

    
}

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

/*------------------
 * Keypad
 * -----------------*/

/*Initialize your keypad*/
static void keypad_init(void)
{
    /*Your code comes here*/
}

/*Will be called by the library to read the mouse*/
static void keypad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
    static uint32_t last_key = 0;

    /*Get the current x and y coordinates*/
    // mouse_get_xy(&data->point.x, &data->point.y);

    /*Get whether the a key is pressed and save the pressed key*/
    uint32_t act_key = keypad_get_key();
    if(act_key != 0) {
        data->state = LV_INDEV_STATE_PR;

        /*Translate the keys to LVGL control characters according to your key definitions*/
        switch(act_key) {
            case 1:
                act_key = LV_KEY_NEXT;
                break;
            case 2:
                act_key = LV_KEY_PREV;
                break;
            case 3:
                act_key = LV_KEY_LEFT;
                break;
            case 4:
                act_key = LV_KEY_RIGHT;
                break;
            case 5:
                act_key = LV_KEY_ENTER;
                break;
        }

        last_key = act_key;
    }
    else {
        data->state = LV_INDEV_STATE_REL;
    }

    data->key = last_key;
}

/*Get the currently being pressed key.  0 if no key is pressed*/
static uint32_t keypad_get_key(void)
{
    /*Your code comes here*/
    
    get_switch_state(analogRead(17),&FWS);
    return Switchstate_to_LVGL(&FWS);
}

/*------------------
 * Encoder
 * -----------------*/

/*Initialize your keypad*/
static void encoder_init(void)
{
    /*Your code comes here*/
    ec11_init();
}

/*Will be called by the library to read the encoder*/
static void encoder_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
    static int last_key = 0;
    int act_key = ec11_scan();
    if(act_key != 0) {
        switch(act_key) {
            case 1:
                act_key = LV_KEY_LEFT;
                encoder_diff = 1;
                //encoder_state = LV_INDEV_STATE_REL;    
                break;
            case 2:
                act_key = LV_KEY_RIGHT;
                encoder_diff = -1;
                //encoder_state = LV_INDEV_STATE_REL;
                break;
            case 3:
                act_key = LV_KEY_RIGHT;
                //encoder_state = LV_INDEV_STATE_REL;
                break;
        }
        last_key = act_key;
        act_key=0;
    }
    else{
        encoder_diff = 0;
        encoder_state = LV_INDEV_STATE_REL;
        }
    data->key = last_key;
    last_key=0;
    data->enc_diff = encoder_diff;
    data->state = encoder_state;
    encoder_diff=0;
    encoder_key=0;
}

/*Call this function in an interrupt to process encoder events (turn, press)*/
static void encoder_handler(void)
{
    /*Your code comes here*/
    encoder_diff += 0;
    encoder_state = LV_INDEV_STATE_REL;
}

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

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

我这里有按键和编码器,本文只讲编码器。

在encoder_init中

static void encoder_init(void)
{
    /*Your code comes here*/
    ec11_init();
}

ec11_init();是 ec11引脚的初始化配置,如

void ec11_init(void)
{ 
  encoder.attachSingleEdge(EC11_A_PIN, EC11_B_PIN);
  pinMode(EC11_K_PIN, INPUT_PULLUP);
}

 

在encoder_read中

/*Will be called by the library to read the encoder*/
static void encoder_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
    static int last_key = 0;
    int act_key = ec11_scan();
    if(act_key != 0) {
        switch(act_key) {
            case 1:
                act_key = LV_KEY_LEFT;
                encoder_diff = 1;
                //encoder_state = LV_INDEV_STATE_REL;    
                break;
            case 2:
                act_key = LV_KEY_RIGHT;
                encoder_diff = -1;
                //encoder_state = LV_INDEV_STATE_REL;
                break;
            case 3:
                act_key = LV_KEY_RIGHT;
                //encoder_state = LV_INDEV_STATE_REL;
                break;
        }
        last_key = act_key;
        act_key=0;
    }
    else{
        encoder_diff = 0;
        encoder_state = LV_INDEV_STATE_REL;
        }
    data->key = last_key;
    last_key=0;
    data->enc_diff = encoder_diff;
    data->state = encoder_state;
    encoder_diff=0;
    encoder_key=0;
}

 encoder_diff = -1;或encoder_diff = 1;,这里的1是指编码器转一格,读数跳几个,1则是123456递增,2则是2468,最好设置为1,不然选择部件的时候会跳着选。

在encoder_handler中

static void encoder_handler(void)
{
    /*Your code comes here*/
    encoder_diff += 0;
    encoder_state = LV_INDEV_STATE_REL;
}

 没有用,我只要用作选择功能,不需要它干活。

三、添加组 

 

	lv_group_t *group_1 = lv_group_create();
	lv_group_add_obj(group_1,ui->start_botton_btn_1);
	lv_group_add_obj(group_1,ui->start_botton_btn_2);
	lv_group_add_obj(group_1,ui->start_botton_btn_3);
	lv_group_add_obj(group_1,ui->start_botton_btn_4);
	lv_indev_set_group(indev_encoder,group_1);	

在每个页面的初始化中添加组,一个个部件添加进编码器事件中就能使用编码器选择了。按键同理。

四、注意

    static lv_indev_drv_t indev_drv;
    static lv_indev_drv_t indev_drv_encoder;

每一个输入设备就对应一个indev_drv,否则多个输入设备公用一个会导致无法输出。

lv_port_indev_init();

主函数中需要在setup初始化中调用indev_init初始化输入设备,否则输入设备没法使用。 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要基于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、付费专栏及课程。

余额充值