40
40
from adafruit_register .i2c_bit import RWBit , ROBit
41
41
from micropython import const
42
42
43
+ try :
44
+ # Used only for typing
45
+ import busio # pylint: disable=unused-import
46
+ except ImportError :
47
+ pass
48
+
43
49
__version__ = "0.0.0-auto.0"
44
50
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ADT7410.git"
45
51
46
52
47
- _ADT7410_TEMPMSB = const (0x0 )
48
- _ADT7410_TEMPLSB = const (0x1 )
49
- _ADT7410_STATUS = const (0x2 )
50
- _ADT7410_CONFIG = const (0x3 )
51
- _ADT7410_THIGHMSB = const (0x4 )
52
- _ADT7410_THIGHLSB = const (0x5 )
53
- _ADT7410_TLOWMSB = const (0x6 )
54
- _ADT7410_TLOWLSB = const (0x7 )
55
- _ADT7410_TCRITMSB = const (0x8 )
56
- _ADT7410_TCRITLSB = const (0x9 )
57
- _ADT7410_THYST = const (0x0A )
58
- _ADT7410_ID = const (0xB )
59
- _ADT7410_SWRST = const (0x2F )
53
+ _ADT7410_TEMPMSB : int = const (0x0 )
54
+ _ADT7410_TEMPLSB : int = const (0x1 )
55
+ _ADT7410_STATUS : int = const (0x2 )
56
+ _ADT7410_CONFIG : int = const (0x3 )
57
+ _ADT7410_THIGHMSB : int = const (0x4 )
58
+ _ADT7410_THIGHLSB : int = const (0x5 )
59
+ _ADT7410_TLOWMSB : int = const (0x6 )
60
+ _ADT7410_TLOWLSB : int = const (0x7 )
61
+ _ADT7410_TCRITMSB : int = const (0x8 )
62
+ _ADT7410_TCRITLSB : int = const (0x9 )
63
+ _ADT7410_THYST : int = const (0x0A )
64
+ _ADT7410_ID : int = const (0xB )
65
+ _ADT7410_SWRST : int = const (0x2F )
60
66
61
67
62
68
class ADT7410 :
@@ -91,17 +97,18 @@ class ADT7410:
91
97
"""
92
98
93
99
# many modes can be set with register objects for simplicity
94
- ready = ROBit (_ADT7410_STATUS , 7 )
95
- ctpin_polarity = RWBit (_ADT7410_CONFIG , 2 )
96
- intpin_polarity = RWBit (_ADT7410_CONFIG , 3 )
97
- comparator_mode = RWBit (_ADT7410_CONFIG , 4 )
98
- high_resolution = RWBit (_ADT7410_CONFIG , 7 )
100
+ ready : ROBit = ROBit (_ADT7410_STATUS , 7 )
101
+ ctpin_polarity : RWBit = RWBit (_ADT7410_CONFIG , 2 )
102
+ intpin_polarity : RWBit = RWBit (_ADT7410_CONFIG , 3 )
103
+ comparator_mode : RWBit = RWBit (_ADT7410_CONFIG , 4 )
104
+ high_resolution : RWBit = RWBit (_ADT7410_CONFIG , 7 )
105
+
99
106
# Status Information configuration
100
- temp_over_critiq = ROBit (_ADT7410_STATUS , 6 )
101
- temp_over_high = ROBit (_ADT7410_STATUS , 5 )
102
- temp_under_low = ROBit (_ADT7410_STATUS , 4 )
107
+ temp_over_critiq : ROBit = ROBit (_ADT7410_STATUS , 6 )
108
+ temp_over_high : ROBit = ROBit (_ADT7410_STATUS , 5 )
109
+ temp_under_low : ROBit = ROBit (_ADT7410_STATUS , 4 )
103
110
104
- def __init__ (self , i2c_bus , address = 0x48 ):
111
+ def __init__ (self , i2c_bus : busio . I2C , address : int = 0x48 ):
105
112
self .i2c_device = I2CDevice (i2c_bus , address )
106
113
self ._buf = bytearray (3 )
107
114
# Verify the manufacturer and device ids to ensure we are talking to
@@ -114,39 +121,39 @@ def __init__(self, i2c_bus, address=0x48):
114
121
self .reset ()
115
122
116
123
@property
117
- def temperature (self ):
124
+ def temperature (self ) -> float :
118
125
"""The temperature in Celsius"""
119
126
temp = self ._read_register (_ADT7410_TEMPMSB , 2 )
120
127
return struct .unpack (">h" , temp )[0 ] / 128
121
128
122
129
@property
123
- def status (self ):
130
+ def status (self ) -> int :
124
131
"""The ADT7410 status registers current value"""
125
132
return self ._read_register (_ADT7410_STATUS )[0 ]
126
133
127
134
@property
128
- def configuration (self ):
135
+ def configuration (self ) -> int :
129
136
"""The ADT7410 configuration register"""
130
137
return self ._read_register (_ADT7410_CONFIG )[0 ]
131
138
132
139
@configuration .setter
133
- def configuration (self , val ) :
140
+ def configuration (self , val : int ) -> None :
134
141
self ._write_register (_ADT7410_CONFIG , val )
135
142
136
- def reset (self ):
143
+ def reset (self ) -> None :
137
144
"""Perform a software reset"""
138
145
self ._write_register (_ADT7410_SWRST )
139
146
time .sleep (0.5 )
140
147
141
- def _read_register (self , addr , num = 1 ) :
148
+ def _read_register (self , addr : int , num : int = 1 ) -> int :
142
149
self ._buf [0 ] = addr
143
150
with self .i2c_device as i2c :
144
151
i2c .write_then_readinto (
145
152
self ._buf , self ._buf , out_end = 1 , in_start = 1 , in_end = num + 1
146
153
)
147
154
return self ._buf [1 : num + 1 ]
148
155
149
- def _write_register (self , addr , data = None ):
156
+ def _write_register (self , addr : int , data : int = None ) -> None :
150
157
self ._buf [0 ] = addr
151
158
end = 1
152
159
if data :
@@ -156,39 +163,39 @@ def _write_register(self, addr, data=None):
156
163
i2c .write (self ._buf , end = end )
157
164
158
165
@property
159
- def high_temperature (self ):
166
+ def high_temperature (self ) -> int :
160
167
"""The over temperature limit value in Celsius"""
161
168
temp = self ._read_register (_ADT7410_THIGHMSB , 2 )
162
169
return struct .unpack (">h" , temp )[0 ] / 128
163
170
164
171
@high_temperature .setter
165
- def high_temperature (self , value ) :
172
+ def high_temperature (self , value : int ) -> None :
166
173
value = struct .pack (">h" , int (value * 128 ))
167
174
self ._write_register (_ADT7410_THIGHMSB , value [0 ])
168
175
self ._write_register (_ADT7410_THIGHLSB , value [1 ])
169
176
170
177
@property
171
- def low_temperature (self ):
178
+ def low_temperature (self ) -> int :
172
179
"""The over temperature limit value in Celsius. Only works when
173
180
comparator mode is selected"""
174
181
temp = self ._read_register (_ADT7410_TLOWMSB , 2 )
175
182
return struct .unpack (">h" , temp )[0 ] / 128
176
183
177
184
@low_temperature .setter
178
- def low_temperature (self , value ) :
185
+ def low_temperature (self , value : int ) -> None :
179
186
value = struct .pack (">h" , int (value * 128 ))
180
187
self ._write_register (_ADT7410_TLOWMSB , value [0 ])
181
188
self ._write_register (_ADT7410_TLOWLSB , value [1 ])
182
189
183
190
@property
184
- def critical_temperature (self ):
191
+ def critical_temperature (self ) -> int :
185
192
"""The critical temperature limit value in Celsius. Only works when
186
193
comparator mode is selected"""
187
194
temp = self ._read_register (_ADT7410_TCRITMSB , 2 )
188
195
return struct .unpack (">h" , temp )[0 ] / 128
189
196
190
197
@critical_temperature .setter
191
- def critical_temperature (self , value ) :
198
+ def critical_temperature (self , value : int ) -> None :
192
199
"""The over temperature limit value in Celsius
193
200
There is a bug in the sensor, so the address 0x09 could no be written to 0x00
194
201
for this reason only odd numbers could be given. We could make the 0x09 with
@@ -200,15 +207,17 @@ def critical_temperature(self, value):
200
207
self ._write_register (_ADT7410_TCRITLSB , value [1 ])
201
208
202
209
@property
203
- def hysteresis (self ):
210
+ def hysteresis (self ) -> int :
204
211
"""The hysteresis temperature limit value in Celsius. Only works when
205
212
comparator mode is selected. From 0 to 15 Celsius"""
206
213
temp = self ._read_register (_ADT7410_THYST )[0 ]
207
214
return temp
208
215
209
216
@hysteresis .setter
210
- def hysteresis (self , value ) :
217
+ def hysteresis (self , value : int ) -> None :
211
218
if value > 15 or isinstance (value , float ):
212
- raise Exception ("Hysteresis value must be an integer lower than 15 Celsius" )
219
+ raise ValueError (
220
+ "Hysteresis value must be an integer lower than 15 Celsius"
221
+ )
213
222
214
223
self ._write_register (_ADT7410_THYST , value )
0 commit comments