vsergeev / lua-periphery

A Lua library for peripheral I/O (GPIO, LED, PWM, SPI, I2C, MMIO, Serial) in Linux.
MIT License
182 stars 38 forks source link

Fix for multiple I2C messages. Pop message table from LUA stack #15

Closed niziak closed 7 years ago

niziak commented 7 years ago

Following error during execution of i2c:transfer()

lua: attempt to index a nil value

occurs when try to transfer more messages at once.

My code to reproduce error:

local msgs = { { 0x00 }, {0x00, 0x00, flags = I2C.I2C_M_RD},
               { 0x01 }, {0x00,       flags = I2C.I2C_M_RD},
               { 0x02 }, {0x00, 0x00, flags = I2C.I2C_M_RD},
               { 0x03 }, {0x00, 0x00, flags = I2C.I2C_M_RD},
             }
i2c:transfer(i2c_addr, msgs)

print(string.format("[0x00] Temperature...: 0x%02x%02x", msgs[2][1], msgs[2][2]))
print(string.format("[0x01] Configuration.: 0x%02x"    , msgs[4][1]))
print(string.format("[0x03] Thys..........: 0x%02x%02x", msgs[6][1], msgs[6][2]))
print(string.format("[0x04] Tos...........: 0x%02x%02x", msgs[8][1], msgs[8][2]))
vsergeev commented 7 years ago

Thanks, cherry-picked in 2d39685. This will go into the next release soon with some c-periphery fixes.