Post your best Lua script examples here

User avatar
By vayu_esp
#9221
sancho wrote:This small script scans i2c bus and returns address of any device responding.
Code: Select allid=0
sda=8
scl=9

-- initialize i2c, set pin1 as sda, set pin0 as scl
i2c.setup(id,sda,scl,i2c.SLOW)

for i=0,127 do
  i2c.start(id)
  resCode = i2c.address(id, i, i2c.TRANSMITTER)
  i2c.stop(id)
  if resCode == true then print("We have a device on address 0x" .. string.format("%02x", i) .. " (" .. i ..")") end
end


Enjoy ;)



when i run the script to detect i2c device, rescode always gets 1, whether or not device is connected.
I am using latest firmware.
and return 1 in below function seems problem?


static int i2c_address( lua_State *L )
{
unsigned id = luaL_checkinteger( L, 1 );
int address = luaL_checkinteger( L, 2 );
int direction = luaL_checkinteger( L, 3 );

MOD_CHECK_ID( i2c, id );
if ( address < 0 || address > 127 )
return luaL_error( L, "wrong arg range" );
lua_pushboolean( L, platform_i2c_send_address( id, (u16)address, direction ) );
return 1;
}
User avatar
By Fr4gg0r
#9348 There was another thread on this topic somewhere..
Anyway, I had the same problem, when the pin in question was pulled low.
In my case GPIO0 after flashing.

The "1" indicates how many arguments shall be returned in lua, it does not represent the actual return value.
The result of "platform_i2c_send_address( id, (u16)address, direction )" is returned in lua.