mirror of
https://github.com/H3ALY/Wireless-PC-Switch.git
synced 2025-12-19 18:28:22 +00:00
Create power_control.h
This commit is contained in:
committed by
GitHub
parent
f70e643b2e
commit
702396f8a2
60
core/power_control.h
Normal file
60
core/power_control.h
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
#ifndef POWER_CONTROL_H
|
||||||
|
#define POWER_CONTROL_H
|
||||||
|
|
||||||
|
#include <PubSubClient.h>
|
||||||
|
#include <SimpleTimer.h>
|
||||||
|
|
||||||
|
class PowerController {
|
||||||
|
private:
|
||||||
|
const int powerButtonPin;
|
||||||
|
const int powerSensePin;
|
||||||
|
String currentStatus = "OFF";
|
||||||
|
String oldStatus = "OFF";
|
||||||
|
PubSubClient& client;
|
||||||
|
|
||||||
|
public:
|
||||||
|
PowerController(int btnPin, int sensePin, PubSubClient& mqttClient)
|
||||||
|
: powerButtonPin(btnPin), powerSensePin(sensePin), client(mqttClient) {}
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
pinMode(powerSensePin, INPUT_PULLDOWN_16);
|
||||||
|
pinMode(powerButtonPin, OUTPUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
void sensePower() {
|
||||||
|
if (pulseIn(powerSensePin, HIGH, 300000) > 100) {
|
||||||
|
setStatus("ON");
|
||||||
|
} else if (digitalRead(powerSensePin) == HIGH) {
|
||||||
|
setStatus("ON");
|
||||||
|
} else {
|
||||||
|
setStatus("OFF");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void setStatus(String status) {
|
||||||
|
if (currentStatus != status) {
|
||||||
|
currentStatus = status;
|
||||||
|
client.publish("state/PC", status.c_str(), true);
|
||||||
|
oldStatus = currentStatus;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void handleCommand(const String& command) {
|
||||||
|
if (command == "ON" && currentStatus == "OFF") {
|
||||||
|
pressPowerButton();
|
||||||
|
} else if (command == "OFF" && currentStatus == "ON") {
|
||||||
|
pressPowerButton();
|
||||||
|
} else if (command == "FORCE OFF") {
|
||||||
|
pressPowerButton(10000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
void pressPowerButton(int pressDuration = 1000) {
|
||||||
|
digitalWrite(powerButtonPin, HIGH);
|
||||||
|
delay(pressDuration);
|
||||||
|
digitalWrite(powerButtonPin, LOW);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user