diff --git a/lib/RS485/RS485_Driver.cpp b/lib/RS485/RS485_Driver.cpp index 2155c41..a5a712a 100644 --- a/lib/RS485/RS485_Driver.cpp +++ b/lib/RS485/RS485_Driver.cpp @@ -3,8 +3,7 @@ #include #include -#define DEBUGLOG_DEFAULT_LOG_LEVEL_TRACE - +//#define DEBUGLOG_DEFAULT_LOG_LEVEL_TRACE #include "utils.h" namespace drivers @@ -306,7 +305,7 @@ namespace drivers *it=highByte(beV); *(++it)=lowByte(beV); }); - + if (!write(multiRequest(device, func, reg, num, requestData))) { LOG_ERROR("Failed send writeMultiInteger command"); diff --git a/lib/RTC/PCF85063_Driver.cpp b/lib/RTC/PCF85063_Driver.cpp index 8d9e90e..b4972fc 100644 --- a/lib/RTC/PCF85063_Driver.cpp +++ b/lib/RTC/PCF85063_Driver.cpp @@ -1,4 +1,4 @@ -#include "WS_PCF85063.h" +#include "PCF85063_Driver.h" #include namespace drivers @@ -157,6 +157,12 @@ namespace drivers return false; } + const std::string PCF85063::getTimeStr() { + datetime_t dt; + readDatetime(dt); + return datetime2str(dt); + } + const std::string PCF85063::datetime2str(datetime_t &datetime) { tm dtime; diff --git a/lib/RTC/PCF85063_Driver.h b/lib/RTC/PCF85063_Driver.h index e87fab1..6169fd0 100644 --- a/lib/RTC/PCF85063_Driver.h +++ b/lib/RTC/PCF85063_Driver.h @@ -100,6 +100,7 @@ namespace drivers I2C &m_i2c; uint8_t m_address; + public: typedef struct { uint16_t year; @@ -129,6 +130,8 @@ namespace drivers const bool readAlarm(datetime_t &time); const bool getAlarmFlag(uint8_t &flags); + const std::string getTimeStr(); + private: const std::string datetime2str(datetime_t &datetime); const uint8_t decToBcd(const int val); diff --git a/src/main.cpp b/src/main.cpp index 3497847..24ec2a0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,9 @@ #include #include #include +#include + +#include #include "utils.h" @@ -25,15 +28,27 @@ void loop() const uint8_t baseRegister(0x00); //////////////// DEVICES //////////////// + // Declared here to keep devices local to the main loop otherwise the kernel crashes // auto i2c = drivers::I2C(); auto relays = drivers::TCA9554PWR(i2c, TCA9554_ADDRESS); auto bus = drivers::MODBUS(9600, SERIAL_8N1); + auto rtc = drivers::PCF85063(i2c, PCF85063_ADDRESS); + + //////////////////////////////////////// + ///////// MAIN LOOP INSIDE LOOP //////// + //////////////////////////////////////// + while (true) { - LOG_INFO("\n\n\n\n[", k++, "] Loop"); + LOG_INFO("[", k++, "] Loop"); std::vector results; std::vector values; + + drivers::PCF85063::datetime_t dt; + rtc.readDatetime(dt); + LOG_INFO("Current Datetime", rtc.getTimeStr().c_str()); + if (bus.readHoldingRegisters(tempBoardAddr, baseRegister, 8, results)) { @@ -43,7 +58,7 @@ void loop() } results.clear(); } - delay(100); + delay(5000); LOG_INFO("\n\n====>Write Status 1"); bus.writeCoils(relayBoardAddr, 0x00, {true, false, true, false, true, false, true, false}); @@ -55,7 +70,7 @@ void loop() bus.writeCoils(relayBoardAddr, 0x00, {false, true, false, true, false, true, false, true}); bus.readCoils(relayBoardAddr, 0x00, 8, values); printBool("====>Status 2", values); - + delay(5000); LOG_INFO("\n\n====>Read Inputs"); bus.readInputs(relayBoardAddr, 0x00, 8, values); @@ -63,4 +78,8 @@ void loop() delay(5000); } + + //////////////////////////////////////// + ///////// MAIN LOOP INSIDE LOOP //////// + //////////////////////////////////////// } \ No newline at end of file