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变为红色,双击Boot0按钮变成绿色,长按之后变成蓝色。
#include <Arduino.h>
#include "OneButton.h"  //https://github.com/mathertel/OneButton
#include <FastLED.h>  //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	//灯珠数量
#define BRIGHTNESS          96  //亮度设置:0 - 255
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);
     // 设置主亮度控制
  FastLED.setBrightness(BRIGHTNESS);
  // 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));
}

  • 2
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值