在本文中,使用WeMos D1 mini开发板实现通过硬件按钮控制继电器开关的功能。
在这一小节中,需要将IDE配置成功。请参考
本文的所有GPIO引脚都尊从WeMos D1 mini的定义。
请参考
#ifndef __WIFISOCKETLED_H__
#define __WIFISOCKETLED_H__
/*JinZhu 17/9/22 Review */
#include
#include
#define LED_PIN LED_BUILTIN
#define BLINK_LED_DELAY 1000
class WIFISocketLEDClass
{
public:
WIFISocketLEDClass();
void begin(int pin = LED_PIN);
void Blink(int time = BLINK_LED_DELAY);
void Blink(int time, int count);
void BlinkDelay(int time = BLINK_LED_DELAY);
void BlinkDelay(int time, int count);
void BlinkHandle();
void Switch(bool state);
bool getState();
private:
Ticker ticker;
int blinkCount;
private:
int pin;
bool state;
};
extern WIFISocketLEDClass WIFISocketLED;
#endif
#include "WIFISocketLED.h"
WIFISocketLEDClass WIFISocketLED;
static void WIFISocketTickerHandle(WIFISocketLEDClass* WIFISocketLED)
{
WIFISocketLED->BlinkHandle();
}
WIFISocketLEDClass::WIFISocketLEDClass()
{
this->pin = LED_PIN;
this->state = false;
this->blinkCount = 0;
}
void WIFISocketLEDClass::BlinkHandle()
{
if (this->blinkCount == 0)
{
ticker.detach();
return;
}
this->blinkCount--;
this->Switch(!this->getState());
}
void WIFISocketLEDClass::Blink(int time)
{
this->ticker.detach();
this->blinkCount = (1 * 2) - 1;
this->ticker.attach_ms(time, WIFISocketTickerHandle, this);
this->BlinkHandle();
}
void WIFISocketLEDClass::Blink(int time, int count)
{
this->ticker.detach();
this->blinkCount = (count * 2) - 1;
this->ticker.attach_ms(time, WIFISocketTickerHandle, this);
this->BlinkHandle();
}
void WIFISocketLEDClass::BlinkDelay(int time)
{
this->Switch(!this->getState());
delay(time);
this->Switch(!this->getState());
delay(time);
}
void WIFISocketLEDClass::BlinkDelay(int time, int count)
{
for (int i = 0; i < count; i++)
{
this->Switch(!this->getState());
delay(time);
this->Switch(!this->getState());
delay(time);
}
}
void WIFISocketLEDClass::begin(int pin)
{
this->pin = pin;
pinMode(this->pin, OUTPUT);
this->Switch(false);
}
void WIFISocketLEDClass::Switch(bool state)
{
this->state = state;
if (state)
{
digitalWrite(this->pin, LOW);
} else
{
digitalWrite(this->pin, HIGH);
}
}
bool WIFISocketLEDClass::getState()
{
return this->state;
}
#ifndef __WIFISOCKETBUTTON_H__
#define __WIFISOCKETBUTTON_H__
/*JinZhu 17/9/22 Review */
#include
#define BUTTON_PIN 4
typedef enum BUTTON_STATE
{
NONE_PRESS = 0,
SHORT_PRESS = 1,
LONG_PRESS = 2,
} BUTTON_STATE;
class WIFISocketButtonClass
{
public:
WIFISocketButtonClass();
void begin(int pin = BUTTON_PIN);
BUTTON_STATE Scan();
private:
int pin;
};
extern WIFISocketButtonClass WIFISocketButton;
#endif
#include "WIFISocketButton.h"
WIFISocketButtonClass WIFISocketButton;
WIFISocketButtonClass::WIFISocketButtonClass()
{
this->pin = BUTTON_PIN;
}
void WIFISocketButtonClass::begin(int pin)
{
this->pin = pin;
pinMode(this->pin, INPUT);
}
BUTTON_STATE WIFISocketButtonClass::Scan()
{
const int LONG_PRESS_TIME = 2000;
const int LONG_PRESS_SETP = 100;
int TimeOutCount = 0;
if (digitalRead(this->pin) == HIGH)
{
return NONE_PRESS;
} else
{
delay(10);
if (digitalRead(this->pin) == HIGH)
{
return SHORT_PRESS;
} else
{
for (TimeOutCount = 0; TimeOutCount < LONG_PRESS_TIME; TimeOutCount += LONG_PRESS_SETP)
{
if (digitalRead(this->pin) == HIGH)
{
break;
}
delay(LONG_PRESS_SETP);
}
if (TimeOutCount < LONG_PRESS_TIME)
{
return SHORT_PRESS;
} else
{
return LONG_PRESS;
}
}
}
}
#ifndef __WIFISOCKETSWITCH_H__
#define __WIFISOCKETSWITCH_H__
/*JinZhu 17/9/22 Review */
#include
#define SWITCH_PIN 5
class WIFISocketSwitchClass
{
public:
WIFISocketSwitchClass();
void begin(int pin = SWITCH_PIN);
void Switch(bool state);
bool getState();
private:
int pin;
bool state;
};
extern WIFISocketSwitchClass WIFISocketSwitch;
#endif
#include "WIFISocketSwitch.h"
#include "WIFISocketLED.h"
WIFISocketSwitchClass WIFISocketSwitch;
WIFISocketSwitchClass::WIFISocketSwitchClass()
{
this->pin = SWITCH_PIN;
}
void WIFISocketSwitchClass::begin(int pin)
{
this->pin = pin;
pinMode(this->pin, OUTPUT);
this->Switch(state);
WIFISocketLED.Switch(state);
}
void WIFISocketSwitchClass::Switch(bool state)
{
this->state = state;
WIFISocketLED.Switch(state);
if (state)
{
digitalWrite(this->pin, HIGH);
} else
{
digitalWrite(this->pin, LOW);
}
}
bool WIFISocketSwitchClass::getState()
{
return state;
}
#include
#include "WIFISocketLED.h"
#include "WIFISocketButton.h"
#include "WIFISocketSwitch.h"
void setup()
{
WIFISocketLED.begin();
WIFISocketButton.begin();
WIFISocketSwitch.begin();
}
void loop()
{
BUTTON_STATE ButtonState = WIFISocketButton.Scan();
if (ButtonState == SHORT_PRESS)
{
boolean switchstate = WIFISocketSwitch.getState();
WIFISocketSwitch.Switch(!switchstate);
}
}