#!/usr/bin/python
import smbus
import time

# Define some constants from the datasheet
# http://www.pibits.net/code/raspberry-pi-bh1750-light-sensor.php


class LDR:
    DEVICE = 0x23  # Default device I2C address
    POWER_DOWN = 0x00  # No active state
    POWER_ON = 0x01  # Power on
    RESET = 0x07  # Reset data register value
    ONE_TIME_HIGH_RES_MODE = 0x20

    def __init__(self):
        self.bus = smbus.SMBus(1)  # Rev 2 Pi uses 1

    def __convertToNumber(self, data):
        # Simple function to convert 2 bytes of data
        # into a decimal number
        return (data[1] + (256 * data[0])) / 1.2

    def readLight(self, addr=None):
        addr = addr if addr else LDR.DEVICE
        data = self.bus.read_i2c_block_data(addr, LDR.ONE_TIME_HIGH_RES_MODE)
        return self.__convertToNumber(data)


if __name__ == "__main__":
    ldr = LDR()
    while True:
        print("Light Level : " + str(ldr.readLight()) + " lux")
        time.sleep(0.5)

