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
5
5
]]
6
6
7
7
local bme280 = {}
8
8
9
9
-- bme280.setup()
10
- -- bme280:setup()
11
- -- bme280:read()
10
+ -- bme280:setup()
11
+ -- bme280:read()
12
12
-- bme280:altitude()
13
13
-- bme280:dewpoint()
14
14
-- bme280:qfe2qnh()
15
15
-- bme280:startreadout()
16
16
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
21
23
local tmr_create , tmr_ALARM_SINGLE = tmr .create , tmr .ALARM_SINGLE
22
24
23
25
@@ -37,7 +39,9 @@ local BME280_REGISTER_PRESS = 0xF7 -- 0xF7-0xF9
37
39
38
40
-- local BME280_FORCED_MODE = 0x01
39
41
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
41
45
42
46
-- Local functions
43
47
local read_reg
@@ -49,6 +53,7 @@ local bme280_startreadout
49
53
-- -- Note that the space between debug and the arglist is there for a reason
50
54
-- -- so that a simple global edit " debug(" -> "-- debug(" or v.v. to
51
55
-- -- toggle debug compiled into the module.
56
+ -- local print, node_heap = print, node.heap
52
57
-- local function debug (fmt, ...) -- upval: cnt (, print, node_heap)
53
58
-- if not bme280.debug then return end
54
59
-- if (...) then fmt = fmt:format(...) end
@@ -61,15 +66,17 @@ local bme280_startreadout
61
66
---- -----------------------------------------------------------------------------
62
67
63
68
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 )
65
71
end
66
72
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 )
69
76
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
71
78
full_init = full_init or true
72
-
79
+
73
80
-- debug("%d %x %d", id, addr, BME280_REGISTER_CHIPID)
74
81
local chipid = read_reg (id , addr , BME280_REGISTER_CHIPID , 1 )
75
82
if not chipid then
@@ -85,9 +92,10 @@ function bme280_setup(self, id, addr, temp_oss, press_oss, humi_oss, power_mode,
85
92
buf [3 ] = read_reg (id , addr , BME280_REGISTER_DIG_H1 , 1 )
86
93
buf [4 ] = read_reg (id , addr , BME280_REGISTER_DIG_H2 , 7 )
87
94
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 {
91
99
setup = bme280_setup ,
92
100
read = bme280_read ,
93
101
startreadout = bme280_startreadout ,
@@ -97,13 +105,13 @@ function bme280_setup(self, id, addr, temp_oss, press_oss, humi_oss, power_mode,
97
105
}
98
106
self .id , self .addr = id , addr
99
107
self ._sensor , self ._config , self ._isbme = sensor , config , isbme
100
-
108
+
101
109
if (full_init ) then
102
110
write_reg (id , addr , BME280_REGISTER_CONFIG , config [1 ])
103
111
if (isbme ) then write_reg (id , addr , BME280_REGISTER_CONTROL_HUM , config [2 ]) end
104
112
write_reg (id , addr , BME280_REGISTER_CONTROL , config [3 ])
105
113
end
106
-
114
+
107
115
return self
108
116
end
109
117
@@ -120,11 +128,13 @@ function bme280_startreadout(self, callback, delay, alt)
120
128
assert (type (callback ) == " function" , " invalid callback parameter" )
121
129
122
130
delay = delay or BME280_SAMPLING_DELAY
123
-
131
+
124
132
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
+
128
138
tmr_create ():alarm (delay , tmr_ALARM_SINGLE ,
129
139
function ()
130
140
callback (bme280_read (self , alt ))
0 commit comments