buttons.ino 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. void updateButtonStates() {
  2. // Read the state of each button and update the boolean array
  3. bool pressed = false;
  4. for (int i = 0; i < numButtons - 1; ++i) {
  5. pressed = digitalRead(buttonPins[i]) == LOW;
  6. if (pressed != buttonStates[i]) {
  7. if (!buttonStates[i]) {
  8. //Button down
  9. Serial.print("[INFO] keydown=");
  10. Serial.println(i);
  11. handleButtonDown(i);
  12. } else {
  13. //Button up
  14. Serial.print("[INFO] keyup=");
  15. Serial.println(i);
  16. handleButtonUp(i);
  17. }
  18. buttonStates[i] = pressed;
  19. }
  20. }
  21. //Read the analog emulated pins
  22. int a0Valu = analogRead(A0);
  23. pressed = (a0Valu < 1000);
  24. if (pressed != buttonStates[4]) {
  25. if (!buttonStates[4]) {
  26. //Button down
  27. Serial.println("[INFO] keydown=4");
  28. handleButtonDown(4);
  29. } else {
  30. //Button up
  31. Serial.println("[INFO] keyup=4");
  32. handleButtonUp(4);
  33. }
  34. buttonStates[4] = pressed;
  35. }
  36. }
  37. // Function to reset all outputs to 0
  38. void resetOutputs() {
  39. // Shift in 8 zeros (all outputs off)
  40. for (int i = 0; i < 8; ++i) {
  41. digitalWrite(SER_PIN, LOW);
  42. digitalWrite(SRCLK_PIN, HIGH);
  43. delay(1); // Optional: Adjust delay if needed
  44. digitalWrite(SRCLK_PIN, LOW);
  45. }
  46. // Latch the data to the output pins
  47. digitalWrite(RCLK_PIN, HIGH);
  48. delay(1); // Optional: Adjust delay if needed
  49. digitalWrite(RCLK_PIN, LOW);
  50. }
  51. //Handle button down and up events, index start from 0
  52. void handleButtonDown(int index) {
  53. }
  54. void handleButtonUp(int index) {
  55. //Generate the base URL from ip and port
  56. String baseURL = "http://" + target_ip.toString() + ":" + String(target_port);
  57. if (index == 0){
  58. baseURL += "/toggle?relay=0";
  59. }else if (index == 1){
  60. baseURL += "/toggle?relay=1";
  61. }else if (index == 2){
  62. baseURL += "/toggle?relay=2";
  63. }else if (index == 3){
  64. baseURL += "/toggle?relay=3";
  65. }else if (index == 4){
  66. resolveTargetRelayIP();
  67. return;
  68. }
  69. Serial.println("Sending request to: " + baseURL);
  70. GetRequest(baseURL);
  71. }