115 lines
3.0 KiB
C++
115 lines
3.0 KiB
C++
#include <esp_now.h>
|
|
#include <esp_wifi.h>
|
|
|
|
#include "Arduino.h"
|
|
#include "Config.hpp"
|
|
#include "Ethernet.hpp"
|
|
#include "SPIFFS.h"
|
|
#include "Server.hpp"
|
|
#include "Switch.hpp"
|
|
#include "TimeSync.hpp"
|
|
#include "WiFi.h"
|
|
#include "esp32-hal-ledc.h"
|
|
#include "kartReciever.hpp"
|
|
|
|
Switch web_entry_switch;
|
|
// 34:85:18:A9:3D:FC
|
|
uint8_t trackside_mac[] = {0x34, 0x85, 0x18, 0xa9, 0x3d, 0xfc};
|
|
|
|
typedef struct struct_message {
|
|
char a[32];
|
|
int b;
|
|
float c;
|
|
bool d;
|
|
} struct_message;
|
|
|
|
// Create a struct_message called myData
|
|
struct_message myData;
|
|
bool tp_state = false;
|
|
|
|
// callback function that will be executed when data is received
|
|
void OnKartMsg(kart_msg* msg) {
|
|
tp_state = !tp_state;
|
|
digitalWrite(21, tp_state);
|
|
Serial.printf("Message\n id: %lu\nTransmission: %lu\n\n", msg->kart_id, msg->transmision);
|
|
digitalWrite(RGB_BUILTIN, 1);
|
|
delay(100);
|
|
digitalWrite(RGB_BUILTIN, 0);
|
|
}
|
|
|
|
void setup() {
|
|
// Initialize Serial Monitor
|
|
Serial.begin(115200);
|
|
randomSeed(analogRead(5));
|
|
config.Init();
|
|
pinMode(21, OUTPUT);
|
|
digitalWrite(21, tp_state);
|
|
ledcAttach(1, 256000, 2);
|
|
ledcWrite(1, 2); // Sets
|
|
pinMode(LED_BUILTIN, OUTPUT);
|
|
|
|
// TimeSync::Sync()
|
|
|
|
eth.init();
|
|
eth.begin();
|
|
kart_receiver.begin();
|
|
kart_receiver.registerCallback(OnKartMsg);
|
|
web_server.Init();
|
|
web_entry_switch.Init(0);
|
|
|
|
// // 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 recv CB to
|
|
// // get recv packer info
|
|
// esp_now_register_recv_cb(OnDataRecv);
|
|
}
|
|
|
|
uint8_t led_state = 0;
|
|
uint32_t last_led_change = 0;
|
|
String speed_in = "";
|
|
|
|
void loop() {
|
|
// Serial.println("loop");
|
|
while (Serial.available()) {
|
|
char c = (char)Serial.read();
|
|
speed_in += c;
|
|
if (c == '\n') {
|
|
// Process the complete line
|
|
Serial.println("Received speed input: " + speed_in);
|
|
if (speed_in.toInt() >= 0) {
|
|
ledcChangeFrequency(1, speed_in.toInt(), 2);
|
|
}
|
|
speed_in = "";
|
|
}
|
|
}
|
|
web_entry_switch.Debounce();
|
|
if (web_entry_switch.RisingEdge()) {
|
|
String response = eth.get("http://example.com");
|
|
Serial.println("GET Response: " + response);
|
|
}
|
|
if (web_server.isRunning()) {
|
|
if (millis() - last_led_change > 500) {
|
|
led_state ^= 1;
|
|
digitalWrite(RGB_BUILTIN, led_state);
|
|
last_led_change = millis();
|
|
}
|
|
if (web_entry_switch.RisingEdge()) {
|
|
Serial.println("Web server toggled");
|
|
web_server.Stop();
|
|
kart_receiver.begin();
|
|
digitalWrite(RGB_BUILTIN, 0);
|
|
}
|
|
} else {
|
|
if (web_entry_switch.Pressed() && web_entry_switch.TimeHeldMs() > 3000) {
|
|
Serial.println("Web server started");
|
|
kart_receiver.end();
|
|
web_server.Start();
|
|
}
|
|
// digitalWrite(LED_BUILTIN, 0);
|
|
}
|
|
delay(1);
|
|
} |