mirror of
https://github.com/g4klx/MMDVMHost
synced 2025-12-21 15:09:23 +08:00
Clean up the I2C controller code.
This commit is contained in:
@@ -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 <cassert>
|
||||
|
||||
#include <sys/types.h>
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
|
||||
#include <setupapi.h>
|
||||
#include <winioctl.h>
|
||||
|
||||
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 <sys/ioctl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <cerrno>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <termios.h>
|
||||
#if defined(__linux__)
|
||||
#include <linux/i2c-dev.h>
|
||||
#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
|
||||
|
||||
Reference in New Issue
Block a user