AP_HAL_PX4: embed PX4_I2C object into I2CDevice

Otherwise we would destroy PX4_I2C object after returning from
I2CDeviceManager::get_device(). Since this implementation is very
simple, just make PX4_I2C as an internal class and embed it into the
I2CDevice object.
This commit is contained in:
Lucas De Marchi 2016-05-10 15:38:17 -03:00
parent f767918e0e
commit 54fd3702c3
2 changed files with 25 additions and 24 deletions

View File

@ -4,26 +4,29 @@
namespace PX4 {
class PX4_I2C : public device::I2C {
public:
PX4_I2C(uint8_t bus);
bool do_transfer(uint8_t address, const uint8_t *send, uint32_t send_len,
uint8_t *recv, uint32_t recv_len);
};
PX4_I2C::PX4_I2C(uint8_t bus) :
I2C("AP_I2C", "/dev/api2c", bus, 0, 400000UL)
I2CDevice::PX4_I2C::PX4_I2C(uint8_t bus)
: I2C("AP_I2C", "/dev/api2c", bus, 0, 400000UL)
{
init();
}
bool PX4_I2C::do_transfer(uint8_t address, const uint8_t *send, uint32_t send_len,
uint8_t *recv, uint32_t recv_len)
bool I2CDevice::PX4_I2C::do_transfer(uint8_t address, const uint8_t *send, uint32_t send_len,
uint8_t *recv, uint32_t recv_len)
{
set_address(address);
return transfer(send, send_len, recv, recv_len) == OK;
}
I2CDevice::I2CDevice(uint8_t bus, uint8_t address)
: _device(bus)
, _address(address)
{
}
I2CDevice::~I2CDevice()
{
}
bool I2CDevice::transfer(const uint8_t *send, uint32_t send_len,
uint8_t *recv, uint32_t recv_len)
{
@ -54,10 +57,6 @@ AP_HAL::Semaphore *I2CDevice::get_semaphore()
return &semaphore;
}
I2CDevice::~I2CDevice()
{
}
I2CDeviceManager::I2CDeviceManager()
{
}
@ -65,9 +64,8 @@ I2CDeviceManager::I2CDeviceManager()
AP_HAL::OwnPtr<AP_HAL::I2CDevice>
I2CDeviceManager::get_device(uint8_t bus, uint8_t address)
{
AP_HAL::OwnPtr<PX4_I2C> i2c { new PX4_I2C(bus) };
auto dev = AP_HAL::OwnPtr<AP_HAL::I2CDevice>(new I2CDevice(*i2c, address));
auto dev = AP_HAL::OwnPtr<AP_HAL::I2CDevice>(new I2CDevice(bus, address));
return dev;
}
}

View File

@ -21,11 +21,7 @@ public:
}
/* AP_HAL::I2CDevice implementation */
I2CDevice(PX4_I2C &device, uint8_t address)
: _device(device)
, _address(address)
{
}
I2CDevice(uint8_t bus, uint8_t address);
~I2CDevice();
@ -61,7 +57,14 @@ public:
int get_fd() override;
protected:
PX4_I2C &_device;
class PX4_I2C : public device::I2C {
public:
PX4_I2C(uint8_t bus);
bool do_transfer(uint8_t address, const uint8_t *send, uint32_t send_len,
uint8_t *recv, uint32_t recv_len);
};
PX4_I2C _device;
uint8_t _address;
uint8_t _retries = 0;