Skip to content

Commit f75f801

Browse files
peterbarkertridge
authored andcommitted
AP_RangeFinder: add and use probe_i2c_buses method
1 parent ce6d78c commit f75f801

File tree

2 files changed

+27
-59
lines changed

2 files changed

+27
-59
lines changed

libraries/AP_RangeFinder/AP_RangeFinder.cpp

Lines changed: 23 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,24 @@ __INITFUNC__ bool RangeFinder::_add_backend(AP_RangeFinder_Backend *backend, uin
281281
return true;
282282
}
283283

284+
/*
285+
* probe all i2c buses for a backend type:
286+
*/
287+
__INITFUNC__ void RangeFinder::probe_i2c_buses(uint8_t instance, uint8_t addr, i2c_probe_fn_t detect_fn)
288+
{
289+
FOREACH_I2C(i) {
290+
auto *device_ptr = hal.i2c_mgr->get_device_ptr(i, addr);
291+
if (device_ptr == nullptr) {
292+
continue;
293+
}
294+
if (_add_backend(detect_fn(state[instance], params[instance], *device_ptr),
295+
instance)) {
296+
return;
297+
}
298+
delete device_ptr;
299+
}
300+
}
301+
284302
/*
285303
detect if an instance of a rangefinder is connected.
286304
*/
@@ -308,18 +326,7 @@ __INITFUNC__ void RangeFinder::detect_instance(uint8_t instance, uint8_t& serial
308326
if (params[instance].address != 0) {
309327
addr = params[instance].address;
310328
}
311-
FOREACH_I2C(i) {
312-
auto *device_ptr = hal.i2c_mgr->get_device_ptr(i, addr);
313-
if (device_ptr == nullptr) {
314-
continue;
315-
}
316-
if (_add_backend(AP_RangeFinder_MaxsonarI2CXL::detect(state[instance], params[instance],
317-
*device_ptr),
318-
instance)) {
319-
break;
320-
}
321-
delete device_ptr;
322-
}
329+
probe_i2c_buses(instance, addr, AP_RangeFinder_MaxsonarI2CXL::detect);
323330
break;
324331
}
325332
#endif
@@ -340,36 +347,15 @@ __INITFUNC__ void RangeFinder::detect_instance(uint8_t instance, uint8_t& serial
340347
instance);
341348
}
342349
#else
343-
FOREACH_I2C(i) {
344-
auto *device_ptr = hal.i2c_mgr->get_device_ptr(i, params[instance].address);
345-
if (device_ptr == nullptr) {
346-
continue;
347-
}
348-
if (_add_backend(AP_RangeFinder_LightWareI2C::detect(state[instance], params[instance],
349-
*device_ptr),
350-
instance)) {
351-
break;
352-
}
353-
}
350+
probe_i2c_buses(instance, params[instance].address, AP_RangeFinder_LightWareI2C::detect);
354351
#endif
355352
}
356353
break;
357354
#endif // AP_RANGEFINDER_LWI2C_ENABLED
358355
#if AP_RANGEFINDER_TRI2C_ENABLED
359356
case Type::TRI2C:
360357
if (params[instance].address) {
361-
FOREACH_I2C(i) {
362-
auto *device_ptr = hal.i2c_mgr->get_device_ptr(i, params[instance].address);
363-
if (device_ptr == nullptr) {
364-
continue;
365-
}
366-
if (_add_backend(AP_RangeFinder_TeraRangerI2C::detect(state[instance], params[instance],
367-
*device_ptr),
368-
instance)) {
369-
break;
370-
}
371-
delete device_ptr;
372-
}
358+
probe_i2c_buses(instance, params[instance].address, AP_RangeFinder_TeraRangerI2C::detect);
373359
}
374360
break;
375361
#endif
@@ -400,18 +386,7 @@ __INITFUNC__ void RangeFinder::detect_instance(uint8_t instance, uint8_t& serial
400386
if (params[instance].address != 0) {
401387
addr = params[instance].address;
402388
}
403-
FOREACH_I2C(i) {
404-
auto *device_ptr = hal.i2c_mgr->get_device_ptr(i, addr);
405-
if (device_ptr == nullptr) {
406-
continue;
407-
}
408-
if (_add_backend(AP_RangeFinder_Benewake_TFMiniPlus::detect(state[instance], params[instance],
409-
*device_ptr),
410-
instance)) {
411-
break;
412-
}
413-
delete device_ptr;
414-
}
389+
probe_i2c_buses(instance, addr, AP_RangeFinder_Benewake_TFMiniPlus::detect);
415390
break;
416391
}
417392
#endif
@@ -421,18 +396,7 @@ __INITFUNC__ void RangeFinder::detect_instance(uint8_t instance, uint8_t& serial
421396
if (params[instance].address != 0) {
422397
addr = params[instance].address;
423398
}
424-
FOREACH_I2C(i) {
425-
auto *device_ptr = hal.i2c_mgr->get_device_ptr(i, addr);
426-
if (device_ptr == nullptr) {
427-
continue;
428-
}
429-
if (_add_backend(AP_RangeFinder_Benewake_TFS20L::detect(state[instance], params[instance],
430-
*device_ptr),
431-
instance)) {
432-
break;
433-
}
434-
delete device_ptr;
435-
}
399+
probe_i2c_buses(instance, addr, AP_RangeFinder_Benewake_TFS20L::detect);
436400
break;
437401
}
438402
#endif

libraries/AP_RangeFinder/AP_RangeFinder.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,10 @@ class RangeFinder
340340

341341
bool _add_backend(AP_RangeFinder_Backend *driver, uint8_t instance, uint8_t serial_instance=0);
342342

343+
// search all i2c buses for a device at addr
344+
using i2c_probe_fn_t = AP_RangeFinder_Backend* (*)(RangeFinder::RangeFinder_State&, AP_RangeFinder_Params&, AP_HAL::I2CDevice&);
345+
void probe_i2c_buses(uint8_t instance, uint8_t addr, i2c_probe_fn_t detect_fn);
346+
343347
uint32_t _log_rfnd_bit = -1;
344348
void Log_RFND() const;
345349
};

0 commit comments

Comments
 (0)