#!/usr/bin/python
# -*- coding: utf-8 -*-
# Author: Matt Hawkins
# Author's Git: https://bitbucket.org/MattHawkinsUK/
# Author's website: https://www.raspberrypi-spy.co.uk
# http://elecrow.com/

import time

import RPi.GPIO as GPIO
import smbus2

# Find the right revision for bus driver
if GPIO.RPI_REVISION == 1:
    bus = smbus2.SMBus(0)
else:
    bus = smbus2.SMBus(1)


class LightSensor(object):

    def __init__(self):
        # Define some constants from the datasheet

        self.DEVICE = 0x5c  # Default device I2C address

        self.POWER_DOWN = 0x00  # No active state
        self.POWER_ON = 0x01  # Power on
        self.RESET = 0x07  # Reset data register value

        # Start measurement at 4lx resolution. Time typically 16ms.
        self.CONTINUOUS_LOW_RES_MODE = 0x13
        # Start measurement at 1lx resolution. Time typically 120ms
        self.CONTINUOUS_HIGH_RES_MODE_1 = 0x10
        # Start measurement at 0.5lx resolution. Time typically 120ms
        self.CONTINUOUS_HIGH_RES_MODE_2 = 0x11
        # Start measurement at 1lx resolution. Time typically 120ms
        # Device is automatically set to Power Down after measurement.
        self.ONE_TIME_HIGH_RES_MODE_1 = 0x20
        # Start measurement at 0.5lx resolution. Time typically 120ms
        # Device is automatically set to Power Down after measurement.
        self.ONE_TIME_HIGH_RES_MODE_2 = 0x21
        # Start measurement at 1lx resolution. Time typically 120ms
        # Device is automatically set to Power Down after measurement.
        self.ONE_TIME_LOW_RES_MODE = 0x23

    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):
        data = bus.read_i2c_block_data(self.DEVICE, self.ONE_TIME_HIGH_RES_MODE_1, 32)
        return self.convertToNumber(data)


def main():
    sensor = LightSensor()
    try:
        while True:
            print("Light Level : {0} lx".format(sensor.readLight()))
            time.sleep(0.5)
    except KeyboardInterrupt:
        pass


if __name__ == "__main__":
    main()
