mirror of
https://github.com/ArduPilot/ardupilot
synced 2025-02-22 07:44:03 -04:00
AP_Scripting: add i2c scan example
This commit is contained in:
parent
7b3097972b
commit
1f0e36e10d
27
libraries/AP_Scripting/examples/i2c_scan.lua
Normal file
27
libraries/AP_Scripting/examples/i2c_scan.lua
Normal file
@ -0,0 +1,27 @@
|
||||
-- This script scans for devices on the i2c bus
|
||||
local address = 0
|
||||
local found = 0
|
||||
|
||||
local i2c_bus = i2c.get_device(0,0)
|
||||
i2c_bus:set_retries(10)
|
||||
|
||||
function update() -- this is the loop which periodically runs
|
||||
|
||||
i2c_bus:set_address(address)
|
||||
|
||||
if i2c_bus:read_registers(0) then
|
||||
gcs:send_text(0, "Found I2C at " .. tostring(address))
|
||||
found = found + 1
|
||||
end
|
||||
|
||||
address = address + 1
|
||||
if address == 127 then
|
||||
address = 0
|
||||
gcs:send_text(0, "Found " .. tostring(found) .. " devices")
|
||||
found = 0
|
||||
end
|
||||
|
||||
return update, 100 -- reschedules the loop
|
||||
end
|
||||
|
||||
return update() -- run immediately before starting to reschedule
|
Loading…
Reference in New Issue
Block a user