Skip to content

Commit 425ad2e

Browse files
author
vsky
committed
Fixing Lua module warnings
1 parent 3649a8f commit 425ad2e

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

lua_modules/bme280/bme280.lua

+35-25
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
--[[
2-
// BME280 Lua module
3-
// Requires i2c and bme280_math module
4-
// Written by Lukas Voborsky, @voborsky
1+
--[[
2+
BME280 Lua module
3+
Requires i2c and bme280_math module
4+
Written by Lukas Voborsky, @voborsky
55
]]
66

77
local bme280 = {}
88

99
-- bme280.setup()
10-
-- bme280:setup()
11-
-- bme280:read()
10+
-- bme280:setup()
11+
-- bme280:read()
1212
-- bme280:altitude()
1313
-- bme280:dewpoint()
1414
-- bme280:qfe2qnh()
1515
-- bme280:startreadout()
1616

17-
local print, type, assert = print, type, assert
18-
local table_concat, node_heap, math_floor = table.concat, node.heap, math.floor
19-
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
20-
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
17+
local type, assert = type, assert
18+
local table_concat, math_floor = table.concat, math.floor
19+
local i2c_start, i2c_stop, i2c_address, i2c_read, i2c_write, i2c_TRANSMITTER, i2c_RECEIVER =
20+
i2c.start, i2c.stop, i2c.address, i2c.read, i2c.write, i2c.TRANSMITTER, i2c.RECEIVER
21+
local bme280_math_setup, bme280_math_read, bme280_math_qfe2qnh, bme280_math_altitude, bme280_math_dewpoint =
22+
bme280_math.setup, bme280_math.read, bme280_math.qfe2qnh, bme280_math.altitude, bme280_math.dewpoint
2123
local tmr_create, tmr_ALARM_SINGLE = tmr.create, tmr.ALARM_SINGLE
2224

2325

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

3840
-- local BME280_FORCED_MODE = 0x01
3941

40-
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
42+
-- maximum measurement time in ms for maximum oversampling for all measures
43+
-- 113 > 1.25 + 2.3*16 + 2.3*16 + 0.575 + 2.3*16 + 0.575 ms
44+
local BME280_SAMPLING_DELAY =113
4145

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

6368
function bme280.setup(id, addr, temp_oss, press_oss, humi_oss, power_mode, inactive_duration, IIR_filter, full_init)
64-
return bme280_setup(nil, id, addr, temp_oss, press_oss, humi_oss, power_mode, inactive_duration, IIR_filter, full_init)
69+
return bme280_setup(nil, id,
70+
addr, temp_oss, press_oss, humi_oss, power_mode, inactive_duration, IIR_filter, full_init)
6571
end
6672

67-
------------------------------------------------ -----------------------------
68-
function bme280_setup(self, id, addr, temp_oss, press_oss, humi_oss, power_mode, inactive_duration, IIR_filter, full_init)
73+
------------------------------------------------------------------------------
74+
function bme280_setup(self, id, addr,
75+
temp_oss, press_oss, humi_oss, power_mode, inactive_duration, IIR_filter, full_init)
6976

70-
local addr = (addr==2) and BME280_I2C_ADDRESS2 or BME280_I2C_ADDRESS1
77+
addr = (addr==2) and BME280_I2C_ADDRESS2 or BME280_I2C_ADDRESS1
7178
full_init = full_init or true
72-
79+
7380
-- debug("%d %x %d", id, addr, BME280_REGISTER_CHIPID)
7481
local chipid = read_reg(id, addr, BME280_REGISTER_CHIPID, 1)
7582
if not chipid then
@@ -85,9 +92,10 @@ function bme280_setup(self, id, addr, temp_oss, press_oss, humi_oss, power_mode,
8592
buf[3] = read_reg(id, addr, BME280_REGISTER_DIG_H1, 1)
8693
buf[4] = read_reg(id, addr, BME280_REGISTER_DIG_H2, 7)
8794
end
88-
89-
local sensor, config = bme280_math_setup(table_concat(buf), temp_oss, press_oss, humi_oss, power_mode, inactive_duration, IIR_filter)
90-
local self = self or {
95+
96+
local sensor, config = bme280_math_setup(table_concat(buf),
97+
temp_oss, press_oss, humi_oss, power_mode, inactive_duration, IIR_filter)
98+
self = self or {
9199
setup = bme280_setup,
92100
read = bme280_read,
93101
startreadout = bme280_startreadout,
@@ -97,13 +105,13 @@ function bme280_setup(self, id, addr, temp_oss, press_oss, humi_oss, power_mode,
97105
}
98106
self.id, self.addr = id, addr
99107
self._sensor, self._config, self._isbme = sensor, config, isbme
100-
108+
101109
if (full_init) then
102110
write_reg(id, addr, BME280_REGISTER_CONFIG, config[1])
103111
if (isbme) then write_reg(id, addr, BME280_REGISTER_CONTROL_HUM, config[2]) end
104112
write_reg(id, addr, BME280_REGISTER_CONTROL, config[3])
105113
end
106-
114+
107115
return self
108116
end
109117

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

122130
delay = delay or BME280_SAMPLING_DELAY
123-
131+
124132
if self._isbme then write_reg(self.id, self.addr, BME280_REGISTER_CONTROL_HUM, self._config[2]) end
125-
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
126-
-- Lua 5.3 integer division // would be more suitable
127-
133+
write_reg(self.id, self.addr, BME280_REGISTER_CONTROL, math_floor(self._config[3]:byte(1)/4)+ 1)
134+
-- math_floor(self._config[3]:byte(1)/4)+ 1
135+
-- an awful way to avoid bit operations but calculate (config[3] & 0xFC) | BME280_FORCED_MODE
136+
-- Lua 5.3 integer division // would be more suitable
137+
128138
tmr_create():alarm(delay, tmr_ALARM_SINGLE,
129139
function()
130140
callback(bme280_read(self, alt))

0 commit comments

Comments
 (0)