wenbo2005.online

学习记录 / ESP8266(MODE)点灯科技

三种按键组件

ESP8266(MODE)点灯科技2024-10-03

(一),定义按键变量

BlinkerButton Button1("button_1");//新建一个按键组件(变量),button_1为键名
BlinkerButton Button2("button_2");
BlinkerButton Button3("button_3");//开关
BlinkerButton Button4("mybutton");//自定义按键,隐藏在开关按键位置的后面

或者:

#define button1 "button_1"      //组件密钥
BlinkerButton Button1(button1);

(二),写回调函数

 //"tap"为点击,"press"长按,"pressup"释放
void button1_callback(const String & state)//按键组件的回调函数
{
  if(state == "tap")
  {
    Serial.println("不要点了!!");
  }
}

  //"tap"为点击,"press"长按,"pressup"释放
void button2_callback(const String & state)//回调函数
{
  if(state == "press")
  {
    Serial.println("不要长按了!!");
  }
}

//"off"为关闭,"on"打开
void button3_callback(const String & state)//开关组件的回调函数
{
  if(state == "off")
  {
    Serial.println("已经关闭");
    Button3.print("off");   //将开关变为关闭状态
    Button3.text("关闭");   //更改软件显示内容
  }
  else if(state == "on")
  {
    Serial.println("已经打开");
    Button3.print("on");    //将开关变为开启状态
    Button3.text("打开");   //更改软件显示内容,并将设置信息发送到手机端!!
  }
}

//自定义发送内容,发送内容有:"自定义内容...","press","pressup"(也有长按与释放)
void mybutton_callback(const String & state)//自定义按键的回调函数
{
  if(state == "1234")
  {
    Serial.println("自定义按键");
  }
}

(三),在初始化中捆绑组件与函数

void setup() {

    ....

    Button1.attach(button1_callback);//将回调函数与组件绑定在一起,当按键按下则会进入回调函数中
    Button2.attach(button2_callback);
    Button3.attach(button3_callback);
    Button4.attach(mybutton_callback);
}

//===========================================================美化============================================

    Button1.text("no");                  //更改软件显示内容

    Button1.icon("fas fa-yin-yang");        //修改组件图标,详见:https://fontawesome.com/v5/search

    Button1.color("#9ACD32");              //修改组件颜色,详见:https://www.w3school.com.cn/tags/html_ref_colornames.asp

    Button1.print();                             //没有此函数,将设置失败