From 0b185a0900e40b1ac8686551bb1667cc1c63815d Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Sat, 23 May 2020 16:49:21 +0100 Subject: [PATCH] Clean up the I2C controller code. --- I2CController.cpp | 63 ++++++++++----------------------------- I2CController.h | 14 +++++++-- MMDVMHost.cpp | 5 +++- MMDVMHost.vcxproj | 2 -- MMDVMHost.vcxproj.filters | 6 ---- Modem.cpp | 7 ++++- Modem.h | 4 +-- 7 files changed, 38 insertions(+), 63 deletions(-) diff --git a/I2CController.cpp b/I2CController.cpp index c6b7c77..9886443 100644 --- a/I2CController.cpp +++ b/I2CController.cpp @@ -17,6 +17,8 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ +#if defined(__linux__) + #include "I2CController.h" #include "Log.h" @@ -24,52 +26,18 @@ #include #include - -#if defined(_WIN32) || defined(_WIN64) - -#include -#include - -CI2CController::CI2CController(const std::string& device, unsigned int speed, unsigned int address, bool assertRTS) : -CSerialController(device, speed, assertRTS), -m_address(address) -{ -} - -CI2CController::~CI2CController() -{ -} - -bool CI2CController::open() -{ - return CSerialController::open(); -} - -int CI2CController::read(unsigned char* buffer, unsigned int length) -{ - return CSerialController::read(buffer, length); -} - -int CI2CController::write(const unsigned char* buffer, unsigned int length) -{ - return CSerialController::write(buffer, length); -} - -#else - #include #include #include #include #include #include -#if defined(__linux__) #include -#endif -CI2CController::CI2CController(const std::string& device, unsigned int speed, unsigned int address, bool assertRTS) : -CSerialController(device, speed, assertRTS), -m_address(address) +CI2CController::CI2CController(const std::string& device, unsigned int address) : +m_device(device), +m_address(address), +m_fd(-1) { } @@ -81,7 +49,6 @@ bool CI2CController::open() { assert(m_fd == -1); -#if defined(__linux__) m_fd = ::open(m_device.c_str(), O_RDWR); if (m_fd < 0) { LogError("Cannot open device - %s", m_device.c_str()); @@ -99,9 +66,6 @@ bool CI2CController::open() ::close(m_fd); return false; } -#else - #warning "I2C controller supports Linux only" -#endif return true; } @@ -117,7 +81,6 @@ int CI2CController::read(unsigned char* buffer, unsigned int length) unsigned int offset = 0U; while (offset < length) { -#if defined(__linux__) ssize_t n = ::read(m_fd, buffer + offset, 1U); if (n < 0) { if (errno != EAGAIN) { @@ -128,7 +91,6 @@ int CI2CController::read(unsigned char* buffer, unsigned int length) if (n > 0) offset += n; -#endif } return length; @@ -144,10 +106,7 @@ int CI2CController::write(const unsigned char* buffer, unsigned int length) unsigned int ptr = 0U; while (ptr < length) { - ssize_t n = 0U; -#if defined(__linux__) - n = ::write(m_fd, buffer + ptr, 1U); -#endif + ssize_t n = ::write(m_fd, buffer + ptr, 1U); if (n < 0) { if (errno != EAGAIN) { LogError("Error returned from write(), errno=%d", errno); @@ -162,4 +121,12 @@ int CI2CController::write(const unsigned char* buffer, unsigned int length) return length; } +void CI2CController::close() +{ + assert(m_fd != -1); + + ::close(m_fd); + m_fd = -1; +} + #endif diff --git a/I2CController.h b/I2CController.h index a67db85..14bc81e 100644 --- a/I2CController.h +++ b/I2CController.h @@ -20,11 +20,13 @@ #ifndef I2CController_H #define I2CController_H -#include "SerialController.h" +#if defined(__linux__) -class CI2CController : public CSerialController { +#include "SerialPort.h" + +class CI2CController : public ISerialPort { public: - CI2CController(const std::string& device, unsigned int speed, unsigned int address = 0x22U, bool assertRTS = false); + CI2CController(const std::string& device, unsigned int address = 0x22U); virtual ~CI2CController(); virtual bool open(); @@ -33,8 +35,14 @@ public: virtual int write(const unsigned char* buffer, unsigned int length); + virtual void close(); + private: + std::string m_device; unsigned int m_address; + int m_fd; }; #endif + +#endif diff --git a/MMDVMHost.cpp b/MMDVMHost.cpp index 9d9a01e..c28f17e 100644 --- a/MMDVMHost.cpp +++ b/MMDVMHost.cpp @@ -1238,10 +1238,13 @@ bool CMMDVMHost::createModem() LogInfo("Modem Parameters"); LogInfo(" Port: %s", port.c_str()); +#if defined(__linux__) LogInfo(" Protocol: %s", protocol.c_str()); if (protocol == "i2c") LogInfo(" I2C Address: %02X", address); - LogInfo(" Speed: %u", speed); + else +#endif + LogInfo(" Speed: %u", speed); LogInfo(" RX Invert: %s", rxInvert ? "yes" : "no"); LogInfo(" TX Invert: %s", txInvert ? "yes" : "no"); LogInfo(" PTT Invert: %s", pttInvert ? "yes" : "no"); diff --git a/MMDVMHost.vcxproj b/MMDVMHost.vcxproj index 63ba164..3ed61f9 100644 --- a/MMDVMHost.vcxproj +++ b/MMDVMHost.vcxproj @@ -187,7 +187,6 @@ - @@ -285,7 +284,6 @@ - diff --git a/MMDVMHost.vcxproj.filters b/MMDVMHost.vcxproj.filters index 8dc6f12..704bf0b 100644 --- a/MMDVMHost.vcxproj.filters +++ b/MMDVMHost.vcxproj.filters @@ -275,9 +275,6 @@ Header Files - - Header Files - Header Files @@ -547,9 +544,6 @@ Source Files - - Source Files - Source Files diff --git a/Modem.cpp b/Modem.cpp index f648d22..cb2c18c 100644 --- a/Modem.cpp +++ b/Modem.cpp @@ -16,7 +16,10 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ +#include "SerialController.h" +#if defined(__linux__) #include "I2CController.h" +#endif #include "DStarDefines.h" #include "DMRDefines.h" #include "YSFDefines.h" @@ -224,9 +227,11 @@ CModem::~CModem() void CModem::setSerialParams(const std::string& protocol, unsigned int address, unsigned int speed) { // Create the serial controller instance according the protocol specified in conf. +#if defined(__linux__) if (protocol == "i2c") - m_serial = new CI2CController(m_port, speed, address, true); + m_serial = new CI2CController(m_port, address); else +#endif m_serial = new CSerialController(m_port, speed, true); } diff --git a/Modem.h b/Modem.h index 48d59b4..4c77d9f 100644 --- a/Modem.h +++ b/Modem.h @@ -19,7 +19,7 @@ #ifndef MODEM_H #define MODEM_H -#include "SerialController.h" +#include "SerialPort.h" #include "RingBuffer.h" #include "Defines.h" #include "Timer.h" @@ -156,7 +156,7 @@ private: bool m_fmEnabled; int m_rxDCOffset; int m_txDCOffset; - CSerialController* m_serial; + ISerialPort* m_serial; unsigned char* m_buffer; unsigned int m_length; unsigned int m_offset;