I've been banging my head against this one for a few hours now! I'm going mad but I'm hoping someone can explain my stupid mistake.
I want to write a userland lua driver for this lux sensor - based on adafruit's arduino example here: https://github.com/adafruit/TSL2561-Arduino-Library
I've got the device to respond to an i2c scan, and the address is what I expect. However I can't get the first stage of initialisation to return a value as expected from her code.
Can anyone help?
My code:
-- This code is GPL v3 by gareth@l0l.org.uk
-- blah blah blah standard licence conditions apply blah blah blah
-- Special hat-tip to lady ada - hacker hero - amongst many others
-- Reads value of TSL2561 I2C Luminosity sensor
-- As used on breakout board by Adafruit
id=0 -- need this to identify (software) IC2 bus?
io_pin= {[0]=3,[2]=4,[4]=2,[5]=1,[12]=6,[13]=7,[14]=5} -- this maps GPIO numbers to internal IO references
sda=io_pin[13] -- connect to pin GPIO13
scl=io_pin[14] -- connect to pin GPIO14
addr=0x39 -- the I2C address of our device
TSL2561_REGISTER_ID=0x0A -- part of the register table
function initialise(addr)
-- initialize i2c with our id and pins in slow mode :-)
i2c.setup(id,sda,scl,i2c.SLOW)
result=read_reg(addr,TSL2561_REGISTER_ID)
print(tonumber(result))
--if bit.band(result, 0x0A)==true then
-- print("Found TSL2561")
--end
end
-- 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
initialise(addr)