<reponame>jmFernandezMola/yaws
# Based on adafruit library for arduino written in c++
# Originaly written by <NAME>/Ladyada for Adafruit Industries

import smbus
import time
from statistics import mean

# COMMANDS
SI1145_PARAM_QUERY  = 0x80
SI1145_PARAM_SET  = 0xA0
SI1145_NOP  = 0x0
SI1145_RESET     = 0x01
SI1145_BUSADDR     = 0x02
SI1145_PS_FORCE     = 0x05
SI1145_ALS_FORCE     = 0x06
SI1145_PSALS_FORCE     = 0x07
SI1145_PS_PAUSE     = 0x09
SI1145_ALS_PAUSE     = 0x0A
SI1145_PSALS_PAUSE     = 0xB
SI1145_PS_AUTO     = 0x0D
SI1145_ALS_AUTO    = 0x0E
SI1145_PSALS_AUTO  = 0x0F
SI1145_GET_CAL     = 0x12

#Parameters
SI1145_PARAM_I2CADDR  = 0x00
SI1145_PARAM_CHLIST    = 0x01
SI1145_PARAM_CHLIST_ENUV  = 0x80
SI1145_PARAM_CHLIST_ENAUX  = 0x40
SI1145_PARAM_CHLIST_ENALSIR  = 0x20
SI1145_PARAM_CHLIST_ENALSVIS  = 0x10
SI1145_PARAM_CHLIST_ENPS1  = 0x01
SI1145_PARAM_CHLIST_ENPS2  = 0x02
SI1145_PARAM_CHLIST_ENPS3  = 0x04

SI1145_PARAM_PSLED12SEL    = 0x02
SI1145_PARAM_PSLED12SEL_PS2NONE  = 0x00
SI1145_PARAM_PSLED12SEL_PS2LED1  = 0x10
SI1145_PARAM_PSLED12SEL_PS2LED2  = 0x20
SI1145_PARAM_PSLED12SEL_PS2LED3  = 0x40
SI1145_PARAM_PSLED12SEL_PS1NONE  = 0x00
SI1145_PARAM_PSLED12SEL_PS1LED1  = 0x01
SI1145_PARAM_PSLED12SEL_PS1LED2  = 0x02
SI1145_PARAM_PSLED12SEL_PS1LED3  = 0x04

SI1145_PARAM_PSLED3SEL    = 0x03
SI1145_PARAM_PSENCODE    = 0x05
SI1145_PARAM_ALSENCODE   = 0x06

SI1145_PARAM_PS1ADCMUX    = 0x07
SI1145_PARAM_PS2ADCMUX    = 0x08
SI1145_PARAM_PS3ADCMUX    = 0x09
SI1145_PARAM_PSADCOUNTER    = 0x0A
SI1145_PARAM_PSADCGAIN  = 0x0B
SI1145_PARAM_PSADCMISC  = 0x0C
SI1145_PARAM_PSADCMISC_RANGE  = 0x20
SI1145_PARAM_PSADCMISC_PSMODE  = 0x04

SI1145_PARAM_ALSIRADCMUX    = 0x0E
SI1145_PARAM_AUXADCMUX    = 0x0F

SI1145_PARAM_ALSVISADCOUNTER    = 0x10
SI1145_PARAM_ALSVISADCGAIN  = 0x11
SI1145_PARAM_ALSVISADCMISC  = 0x12
SI1145_PARAM_ALSVISADCMISC_VISRANGE  = 0x20

SI1145_PARAM_ALSIRADCOUNTER    = 0x1D
SI1145_PARAM_ALSIRADCGAIN  = 0x1E
SI1145_PARAM_ALSIRADCMISC  = 0x1F
SI1145_PARAM_ALSIRADCMISC_RANGE  = 0x20

SI1145_PARAM_ADCCOUNTER_511CLK  = 0x70

SI1145_PARAM_ADCMUX_SMALLIR   = 0x00
SI1145_PARAM_ADCMUX_LARGEIR   = 0x03

# REGISTERS
SI1145_REG_PARTID   = 0x00
SI1145_REG_REVID   = 0x01
SI1145_REG_SEQID   = 0x02

SI1145_REG_INTCFG   = 0x03
SI1145_REG_INTCFG_INTOE  = 0x01
SI1145_REG_INTCFG_INTMODE  = 0x02

SI1145_REG_IRQEN   = 0x04
SI1145_REG_IRQEN_ALSEVERYSAMPLE  = 0x01
SI1145_REG_IRQEN_PS1EVERYSAMPLE  = 0x04
SI1145_REG_IRQEN_PS2EVERYSAMPLE  = 0x08
SI1145_REG_IRQEN_PS3EVERYSAMPLE  = 0x10


SI1145_REG_IRQMODE1  = 0x05
SI1145_REG_IRQMODE2  = 0x06

SI1145_REG_HWKEY   = 0x07
SI1145_REG_MEASRATE0  = 0x08
SI1145_REG_MEASRATE1   = 0x09
SI1145_REG_PSRATE   = 0x0A
SI1145_REG_PSLED21   = 0x0F
SI1145_REG_PSLED3   = 0x10
SI1145_REG_UCOEFF0   = 0x13
SI1145_REG_UCOEFF1   = 0x14
SI1145_REG_UCOEFF2   = 0x15
SI1145_REG_UCOEFF3   = 0x16
SI1145_REG_PARAMWR   = 0x17
SI1145_REG_COMMAND   = 0x18
SI1145_REG_RESPONSE   = 0x20
SI1145_REG_IRQSTAT   = 0x21
SI1145_REG_IRQSTAT_ALS   = 0x01

SI1145_REG_ALSVISDATA0  = 0x22
SI1145_REG_ALSVISDATA1  = 0x23
SI1145_REG_ALSIRDATA0  = 0x24
SI1145_REG_ALSIRDATA1  = 0x25
SI1145_REG_PS1DATA0  = 0x26
SI1145_REG_PS1DATA1  = 0x27
SI1145_REG_PS2DATA0  = 0x28
SI1145_REG_PS2DATA1  = 0x29
SI1145_REG_PS3DATA0  = 0x2A
SI1145_REG_PS3DATA1  = 0x2B
SI1145_REG_UVINDEX0  = 0x2C
SI1145_REG_UVINDEX1  = 0x2D
SI1145_REG_PARAMRD  = 0x2E
SI1145_REG_CHIPSTAT  = 0x30

SI1145_ADDR  = 0x60

def initPeripheral():
    bus = smbus.SMBus(1)
    return bus

def readUV():
    return read16(0x2C)

def readIR():
    return read16(0x24)

def readVisible():
    return read16(0x22)

def read16(addr):
    global i2c1145
    return ((i2c1145.read_byte_data(SI1145_ADDR, addr+1)<<8) | i2c1145.read_byte_data(SI1145_ADDR, addr))

def read8(addr):
    global i2c1145
    return i2c1145.read_byte_data(SI1145_ADDR, addr)

def write8(addr,value):
    global i2c1145
    return i2c1145.write_byte_data(SI1145_ADDR, addr, value)
    return

def readParam():
    return

def writeParam(param, value):
    write8(SI1145_REG_PARAMWR, value)
    write8(SI1145_REG_COMMAND, param | SI1145_PARAM_SET)
    return read8(SI1145_REG_PARAMRD)

def forceConversion():
    write8(SI1145_REG_COMMAND, SI1145_PSALS_FORCE);
    return 

def getLight(num_meas):
#Try read
    id = read8(SI1145_REG_PARTID)
    if (id != 0x45):
        raise Exception("Ooops, no chip detected!")

# Set UV coef to default:

    write8(SI1145_REG_UCOEFF0, 0x7B);
    write8(SI1145_REG_UCOEFF1, 0x6B);
    write8(SI1145_REG_UCOEFF2, 0x01);
    write8(SI1145_REG_UCOEFF3, 0x00);

# Enable UV, IR and VIS
    writeParam(SI1145_PARAM_CHLIST, SI1145_PARAM_CHLIST_ENUV |
      SI1145_PARAM_CHLIST_ENALSIR | SI1145_PARAM_CHLIST_ENALSVIS)

# Measurement rate for forced
    write8(SI1145_REG_MEASRATE0, 0x0)
    write8(SI1145_REG_MEASRATE1, 0x0)

# Adjust ADC for IR. Use SI1145_PARAM_ADCMUX_SMALLIR in case of overflow
    writeParam(SI1145_PARAM_ALSIRADCMUX, SI1145_PARAM_ADCMUX_SMALLIR)
# fastest clocks, clock div 1
    writeParam(SI1145_PARAM_ALSIRADCGAIN, 0)
# take 511 clocks to measure
    writeParam(SI1145_PARAM_ALSIRADCOUNTER, SI1145_PARAM_ADCCOUNTER_511CLK)
# in normal range mode
    writeParam(SI1145_PARAM_ALSIRADCMISC, 0)

# fastest clocks, clock div 1
    writeParam(SI1145_PARAM_ALSVISADCGAIN, 0)
# take 511 clocks to measure
    writeParam(SI1145_PARAM_ALSVISADCOUNTER, SI1145_PARAM_ADCCOUNTER_511CLK)
# in normal range mode (not in high sensitivity)
#writeParam(SI1145_PARAM_ALSVISADCMISC, SI1145_PARAM_ALSVISADCMISC_VISRANGE)
    writeParam(SI1145_PARAM_ALSVISADCMISC, 0) #normal range

# Measure now
    visibleMeasurements = []
    irMeasurements = []
    uvMeasurements = []
    for i in range(num_meas):
        forceConversion()
        time.sleep(0.5)
	visibleMeasurements.append( (readVisible() - 0xFF)/0.282 )
	irMeasurements.append( (readIR() - 0xFF)/0.244 )
        uvMeasurements.append( readUV() * 0.01 )

    meanVis = mean(visibleMeasurements)
    meanIR = mean(irMeasurements)
    meanUV = mean(uvMeasurements)
        
    #print("The read value for visible light is {}".format( meanVis))
    #print("The read value for IR light is {}".format( meanIR))
    #print("The read value for UV index is {}\n".format( meanUV ))

    return([meanVis, meanIR, meanUV])

i2c1145 =  initPeripheral()


