1 from machine
import I2C
, Pin
3 from micropython
import const
10 als_conf_0
= const(0x00)
18 interrupt
= const(0x06)
20 #gain 0.125 0.25 1 2 integration time
21 confValues
= { 25: {1/8: bytearray([0x00, 0x13]), 1/4: bytearray([0x00,0x1B]), 1: bytearray([0x00, 0x01]), 2: bytearray([0x00, 0x0B])}
, #25
22 50: {1/8: bytearray([0x00, 0x12]), 1/4: bytearray([0x00,0x1A]), 1: bytearray([0x00, 0x02]), 2: bytearray([0x00, 0x0A])}
, #50
23 100:{1/8: bytearray([0x00, 0x10]), 1/4: bytearray([0x00,0x18]), 1: bytearray([0x00, 0x00]), 2: bytearray([0x00, 0x08])}
, #100
24 200:{1/8: bytearray([0x40, 0x10]), 1/4: bytearray([0x40,0x18]), 1: bytearray([0x40, 0x00]), 2: bytearray([0x40, 0x08])}
, #200
25 400:{1/8: bytearray([0x80, 0x10]), 1/4: bytearray([0x80,0x18]), 1: bytearray([0x80, 0x00]), 2: bytearray([0x80, 0x08])}
, #400
26 800:{1/8: bytearray([0xC0, 0x10]), 1/4: bytearray([0xC0,0x18]), 1: bytearray([0xC0, 0x00]), 2: bytearray([0xC0, 0x08])}
} #800
28 #gain 0.125, 0.25, 1, 2 integration time
29 gainValues
= { 25: {1/8: 1.8432, 1/4: 0.9216, 1: 0.2304, 2: 0.1152}
, #25
30 50: {1/8: 0.9216, 1/4: 0.4608, 1: 0.1152, 2: 0.0576}
, #50
31 100:{1/8: 0.4608, 1/4: 0.2304, 1: 0.0288, 2: 0.0144}
, #100
32 200:{1/8: 0.2304, 1/4: 0.1152, 1: 0.0288, 2: 0.0144}
, #200
33 400:{1/8: 0.1152, 1/4: 0.0576, 1: 0.0144, 2: 0.0072}
, #400
34 800:{1/8: 0.0876, 1/4: 0.0288, 1: 0.0072, 2: 0.0036}
} #800
39 # Reference data sheet Table 1 for configuration settings
41 interrupt_high
= bytearray([0x00, 0x00]) # Clear values
42 # Reference data sheet Table 2 for High Threshold
44 interrupt_low
= bytearray([0x00, 0x00]) # Clear values
45 # Reference data sheet Table 3 for Low Threshold
47 power_save_mode
= bytearray([0x00, 0x00]) # clear values
48 # Reference data sheet Table 4 for Power Saving Modes
60 self
.address
= address
62 raise ValueError('An I2C object is required.')
65 confValuesForIt
= confValues
.get(it
)
66 gainValuesForIt
= gainValues
.get(it
)
67 if confValuesForIt
is not None and gainValuesForIt
is not None:
68 confValueForGain
= confValuesForIt
.get(gain
)
69 gainValueForGain
= gainValuesForIt
.get(gain
)
70 if confValueForGain
is not None and gainValueForGain
is not None:
71 self
.confValues
= confValueForGain
72 self
.gain
= gainValueForGain
74 raise ValueError('Wrong gain value. Use 1/8, 1/4, 1, 2')
76 raise ValueError('Wrong integration time value. Use 25, 50, 100, 200, 400, 800')
82 # load calibration data
83 #self.i2c.writeto_mem(self.address, als_conf_0, bytearray([0x00,0x13]) )
84 self
.i2c
.writeto_mem(self
.address
, als_conf_0
, self
.confValues
)
85 self
.i2c
.writeto_mem(self
.address
, als_WH
, interrupt_high
)
86 self
.i2c
.writeto_mem(self
.address
, als_WL
, interrupt_low
)
87 self
.i2c
.writeto_mem(self
.address
, pow_sav
, power_save_mode
)
90 """ Functions is verified is module has detecedself.
91 this function not implemented for this time
96 """ Reads the data from the sensor and returns the data.
99 the number of lux detect by this captor.
101 #The frequency to read the sensor should be set greater than
102 # the integration time (and the power saving delay if set).
103 # Reading at a faster frequency will not cause an error, but
104 # will result in reading the previous data
106 self
.lux
= bytearray(2)
108 time
.sleep(.04) # 40ms
110 self
.i2c
.readfrom_mem_into(self
.address
, als
, self
.lux
)
111 self
.lux
= self
.lux
[0]+self
.lux
[1]*256
112 self
.lux
=self
.lux
*self
.gain
113 return(int(round(self
.lux
,0)))