Arduino OneButton按键处理库实现单击/双击/长按功能

Arduino OneButton按键处理库实现单击/双击长按功能


✨在Arduino开发平台下,按键的单击/双击/长按功能,在通过使用OneButton库,很容易就可以轻松实现。这就是支持C/C++模块化设计的好处,避免重复性开发的工作。

  • 本文将具体接收OneButton库的相关调用函数介绍说明以及有关常用按键操作的使用方法。
  • OneButton库github地址:https://github.com/mathertel/OneButton

OneButton接口函数

  • OneButton(const int pin, const boolean activeLow = true, const bool pullupActive = true);//实例化OneButton对象
  • pin,必填参数,指定引脚号。
  • bool,类型,可选,默认参数是true:按下为低电平; false : 按下为高电平
  • bool,类型,可选,默认参数是true,也就是将引脚上拉开启。
  • setClickTicks(const unsigned int ms):设置单击时间
  • setDebounceMs(const unsigned int ms):设置双击时间
  • setPressTicks(const unsigned int ms):设置长按时间
  • attachClick(callbackFunction newFunction);:单击时调用的函数。
  • attachDoubleClick(callbackFunction newFunction);:双击时调用的函数。
  • attachMultiClick(callbackFunction newFunction);:多次按此单击时调用的函数。
  • attachLongPressStart(callbackFunction newFunction);:长按开始时调用的函数。
  • attachLongPressStop(callbackFunction newFunction);:长按结束调用的函数。
  • attachDuringLongPress(callbackFunction newFunction);:长按期间调用的函数。
  • tick(void);按键扫描函数。
  • tick(bool level);:重新给按键引脚电平状态。
  • reset(void);:重启按键状态。
  • getNumberClicks(void);:获取按键次数(单击或多击)。
  • bool isIdle() :查询当前按键状态。如果当前正在处理按钮按流,则返回true。(这允许对电源敏感的应用程序知道何时可以安全地关闭主CPU)
  • isLongPressed():当检测到长按时为True

测试代码

  • 测试对象:ESP32S3
  • 单击按下esp32板上的 boot0按键时,板载ws2812变为红色,双击按钮变成绿色,长按之后变成蓝色。
#include 
#include "OneButton.h"  //https://github.com/mathertel/OneButton
#include   //https://github.com/FastLED/FastLED

#define KEY 0 //esp32 BOOT0按键引脚

#define LED_PIN 48  //ESP32-S3-DevKitC-1 RGB GPIO38 /YD:GPIO48
#define NUM_LEDS 1
OneButton button(KEY, true);

CRGB leds[NUM_LEDS];

void click();/******单击******/
void doubleclick();/******双击******/
void longPressStart();/******长按开始******/
void duringLongPress();/******长按期间******/
void longPressStop();/******长按结束******/
void attachPressStart();

void setup()
{
  Serial.begin(115200);
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  // pinMode(RGB_PIN,OUTPUT);
  // digitalWrite(RGB_PIN,LOW);
  button.reset();//清除按钮状态机的状态
  button.attachClick(click);//注册单击
  button.attachDoubleClick(doubleclick);//注册双击
  button.attachLongPressStart(longPressStart);//注册长按开始
  button.attachDuringLongPress(duringLongPress);//注册长按
  button.attachLongPressStop(longPressStop);//注册长按结束
  button.attachDuringLongPress(attachPressStart);//按下键就会持续触发
    leds[0] = CRGB(255, 0, 0); // 设置颜色为红色
  FastLED.show();
  delay(1000);
  leds[0] = CRGB(0, 255, 0); // 设置颜色为绿色
  FastLED.show();
    delay(1000);
  leds[0] = CRGB(0, 0, 255); // 设置颜色为红色
  FastLED.show();
  delay(1000);
  leds[0] = CRGB(0, 0, 0); // 关闭
  FastLED.show();
}
void loop()
{
  button.tick();
  delay(10);
}
/******单击******/
void click()
{

  Serial.println("click");
 leds[0] = CRGB(255, 0, 0); // 设置颜色为红色
  FastLED.show();
}
/******双击******/
void doubleclick()
{
  Serial.println("Doubleclick");
 leds[0] = CRGB(0, 255, 0); // 设置颜色为绿色
  FastLED.show();
}
/******长按开始******/
void longPressStart()
{
  Serial.println("LongPressStart");
}
/******长按期间******/
void duringLongPress()
{
  if (button.isLongPressed())
  {
    Serial.printf("DuringLongPress,KEY STATE:%d\r\n",digitalRead(KEY));
    delay(50);//稍作延时处
  }
}
/******长按结束******/
void longPressStop()
{
  Serial.println("LongPressStop"); 
  leds[0] = CRGB(51, 51, 153); // 设置颜色为靛蓝
  FastLED.show();

}
void attachPressStart()
{
  Serial.printf("attachPressStart,KEY STATE:%d\r\n",digitalRead(KEY));
}

你可能感兴趣的:(Arduino,ESP32,入门实例教程,OneButton)