mirror of
https://github.com/ArduPilot/ardupilot
synced 2025-01-22 00:28:30 -04:00
AP_HAL_Linux: I2CDevice: move implementation to outside
As we add methods, it's becoming too complex to be implemented together with the declaration.
This commit is contained in:
parent
7b39f3d0a7
commit
8e5aba8653
@ -75,44 +75,46 @@ static inline char *startswith(const char *s, const char *prefix)
|
||||
/* Private struct to maintain for each bus */
|
||||
class I2CBus {
|
||||
public:
|
||||
~I2CBus()
|
||||
{
|
||||
if (fd >= 0) {
|
||||
::close(fd);
|
||||
}
|
||||
}
|
||||
|
||||
int open(uint8_t n)
|
||||
{
|
||||
char path[sizeof("/dev/i2c-XXX")];
|
||||
int r;
|
||||
|
||||
if (fd >= 0) {
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
r = snprintf(path, sizeof(path), "/dev/i2c-%u", n);
|
||||
if (r < 0 || r >= (int)sizeof(path)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
fd = ::open(path, O_RDWR | O_CLOEXEC);
|
||||
if (fd < 0) {
|
||||
return -errno;
|
||||
}
|
||||
|
||||
bus = n;
|
||||
|
||||
return fd;
|
||||
}
|
||||
~I2CBus();
|
||||
int open(uint8_t n);
|
||||
|
||||
Semaphore sem;
|
||||
int fd = -1;
|
||||
uint8_t bus;
|
||||
|
||||
uint8_t ref;
|
||||
};
|
||||
|
||||
I2CBus::~I2CBus()
|
||||
{
|
||||
if (fd >= 0) {
|
||||
::close(fd);
|
||||
}
|
||||
}
|
||||
|
||||
int I2CBus::open(uint8_t n)
|
||||
{
|
||||
char path[sizeof("/dev/i2c-XXX")];
|
||||
int r;
|
||||
|
||||
if (fd >= 0) {
|
||||
return -EBUSY;
|
||||
}
|
||||
|
||||
r = snprintf(path, sizeof(path), "/dev/i2c-%u", n);
|
||||
if (r < 0 || r >= (int)sizeof(path)) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
fd = ::open(path, O_RDWR | O_CLOEXEC);
|
||||
if (fd < 0) {
|
||||
return -errno;
|
||||
}
|
||||
|
||||
bus = n;
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
I2CDevice::~I2CDevice()
|
||||
{
|
||||
// Unregister itself from the I2CDeviceManager
|
||||
|
Loading…
Reference in New Issue
Block a user