Files
ROFLS_Arena/ROFLS_Arena_Remote/ROFLS_Arena_Remote.ino

155 lines
4.1 KiB
C++

// ROFLS+ Referee Remote
#include <WiFi.h>
#include <esp_now.h> // automatically installed for ESP32 boards, I think?
#include <Preferences.h> // automatically installed for ESP32 boards
#include <avdweb_Switch.h> // https://github.com/avdwebLibraries/avdweb_Switch
// 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 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);
bool buttonSTARTvar = false;
bool buttonPAUSEvar = false;
bool buttonPITvar = false;
bool buttonRESETvar = false;
bool sendDATA = false;
// ESP-NOW config
// REPLACE WITH YOUR RECEIVER MAC Address
uint8_t broadcastAddress[] = {0x84, 0xFC, 0xE6, 0xC7, 0x1A, 0x8C};
// Structure example to send data
// Must match the receiver structure
typedef struct struct_message_send {
int boardID;
bool buttonSTART;
bool buttonSTARTforced;
bool buttonPAUSE;
bool buttonPIT;
bool buttonPIThold;
bool buttonRESET;
bool buttonREDTEAM;
bool buttonREDTEAMtapout;
bool buttonBLUETEAM;
bool buttonBLUETEAMtapout;
} struct_message_send;
// Create a struct_message called myData
struct_message_send sendDATA;
esp_now_peer_info_t peerInfo;
// 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");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
void setup() {
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
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);
// Register peer
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
}
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);
}