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:
Lucas De Marchi 2016-07-16 02:16:09 -03:00
parent 7b39f3d0a7
commit 8e5aba8653
1 changed files with 33 additions and 31 deletions

View File

@ -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