From 06a286eadd8242eb51fdbd9cbb00ef6e27cb19d0 Mon Sep 17 00:00:00 2001 From: Robin Cerny Date: Mon, 3 Mar 2025 20:41:12 +0100 Subject: [PATCH] fixed remote PitHold, added persistant boardID --- ROFLS_Arena_Controller/Input_Handler.ino | 16 +- .../ROFLS_Arena_Controller.ino | 15 +- ROFLS_Arena_Remote/Input_Handler.ino | 28 ++-- ROFLS_Arena_Remote/ROFLS_Arena_Remote.ino | 152 ++++++++++-------- 4 files changed, 113 insertions(+), 98 deletions(-) diff --git a/ROFLS_Arena_Controller/Input_Handler.ino b/ROFLS_Arena_Controller/Input_Handler.ino index 313739a..70f9606 100644 --- a/ROFLS_Arena_Controller/Input_Handler.ino +++ b/ROFLS_Arena_Controller/Input_Handler.ino @@ -9,13 +9,15 @@ void pollInput (){ switchRUMBLE.poll(); switchPIT.poll(); - buttonSTARTvar = buttonSTART.singleClick(); - buttonSTARTforced = buttonSTART.longPress(); - buttonPAUSEvar = buttonPAUSE.singleClick(); - buttonPITvar = buttonPIT.singleClick(); - buttonPIThold = buttonPIT.on(); - buttonRESETvar = buttonRESET.longPress(); - + // only set the var if the button was actually pushed or released, to prevent overriding data from the remote + if (buttonSTART.singleClick() || buttonSTART.longPress() || buttonPAUSE.singleClick() || buttonPIT.singleClick() || buttonPIT.switched() || buttonRESET.longPress()) { + buttonSTARTvar = buttonSTART.singleClick(); + buttonSTARTforced = buttonSTART.longPress(); + buttonPAUSEvar = buttonPAUSE.singleClick(); + buttonPITvar = buttonPIT.singleClick(); + buttonPIThold = buttonPIT.on(); + buttonRESETvar = buttonRESET.longPress(); + } // if (buttonREDTEAM.pushed()) { // buttonREDTEAMvar = true; // if (ARENA_READY && !REDTEAM_READY) { diff --git a/ROFLS_Arena_Controller/ROFLS_Arena_Controller.ino b/ROFLS_Arena_Controller/ROFLS_Arena_Controller.ino index 3f7989b..a210fe4 100644 --- a/ROFLS_Arena_Controller/ROFLS_Arena_Controller.ino +++ b/ROFLS_Arena_Controller/ROFLS_Arena_Controller.ino @@ -78,15 +78,8 @@ typedef struct struct_message_Clock { struct_message_Clock sendClockDATA; // send config, pilot buttons: -uint8_t broadcastAddressREDTEAMbutton[] = {0x48, 0x27, 0xE2, 0x5D, 0xB6, 0x84}; -uint8_t broadcastAddressBLUETEAMbutton[] = {0x48, 0x27, 0xE2, 0x5D, 0xB6, 0x84}; -// struct for pilot button data: -typedef struct struct_message_TEAMButton { - bool REDTEAMButtonLED; - bool BLUETEAMButtonLED; -} struct_message_TEAMButton; - -struct_message_TEAMButton sendTEAMButtonDATA; +uint8_t broadcastAddressREDTEAMbutton[] = {0x84, 0xFC, 0xE6, 0xC7, 0x27, 0x14}; +uint8_t broadcastAddressBLUETEAMbutton[] = {0x84, 0xFC, 0xE6, 0xC7, 0x1A, 0x02}; esp_now_peer_info_t peerInfo; @@ -285,10 +278,10 @@ void loop() { openPITmanually(); } // automatic pit release - if (FightCountDown[0].remaining() == PITreleaseTime && switchPIT.on() && buttonPIThold == false) { + if (FightCountDown[0].remaining() <= PITreleaseTime && FightCountDown[0].remaining() != 0 && !switchRUMBLE.on() && switchPIT.on() && buttonPIThold == false) { openPIT(); } - if (rumbleTIME.elapsed() == PITreleaseTime && switchPIT.on() && buttonPIThold == false) { + if (rumbleTIME.elapsed() >= PITreleaseTime && switchRUMBLE.on() && switchPIT.on() && buttonPIThold == false) { openPIT(); } // reset button logic diff --git a/ROFLS_Arena_Remote/Input_Handler.ino b/ROFLS_Arena_Remote/Input_Handler.ino index 51a4fe8..a4224aa 100644 --- a/ROFLS_Arena_Remote/Input_Handler.ino +++ b/ROFLS_Arena_Remote/Input_Handler.ino @@ -6,17 +6,23 @@ void pollInput (){ buttonPAUSE.poll(); buttonPIT.poll(); buttonRESET.poll(); + buttonTEAM.poll(); - if (buttonSTART.pushed()) { - buttonSTARTvar = true; - } - if (buttonPAUSE.pushed()) { - buttonPAUSEvar = true; - } - if (buttonPIT.pushed()) { - buttonPITvar = true; - } - if (buttonRESET.pushed()) { - buttonRESETvar = true; + // only set the var if the button was actually pushed or released, to prevent overriding data from the controller + if (buttonSTART.singleClick() || buttonSTART.longPress() || buttonPAUSE.singleClick() || buttonPIT.singleClick() || buttonPIT.switched() || buttonRESET.longPress() || buttonTEAM.singleClick() || buttonTEAM.longPress()) { + sendDATAvar = true; + buttonSTARTvar = buttonSTART.singleClick(); + buttonSTARTforced = buttonSTART.longPress(); + buttonPAUSEvar = buttonPAUSE.singleClick(); + buttonPITvar = buttonPIT.singleClick(); + buttonPIThold = buttonPIT.on(); + buttonRESETvar = buttonRESET.longPress(); + if (boardID == 1) { + buttonREDTEAMvar = buttonTEAM.singleClick(); + buttonREDTEAMtapout = buttonTEAM.longPress(); + } else if (boardID == 2) { + buttonBLUETEAMvar = buttonTEAM.singleClick(); + buttonBLUETEAMtapout = buttonTEAM.longPress(); + } } } \ No newline at end of file diff --git a/ROFLS_Arena_Remote/ROFLS_Arena_Remote.ino b/ROFLS_Arena_Remote/ROFLS_Arena_Remote.ino index 181db68..cdf3008 100644 --- a/ROFLS_Arena_Remote/ROFLS_Arena_Remote.ino +++ b/ROFLS_Arena_Remote/ROFLS_Arena_Remote.ino @@ -5,30 +5,43 @@ #include // automatically installed for ESP32 boards #include // https://github.com/avdwebLibraries/avdweb_Switch +// set board ID +// This is only needed for the initial flashing, as it writes the ID in to the ESPs internal flash +// +// 0 = referee remote +// 1 = Red Team Button +// 2 = Blue Team Button +const bool writeBoardID = false; +int boardID = 0; + + // Hardware connections #define START_BTN_PIN 1 #define PAUSE_BTN_PIN 2 #define PIT_BTN_PIN 3 #define RESET_BTN_PIN 4 -#define REDTEAM_BTN_PIN 5 -#define BLUETEAM_BTN_PIN 6 -#define REDTEAM_LED_PIN 39 -#define BLUETEAM_LED_PIN 40 +#define TEAM_BTN_PIN 16 +#define TEAM_LED_PIN 17 // define buttons and switches Switch buttonSTART = Switch(START_BTN_PIN, INPUT_PULLUP, LOW, 50, 1000); Switch buttonPAUSE = Switch(PAUSE_BTN_PIN); Switch buttonPIT = Switch(PIT_BTN_PIN, INPUT_PULLUP, LOW, 50, 1000); Switch buttonRESET = Switch(RESET_BTN_PIN, INPUT_PULLUP, LOW, 50, 1000); -Switch buttonREDTEAM = Switch(REDTEAM_BTN_PIN, INPUT_PULLUP, LOW, 50, 1000); -Switch buttonBLUETEAM = Switch(BLUETEAM_BTN_PIN, INPUT_PULLUP, LOW, 50, 1000); +Switch buttonTEAM = Switch(TEAM_BTN_PIN, INPUT_PULLUP, LOW, 50, 1000); bool buttonSTARTvar = false; +bool buttonSTARTforced = false; bool buttonPAUSEvar = false; bool buttonPITvar = false; +bool buttonPIThold = false; bool buttonRESETvar = false; +bool buttonREDTEAMvar = false; +bool buttonREDTEAMtapout = false; +bool buttonBLUETEAMvar = false; +bool buttonBLUETEAMtapout = false; -bool sendDATA = false; +bool sendDATAvar = false; // ESP-NOW config @@ -56,6 +69,8 @@ struct_message_send sendDATA; esp_now_peer_info_t peerInfo; +Preferences preferences; + // callback when data is sent void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { Serial.print("\r\nLast Packet Send Status:\t"); @@ -65,6 +80,23 @@ void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { void setup() { Serial.begin(115200); + // write boardID to internal Flash if needed + if (writeBoardID) { + // open preferences, namespace, RW mode = false + preferences.begin("Arena-remote", false); + preferences.putUInt("boardID", boardID); + } else { + // open preferences, namespace, RO mode = true + preferences.begin("Arena-remote", true); + } + // read board ID from internal flash + boardID = preferences.getUInt("boardID", 0); + preferences.end(); + + // configure and set the LED output pin + pinMode(TEAM_LED_PIN, OUTPUT); + digitalWrite(TEAM_LED_PIN, LOW); + // Set device as a Wi-Fi Station WiFi.mode(WIFI_STA); @@ -74,8 +106,8 @@ void setup() { return; } // Once ESPNow is successfully Init, we will register for Send CB to - // get the status of Trasnmitted packet - //esp_now_register_send_cb(OnDataSent); + // get the status of Transmitted packet + esp_now_register_send_cb(OnDataSent); // Register peer memcpy(peerInfo.peer_addr, broadcastAddress, 6); @@ -86,69 +118,51 @@ void setup() { Serial.println("Failed to add peer"); return; } + + // populate sendDATA struct with some data: + sendDATA.boardID = boardID; + sendDATA.buttonSTART = false; + sendDATA.buttonSTARTforced = false; + sendDATA.buttonPAUSE = false; + sendDATA.buttonPIT = false; + sendDATA.buttonPIThold = false; + sendDATA.buttonRESET = false; + sendDATA.buttonREDTEAM = false; + sendDATA.buttonREDTEAMtapout = false; + sendDATA.buttonBLUETEAM = false; + sendDATA.buttonBLUETEAMtapout = false; +} + +void sendDATA_to_controller() { +// if ((sendDATA.buttonSTART != buttonSTARTvar) || (sendDATA.buttonSTARTforced != buttonSTARTforced) || (sendDATA.buttonPAUSE !=buttonPAUSEvar) || (sendDATA.buttonPIT != buttonPITvar) || (sendDATA.buttonPIThold != buttonPIThold) || (sendDATA.buttonRESET != buttonRESETvar) || (sendDATA.buttonREDTEAM != buttonREDTEAMvar) || (sendDATA.buttonREDTEAMtapout != buttonREDTEAMtapout) || (sendDATA.buttonBLUETEAM != buttonBLUETEAMvar) || (sendDATA.buttonBLUETEAMtapout != buttonBLUETEAMtapout)) { + if (sendDATAvar) { + sendDATAvar = false; + // prepare sendDATA data + sendDATA.boardID = boardID; + sendDATA.buttonSTART = buttonSTARTvar; + sendDATA.buttonSTARTforced = buttonSTARTforced; + sendDATA.buttonPAUSE = buttonPAUSEvar; + sendDATA.buttonPIT = buttonPITvar; + sendDATA.buttonPIThold = buttonPIThold; + sendDATA.buttonRESET = buttonRESETvar; + sendDATA.buttonREDTEAM = buttonREDTEAMvar; + sendDATA.buttonREDTEAMtapout = buttonREDTEAMtapout; + sendDATA.buttonBLUETEAM = buttonBLUETEAMvar; + sendDATA.buttonBLUETEAMtapout = buttonBLUETEAMtapout; + // send data + esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &sendDATA, sizeof(sendDATA)); + // check if transmission was successfull + // if (result == ESP_OK) { + // Serial.println("Sent with success"); + // } + // else { + // Serial.println("Error sending the data"); + // } + } } void loop() { // poll all the switch/button inputs pollInput(); - // start button logic - if (buttonSTARTvar) { - buttonSTARTvar = false; - remoteDATA.buttonSTARTremote = true; - sendDATA = true; - } - // pause button logic - if (buttonPAUSEvar) { - buttonPAUSEvar = false; - remoteDATA.buttonPAUSEremote = true; - sendDATA = true; - } - // pit button logic - if (buttonPITvar) { - buttonPITvar = false; - remoteDATA.buttonPITremote = true; - sendDATA = true; - } - // reset button logic - if (buttonRESETvar) { - buttonRESETvar = false; - remoteDATA.buttonRESETremote = true; - sendDATA = true; - } - - // Set values to send - //remoteDATA.buttonSTARTremote = false; - //remoteDATA.buttonPAUSEremote = false; - //remoteDATA.buttonPITremote = false; - //remoteDATA.buttonRESETremote = false; - - // Send message via ESP-NOW - //esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &remoteDATA, sizeof(remoteDATA)); - if (sendDATA == true) { - esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &remoteDATA, sizeof(remoteDATA)); - sendDATA = false; - // clear remoteDATA struct - remoteDATA.buttonSTARTremote = false; - remoteDATA.buttonPAUSEremote = false; - remoteDATA.buttonPITremote = false; - remoteDATA.buttonRESETremote = false; - - if (result == ESP_OK) { - Serial.println("Sent with success"); - } - else { - Serial.println("Error sending the data"); - } - } - - - /* - if (result == ESP_OK) { - Serial.println("Sent with success"); - } - else { - Serial.println("Error sending the data"); - } - */ - //delay(2000); + sendDATA_to_controller(); }