upython/tmp/bh1750fvi.py

40 lines
1.4 KiB
Python
Raw Normal View History

2022-04-02 23:48:11 +00:00
# Copyright 2016 Peter Dahlberg
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import time
OP_SINGLE_HRES1 = 0x20
OP_SINGLE_HRES2 = 0x21
OP_SINGLE_LRES = 0x23
DELAY_HMODE = 180 # 180ms in H-mode
DELAY_LMODE = 24 # 24ms in L-mode
def sample(i2c, mode=OP_SINGLE_HRES1, i2c_addr=0x23):
"""
Performs a single sampling. returns the result in lux
"""
i2c.writeto(i2c_addr, b"\x00") # make sure device is in a clean state
i2c.writeto(i2c_addr, b"\x01") # power up
i2c.writeto(i2c_addr, bytes([mode])) # set measurement mode
time.sleep_ms(DELAY_LMODE if mode == OP_SINGLE_LRES else DELAY_HMODE)
raw = i2c.readfrom(i2c_addr, 2)
i2c.writeto(i2c_addr, b"\x00") # power down again
# we must divide the end result by 1.2 to get the lux
return ((raw[0] << 24) | (raw[1] << 16)) // 78642