2021-03-03 03:42:10 -04:00
|
|
|
-- This script scans for devices on the i2c bus
|
2024-05-03 15:22:25 -03:00
|
|
|
|
|
|
|
---@diagnostic disable: need-check-nil
|
|
|
|
|
2021-03-03 03:42:10 -04:00
|
|
|
local address = 0
|
|
|
|
local found = 0
|
|
|
|
|
2022-09-03 13:02:38 -03:00
|
|
|
local i2c_bus = i2c:get_device(0,0)
|
2021-03-03 03:42:10 -04:00
|
|
|
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
|