I2C scanner - my first lua program
Posted: Sun Dec 28, 2014 5:22 pm
<edit> there is an improved version of script below</edit>
Hey!
Inspired by the great work going on here - and especially delighted that Zeroday has opened up the lua firmware - I decided to try writing a program in lua.
I needed to start working with i2c so I decided to port a scanner sketch over - see here for an arduino(ish) example: http://jeelabs.net/boards/6/topics/4356
So after a bit of head-scratching here it is (any improvements, suggestions etc welcomed!):
I am really starting to enjoy lua - it seems really easy to just do stuff!
Keep up the good work everybody
2015 is gonna be a great year for IoT gadgets!!!
G
Hey!
Inspired by the great work going on here - and especially delighted that Zeroday has opened up the lua firmware - I decided to try writing a program in lua.
I needed to start working with i2c so I decided to port a scanner sketch over - see here for an arduino(ish) example: http://jeelabs.net/boards/6/topics/4356
So after a bit of head-scratching here it is (any improvements, suggestions etc welcomed!):
Code: Select all
-- Based on work by zeroday & sancho among many other open source authors
-- This code is public domain, attribution to gareth@l0l.org.uk appreciated.
id=0 -- need this to identify (software) IC2 bus?
sda=3 -- connect to pin GPIO0
scl=4 -- connect to pin GPIO2
-- initialize i2c with our id and pins in slow mode :-)
i2c.setup(id,sda,scl,i2c.SLOW)
-- user defined function: read from reg_addr content of dev_addr
function read_reg(dev_addr, reg_addr)
i2c.start(id)
i2c.address(id, dev_addr ,i2c.TRANSMITTER)
i2c.write(id,reg_addr)
i2c.stop(id)
i2c.start(id)
i2c.address(id, dev_addr,i2c.RECEIVER)
c=i2c.read(id,1)
i2c.stop(id)
return c
end
print("Scanning I2C Bus")
for i=0,127 do
if (string.byte(read_reg(i, 0))==0) then
print("Device found at address "..string.format("%02X",i))
end
end
I am really starting to enjoy lua - it seems really easy to just do stuff!
Keep up the good work everybody
2015 is gonna be a great year for IoT gadgets!!!
G