light sensor class and test added

This commit is contained in:
Thomas Klaehn 2016-03-20 17:32:17 +00:00 committed by Thomas Klaehn
parent 4d29363e27
commit 25f163b4cc
2 changed files with 22 additions and 0 deletions

12
light_sensor.py Normal file
View File

@ -0,0 +1,12 @@
import smbus
class light_sensor:
def __init__(self, bus = 1, addr = 0x23):
self.bus = bus
self.addr = addr
self.bus = smbus.SMBus(self.bus)
def read(self):
data = self.bus.read_i2c_block_data(self.addr, 0x10)
lx = int(round((data[0] * 256 + data[1]) / 1.2, 0))
return lx

10
light_sensor_test.py Executable file
View File

@ -0,0 +1,10 @@
#!/usr/bin/python2
from light_sensor import light_sensor
from time import sleep
ls = light_sensor(1, 0x23)
while True:
print "light: " + str(ls.read()) + " lx."
sleep(1)