Post your best Lua script examples here

User avatar
By Rowell H
#13849 I'm currently trying to convert Adafruit's MPL3115A2 library to Lua.

The snippet below is from the getTemperature method

Code: Select allWire.requestFrom((uint8_t)MPL3115A2_ADDRESS, (uint8_t)2);// send data n-bytes read
t = Wire.read(); // receive DATA
t <<= 8;
t |= Wire.read(); // receive DATA
t >>= 4;


Here's how I did it in Lua

Code: Select allt = i2c.read(0, 1)
t1 = bit.lshift(t, 8)
t2 = bit.bor(t1, i2c.read(0, 1))
t3 = bit.rshift(t2, 4)


*drumroll* It doesn't work. I'm sure that I have problems in other places as well, it's mainly with the Wire.

I'd like to get some input for this.
User avatar
By Rowell H
#14501 Ok, I've figured out how to convert the above lines to lua... still no luck though.

Code: Select alllocal moduleName = ...
local M = {}
_G[moduleName] = M

local id = 0
local dev_addr = 0x60

local function read_data(ADDR, commands, length)
    i2c.start(id)
    i2c.address(id, ADDR, i2c.TRANSMITTER)
    i2c.write(id, commands)
    i2c.stop(id)
    i2c.start(id)
    i2c.address(id, ADDR, i2c.RECEIVER)
    tmr.delay(50000)
    c = i2c.read(id, length)
    i2c.stop(id)
    return c
end

local function write_data(ADDR, register, data)
    i2c.start(id)
    i2c.address(id, ADDR, i2c.TRANSMITTER)
    i2c.write(id, register)
    i2c.write(id, data)
    i2c.stop(id)
end

function M.init(d, l)
  if (d ~= nil) and (l ~= nil) and (d >= 0) and (d <= 11) and (l >= 0) and ( l <= 11) and (d ~= l) then
    sda = d
    scl = l
  else
    print("iic config failed!") return nil
  end
    i2c.setup(id, sda, scl, i2c.SLOW)
end

function M.configure()
    write_data(dev_addr, 0x26, 0xb9)
    write_data(dev_addr, 0x13, 0x07)
end

function M.readTemp()
    h, l = string.byte(read_data(dev_addr, 0x04, 2), 1, 2)
    h1 = bit.lshift(h, 8)
    l2 = bit.bor(h1, l)
    rawtemp = bit.rshift(l2, 4)
    return rawtemp/16.0
end

return M


Inside readTemp(), h and l both return 255.

configure() is supposed to behave like begin() in the original Arduino library. Any help would be great.
User avatar
By Steve Morgan
#24325 This little blighter caused me all sorts of trouble in LUA (I'm having more trouble with it now using the Arduino IDE, but that's another story).

It turns out that the MPL3115A2 doesn't like the i2c.stop(id) after you've written the register address. Remove the call after i2c.write(...) in read_data and see how you get on.
User avatar
By Giuseppe
#45418 Hi at all,
how can i translate this arduino code:
Code: Select all   SPI.beginTransaction(SPISettings(SPI_CLOCK_DIV4, MSBFIRST, SPI_MODE0));   // Set the settings to work with SPI bus
   digitalWrite(_chipSelectPin, LOW);      // Select slave
   SPI.transfer(reg & 0x7E);            // MSB == 0 is for writing. LSB is not used in address. Datasheet section 8.1.2.3.
   for (byte index = 0; index < count; index++) {
      SPI.transfer(values[index]);
   }
   digitalWrite(_chipSelectPin, HIGH);      // Release slave again
   SPI.endTransaction(); // Stop using the SPI bus