12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- void checkPowerLEDInput() {
- in_pwrled_currState = digitalRead(PIN_IN_PWRLED);
- if (in_pwrled_lastState == !IN_PWRLED_ONSTATE && in_pwrled_currState == IN_PWRLED_ONSTATE) {
- // PC power LED changed to ON
- in_pwrled_lastState = in_pwrled_currState;
- in_pwrled_millis = millis();
- }
- else if (in_pwrled_lastState == IN_PWRLED_ONSTATE && in_pwrled_currState == !IN_PWRLED_ONSTATE) {
- // PC power LED changed to OFF
- in_pwrled_lastState = in_pwrled_currState;
- in_pwrled_millis = millis();
- }
- else if (in_pwrled_millis == 0) { // get initial state after boot
- in_pwrled_lastState = in_pwrled_currState;
- in_pwrled_millis = millis();
- }
- if (in_pwrled_millis > 0 && (millis() - in_pwrled_millis) > 2000) {
- // last Power LED status change was more than 2s ago
- if (in_pwrled_currState == IN_PWRLED_ONSTATE) { //LED is ON
- if (PCstate != 1) {
- PCstate = 1;
- Serial.println("PC state changed to ON");
- publishPCPowerState(false);
- PC_standby_pending_millis = 0;
- }
- }
- else if (in_pwrled_currState == !IN_PWRLED_ONSTATE) { //LED is OFF
- if (PCstate != 0) {
- PCstate = 0;
- Serial.println("PC state changed to OFF");
- publishPCPowerState(false);
- PC_standby_pending_millis = 0;
- }
- }
- }
- else if ( PCstate != 2 && in_pwrled_millis > 0 && (millis() - in_pwrled_millis) > 100 ) {
- // Power LED toggles permanently -> it blinks because PC is in standby
- if ( PCstate != 2 && PC_standby_pending_millis == 0 ) {
- PC_standby_pending_millis = millis();
- //Serial.println("PC state - pending status");
- }
- else if ( PC_standby_pending_millis > 0 && (millis() - PC_standby_pending_millis) > 2500 ) {
- PCstate = 2;
- Serial.println("PC state changed to SLEEP");
- publishPCPowerState(false);
- }
- }
- }
- void publishPCPowerState(boolean force) {
- if(!mqtt_outRetain || PCstate != PCstate_lastPublished || force) {
- PCstate_lastPublished = PCstate;
- if(PCstate == 0) {
- sprintf(tmp_topic_pub, "%s/%s", mqtt_topic_out, "PC-PowerStateText");
- mqttclient.publish(tmp_topic_pub, "OFF", mqtt_outRetain);
- sprintf(tmp_topic_pub, "%s/%s", mqtt_topic_out, "PC-SleepState");
- mqttclient.publish(tmp_topic_pub, "OFF", mqtt_outRetain);
- sprintf(tmp_topic_pub, "%s/%s", mqtt_topic_out, "PC-PowerOnState");
- mqttclient.publish(tmp_topic_pub, "OFF", mqtt_outRetain);
- }
- else if(PCstate == 1) {
- sprintf(tmp_topic_pub, "%s/%s", mqtt_topic_out, "PC-PowerStateText");
- mqttclient.publish(tmp_topic_pub, "ON", mqtt_outRetain);
- sprintf(tmp_topic_pub, "%s/%s", mqtt_topic_out, "PC-SleepState");
- mqttclient.publish(tmp_topic_pub, "OFF", mqtt_outRetain);
- sprintf(tmp_topic_pub, "%s/%s", mqtt_topic_out, "PC-PowerOnState");
- mqttclient.publish(tmp_topic_pub, "ON", mqtt_outRetain);
- }
- else if(PCstate == 2) {
- sprintf(tmp_topic_pub, "%s/%s", mqtt_topic_out, "PC-PowerStateText");
- mqttclient.publish(tmp_topic_pub, "SLEEP", mqtt_outRetain);
- sprintf(tmp_topic_pub, "%s/%s", mqtt_topic_out, "PC-SleepState");
- mqttclient.publish(tmp_topic_pub, "ON", mqtt_outRetain);
- sprintf(tmp_topic_pub, "%s/%s", mqtt_topic_out, "PC-PowerOnState");
- mqttclient.publish(tmp_topic_pub, "OFF", mqtt_outRetain);
- }
- }
- }
|