-
Notifications
You must be signed in to change notification settings - Fork 0
/
veml7700.py
185 lines (152 loc) · 7.44 KB
/
veml7700.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# https://github.com/palouf34/veml7700/blob/master/veml7700.py
# Copyright (c) 2019 Joseph Hopfmüller
# This module is a fork of Christophe Rousseaus module.
# see: https://github.com/palouf34/veml7700.git
#
# Original coypright notices are reproduced below.
# Changes:
# - fixed critical error in value calculation
# - restructured the constants into dictionaries
# -> with this you have to pass the integration time and gain when initializing the module
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#
#
# Original copyright notices:
#
# Authors: Christophe ROUSSEAU, 2019
#
# This module borrows from the DFROBOT VEML7700 Python library. Original
# Copyright notices are reproduced below.
#
# Those libraries were written for the NODEMCU(LOLIN). This modification is
# intended for the MicroPython and esp8266 boards.
#
#
# Based on the BH1750 driver with VMEL7700 changes provided by
# Auteur : iTechnoFrance
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from machine import I2C, Pin, SoftI2C
import time
from micropython import const
#start const
# default address
addr = const(0x10)
# Write registers
als_conf_0 = const(0x00)
als_WH = const(0x01)
als_WL = const(0x02)
pow_sav = const(0x03)
# Read registers
als = const(0x04)
white = const(0x05)
interrupt = const(0x06)
#gain 0.125 0.25 1 2 integration time
confValues = { 25: {1/8: bytearray([0x00, 0x13]), 1/4: bytearray([0x00,0x1B]), 1: bytearray([0x00, 0x01]), 2: bytearray([0x00, 0x0B])}, #25
50: {1/8: bytearray([0x00, 0x12]), 1/4: bytearray([0x00,0x1A]), 1: bytearray([0x00, 0x02]), 2: bytearray([0x00, 0x0A])}, #50
100:{1/8: bytearray([0x00, 0x10]), 1/4: bytearray([0x00,0x18]), 1: bytearray([0x00, 0x00]), 2: bytearray([0x00, 0x08])}, #100
200:{1/8: bytearray([0x40, 0x10]), 1/4: bytearray([0x40,0x18]), 1: bytearray([0x40, 0x00]), 2: bytearray([0x40, 0x08])}, #200
400:{1/8: bytearray([0x80, 0x10]), 1/4: bytearray([0x80,0x18]), 1: bytearray([0x80, 0x00]), 2: bytearray([0x80, 0x08])}, #400
800:{1/8: bytearray([0xC0, 0x10]), 1/4: bytearray([0xC0,0x18]), 1: bytearray([0xC0, 0x00]), 2: bytearray([0xC0, 0x08])}} #800
#gain 0.125, 0.25, 1, 2 integration time
gainValues = { 25: {1/8: 1.8432, 1/4: 0.9216, 1: 0.2304, 2: 0.1152}, #25
50: {1/8: 0.9216, 1/4: 0.4608, 1: 0.1152, 2: 0.0576}, #50
100:{1/8: 0.4608, 1/4: 0.2304, 1: 0.0288, 2: 0.0144}, #100
200:{1/8: 0.2304, 1/4: 0.1152, 1: 0.0288, 2: 0.0144}, #200
400:{1/8: 0.1152, 1/4: 0.0576, 1: 0.0144, 2: 0.0072}, #400
800:{1/8: 0.0876, 1/4: 0.0288, 1: 0.0072, 2: 0.0036}} #800
# fin des constante
# Reference data sheet Table 1 for configuration settings
interrupt_high = bytearray([0x00, 0x00]) # Clear values
# Reference data sheet Table 2 for High Threshold
interrupt_low = bytearray([0x00, 0x00]) # Clear values
# Reference data sheet Table 3 for Low Threshold
power_save_mode= bytearray([0x00, 0x00]) # clear values
# Reference data sheet Table 4 for Power Saving Modes
class VEML7700:
def __init__(self,
address: int =addr,
i2c: I2C | SoftI2C | None = None,
it=25,
gain=1/8,
**kwargs):
self.address = address
if i2c is None:
raise ValueError('An I2C object is required.')
self.i2c = i2c
confValuesForIt = confValues.get(it)
gainValuesForIt = gainValues.get(it)
if confValuesForIt is not None and gainValuesForIt is not None:
confValueForGain = confValuesForIt.get(gain)
gainValueForGain = gainValuesForIt.get(gain)
if confValueForGain is not None and gainValueForGain is not None:
self.confValues = confValueForGain
self.gain = gainValueForGain
else:
raise ValueError('Wrong gain value. Use 1/8, 1/4, 1, 2')
else:
raise ValueError('Wrong integration time value. Use 25, 50, 100, 200, 400, 800')
self.init()
def init(self):
# load calibration data
#self.i2c.writeto_mem(self.address, als_conf_0, bytearray([0x00,0x13]) )
self.i2c.writeto_mem(self.address, als_conf_0, self.confValues )
self.i2c.writeto_mem(self.address, als_WH, interrupt_high )
self.i2c.writeto_mem(self.address, als_WL, interrupt_low)
self.i2c.writeto_mem(self.address, pow_sav, power_save_mode)
def detect(self):
""" Functions is verified is module has detecedself.
this function not implemented for this time
"""
raise RuntimeError("Not yet implemented") # added HT 20240326
def read_lux(self):
""" Reads the data from the sensor and returns the data.
Returns:
the number of lux detect by this captor.
"""
# The frequency to read the sensor should be set greater than
# the integration time (and the power saving delay if set).
# Reading at a faster frequency will not cause an error, but
# will result in reading the previous data
self.lux = bytearray(2)
time.sleep(.04) # 40ms
self.i2c.readfrom_mem_into(self.address, als, self.lux)
self.lux= self.lux[0]+self.lux[1]*256
self.lux=self.lux*self.gain
return(int(round(self.lux,0)))