Arduino ESP32全功能旋钮 使用蓝牙连接和EC11旋转编码器

支持长按、短按、双击、顺时针逆时针、按钮按下顺时针逆时针
ec11编码器原理不再介绍

使用如下旋转编码器:
Arduino ESP32全功能旋钮 使用蓝牙连接和EC11旋转编码器_第1张图片
开发板如下:
Arduino ESP32全功能旋钮 使用蓝牙连接和EC11旋转编码器_第2张图片

接线:
 CLK --> GPIO14
 DT  --> GPIO12
 SW  --> GPIO19
 +   --> 3V3
 GND --> GND

使用如下库:

lib_deps = 
	mathertel/OneButton@^2.0.3
	t-vk/ESP32 BLE Keyboard@^0.3.2
	madhephaestus/ESP32Encoder@^0.10.1

库使用可参考文档和其他文章。
为了节省MCU利用率,使用中断方式而不使用轮询方式。

代码如下,可实现功能:
模式0自定义短按、双击、顺时针逆时针、按钮按下顺时针逆时针功能
模式1自定义短按、双击、顺时针逆时针、按钮按下顺时针逆时针功能
长按切换两种模式(可自行添加多种模式),(按下旋转松手后不会切换模式)

此示例中:
模式0:短按静音/取消静音,双击蓝牙输入测试,顺时针音量加,逆时针音量键、按钮按下顺时针逆时针无功能。
模式1:短按Enter,双击蓝牙输入测试,顺时针右方向键,逆时针左方向键,按钮按下顺时针下方向键,按钮按下逆时针上方向键。
烧录后串口查看当前动作、模式和编码器计数
电脑蓝牙连接ESP32-BleKeyboard

#include 
#include "BleKeyboard.h"
#include "OneButton.h"
#include 

//按键配置
#define EC11_A_PIN 12
#define EC11_B_PIN 14
#define EC11_K_PIN 19//按键引脚

//键盘名称:ESP32-BleKeyboard
BleKeyboard bleKeyboard("ESP32-Keyboard", "Espressif", 100);
ESP32Encoder encoder;
OneButton SW(EC11_K_PIN, true);

int lastEncoderValue = 0;
int now_count = 0;
int mode = 0;   //0,1两种mode,可自行添加
bool activate = true;

//按键单击回调函数
void click() {
    if (mode == 0) {
        Serial.println("Click: MUTE");
        bleKeyboard.write(KEY_MEDIA_MUTE);
    }
    if (mode == 1) {
        Serial.println("Enter");
        bleKeyboard.write(KEY_RETURN);
    }
}

//按键长按回调函数
void longclick() {
    if (activate) {   //如果旋钮转动,则不切换状态
        Serial.println("Longclick: Mode Change");
        Serial.print("current mode: ");
        if (mode == 0) {
            mode = 1;
            Serial.println("Arrow");
            return;
        }
        if (mode == 1) {
            mode = 0;
            Serial.println("Media");
            return;
        }
    }
    activate = true;
}

//按键双击回调函数
void doubleclick() {
    if (mode == 0) {
         Serial.println("Doubleclick: Input test");
   		 bleKeyboard.print("ESP32-BLEKeyboard Input Test");
    }
    if (mode == 1) {
         Serial.println("Doubleclick: Input test");
    	bleKeyboard.print("ESP32-BLEKeyboard Input Test");
    }
}

void setup() {
    Serial.begin(115200);
    ESP32Encoder::useInternalWeakPullResistors = UP;
    encoder.attachSingleEdge(EC11_A_PIN, EC11_B_PIN);

    pinMode(EC11_K_PIN, INPUT_PULLUP);

    //初始化按键事件检测
    SW.attachClick(click);
    SW.attachDoubleClick(doubleclick);
    SW.attachLongPressStop(longclick);
    SW.setDebounceTicks(20);//滤波(ms)
    SW.setClickTicks(200);
    SW.setPressTicks(500);

    Serial.println("Starting BLE work");
    bleKeyboard.begin();
}

void loop() {
    SW.tick();

    if (bleKeyboard.isConnected()) {
        if (lastEncoderValue != encoder.getCount()) {
            now_count = encoder.getCount();
            if (now_count != lastEncoderValue) {
                if (!SW.isIdle()) {         //检测按键是否空闲
                    activate = false;
                    Serial.print("(Long_pressed)Encoder value: ");
                    Serial.println(now_count);
                } else {
                    Serial.print("Encoder value: ");
                    Serial.println(now_count);
                }
            }

            if (now_count > lastEncoderValue) {
                if (!SW.isIdle()) {         //检测按键是否空闲
                    if (mode == 0) {}     //模式0按钮按下顺时针功能
                    if (mode == 1) {		//模式1按钮按下顺时针功能
                        bleKeyboard.write(KEY_DOWN_ARROW);
                        Serial.println("DOWN_ARROW");
                    }
                } else {
                    if (mode == 0) {	//模式0顺时针功能
                        bleKeyboard.write(KEY_MEDIA_VOLUME_UP);
                        Serial.println("MEDIA_VOLUME_UP");
                    }
                    if (mode == 1) {	//模式1顺时针功能
                        bleKeyboard.write(KEY_RIGHT_ARROW);
                        Serial.println("RIGHT_ARROW");
                    }
                }
            }
            if (now_count < lastEncoderValue) {
                if (!SW.isIdle()) {         //检测按键是否空闲
                    if (mode == 0) {}    //模式0按钮按下逆时针功能
                    if (mode == 1) {	//模式1按钮按下逆时针功能
                        bleKeyboard.write(KEY_UP_ARROW);
                        Serial.println("UP_ARROW");
                    }
                } else {
                    if (mode == 0) {	//模式0逆时针功能
                        bleKeyboard.write(KEY_MEDIA_VOLUME_DOWN);
                        Serial.println("MEDIA_VOLUME_DOWN");
                    }
                    if (mode == 1) {	//模式1逆时针功能
                        bleKeyboard.write(KEY_LEFT_ARROW);
                        Serial.println("LEFT_ARROW");
                    }
                }
            }
            lastEncoderValue = now_count;
        }
    }
}

你可能感兴趣的:(嵌入式硬件,单片机,c++,c语言)