169 lines
5.2 KiB
C++
169 lines
5.2 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
|
|
|
|
// 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 10
|
|
#define PAUSE_BTN_PIN 8
|
|
#define PIT_BTN_PIN 1
|
|
#define RESET_BTN_PIN 3
|
|
#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 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 sendDATAvar = 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;
|
|
|
|
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");
|
|
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
|
|
}
|
|
|
|
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);
|
|
|
|
// 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 Transmitted 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;
|
|
}
|
|
|
|
// 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();
|
|
sendDATA_to_controller();
|
|
}
|