fixed remote PitHold, added persistant boardID
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,30 +5,43 @@
|
||||
#include <Preferences.h> // automatically installed for ESP32 boards
|
||||
#include <avdweb_Switch.h> // 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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user