inputs.ino 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. void checkPowerLEDInput() {
  2. in_pwrled_currState = digitalRead(PIN_IN_PWRLED);
  3. if (in_pwrled_lastState == !IN_PWRLED_ONSTATE && in_pwrled_currState == IN_PWRLED_ONSTATE) {
  4. // PC power LED changed to ON
  5. in_pwrled_lastState = in_pwrled_currState;
  6. in_pwrled_millis = millis();
  7. }
  8. else if (in_pwrled_lastState == IN_PWRLED_ONSTATE && in_pwrled_currState == !IN_PWRLED_ONSTATE) {
  9. // PC power LED changed to OFF
  10. in_pwrled_lastState = in_pwrled_currState;
  11. in_pwrled_millis = millis();
  12. }
  13. else if (in_pwrled_millis == 0) { // get initial state after boot
  14. in_pwrled_lastState = in_pwrled_currState;
  15. in_pwrled_millis = millis();
  16. }
  17. if (in_pwrled_millis > 0 && (millis() - in_pwrled_millis) > 2000) {
  18. // last Power LED status change was more than 2s ago
  19. if (in_pwrled_currState == IN_PWRLED_ONSTATE) { //LED is ON
  20. if (PCstate != 1) {
  21. PCstate = 1;
  22. Serial.println("PC state changed to ON");
  23. publishPCPowerState(false);
  24. PC_standby_pending_millis = 0;
  25. }
  26. }
  27. else if (in_pwrled_currState == !IN_PWRLED_ONSTATE) { //LED is OFF
  28. if (PCstate != 0) {
  29. PCstate = 0;
  30. Serial.println("PC state changed to OFF");
  31. publishPCPowerState(false);
  32. PC_standby_pending_millis = 0;
  33. }
  34. }
  35. }
  36. else if ( PCstate != 2 && in_pwrled_millis > 0 && (millis() - in_pwrled_millis) > 100 ) {
  37. // Power LED toggles permanently -> it blinks because PC is in standby
  38. if ( PCstate != 2 && PC_standby_pending_millis == 0 ) {
  39. PC_standby_pending_millis = millis();
  40. //Serial.println("PC state - pending status");
  41. }
  42. else if ( PC_standby_pending_millis > 0 && (millis() - PC_standby_pending_millis) > 2500 ) {
  43. PCstate = 2;
  44. Serial.println("PC state changed to SLEEP");
  45. publishPCPowerState(false);
  46. }
  47. }
  48. }
  49. void publishPCPowerState(boolean force) {
  50. if(!mqtt_outRetain || PCstate != PCstate_lastPublished || force) {
  51. PCstate_lastPublished = PCstate;
  52. if(PCstate == 0) {
  53. sprintf(tmp_topic_pub, "%s/%s", mqtt_topic_out, "PC-PowerStateText");
  54. mqttclient.publish(tmp_topic_pub, "OFF", mqtt_outRetain);
  55. sprintf(tmp_topic_pub, "%s/%s", mqtt_topic_out, "PC-SleepState");
  56. mqttclient.publish(tmp_topic_pub, "OFF", mqtt_outRetain);
  57. sprintf(tmp_topic_pub, "%s/%s", mqtt_topic_out, "PC-PowerOnState");
  58. mqttclient.publish(tmp_topic_pub, "OFF", mqtt_outRetain);
  59. }
  60. else if(PCstate == 1) {
  61. sprintf(tmp_topic_pub, "%s/%s", mqtt_topic_out, "PC-PowerStateText");
  62. mqttclient.publish(tmp_topic_pub, "ON", mqtt_outRetain);
  63. sprintf(tmp_topic_pub, "%s/%s", mqtt_topic_out, "PC-SleepState");
  64. mqttclient.publish(tmp_topic_pub, "OFF", mqtt_outRetain);
  65. sprintf(tmp_topic_pub, "%s/%s", mqtt_topic_out, "PC-PowerOnState");
  66. mqttclient.publish(tmp_topic_pub, "ON", mqtt_outRetain);
  67. }
  68. else if(PCstate == 2) {
  69. sprintf(tmp_topic_pub, "%s/%s", mqtt_topic_out, "PC-PowerStateText");
  70. mqttclient.publish(tmp_topic_pub, "SLEEP", mqtt_outRetain);
  71. sprintf(tmp_topic_pub, "%s/%s", mqtt_topic_out, "PC-SleepState");
  72. mqttclient.publish(tmp_topic_pub, "ON", mqtt_outRetain);
  73. sprintf(tmp_topic_pub, "%s/%s", mqtt_topic_out, "PC-PowerOnState");
  74. mqttclient.publish(tmp_topic_pub, "OFF", mqtt_outRetain);
  75. }
  76. }
  77. }