From 473d5f827be49fd57194f9acc6f82a41d907170e Mon Sep 17 00:00:00 2001 From: Boris Brock Date: Wed, 24 Jul 2024 19:22:07 +0200 Subject: [PATCH] Refactoring --- src/Components/Modbus/ModbusRTU.cpp | 9 +++++---- src/Configuration/Constants.h | 11 ----------- src/Configuration/Pins.h | 12 ++++++++++++ src/Main.cpp | 5 +++-- 4 files changed, 20 insertions(+), 17 deletions(-) create mode 100644 src/Configuration/Pins.h diff --git a/src/Components/Modbus/ModbusRTU.cpp b/src/Components/Modbus/ModbusRTU.cpp index 1a7c1a0..fc90750 100644 --- a/src/Components/Modbus/ModbusRTU.cpp +++ b/src/Components/Modbus/ModbusRTU.cpp @@ -2,11 +2,12 @@ #include #include "HardwareSerial.h" #include "ModbusClientRTU.h" +#include "../../Configuration/Pins.h" #include "../../Configuration/Constants.h" #include "ModbusRTU.h" -ModbusClientRTU gModbusRTU(Constants::Pins::PinRTS); // Create a ModbusRTU client instance -HardwareSerial gRs485Serial(1); // Define a Serial for UART1 +ModbusClientRTU gModbusRTU(Pins::PinRTS); // Create a ModbusRTU client instance +HardwareSerial gRs485Serial(1); // Define a Serial for UART1 constexpr uint8_t RegisterSize = 2; ModbusRTU *ModbusRTU::Instance() @@ -23,8 +24,8 @@ void ModbusRTU::Init() gRs485Serial.begin( Constants::HeidelbergWallbox::ModbusBaudrate, SERIAL_8E1, - Constants::Pins::PinRX, - Constants::Pins::PinTX); + Pins::PinRX, + Pins::PinTX); // Start Modbus RTU Serial.println("Creating Modbus RTU instance"); diff --git a/src/Configuration/Constants.h b/src/Configuration/Constants.h index 5aa628a..2941b9b 100644 --- a/src/Configuration/Constants.h +++ b/src/Configuration/Constants.h @@ -2,17 +2,6 @@ namespace Constants { - namespace Pins - { - // Pin connections: - // ESP32 GPIO18 -> MAXRS485 RO (Receiver Output) - // ESP32 GPIO19 -> MAXRS485 DI (Driver Input) - // ESP32 GPIO21 -> MAXRS485 DE+RE - constexpr uint8_t PinRX = GPIO_NUM_18; - constexpr uint8_t PinTX = GPIO_NUM_19; - constexpr uint8_t PinRTS = GPIO_NUM_21; - }; - namespace WiFi { constexpr const char *HotspotSSID = "HeidelBridge"; diff --git a/src/Configuration/Pins.h b/src/Configuration/Pins.h new file mode 100644 index 0000000..1d4ce20 --- /dev/null +++ b/src/Configuration/Pins.h @@ -0,0 +1,12 @@ +#pragma once + +namespace Pins +{ + // Pin connections: + // ESP32 GPIO18 -> MAXRS485 RO (Receiver Output) + // ESP32 GPIO19 -> MAXRS485 DI (Driver Input) + // ESP32 GPIO21 -> MAXRS485 DE+RE + constexpr uint8_t PinRX = GPIO_NUM_18; + constexpr uint8_t PinTX = GPIO_NUM_19; + constexpr uint8_t PinRTS = GPIO_NUM_21; +}; \ No newline at end of file diff --git a/src/Main.cpp b/src/Main.cpp index 493475c..725d4c3 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -20,7 +20,7 @@ void setup() // Make sure WiFi connection is up and running WifiConnection::Init(); - // Initialize all other modules + // Initialize wallbox #ifndef DUMMY_WALLBOX Serial.println("Starting with Heidelberg wallbox in real mode"); ModbusRTU::Instance()->Init(); @@ -29,8 +29,9 @@ void setup() Serial.println("Starting with dummy wallbox in simulated mode"); gWallbox = DummyWallbox::Instance(); #endif - gWallbox->Init(); + + // Start Modbus TCP server ModbusTCP::Init(gWallbox); Serial.println("Setup complete");