]> vault307.fbx.one Git - SensorLib.git/blob - veml7700.py
sensors
[SensorLib.git] / veml7700.py
1 from machine import I2C, Pin
2 import time
3 from micropython import const
4
5 #start const
6 # default address
7 addr = const(0x10)
8
9 # Write registers
10 als_conf_0 = const(0x00)
11 als_WH = const(0x01)
12 als_WL = const(0x02)
13 pow_sav = const(0x03)
14
15 # Read registers
16 als = const(0x04)
17 white = const(0x05)
18 interrupt = const(0x06)
19
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
27
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
35
36
37 # fin des constante
38
39 # Reference data sheet Table 1 for configuration settings
40
41 interrupt_high = bytearray([0x00, 0x00]) # Clear values
42 # Reference data sheet Table 2 for High Threshold
43
44 interrupt_low = bytearray([0x00, 0x00]) # Clear values
45 # Reference data sheet Table 3 for Low Threshold
46
47 power_save_mode= bytearray([0x00, 0x00]) # clear values
48 # Reference data sheet Table 4 for Power Saving Modes
49
50
51 class VEML7700:
52
53 def __init__(self,
54 address=addr,
55 i2c=None,
56 it=25,
57 gain=1/8,
58 **kwargs):
59
60 self.address = address
61 if i2c is None:
62 raise ValueError('An I2C object is required.')
63 self.i2c = i2c
64
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
73 else:
74 raise ValueError('Wrong gain value. Use 1/8, 1/4, 1, 2')
75 else:
76 raise ValueError('Wrong integration time value. Use 25, 50, 100, 200, 400, 800')
77
78 self.init()
79
80 def init(self):
81
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)
88
89 def detect(self):
90 """ Functions is verified is module has detecedself.
91 this function not implemented for this time
92 """
93 None
94
95 def read_lux(self):
96 """ Reads the data from the sensor and returns the data.
97
98 Returns:
99 the number of lux detect by this captor.
100 """
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
105
106 self.lux = bytearray(2)
107
108 time.sleep(.04) # 40ms
109
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)))