Commit f45e6fec authored by HuangRui's avatar HuangRui
Browse files

Followed Vladimir Dronnikov's advices.

https://github.com/nodemcu/nodemcu-firmware/pull/156
parent 9cab7513
...@@ -7,10 +7,10 @@ ...@@ -7,10 +7,10 @@
-- MIT license, http://opensource.org/licenses/MIT -- MIT license, http://opensource.org/licenses/MIT
-- *************************************************************************** -- ***************************************************************************
local moduleName = ... local moduleName = ...
local M = {} local M = {}
_G[moduleName] = M _G[moduleName] = M
--I2C slave address of GY-30 --I2C slave address of GY-30
local GY_30_address = 0X23 local GY_30_address = 0x23
-- i2c interface ID -- i2c interface ID
local id = 0 local id = 0
--LUX --LUX
...@@ -18,12 +18,14 @@ local moduleName = ... ...@@ -18,12 +18,14 @@ local moduleName = ...
--CMD --CMD
local CMD = 0x10 local CMD = 0x10
local init = false local init = false
--Make it more faster
local i2c = i2c
function M.init(sda, scl) function M.init(sda, scl)
i2c.setup(id, sda, scl, i2c.SLOW) i2c.setup(id, sda, scl, i2c.SLOW)
--print("i2c ok..") --print("i2c ok..")
init = true init = true
end end
local function read_data(ADDR, commands, length) local function read_data(ADDR, commands, length)
i2c.start(id) i2c.start(id)
i2c.address(id, ADDR, i2c.TRANSMITTER) i2c.address(id, ADDR, i2c.TRANSMITTER)
i2c.write(id, commands) i2c.write(id, commands)
...@@ -34,21 +36,22 @@ local moduleName = ... ...@@ -34,21 +36,22 @@ local moduleName = ...
c = i2c.read(id, length) c = i2c.read(id, length)
i2c.stop(id) i2c.stop(id)
return c return c
end end
local function read_lux() local function read_lux()
dataT = read_data(GY_30_address, CMD, 2) dataT = read_data(GY_30_address, CMD, 2)
UT = string.byte(dataT, 1) * 256 + string.byte(dataT, 2) --Make it more faster
UT = dataT:byte(1) * 256 + dataT:byte(2)
l = (UT*1000/12) l = (UT*1000/12)
return(l) return(l)
end end
function M.read() function M.read()
if (not init) then if (not init) then
print("init() must be called before read.") print("init() must be called before read.")
else else
read_lux() read_lux()
end end
end end
function M.getlux() function M.getlux()
return l return l
end end
return M return M
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment