added duplicate console to mqtt

This commit is contained in:
2022-02-14 19:31:24 +01:00
parent 6cacea2df3
commit 3a2e51713b
19 changed files with 262 additions and 166 deletions
+116 -99
View File
@@ -9,6 +9,8 @@
#include "driveModi/driveManager.h"
#include "moveControl.h"
#include "network.h"
#include "outputBufMqtt.h"
#include "debugMqtt.h"
#include "menu.h"
#include "menuAction.h"
@@ -23,23 +25,38 @@ MoveControl moveController;
DriveManager driveManager(&moveController);
Menu* main_m;
LiquidCrystal_I2C* lcd;
OutputBufMqtt* bufMqtt;
DebugMqtt* debugMqtt;
// TODO: Platzhalter, muss irgendwann weg
void dummy(){std::cout << "Dummy in Action" <<std::endl;}
void callbackControllerAction();
void callbackControllerConnect();
void controllerPrintBattery();
void i2cScanner();
void restart();
void callbackControllerAction(void);
void callbackControllerConnect(void);
void i2cScanner(void);
void restart(void);
void makeMenu(void);
void setup() {
Serial.begin(115200);
Network::setIps();
Network::connectWifi();
Network::setupMQTT();
Network::checkMQTT();
DebugMqtt::init(Network::getMqtttClient(), Loglevel::debug);
std::cout << "Activate additional output via MQTT..." << std::endl;
debugMqtt = new DebugMqtt("Console");
bufMqtt = new OutputBufMqtt(debugMqtt);
std::cout.rdbuf(bufMqtt);
std::cout << "Welcome to Kleiax-Rover" << std::endl;
std::cout << "All actions from the main programm run on Core -> " << xPortGetCoreID() << std::endl;
Ps3.attach(callbackControllerAction);
Ps3.attachOnConnect(callbackControllerConnect);
std::cout << "\nReady to connect a PS3 Controller... \n";
Ps3.begin(CONTROLLER_MAC);
Wire.begin(21, 19);
// i2cScanner();
lcd = new LiquidCrystal_I2C(0x3F,16,2);
@@ -49,14 +66,97 @@ void setup() {
lcd->setCursor(0, 1);
lcd->print("Press PS-Button");
Network::setIps();
Network::connectWifi();
Network::setupMQTT();
makeMenu();
}
Ps3.attach(callbackControllerAction);
Ps3.attachOnConnect(callbackControllerConnect);
std::cout << "\nReady to connect a PS3 Controller... \n";
Ps3.begin(CONTROLLER_MAC);
void loop() {
Network::checkMQTT();
driveManager.loop();
}
void callbackControllerAction() {
auto isDelayReached = [](bool reset = false) -> bool {
static uint32_t lastMillis = 0;
if (reset)
lastMillis = millis();
static const uint16_t delay = 300;
bool res = false;
if (millis() - lastMillis > delay)
res = true;
return res;
};
// Menu controlling
if (isDelayReached()) {
if (Ps3.data.button.up)
main_m->up();
else if (Ps3.data.button.down)
main_m->down();
else if (Ps3.data.button.left)
main_m->left();
else if (Ps3.data.button.right)
main_m->right();
else if (Ps3.data.button.circle)
main_m->yes();
else if (Ps3.data.button.cross)
main_m->no();
else if (Ps3.data.button.select)
debugMqtt->sendMsg(Loglevel::info, "Here we are: callbackController..");
isDelayReached(true);
}
// This have to be stand here because it have to be run on the other Core
main_m->update();
}
void callbackControllerConnect() {
std::cout << "Controller connected to ESP32" << std::endl;
std::cout << "All actions from the Controller run on Core -> " << xPortGetCoreID() << std::endl;
// TODO: Display is not functional without this seconde init, first init is in setup()
lcd->init();
main_m->printMenu();
}
void i2cScanner() {
std::cout << "\nI2C Scanner" << std::endl;
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ ) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
nDevices++;
}
else if (error==4) {
Serial.print("Unknow error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
}
void restart() {
ESP.restart();
}
void makeMenu() {
auto dummy = []() {
std::cout << "Dummy in Action" <<std::endl;
};
// Create Menu
main_m = new Menu();
@@ -112,87 +212,4 @@ void setup() {
MenuAction* pidr_e = new MenuAction("Right", pidr_m);
pid_m->addEntry(pidl_e);
pid_m->addEntry(pidr_e);
}
void loop() {
Network::checkMQTT();
driveManager.loop();
}
bool isDelayReached(bool reset = false) {
static uint32_t lastMillis = 0;
if (reset)
lastMillis = millis();
static const uint16_t delay = 300;
bool res = false;
if (millis() - lastMillis > delay)
res = true;
return res;
}
void callbackControllerAction() {
// Menu controlling
if (isDelayReached()) {
if (Ps3.data.button.up)
main_m->up();
else if (Ps3.data.button.down)
main_m->down();
else if (Ps3.data.button.left)
main_m->left();
else if (Ps3.data.button.right)
main_m->right();
else if (Ps3.data.button.circle)
main_m->yes();
else if (Ps3.data.button.cross)
main_m->no();
isDelayReached(true);
}
// This have to be stand here because it have to be run on the other Core
main_m->update();
}
void callbackControllerConnect() {
std::cout << "Controller connected to ESP32" << std::endl;
std::cout << "All actions from the Controller run on Core -> " << xPortGetCoreID() << std::endl;
// TODO: Display is not functional without this seconde init, first init is in setup()
lcd->init();
main_m->printMenu();
}
void i2cScanner() {
std::cout << "\nI2C Scanner" << std::endl;
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ ) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
nDevices++;
}
else if (error==4) {
Serial.print("Unknow error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
}
void restart() {
ESP.restart();
}