33
33
from adafruit_bus_device .i2c_device import I2CDevice
34
34
from micropython import const
35
35
36
+ try :
37
+ import typing # pylint: disable=unused-import
38
+ from busio import I2C
39
+ except ImportError :
40
+ pass
41
+
36
42
_VEML6075_ADDR = const (0x10 )
37
43
38
44
_REG_CONF = const (0x00 )
@@ -69,17 +75,17 @@ class VEML6075:
69
75
70
76
def __init__ (
71
77
self ,
72
- i2c_bus ,
78
+ i2c_bus : I2C ,
73
79
* ,
74
- integration_time = 50 ,
75
- high_dynamic = True ,
76
- uva_a_coef = 2.22 ,
77
- uva_b_coef = 1.33 ,
78
- uvb_c_coef = 2.95 ,
79
- uvb_d_coef = 1.74 ,
80
- uva_response = 0.001461 ,
81
- uvb_response = 0.002591
82
- ):
80
+ integration_time : int = 50 ,
81
+ high_dynamic : bool = True ,
82
+ uva_a_coef : float = 2.22 ,
83
+ uva_b_coef : float = 1.33 ,
84
+ uvb_c_coef : float = 2.95 ,
85
+ uvb_d_coef : float = 1.74 ,
86
+ uva_response : float = 0.001461 ,
87
+ uvb_response : float = 0.002591 ,
88
+ ) -> None :
83
89
# Set coefficients
84
90
self ._a = uva_a_coef
85
91
self ._b = uva_b_coef
@@ -96,7 +102,7 @@ def __init__(
96
102
# read ID!
97
103
veml_id = self ._read_register (_REV_ID )
98
104
if veml_id != 0x26 :
99
- raise RuntimeError ("Incorrect VEML6075 ID 0x%02X" % veml_id )
105
+ raise RuntimeError (f "Incorrect VEML6075 ID { hex ( veml_id ). upper () } " )
100
106
101
107
# shut down
102
108
self ._write_register (_REG_CONF , 0x01 )
@@ -111,7 +117,7 @@ def __init__(
111
117
conf &= ~ 0x01 # Power on
112
118
self ._write_register (_REG_CONF , conf )
113
119
114
- def _take_reading (self ):
120
+ def _take_reading (self ) -> None :
115
121
"""Perform a full reading and calculation of all UV calibrated values"""
116
122
time .sleep (0.1 )
117
123
uva = self ._read_register (_REG_UVA )
@@ -126,25 +132,25 @@ def _take_reading(self):
126
132
# (uva, uvb, uvcomp1, uvcomp2, dark))
127
133
128
134
@property
129
- def uva (self ):
135
+ def uva (self ) -> float :
130
136
"""The calibrated UVA reading, in 'counts' over the sample period"""
131
137
self ._take_reading ()
132
138
return self ._uvacalc
133
139
134
140
@property
135
- def uvb (self ):
141
+ def uvb (self ) -> float :
136
142
"""The calibrated UVB reading, in 'counts' over the sample period"""
137
143
self ._take_reading ()
138
144
return self ._uvbcalc
139
145
140
146
@property
141
- def uv_index (self ):
147
+ def uv_index (self ) -> float :
142
148
"""The calculated UV Index"""
143
149
self ._take_reading ()
144
150
return ((self ._uvacalc * self ._uvaresp ) + (self ._uvbcalc * self ._uvbresp )) / 2
145
151
146
152
@property
147
- def integration_time (self ):
153
+ def integration_time (self ) -> int :
148
154
"""The amount of time the VEML is sampling data for, in millis.
149
155
Valid times are 50, 100, 200, 400 or 800ms"""
150
156
key = (self ._read_register (_REG_CONF ) >> 4 ) & 0x7
@@ -154,7 +160,7 @@ def integration_time(self):
154
160
raise RuntimeError ("Invalid integration time" )
155
161
156
162
@integration_time .setter
157
- def integration_time (self , val ) :
163
+ def integration_time (self , val : int ) -> None :
158
164
if ( # pylint: disable=consider-iterating-dictionary
159
165
not val in _VEML6075_UV_IT .keys ()
160
166
):
@@ -164,14 +170,14 @@ def integration_time(self, val):
164
170
conf |= _VEML6075_UV_IT [val ] << 4
165
171
self ._write_register (_REG_CONF , conf )
166
172
167
- def _read_register (self , register ) :
173
+ def _read_register (self , register : int ) -> int :
168
174
"""Read a 16-bit value from the `register` location"""
169
175
self ._buffer [0 ] = register
170
176
with self ._i2c as i2c :
171
177
i2c .write_then_readinto (self ._buffer , self ._buffer , out_end = 1 , in_end = 2 )
172
178
return (self ._buffer [1 ] << 8 ) | self ._buffer [0 ]
173
179
174
- def _write_register (self , register , value ) :
180
+ def _write_register (self , register : int , value : int ) -> None :
175
181
"""Write a 16-bit value to the `register` location"""
176
182
self ._buffer [0 ] = register
177
183
self ._buffer [1 ] = value
0 commit comments