Skip to content

Commit

Permalink
Fixing Lua module warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
vsky committed May 29, 2020
1 parent 3649a8f commit 425ad2e
Showing 1 changed file with 35 additions and 25 deletions.
60 changes: 35 additions & 25 deletions lua_modules/bme280/bme280.lua
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
--[[
// BME280 Lua module
// Requires i2c and bme280_math module
// Written by Lukas Voborsky, @voborsky
--[[
BME280 Lua module
Requires i2c and bme280_math module
Written by Lukas Voborsky, @voborsky
]]

local bme280 = {}

-- bme280.setup()
-- bme280:setup()
-- bme280:read()
-- bme280:setup()
-- bme280:read()
-- bme280:altitude()
-- bme280:dewpoint()
-- bme280:qfe2qnh()
-- bme280:startreadout()

local print, type, assert = print, type, assert
local table_concat, node_heap, math_floor = table.concat, node.heap, math.floor
local i2c_start, i2c_stop, i2c_address, i2c_read, i2c_write, i2c_TRANSMITTER, i2c_RECEIVER = i2c.start, i2c.stop, i2c.address, i2c.read, i2c.write, i2c.TRANSMITTER, i2c.RECEIVER
local bme280_math_setup, bme280_math_read, bme280_math_qfe2qnh, bme280_math_altitude, bme280_math_dewpoint = bme280_math.setup, bme280_math.read, bme280_math.qfe2qnh, bme280_math.altitude, bme280_math.dewpoint
local type, assert = type, assert
local table_concat, math_floor = table.concat, math.floor
local i2c_start, i2c_stop, i2c_address, i2c_read, i2c_write, i2c_TRANSMITTER, i2c_RECEIVER =
i2c.start, i2c.stop, i2c.address, i2c.read, i2c.write, i2c.TRANSMITTER, i2c.RECEIVER
local bme280_math_setup, bme280_math_read, bme280_math_qfe2qnh, bme280_math_altitude, bme280_math_dewpoint =
bme280_math.setup, bme280_math.read, bme280_math.qfe2qnh, bme280_math.altitude, bme280_math.dewpoint
local tmr_create, tmr_ALARM_SINGLE = tmr.create, tmr.ALARM_SINGLE


Expand All @@ -37,7 +39,9 @@ local BME280_REGISTER_PRESS = 0xF7 -- 0xF7-0xF9

-- local BME280_FORCED_MODE = 0x01

local BME280_SAMPLING_DELAY =113 -- maximum measurement time in ms for maximum oversampling for all measures = 1.25 + 2.3*16 + 2.3*16 + 0.575 + 2.3*16 + 0.575 ms
-- maximum measurement time in ms for maximum oversampling for all measures
-- 113 > 1.25 + 2.3*16 + 2.3*16 + 0.575 + 2.3*16 + 0.575 ms
local BME280_SAMPLING_DELAY =113

-- Local functions
local read_reg
Expand All @@ -49,6 +53,7 @@ local bme280_startreadout
-- -- Note that the space between debug and the arglist is there for a reason
-- -- so that a simple global edit " debug(" -> "-- debug(" or v.v. to
-- -- toggle debug compiled into the module.
-- local print, node_heap = print, node.heap
-- local function debug (fmt, ...) -- upval: cnt (, print, node_heap)
-- if not bme280.debug then return end
-- if (...) then fmt = fmt:format(...) end
Expand All @@ -61,15 +66,17 @@ local bme280_startreadout
---------------------------------------------------------------------------------

function bme280.setup(id, addr, temp_oss, press_oss, humi_oss, power_mode, inactive_duration, IIR_filter, full_init)
return bme280_setup(nil, id, addr, temp_oss, press_oss, humi_oss, power_mode, inactive_duration, IIR_filter, full_init)
return bme280_setup(nil, id,
addr, temp_oss, press_oss, humi_oss, power_mode, inactive_duration, IIR_filter, full_init)
end

------------------------------------------------ -----------------------------
function bme280_setup(self, id, addr, temp_oss, press_oss, humi_oss, power_mode, inactive_duration, IIR_filter, full_init)
------------------------------------------------------------------------------
function bme280_setup(self, id, addr,
temp_oss, press_oss, humi_oss, power_mode, inactive_duration, IIR_filter, full_init)

local addr = (addr==2) and BME280_I2C_ADDRESS2 or BME280_I2C_ADDRESS1
addr = (addr==2) and BME280_I2C_ADDRESS2 or BME280_I2C_ADDRESS1
full_init = full_init or true

-- debug("%d %x %d", id, addr, BME280_REGISTER_CHIPID)
local chipid = read_reg(id, addr, BME280_REGISTER_CHIPID, 1)
if not chipid then
Expand All @@ -85,9 +92,10 @@ function bme280_setup(self, id, addr, temp_oss, press_oss, humi_oss, power_mode,
buf[3] = read_reg(id, addr, BME280_REGISTER_DIG_H1, 1)
buf[4] = read_reg(id, addr, BME280_REGISTER_DIG_H2, 7)
end

local sensor, config = bme280_math_setup(table_concat(buf), temp_oss, press_oss, humi_oss, power_mode, inactive_duration, IIR_filter)
local self = self or {

local sensor, config = bme280_math_setup(table_concat(buf),
temp_oss, press_oss, humi_oss, power_mode, inactive_duration, IIR_filter)
self = self or {
setup = bme280_setup,
read = bme280_read,
startreadout = bme280_startreadout,
Expand All @@ -97,13 +105,13 @@ function bme280_setup(self, id, addr, temp_oss, press_oss, humi_oss, power_mode,
}
self.id, self.addr = id, addr
self._sensor, self._config, self._isbme = sensor, config, isbme

if (full_init) then
write_reg(id, addr, BME280_REGISTER_CONFIG, config[1])
if (isbme) then write_reg(id, addr, BME280_REGISTER_CONTROL_HUM, config[2]) end
write_reg(id, addr, BME280_REGISTER_CONTROL, config[3])
end

return self
end

Expand All @@ -120,11 +128,13 @@ function bme280_startreadout(self, callback, delay, alt)
assert(type(callback) == "function", "invalid callback parameter")

delay = delay or BME280_SAMPLING_DELAY

if self._isbme then write_reg(self.id, self.addr, BME280_REGISTER_CONTROL_HUM, self._config[2]) end
write_reg(self.id, self.addr, BME280_REGISTER_CONTROL, math_floor(self._config[3]:byte(1)/4)+ 1) -- awful way to avoid bit operations but calculate (config[3] & 0xFC) | BME280_FORCED_MODE
-- Lua 5.3 integer division // would be more suitable

write_reg(self.id, self.addr, BME280_REGISTER_CONTROL, math_floor(self._config[3]:byte(1)/4)+ 1)
-- math_floor(self._config[3]:byte(1)/4)+ 1
-- an awful way to avoid bit operations but calculate (config[3] & 0xFC) | BME280_FORCED_MODE
-- Lua 5.3 integer division // would be more suitable

tmr_create():alarm(delay, tmr_ALARM_SINGLE,
function()
callback(bme280_read(self, alt))
Expand Down

0 comments on commit 425ad2e

Please sign in to comment.