mirror of https://github.com/IoTcat/sola.git
parent
3fcf3c87f1
commit
8ee1d58c34
18 changed files with 1626 additions and 25 deletions
@ -0,0 +1,65 @@ |
|||||||
|
/*
|
||||||
|
* @Author: IoTcat (https://iotcat.me)
|
||||||
|
* @Date: 2019-08-20 09:58:28
|
||||||
|
* @Last Modified by:
|
||||||
|
* @Last Modified time: 2019-08-30 12:35:44 |
||||||
|
*/ |
||||||
|
#ifndef __BUZ_H__ |
||||||
|
#define __BUZ_H__ |
||||||
|
|
||||||
|
class Buz{ |
||||||
|
public: |
||||||
|
Buz(const int& pin){ |
||||||
|
this->_pin = pin; |
||||||
|
}; |
||||||
|
~Buz(){}; |
||||||
|
|
||||||
|
void ini() { |
||||||
|
pinMode(this->_pin, OUTPUT); |
||||||
|
this->_fStatus = this->getStatus(); |
||||||
|
}; |
||||||
|
|
||||||
|
void loop(){ |
||||||
|
if(millis() < this->_toT){ |
||||||
|
analogWrite(this->_pin, (int)((float)((this->_toT - millis()) % 3000) * 255 / 3000)); |
||||||
|
this->_status = true; |
||||||
|
}else{ |
||||||
|
analogWrite(this->_pin, 0); |
||||||
|
this->_status = false; |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
inline void on(const int& t = 12*60*1000){ |
||||||
|
this->_toT = millis() + t; |
||||||
|
} |
||||||
|
|
||||||
|
inline void off(){ |
||||||
|
this->_toT = 0; |
||||||
|
} |
||||||
|
|
||||||
|
inline void once(const int& times = 4){ |
||||||
|
this->_toT = millis() + 3000 * times; |
||||||
|
} |
||||||
|
|
||||||
|
inline const bool getStatus() const{ |
||||||
|
return this->_status; |
||||||
|
} |
||||||
|
|
||||||
|
inline const bool isStateChange() { |
||||||
|
if(this->_fStatus != this->getStatus()){ |
||||||
|
this->_fStatus = this->getStatus(); |
||||||
|
return true; |
||||||
|
}else{ |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private: |
||||||
|
static unsigned int _toT; |
||||||
|
unsigned int _pin;
|
||||||
|
bool _status, _fStatus; |
||||||
|
}; |
||||||
|
|
||||||
|
unsigned int Buz::_toT = 0; |
||||||
|
|
||||||
|
#endif //__BUZ_H__
|
@ -0,0 +1 @@ |
|||||||
|
./bin/ |
@ -0,0 +1,65 @@ |
|||||||
|
/*
|
||||||
|
* @Author: IoTcat (https://iotcat.me)
|
||||||
|
* @Date: 2019-08-20 09:58:28
|
||||||
|
* @Last Modified by:
|
||||||
|
* @Last Modified time: 2019-08-30 12:35:44 |
||||||
|
*/ |
||||||
|
#ifndef __BUZ_H__ |
||||||
|
#define __BUZ_H__ |
||||||
|
|
||||||
|
class Buz{ |
||||||
|
public: |
||||||
|
Buz(const int& pin){ |
||||||
|
this->_pin = pin; |
||||||
|
}; |
||||||
|
~Buz(){}; |
||||||
|
|
||||||
|
void ini() { |
||||||
|
pinMode(this->_pin, OUTPUT); |
||||||
|
this->_fStatus = this->getStatus(); |
||||||
|
}; |
||||||
|
|
||||||
|
void loop(){ |
||||||
|
if(millis() < this->_toT){ |
||||||
|
analogWrite(this->_pin, (int)((float)((this->_toT - millis()) % 3000) * 255 / 3000)); |
||||||
|
this->_status = true; |
||||||
|
}else{ |
||||||
|
analogWrite(this->_pin, 0); |
||||||
|
this->_status = false; |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
inline void on(const int& t = 12*60*1000){ |
||||||
|
this->_toT = millis() + t; |
||||||
|
} |
||||||
|
|
||||||
|
inline void off(){ |
||||||
|
this->_toT = 0; |
||||||
|
} |
||||||
|
|
||||||
|
inline void once(const int& times = 4){ |
||||||
|
this->_toT = millis() + 3000 * times; |
||||||
|
} |
||||||
|
|
||||||
|
inline const bool getStatus() const{ |
||||||
|
return this->_status; |
||||||
|
} |
||||||
|
|
||||||
|
inline const bool isStateChange() { |
||||||
|
if(this->_fStatus != this->getStatus()){ |
||||||
|
this->_fStatus = this->getStatus(); |
||||||
|
return true; |
||||||
|
}else{ |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private: |
||||||
|
static unsigned int _toT; |
||||||
|
unsigned int _pin;
|
||||||
|
bool _status, _fStatus; |
||||||
|
}; |
||||||
|
|
||||||
|
unsigned int Buz::_toT = 0; |
||||||
|
|
||||||
|
#endif //__BUZ_H__
|
@ -0,0 +1,295 @@ |
|||||||
|
/*
|
||||||
|
* @Author: IoTcat (https://iotcat.me)
|
||||||
|
* @Date: 2019-08-20 09:57:58
|
||||||
|
* @Last Modified by:
|
||||||
|
* @Last Modified time: 2019-08-30 14:44:11 |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <PubSubClient.h> |
||||||
|
#include <ESP8266WiFi.h> |
||||||
|
|
||||||
|
#include "led.h" |
||||||
|
//#include "buz.h"
|
||||||
|
#include "swi.h" |
||||||
|
#include "pir.h" |
||||||
|
#include "relay.h" |
||||||
|
#include "mode.h" |
||||||
|
#include "lightCtl.h" |
||||||
|
|
||||||
|
const char* ssid = "yimian-iot"; |
||||||
|
const char* password = "1234567890."; |
||||||
|
const char* mqtt_server = "192.168.3.4"; // change this to the mqtt server
|
||||||
|
|
||||||
|
const char* topicInCtl = "hass/ctl/din/#"; // change this to the outgoing messages
|
||||||
|
const char* topicInRefresh = "hass/refresh"; |
||||||
|
|
||||||
|
const String clientId = "din"; |
||||||
|
const unsigned short pirNum = 4; |
||||||
|
|
||||||
|
WiFiClient espClient; |
||||||
|
PubSubClient client(espClient); |
||||||
|
|
||||||
|
//Buz buz(D3);
|
||||||
|
Swi swi(D1, D2); |
||||||
|
Pir p[pirNum] = {D6, D13, D11, D12}; |
||||||
|
LED led(D0); |
||||||
|
Relay light(D5, false); |
||||||
|
Mode mode; |
||||||
|
LightCtl lightCtl(&light, &led, &mode); |
||||||
|
|
||||||
|
int pirCnt = 0; //计数区域内激活的pir个数
|
||||||
|
unsigned long int LastActTime = 0; |
||||||
|
|
||||||
|
void setup() { |
||||||
|
Serial.begin(115200); |
||||||
|
WiFi.begin(ssid, password); |
||||||
|
|
||||||
|
while (WiFi.status() != WL_CONNECTED) { |
||||||
|
delay(500); |
||||||
|
Serial.print("."); |
||||||
|
} |
||||||
|
|
||||||
|
Serial.println(""); |
||||||
|
Serial.println("WiFi connected"); |
||||||
|
Serial.println("IP address: "); |
||||||
|
Serial.println(WiFi.localIP()); |
||||||
|
client.setServer(mqtt_server, 1883); // default port, change it...
|
||||||
|
client.setCallback(callback); |
||||||
|
|
||||||
|
swi.on("toggle", swiToggle); |
||||||
|
|
||||||
|
|
||||||
|
//buz.ini();
|
||||||
|
swi.ini(); |
||||||
|
for(auto i : p){ |
||||||
|
i.ini(); |
||||||
|
} |
||||||
|
led.ini(); |
||||||
|
light.ini(); |
||||||
|
mode.ini(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void callback(char* topic, byte* payload, unsigned int length) { |
||||||
|
Serial.print("Message arrived ["); |
||||||
|
Serial.print(topic); |
||||||
|
Serial.print("] "); |
||||||
|
String s = ""; |
||||||
|
for (int i = 0; i < length; i++) { |
||||||
|
s += (char)payload[i]; |
||||||
|
} |
||||||
|
Serial.print(s); |
||||||
|
Serial.println(""); |
||||||
|
|
||||||
|
mqtt_refresh(topic, s); |
||||||
|
mqtt_mode(topic, s); |
||||||
|
mqtt_lightCtl(topic, s); |
||||||
|
mqtt_light(topic, s); |
||||||
|
//mqtt_buz(topic, s);
|
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
void reconnect() { |
||||||
|
// Loop until we're reconnected
|
||||||
|
while (!client.connected()) { |
||||||
|
mode.isOffline(true); |
||||||
|
Serial.print("Attempting MQTT connection..."); |
||||||
|
|
||||||
|
// Attempt to connect
|
||||||
|
if (client.connect(clientId.c_str())) { |
||||||
|
Serial.println("connected"); |
||||||
|
mode.isOffline(false); |
||||||
|
// Once connected, publish an announcement...
|
||||||
|
// client.publish(topicOut, "Hello from the Gateway!");
|
||||||
|
// ... and resubscribe
|
||||||
|
client.subscribe(topicInCtl); |
||||||
|
client.subscribe(topicInRefresh); |
||||||
|
} else { |
||||||
|
Serial.print("failed, rc="); |
||||||
|
Serial.print(client.state()); |
||||||
|
Serial.println(" try again in 5 seconds"); |
||||||
|
// Wait 3 seconds before retrying
|
||||||
|
delay(3000); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void loop() { |
||||||
|
|
||||||
|
// mqtt core
|
||||||
|
if (!client.connected()) { |
||||||
|
reconnect(); |
||||||
|
} |
||||||
|
client.loop(); |
||||||
|
|
||||||
|
// component core
|
||||||
|
//buz.loop();
|
||||||
|
swi.loop(); |
||||||
|
led.loop(); |
||||||
|
|
||||||
|
// pir snsr
|
||||||
|
for(unsigned short i = 0; i < pirNum; i ++){ |
||||||
|
p[i].loop(); |
||||||
|
if(p[i].isPeopleIn()){ |
||||||
|
client.publish(String("hass/snsr/"+clientId+"/p"+i).c_str(), "1"); |
||||||
|
pirCnt ++; |
||||||
|
} |
||||||
|
if(p[i].isPeopleOut()){ |
||||||
|
client.publish(String("hass/snsr/"+clientId+"/p"+i).c_str(), "0"); |
||||||
|
pirCnt --; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Auto & offline Mode
|
||||||
|
if(pirCnt < 0 || pirCnt > pirNum) pirCnt = 0; |
||||||
|
if(mode.isAuto() && mode.isOffline()){ |
||||||
|
if(pirCnt > 1) { |
||||||
|
lightCtl.on(); |
||||||
|
LastActTime = millis(); |
||||||
|
} |
||||||
|
else if(pirCnt == 0 && LastActTime > millis()-5000) lightCtl.off(); |
||||||
|
} |
||||||
|
|
||||||
|
// lightCtl trigger
|
||||||
|
if(lightCtl.isStateChange()){ |
||||||
|
client.publish(String("hass/snsr/"+clientId+"/lightCtl").c_str(), String(lightCtl.getStatus()).c_str()); |
||||||
|
} |
||||||
|
// light trigger
|
||||||
|
if(light.isStateChange()){ |
||||||
|
client.publish(String("hass/snsr/"+clientId+"/light").c_str(), String(light.getStatus()).c_str()); |
||||||
|
} |
||||||
|
// buz trigger
|
||||||
|
/*if(buz.isStateChange()){
|
||||||
|
client.publish(String("hass/snsr/"+clientId+"/buz").c_str(), String(buz.getStatus()).c_str()); |
||||||
|
} */ |
||||||
|
// led trigger
|
||||||
|
if(led.isStateChange()){ |
||||||
|
client.publish(String("hass/snsr/"+clientId+"/led").c_str(), led.getMode().c_str()); |
||||||
|
} |
||||||
|
// mode trigger
|
||||||
|
if(mode.isNightChange()){ |
||||||
|
client.publish(String("hass/snsr/"+clientId+"/mode/isNight").c_str(), String(mode.isNight()).c_str()); |
||||||
|
}
|
||||||
|
if(mode.isAutoChange()){ |
||||||
|
client.publish(String("hass/snsr/"+clientId+"/mode/isAuto").c_str(), String(mode.isAuto()).c_str()); |
||||||
|
} |
||||||
|
if(mode.isMidnightChange()){ |
||||||
|
client.publish(String("hass/snsr/"+clientId+"/mode/isMidnight").c_str(), String(mode.isMidnight()).c_str()); |
||||||
|
} |
||||||
|
if(mode.isOfflineChange()){ |
||||||
|
client.publish(String("hass/snsr/"+clientId+"/mode/isOffline").c_str(), String(mode.isOffline()).c_str()); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/**** mqtt func ****/ |
||||||
|
|
||||||
|
void mqtt_refresh(const String& subject, const String& content){ |
||||||
|
|
||||||
|
// send all status
|
||||||
|
if(subject == topicInRefresh){ |
||||||
|
client.publish(String("hass/snsr/"+clientId+"/lightCtl").c_str(), String(lightCtl.getStatus()).c_str()); |
||||||
|
client.publish(String("hass/snsr/"+clientId+"/mode/isNight").c_str(), String(mode.isNight()).c_str()); |
||||||
|
client.publish(String("hass/snsr/"+clientId+"/mode/isAuto").c_str(), String(mode.isAuto()).c_str()); |
||||||
|
client.publish(String("hass/snsr/"+clientId+"/mode/isMidnight").c_str(), String(mode.isMidnight()).c_str()); |
||||||
|
client.publish(String("hass/snsr/"+clientId+"/mode/isOffline").c_str(), String(mode.isOffline()).c_str()); |
||||||
|
client.publish(String("hass/snsr/"+clientId+"/light").c_str(), String(light.getStatus()).c_str()); |
||||||
|
//client.publish(String("hass/snsr/"+clientId+"/buz").c_str(), String(buz.getStatus()).c_str());
|
||||||
|
client.publish(String("hass/snsr/"+clientId+"/swi").c_str(), String(swi.state()).c_str()); |
||||||
|
for(unsigned short i = 0; i < pirNum; i ++){ |
||||||
|
client.publish(String("hass/snsr/"+clientId+"/p"+i).c_str(), String(p[i].getState()).c_str()); |
||||||
|
} |
||||||
|
client.publish(String("hass/snsr/"+clientId+"/led").c_str(), led.getMode().c_str()); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
void mqtt_mode(const String& subject, const String& content){ |
||||||
|
|
||||||
|
if(subject == String("hass/ctl/"+clientId+"/mode/isNight")){ |
||||||
|
if(content == "0"){ |
||||||
|
mode.isNight(false); |
||||||
|
} |
||||||
|
if(content == "1"){ |
||||||
|
mode.isNight(true); |
||||||
|
} |
||||||
|
} |
||||||
|
if(subject == String("hass/ctl/"+clientId+"/mode/isAuto")){ |
||||||
|
if(content == "0"){ |
||||||
|
mode.isAuto(false); |
||||||
|
} |
||||||
|
if(content == "1"){ |
||||||
|
mode.isAuto(true); |
||||||
|
} |
||||||
|
} |
||||||
|
if(subject == String("hass/ctl/"+clientId+"/mode/isMidnight")){ |
||||||
|
if(content == "0"){ |
||||||
|
mode.isMidnight(false); |
||||||
|
} |
||||||
|
if(content == "1"){ |
||||||
|
mode.isMidnight(true); |
||||||
|
} |
||||||
|
} |
||||||
|
if(subject == String("hass/ctl/"+clientId+"/mode/isOffline")){ |
||||||
|
if(content == "0"){ |
||||||
|
mode.isOffline(false); |
||||||
|
} |
||||||
|
if(content == "1"){ |
||||||
|
mode.isOffline(true); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
void mqtt_lightCtl(const String& subject, const String& content){ |
||||||
|
if(mode.isAuto() && !mode.isOffline() && subject == String("hass/ctl/"+clientId+"/lightCtl")){ |
||||||
|
if(content == "0"){ |
||||||
|
lightCtl.off(); |
||||||
|
} |
||||||
|
if(content == "1"){ |
||||||
|
lightCtl.on(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void mqtt_light(const String& subject, const String& content){ |
||||||
|
if(subject == String("hass/ctl/"+clientId+"/light")){ |
||||||
|
if(content == "0"){ |
||||||
|
light.off(); |
||||||
|
} |
||||||
|
if(content == "1"){ |
||||||
|
light.on(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
/*void mqtt_buz(const String& subject, const String& content){
|
||||||
|
if(subject == String("hass/ctl/"+clientId+"/buz")){ |
||||||
|
if(content == "0"){ |
||||||
|
buz.off(); |
||||||
|
} |
||||||
|
if(content == "1"){ |
||||||
|
buz.on(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
*/ |
||||||
|
|
||||||
|
/**** swi ****/ |
||||||
|
void swiToggle(){ |
||||||
|
client.publish(String("hass/snsr/"+clientId+"/swi").c_str(), String(swi.state()).c_str()); |
||||||
|
//lightCtl.toggle();
|
||||||
|
light.toggle(); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**** lightCtl ****/ |
@ -0,0 +1,97 @@ |
|||||||
|
/*
|
||||||
|
* @Author: IoTcat (https://iotcat.me)
|
||||||
|
* @Date: 2019-08-20 09:58:21
|
||||||
|
* @Last Modified by:
|
||||||
|
* @Last Modified time: 2019-08-30 13:12:01 |
||||||
|
*/ |
||||||
|
#ifndef __LED_H__ |
||||||
|
#define __LED_H__ |
||||||
|
|
||||||
|
class LED { |
||||||
|
public: |
||||||
|
|
||||||
|
LED(const int& pin){ |
||||||
|
this->_pin = pin; |
||||||
|
this->_mode = "on"; |
||||||
|
}; |
||||||
|
~LED(){}; |
||||||
|
|
||||||
|
inline void on(){ |
||||||
|
digitalWrite(this->_pin, HIGH); |
||||||
|
this->_mode = "on"; |
||||||
|
} |
||||||
|
|
||||||
|
inline void off(){ |
||||||
|
digitalWrite(this->_pin, LOW); |
||||||
|
this->_mode = "off"; |
||||||
|
} |
||||||
|
|
||||||
|
inline const bool toggle(){ |
||||||
|
digitalWrite(this->_pin, !digitalRead(this->_pin)); |
||||||
|
this->_mode = (digitalRead(this->_pin)) ? "on" : "off"; |
||||||
|
return digitalRead(this->_pin); |
||||||
|
} |
||||||
|
|
||||||
|
inline void ini(){ |
||||||
|
pinMode(this->_pin, OUTPUT); |
||||||
|
this->_fStatus = this->getMode(); |
||||||
|
} |
||||||
|
|
||||||
|
inline void loop() const{ |
||||||
|
if(this->_mode == "on" || this->_mode == "off"){ |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
if(this->_mode == "star"){ |
||||||
|
if(millis() % this->_period < this->_period / 2){ |
||||||
|
digitalWrite(this->_pin, HIGH); |
||||||
|
}else{ |
||||||
|
digitalWrite(this->_pin, LOW); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if(this->_mode == "breath"){ |
||||||
|
int val; |
||||||
|
if(millis() % this->_period < this->_period / 2){ |
||||||
|
val = millis() % this->_period; |
||||||
|
val = (float)val * 255 / (this->_period / 2); |
||||||
|
}else{ |
||||||
|
val = this->_period - millis() % this->_period; |
||||||
|
val = (float)val * 255 / (this->_period / 2); |
||||||
|
} |
||||||
|
analogWrite(this->_pin, val); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
void star(const unsigned int& period = 2000){ |
||||||
|
this->_mode = "star"; |
||||||
|
this->_period = period; |
||||||
|
} |
||||||
|
|
||||||
|
void breath(const unsigned int& period = 3000){ |
||||||
|
this->_mode = "breath"; |
||||||
|
this->_period = period; |
||||||
|
} |
||||||
|
|
||||||
|
inline const String getMode() const{ |
||||||
|
return this->_mode; |
||||||
|
} |
||||||
|
|
||||||
|
inline const bool isStateChange(){ |
||||||
|
if(this->_fStatus != this->getMode()){ |
||||||
|
this->_fStatus = this->getMode(); |
||||||
|
return true; |
||||||
|
}else{ |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private: |
||||||
|
unsigned short _pin; |
||||||
|
String _mode, _fStatus; |
||||||
|
unsigned int _period; |
||||||
|
|
||||||
|
}; |
||||||
|
|
||||||
|
#endif //__LED_H__
|
@ -0,0 +1,107 @@ |
|||||||
|
/*
|
||||||
|
* @Author: IoTcat (https://iotcat.me)
|
||||||
|
* @Date: 2019-08-30 13:36:43
|
||||||
|
* @Last Modified by:
|
||||||
|
* @Last Modified time: 2019-08-30 14:17:59 |
||||||
|
*/ |
||||||
|
|
||||||
|
#ifndef __LIGHTCTL_H__ |
||||||
|
#define __LIGHTCTL_H__ |
||||||
|
|
||||||
|
class LightCtl{ |
||||||
|
public: |
||||||
|
LightCtl(Relay* _light, LED* _led, Mode* _mode){ |
||||||
|
light = _light; |
||||||
|
led = _led; |
||||||
|
mode = _mode; |
||||||
|
_fStatus = _status; |
||||||
|
}; |
||||||
|
~LightCtl(){ |
||||||
|
delete light, led, mode; |
||||||
|
}; |
||||||
|
|
||||||
|
void on(){ |
||||||
|
if(mode->isOffline()){ |
||||||
|
if(mode->nightMode() == 0){ |
||||||
|
light->off(); |
||||||
|
led->star(); |
||||||
|
} |
||||||
|
if(mode->nightMode() == 1){ |
||||||
|
light->on(); |
||||||
|
led->star(); |
||||||
|
} |
||||||
|
if(mode->nightMode() == 2){ |
||||||
|
light->off(); |
||||||
|
led->on(); |
||||||
|
} |
||||||
|
}else{ |
||||||
|
if(mode->nightMode() == 0){ |
||||||
|
light->off(); |
||||||
|
led->breath(); |
||||||
|
} |
||||||
|
if(mode->nightMode() == 1){ |
||||||
|
light->on(); |
||||||
|
led->breath(); |
||||||
|
} |
||||||
|
if(mode->nightMode() == 2){ |
||||||
|
light->off(); |
||||||
|
led->on(); |
||||||
|
} |
||||||
|
} |
||||||
|
_status = true; |
||||||
|
} |
||||||
|
void off(){ |
||||||
|
if(mode->isOffline()){ |
||||||
|
if(mode->nightMode() == 0){ |
||||||
|
light->off(); |
||||||
|
led->star(); |
||||||
|
} |
||||||
|
if(mode->nightMode() == 1){ |
||||||
|
light->off(); |
||||||
|
led->star(); |
||||||
|
} |
||||||
|
if(mode->nightMode() == 2){ |
||||||
|
light->off(); |
||||||
|
led->star(); |
||||||
|
} |
||||||
|
}else{ |
||||||
|
light->off(); |
||||||
|
led->off(); |
||||||
|
} |
||||||
|
_status = false; |
||||||
|
} |
||||||
|
|
||||||
|
inline const bool toggle(){ |
||||||
|
if(_status){ |
||||||
|
off(); |
||||||
|
}else{ |
||||||
|
on(); |
||||||
|
} |
||||||
|
return _status; |
||||||
|
} |
||||||
|
|
||||||
|
inline const bool isStateChange(){ |
||||||
|
if(_fStatus != _status){ |
||||||
|
_fStatus = _status; |
||||||
|
return true; |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
inline const bool getStatus(){ |
||||||
|
return _status; |
||||||
|
} |
||||||
|
|
||||||
|
private: |
||||||
|
Relay* light; |
||||||
|
LED* led; |
||||||
|
Mode* mode; |
||||||
|
bool _status, _fStatus; |
||||||
|
|
||||||
|
}; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif //__LIGHTCTL_H__
|
@ -0,0 +1,115 @@ |
|||||||
|
/*
|
||||||
|
* @Author: IoTcat (https://iotcat.me)
|
||||||
|
* @Date: 2019-08-30 11:14:59
|
||||||
|
* @Last Modified by:
|
||||||
|
* @Last Modified time: 2019-08-30 13:31:16 |
||||||
|
*/ |
||||||
|
|
||||||
|
#ifndef __MODE_H__ |
||||||
|
#define __MODE_H__ |
||||||
|
|
||||||
|
class Mode{ |
||||||
|
public: |
||||||
|
Mode() : _isNight(false), _fIsNight(false), _isAuto(true), _fIsAuto(true), \
|
||||||
|
_isMidnight(false), _fIsMidnight(false), _isOffline(true), _fIsOffline(true){}; |
||||||
|
~Mode(){}; |
||||||
|
|
||||||
|
inline void ini() const{ |
||||||
|
} |
||||||
|
inline void begin() const{ |
||||||
|
this->ini(); |
||||||
|
} |
||||||
|
|
||||||
|
inline void core() const{}; |
||||||
|
inline void loop() const{ |
||||||
|
this->core(); |
||||||
|
}; |
||||||
|
|
||||||
|
inline const bool isNight() const{ |
||||||
|
return this->_isNight; |
||||||
|
} |
||||||
|
inline const bool isNight(const bool& state){ |
||||||
|
this->_isNight = state; |
||||||
|
return this->isNight(); |
||||||
|
} |
||||||
|
inline const bool isNightChange(){ |
||||||
|
if(this->_isNight != this->_fIsNight){ |
||||||
|
this->_fIsNight = this->_isNight; |
||||||
|
return true; |
||||||
|
}else{ |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
inline const bool isAuto() const{ |
||||||
|
return this->_isAuto; |
||||||
|
} |
||||||
|
inline const bool isAuto(const bool& state){ |
||||||
|
this->_isAuto = state; |
||||||
|
return this->isAuto(); |
||||||
|
} |
||||||
|
inline const bool isAutoChange(){ |
||||||
|
if(this->_isAuto != this->_fIsAuto){ |
||||||
|
this->_fIsAuto = this->_isAuto; |
||||||
|
return true; |
||||||
|
}else{ |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
inline const bool isMidnight() const{ |
||||||
|
return this->_isMidnight; |
||||||
|
} |
||||||
|
inline const bool isMidnight(const bool& state){ |
||||||
|
this->_isMidnight = state; |
||||||
|
return this->isMidnight(); |
||||||
|
} |
||||||
|
inline const bool isMidnightChange(){ |
||||||
|
if(this->_isMidnight != this->_fIsMidnight){ |
||||||
|
this->_fIsMidnight = this->_isMidnight; |
||||||
|
return true; |
||||||
|
}else{ |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
inline const bool isOffline() const{ |
||||||
|
return this->_isOffline; |
||||||
|
} |
||||||
|
inline const bool isOffline(const bool& state){ |
||||||
|
this->_isOffline = state; |
||||||
|
return this->isOffline(); |
||||||
|
} |
||||||
|
inline const bool isOfflineChange(){ |
||||||
|
if(this->_isOffline != this->_fIsOffline){ |
||||||
|
this->_fIsOffline = this->_isOffline; |
||||||
|
return true; |
||||||
|
}else{ |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
inline const unsigned short nightMode(){ |
||||||
|
if(!this->isNight() && !this->isMidnight()){ |
||||||
|
return 0; |
||||||
|
} |
||||||
|
if(this->isNight() && !this->isMidnight()){ |
||||||
|
return 1; |
||||||
|
} |
||||||
|
if(this->isMidnight()){ |
||||||
|
return 2; |
||||||
|
} |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
private: |
||||||
|
bool _isNight, _isAuto, _isMidnight, _isOffline; |
||||||
|
bool _fIsNight, _fIsAuto, _fIsMidnight, _fIsOffline; |
||||||
|
|
||||||
|
|
||||||
|
}; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif //__MODE_H__
|
@ -0,0 +1,57 @@ |
|||||||
|
/*
|
||||||
|
* @Author: IoTcat (https://iotcat.me)
|
||||||
|
* @Date: 2019-08-20 11:15:20
|
||||||
|
* @Last Modified by:
|
||||||
|
* @Last Modified time: 2019-08-20 11:28:17 |
||||||
|
*/ |
||||||
|
|
||||||
|
#ifndef __PIR_H__ |
||||||
|
#define __PIR_H__ |
||||||
|
|
||||||
|
class Pir{ |
||||||
|
public: |
||||||
|
Pir(const int& pin){ |
||||||
|
this->_pin = pin; |
||||||
|
}; |
||||||
|
~Pir(){}; |
||||||
|
|
||||||
|
void ini() { |
||||||
|
pinMode(this->_pin, INPUT); |
||||||
|
this->_fState = this->getState(); |
||||||
|
}; |
||||||
|
void begin() { |
||||||
|
this->ini(); |
||||||
|
}; |
||||||
|
|
||||||
|
void loop(){ |
||||||
|
|
||||||
|
}; |
||||||
|
|
||||||
|
const bool isPeopleIn(){ |
||||||
|
if(this->_fState == false && this->getState() == true){ |
||||||
|
this->_fState = true; |
||||||
|
return true; |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
const bool isPeopleOut(){ |
||||||
|
if(this->_fState == true && this->getState() == false){ |
||||||
|
this->_fState = false; |
||||||
|
return true; |
||||||
|
} |
||||||
|
return false; |
||||||
|
}; |
||||||
|
|
||||||
|
const bool getState(){ |
||||||
|
return digitalRead(this->_pin); |
||||||
|
}; |
||||||
|
|
||||||
|
private: |
||||||
|
unsigned short _pin; |
||||||
|
bool _fState; |
||||||
|
|
||||||
|
}; |
||||||
|
|
||||||
|
|
||||||
|
#endif //__PIR_H__
|
@ -0,0 +1,62 @@ |
|||||||
|
/*
|
||||||
|
* @Author: IoTcat (https://iotcat.me)
|
||||||
|
* @Date: 2019-08-30 11:00:00
|
||||||
|
* @Last Modified by:
|
||||||
|
* @Last Modified time: 2019-08-30 12:35:33 |
||||||
|
*/ |
||||||
|
|
||||||
|
#ifndef __RELAY_H__ |
||||||
|
#define __RELAY_H__ |
||||||
|
|
||||||
|
class Relay{ |
||||||
|
public: |
||||||
|
Relay(const int& pin, const bool& onStatus = true){ |
||||||
|
this->_pin = pin; |
||||||
|
this->_onStatus = onStatus; |
||||||
|
}; |
||||||
|
~Relay(){}; |
||||||
|
|
||||||
|
inline void ini() { |
||||||
|
pinMode(this->_pin, OUTPUT); |
||||||
|
this->_fStatus = this->getStatus(); |
||||||
|
}; |
||||||
|
inline void begin() { |
||||||
|
this->ini(); |
||||||
|
}; |
||||||
|
inline void core() const{}; |
||||||
|
inline void loop() const{ |
||||||
|
this->core(); |
||||||
|
} |
||||||
|
|
||||||
|
inline void on() const{ |
||||||
|
digitalWrite(this->_pin, _onStatus); |
||||||
|
} |
||||||
|
inline void off() const{ |
||||||
|
digitalWrite(this->_pin, !_onStatus); |
||||||
|
} |
||||||
|
inline const bool toggle() const{ |
||||||
|
digitalWrite(this->_pin, !digitalRead(this->_pin)); |
||||||
|
return this->getStatus(); |
||||||
|
} |
||||||
|
inline const bool getStatus() const{ |
||||||
|
return (digitalRead(this->_pin) == this->_onStatus) ? true : false; |
||||||
|
} |
||||||
|
|
||||||
|
inline const bool isStateChange() { |
||||||
|
if(this->getStatus() != this->_fStatus){ |
||||||
|
this->_fStatus = this->getStatus(); |
||||||
|
return true; |
||||||
|
}else{ |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private: |
||||||
|
unsigned short _pin; |
||||||
|
bool _onStatus, _fStatus; |
||||||
|
|
||||||
|
}; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif //__RELAY_H__
|
@ -0,0 +1,91 @@ |
|||||||
|
/*
|
||||||
|
* @Author: IoTcat (https://iotcat.me)
|
||||||
|
* @Date: 2019-08-20 10:04:18
|
||||||
|
* @Last Modified by:
|
||||||
|
* @Last Modified time: 2019-08-20 10:40:35 |
||||||
|
*/ |
||||||
|
|
||||||
|
#ifndef __SWI_H__ |
||||||
|
#define __SWI_H__ |
||||||
|
|
||||||
|
class Swi{ |
||||||
|
public: |
||||||
|
Swi(const int& pin0, const int& pin1){ |
||||||
|
_pin0 = pin0; |
||||||
|
_pin1 = pin1; |
||||||
|
};
|
||||||
|
~Swi(){}; |
||||||
|
|
||||||
|
static inline void ini() { |
||||||
|
pinMode(_pin0, INPUT); |
||||||
|
pinMode(_pin1, OUTPUT); |
||||||
|
_fState = state(); |
||||||
|
}; |
||||||
|
|
||||||
|
static inline void begin() { |
||||||
|
ini(); |
||||||
|
}; |
||||||
|
|
||||||
|
static inline void loop(){ |
||||||
|
const bool sta = state(); |
||||||
|
|
||||||
|
if(sta != _fState){ |
||||||
|
if(sta == true){ |
||||||
|
if(_isDefOn) _on(); |
||||||
|
}else{ |
||||||
|
if(_isDefOff) _off(); |
||||||
|
} |
||||||
|
if(_isDefToggle) _toggle(); |
||||||
|
_fState = sta; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
static inline void on(const String& event, void (*f)(void)){ |
||||||
|
if(event == "on"){ |
||||||
|
_on = f; |
||||||
|
_isDefOn = true; |
||||||
|
} |
||||||
|
if(event == "off"){ |
||||||
|
_off = f; |
||||||
|
_isDefOff = true; |
||||||
|
} |
||||||
|
if(event == "toggle"){ |
||||||
|
_toggle = f; |
||||||
|
_isDefToggle = true; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
static const bool state(const unsigned int& total = 600, const unsigned int& threshole = 500){ |
||||||
|
unsigned int cnt = 0; |
||||||
|
for(unsigned int i = 0; i < total; i ++){ |
||||||
|
digitalWrite(_pin1, i % 2); |
||||||
|
if(digitalRead(_pin0) == i%2){ |
||||||
|
cnt ++; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if(cnt >= threshole){ |
||||||
|
return true; |
||||||
|
}else{ |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private: |
||||||
|
static unsigned short _pin0, _pin1; |
||||||
|
static bool _fState; |
||||||
|
static bool _isDefOn, _isDefOff, _isDefToggle; |
||||||
|
static void (*_on)(void); |
||||||
|
static void (*_off)(void); |
||||||
|
static void (*_toggle)(void); |
||||||
|
|
||||||
|
|
||||||
|
}; |
||||||
|
|
||||||
|
unsigned short Swi::_pin0, Swi::_pin1; |
||||||
|
bool Swi::_fState, Swi::_isDefOn = false, Swi::_isDefOff = false, Swi::_isDefToggle = false; |
||||||
|
void (*Swi::_on)(void); |
||||||
|
void (*Swi::_off)(void); |
||||||
|
void (*Swi::_toggle)(void); |
||||||
|
|
||||||
|
#endif //__SWI_H__
|
@ -0,0 +1,107 @@ |
|||||||
|
/*
|
||||||
|
* @Author: IoTcat (https://iotcat.me)
|
||||||
|
* @Date: 2019-08-30 13:36:43
|
||||||
|
* @Last Modified by:
|
||||||
|
* @Last Modified time: 2019-08-30 14:17:59 |
||||||
|
*/ |
||||||
|
|
||||||
|
#ifndef __LIGHTCTL_H__ |
||||||
|
#define __LIGHTCTL_H__ |
||||||
|
|
||||||
|
class LightCtl{ |
||||||
|
public: |
||||||
|
LightCtl(Relay* _light, LED* _led, Mode* _mode){ |
||||||
|
light = _light; |
||||||
|
led = _led; |
||||||
|
mode = _mode; |
||||||
|
_fStatus = _status; |
||||||
|
}; |
||||||
|
~LightCtl(){ |
||||||
|
delete light, led, mode; |
||||||
|
}; |
||||||
|
|
||||||
|
void on(){ |
||||||
|
if(mode->isOffline()){ |
||||||
|
if(mode->nightMode() == 0){ |
||||||
|
light->off(); |
||||||
|
led->star(); |
||||||
|
} |
||||||
|
if(mode->nightMode() == 1){ |
||||||
|
light->on(); |
||||||
|
led->star(); |
||||||
|
} |
||||||
|
if(mode->nightMode() == 2){ |
||||||
|
light->off(); |
||||||
|
led->on(); |
||||||
|
} |
||||||
|
}else{ |
||||||
|
if(mode->nightMode() == 0){ |
||||||
|
light->off(); |
||||||
|
led->breath(); |
||||||
|
} |
||||||
|
if(mode->nightMode() == 1){ |
||||||
|
light->on(); |
||||||
|
led->breath(); |
||||||
|
} |
||||||
|
if(mode->nightMode() == 2){ |
||||||
|
light->off(); |
||||||
|
led->on(); |
||||||
|
} |
||||||
|
} |
||||||
|
_status = true; |
||||||
|
} |
||||||
|
void off(){ |
||||||
|
if(mode->isOffline()){ |
||||||
|
if(mode->nightMode() == 0){ |
||||||
|
light->off(); |
||||||
|
led->star(); |
||||||
|
} |
||||||
|
if(mode->nightMode() == 1){ |
||||||
|
light->off(); |
||||||
|
led->star(); |
||||||
|
} |
||||||
|
if(mode->nightMode() == 2){ |
||||||
|
light->off(); |
||||||
|
led->star(); |
||||||
|
} |
||||||
|
}else{ |
||||||
|
light->off(); |
||||||
|
led->off(); |
||||||
|
} |
||||||
|
_status = false; |
||||||
|
} |
||||||
|
|
||||||
|
inline const bool toggle(){ |
||||||
|
if(_status){ |
||||||
|
off(); |
||||||
|
}else{ |
||||||
|
on(); |
||||||
|
} |
||||||
|
return _status; |
||||||
|
} |
||||||
|
|
||||||
|
inline const bool isStateChange(){ |
||||||
|
if(_fStatus != _status){ |
||||||
|
_fStatus = _status; |
||||||
|
return true; |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
inline const bool getStatus(){ |
||||||
|
return _status; |
||||||
|
} |
||||||
|
|
||||||
|
private: |
||||||
|
Relay* light; |
||||||
|
LED* led; |
||||||
|
Mode* mode; |
||||||
|
bool _status, _fStatus; |
||||||
|
|
||||||
|
}; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif //__LIGHTCTL_H__
|
@ -0,0 +1,115 @@ |
|||||||
|
/*
|
||||||
|
* @Author: IoTcat (https://iotcat.me)
|
||||||
|
* @Date: 2019-08-30 11:14:59
|
||||||
|
* @Last Modified by:
|
||||||
|
* @Last Modified time: 2019-08-30 13:31:16 |
||||||
|
*/ |
||||||
|
|
||||||
|
#ifndef __MODE_H__ |
||||||
|
#define __MODE_H__ |
||||||
|
|
||||||
|
class Mode{ |
||||||
|
public: |
||||||
|
Mode() : _isNight(false), _fIsNight(false), _isAuto(true), _fIsAuto(true), \
|
||||||
|
_isMidnight(false), _fIsMidnight(false), _isOffline(true), _fIsOffline(true){}; |
||||||
|
~Mode(){}; |
||||||
|
|
||||||
|
inline void ini() const{ |
||||||
|
} |
||||||
|
inline void begin() const{ |
||||||
|
this->ini(); |
||||||
|
} |
||||||
|
|
||||||
|
inline void core() const{}; |
||||||
|
inline void loop() const{ |
||||||
|
this->core(); |
||||||
|
}; |
||||||
|
|
||||||
|
inline const bool isNight() const{ |
||||||
|
return this->_isNight; |
||||||
|
} |
||||||
|
inline const bool isNight(const bool& state){ |
||||||
|
this->_isNight = state; |
||||||
|
return this->isNight(); |
||||||
|
} |
||||||
|
inline const bool isNightChange(){ |
||||||
|
if(this->_isNight != this->_fIsNight){ |
||||||
|
this->_fIsNight = this->_isNight; |
||||||
|
return true; |
||||||
|
}else{ |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
inline const bool isAuto() const{ |
||||||
|
return this->_isAuto; |
||||||
|
} |
||||||
|
inline const bool isAuto(const bool& state){ |
||||||
|
this->_isAuto = state; |
||||||
|
return this->isAuto(); |
||||||
|
} |
||||||
|
inline const bool isAutoChange(){ |
||||||
|
if(this->_isAuto != this->_fIsAuto){ |
||||||
|
this->_fIsAuto = this->_isAuto; |
||||||
|
return true; |
||||||
|
}else{ |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
inline const bool isMidnight() const{ |
||||||
|
return this->_isMidnight; |
||||||
|
} |
||||||
|
inline const bool isMidnight(const bool& state){ |
||||||
|
this->_isMidnight = state; |
||||||
|
return this->isMidnight(); |
||||||
|
} |
||||||
|
inline const bool isMidnightChange(){ |
||||||
|
if(this->_isMidnight != this->_fIsMidnight){ |
||||||
|
this->_fIsMidnight = this->_isMidnight; |
||||||
|
return true; |
||||||
|
}else{ |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
inline const bool isOffline() const{ |
||||||
|
return this->_isOffline; |
||||||
|
} |
||||||
|
inline const bool isOffline(const bool& state){ |
||||||
|
this->_isOffline = state; |
||||||
|
return this->isOffline(); |
||||||
|
} |
||||||
|
inline const bool isOfflineChange(){ |
||||||
|
if(this->_isOffline != this->_fIsOffline){ |
||||||
|
this->_fIsOffline = this->_isOffline; |
||||||
|
return true; |
||||||
|
}else{ |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
inline const unsigned short nightMode(){ |
||||||
|
if(!this->isNight() && !this->isMidnight()){ |
||||||
|
return 0; |
||||||
|
} |
||||||
|
if(this->isNight() && !this->isMidnight()){ |
||||||
|
return 1; |
||||||
|
} |
||||||
|
if(this->isMidnight()){ |
||||||
|
return 2; |
||||||
|
} |
||||||
|
return 0; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
private: |
||||||
|
bool _isNight, _isAuto, _isMidnight, _isOffline; |
||||||
|
bool _fIsNight, _fIsAuto, _fIsMidnight, _fIsOffline; |
||||||
|
|
||||||
|
|
||||||
|
}; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif //__MODE_H__
|
@ -0,0 +1,57 @@ |
|||||||
|
/*
|
||||||
|
* @Author: IoTcat (https://iotcat.me)
|
||||||
|
* @Date: 2019-08-20 11:15:20
|
||||||
|
* @Last Modified by:
|
||||||
|
* @Last Modified time: 2019-08-20 11:28:17 |
||||||
|
*/ |
||||||
|
|
||||||
|
#ifndef __PIR_H__ |
||||||
|
#define __PIR_H__ |
||||||
|
|
||||||
|
class Pir{ |
||||||
|
public: |
||||||
|
Pir(const int& pin){ |
||||||
|
this->_pin = pin; |
||||||
|
}; |
||||||
|
~Pir(){}; |
||||||
|
|
||||||
|
void ini() { |
||||||
|
pinMode(this->_pin, INPUT); |
||||||
|
this->_fState = this->getState(); |
||||||
|
}; |
||||||
|
void begin() { |
||||||
|
this->ini(); |
||||||
|
}; |
||||||
|
|
||||||
|
void loop(){ |
||||||
|
|
||||||
|
}; |
||||||
|
|
||||||
|
const bool isPeopleIn(){ |
||||||
|
if(this->_fState == false && this->getState() == true){ |
||||||
|
this->_fState = true; |
||||||
|
return true; |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
const bool isPeopleOut(){ |
||||||
|
if(this->_fState == true && this->getState() == false){ |
||||||
|
this->_fState = false; |
||||||
|
return true; |
||||||
|
} |
||||||
|
return false; |
||||||
|
}; |
||||||
|
|
||||||
|
const bool getState(){ |
||||||
|
return digitalRead(this->_pin); |
||||||
|
}; |
||||||
|
|
||||||
|
private: |
||||||
|
unsigned short _pin; |
||||||
|
bool _fState; |
||||||
|
|
||||||
|
}; |
||||||
|
|
||||||
|
|
||||||
|
#endif //__PIR_H__
|
@ -0,0 +1,62 @@ |
|||||||
|
/*
|
||||||
|
* @Author: IoTcat (https://iotcat.me)
|
||||||
|
* @Date: 2019-08-30 11:00:00
|
||||||
|
* @Last Modified by:
|
||||||
|
* @Last Modified time: 2019-08-30 12:35:33 |
||||||
|
*/ |
||||||
|
|
||||||
|
#ifndef __RELAY_H__ |
||||||
|
#define __RELAY_H__ |
||||||
|
|
||||||
|
class Relay{ |
||||||
|
public: |
||||||
|
Relay(const int& pin, const bool& onStatus = true){ |
||||||
|
this->_pin = pin; |
||||||
|
this->_onStatus = onStatus; |
||||||
|
}; |
||||||
|
~Relay(){}; |
||||||
|
|
||||||
|
inline void ini() { |
||||||
|
pinMode(this->_pin, OUTPUT); |
||||||
|
this->_fStatus = this->getStatus(); |
||||||
|
}; |
||||||
|
inline void begin() { |
||||||
|
this->ini(); |
||||||
|
}; |
||||||
|
inline void core() const{}; |
||||||
|
inline void loop() const{ |
||||||
|
this->core(); |
||||||
|
} |
||||||
|
|
||||||
|
inline void on() const{ |
||||||
|
digitalWrite(this->_pin, _onStatus); |
||||||
|
} |
||||||
|
inline void off() const{ |
||||||
|
digitalWrite(this->_pin, !_onStatus); |
||||||
|
} |
||||||
|
inline const bool toggle() const{ |
||||||
|
digitalWrite(this->_pin, !digitalRead(this->_pin)); |
||||||
|
return this->getStatus(); |
||||||
|
} |
||||||
|
inline const bool getStatus() const{ |
||||||
|
return (digitalRead(this->_pin) == this->_onStatus) ? true : false; |
||||||
|
} |
||||||
|
|
||||||
|
inline const bool isStateChange() { |
||||||
|
if(this->getStatus() != this->_fStatus){ |
||||||
|
this->_fStatus = this->getStatus(); |
||||||
|
return true; |
||||||
|
}else{ |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private: |
||||||
|
unsigned short _pin; |
||||||
|
bool _onStatus, _fStatus; |
||||||
|
|
||||||
|
}; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif //__RELAY_H__
|
@ -0,0 +1,91 @@ |
|||||||
|
/*
|
||||||
|
* @Author: IoTcat (https://iotcat.me)
|
||||||
|
* @Date: 2019-08-20 10:04:18
|
||||||
|
* @Last Modified by:
|
||||||
|
* @Last Modified time: 2019-08-20 10:40:35 |
||||||
|
*/ |
||||||
|
|
||||||
|
#ifndef __SWI_H__ |
||||||
|
#define __SWI_H__ |
||||||
|
|
||||||
|
class Swi{ |
||||||
|
public: |
||||||
|
Swi(const int& pin0, const int& pin1){ |
||||||
|
_pin0 = pin0; |
||||||
|
_pin1 = pin1; |
||||||
|
};
|
||||||
|
~Swi(){}; |
||||||
|
|
||||||
|
static inline void ini() { |
||||||
|
pinMode(_pin0, INPUT); |
||||||
|
pinMode(_pin1, OUTPUT); |
||||||
|
_fState = state(); |
||||||
|
}; |
||||||
|
|
||||||
|
static inline void begin() { |
||||||
|
ini(); |
||||||
|
}; |
||||||
|
|
||||||
|
static inline void loop(){ |
||||||
|
const bool sta = state(); |
||||||
|
|
||||||
|
if(sta != _fState){ |
||||||
|
if(sta == true){ |
||||||
|
if(_isDefOn) _on(); |
||||||
|
}else{ |
||||||
|
if(_isDefOff) _off(); |
||||||
|
} |
||||||
|
if(_isDefToggle) _toggle(); |
||||||
|
_fState = sta; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
static inline void on(const String& event, void (*f)(void)){ |
||||||
|
if(event == "on"){ |
||||||
|
_on = f; |
||||||
|
_isDefOn = true; |
||||||
|
} |
||||||
|
if(event == "off"){ |
||||||
|
_off = f; |
||||||
|
_isDefOff = true; |
||||||
|
} |
||||||
|
if(event == "toggle"){ |
||||||
|
_toggle = f; |
||||||
|
_isDefToggle = true; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
static const bool state(const unsigned int& total = 600, const unsigned int& threshole = 500){ |
||||||
|
unsigned int cnt = 0; |
||||||
|
for(unsigned int i = 0; i < total; i ++){ |
||||||
|
digitalWrite(_pin1, i % 2); |
||||||
|
if(digitalRead(_pin0) == i%2){ |
||||||
|
cnt ++; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if(cnt >= threshole){ |
||||||
|
return true; |
||||||
|
}else{ |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private: |
||||||
|
static unsigned short _pin0, _pin1; |
||||||
|
static bool _fState; |
||||||
|
static bool _isDefOn, _isDefOff, _isDefToggle; |
||||||
|
static void (*_on)(void); |
||||||
|
static void (*_off)(void); |
||||||
|
static void (*_toggle)(void); |
||||||
|
|
||||||
|
|
||||||
|
}; |
||||||
|
|
||||||
|
unsigned short Swi::_pin0, Swi::_pin1; |
||||||
|
bool Swi::_fState, Swi::_isDefOn = false, Swi::_isDefOff = false, Swi::_isDefToggle = false; |
||||||
|
void (*Swi::_on)(void); |
||||||
|
void (*Swi::_off)(void); |
||||||
|
void (*Swi::_toggle)(void); |
||||||
|
|
||||||
|
#endif //__SWI_H__
|
Loading…
Reference in new issue