16 |
ron |
1 |
#!/usr/bin/python3
|
5 |
ron |
2 |
# -*- coding: utf-8 -*-
|
|
|
3 |
#
|
|
|
4 |
# sensors/light.py
|
|
|
5 |
#
|
|
|
6 |
# Copyright 2020 Ron Wellsted <ron@wellsted.org.uk>
|
|
|
7 |
#
|
|
|
8 |
# This program is free software; you can redistribute it and/or modify
|
|
|
9 |
# it under the terms of the GNU General Public License as published by
|
|
|
10 |
# the Free Software Foundation; either version 2 of the License, or
|
|
|
11 |
# (at your option) any later version.
|
|
|
12 |
#
|
|
|
13 |
# This program is distributed in the hope that it will be useful,
|
|
|
14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
16 |
# GNU General Public License for more details.
|
|
|
17 |
#
|
|
|
18 |
# You should have received a copy of the GNU General Public License
|
|
|
19 |
# along with this program; if not, write to the Free Software
|
|
|
20 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
21 |
# MA 02110-1301, USA.
|
|
|
22 |
#
|
|
|
23 |
#
|
16 |
ron |
24 |
from sensors import i2cbus, bh1750_address
|
25 |
ron |
25 |
import smbus
|
16 |
ron |
26 |
|
25 |
ron |
27 |
# Define some constants from the datasheet
|
|
|
28 |
DEVICE = 0x23 # Default device I2C address
|
|
|
29 |
POWER_DOWN = 0x00 # No active state
|
|
|
30 |
POWER_ON = 0x01 # Power on
|
|
|
31 |
RESET = 0x07 # Reset data register value
|
|
|
32 |
ONE_TIME_HIGH_RES_MODE = 0x20
|
|
|
33 |
|
|
|
34 |
bus = smbus.SMBus(i2cbus) # Rev 2 Pi uses 1
|
|
|
35 |
|
|
|
36 |
def convertToNumber(data):
|
|
|
37 |
# Simple function to convert 2 bytes of data
|
|
|
38 |
# into a decimal number
|
|
|
39 |
return ((data[1] + (256 * data[0])) / 1.2)
|
|
|
40 |
|
6 |
ron |
41 |
def read_light():
|
25 |
ron |
42 |
data = bus.read_i2c_block_data(bh1750_address,ONE_TIME_HIGH_RES_MODE)
|
|
|
43 |
return convertToNumber(data)
|