28
28
from micropython import const
29
29
from adafruit_bus_device .i2c_device import I2CDevice
30
30
31
+ try :
32
+ from typing_extensions import Literal
33
+ from busio import I2C
34
+ except ImportError :
35
+ pass
31
36
32
37
__version__ = "0.0.0+auto.0"
33
38
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TMP007.git"
@@ -65,15 +70,25 @@ class TMP007:
65
70
# thread safe!
66
71
_BUFFER = bytearray (4 )
67
72
68
- def __init__ (self , i2c , address = _TMP007_I2CADDR , samplerate = CFG_16SAMPLE ):
73
+ def __init__ (
74
+ self ,
75
+ i2c : I2C ,
76
+ address : int = _TMP007_I2CADDR ,
77
+ samplerate : Literal [
78
+ CFG_1SAMPLE ,
79
+ CFG_2SAMPLE ,
80
+ CFG_4SAMPLE ,
81
+ CFG_8SAMPLE ,
82
+ CFG_16SAMPLE ,
83
+ ] = CFG_16SAMPLE ,
84
+ ) -> None :
69
85
"""Initialize TMP007 device on the specified I2C address and bus number.
70
86
Address defaults to 0x40 and bus number defaults to the appropriate bus
71
87
for the hardware.
72
88
Start taking temperature measurements. Samplerate can be one of
73
89
TMP007_CFG_1SAMPLE, TMP007_CFG_2SAMPLE, TMP007_CFG_4SAMPLE,
74
90
TMP007_CFG_8SAMPLE, or TMP007_CFG_16SAMPLE. The default is 16 samples
75
- for the highest resolution. Returns True if the device is intialized,
76
- False otherwise.
91
+ for the highest resolution.
77
92
"""
78
93
self ._device = I2CDevice (i2c , address )
79
94
self ._write_u16 (_TMP007_CONFIG , _TMP007_CFG_RESET )
@@ -98,22 +113,22 @@ def __init__(self, i2c, address=_TMP007_I2CADDR, samplerate=CFG_16SAMPLE):
98
113
if dev_id != 0x78 :
99
114
raise RuntimeError ("Init failed - Did not find TMP007" )
100
115
101
- def sleep (self ):
116
+ def sleep (self ) -> None :
102
117
"""Put TMP007 into low power sleep mode. No measurement data will be
103
118
updated while in sleep mode.
104
119
"""
105
120
control = self ._read_u16 (_TMP007_CONFIG )
106
121
control &= ~ (_TMP007_CFG_MODEON )
107
122
self ._write_u16 (_TMP007_CONFIG , control )
108
123
109
- def wake (self ):
124
+ def wake (self ) -> None :
110
125
"""Wake up TMP007 from low power sleep mode."""
111
126
control = self ._read_u16 (_TMP007_CONFIG )
112
127
control |= _TMP007_CFG_MODEON
113
128
self ._write_u16 (_TMP007_CONFIG , control )
114
129
115
130
@property
116
- def raw_voltage (self ):
131
+ def raw_voltage (self ) -> int :
117
132
"""Read raw voltage from TMP007 sensor. Meant to be used in the
118
133
calculation of temperature values.
119
134
"""
@@ -123,59 +138,59 @@ def raw_voltage(self):
123
138
return raw
124
139
125
140
@property
126
- def raw_sensor_temperature (self ):
141
+ def raw_sensor_temperature (self ) -> int :
127
142
"""Read raw die temperature from TMP007 sensor. Meant to be used in the
128
143
calculation of temperature values.
129
144
"""
130
145
raw = self ._read_u16 (_TMP007_TAMB )
131
146
return raw >> 2
132
147
133
148
@property
134
- def die_temperature (self ):
149
+ def die_temperature (self ) -> float :
135
150
"""Read sensor die temperature and return its value in degrees celsius."""
136
151
t_die = self .raw_sensor_temperature
137
152
return t_die * 0.03125
138
153
139
154
@property
140
- def temperature (self ):
155
+ def temperature (self ) -> float :
141
156
"""Read object temperature from TMP007 sensor."""
142
157
raw = self ._read_u16 (_TMP007_TOBJ )
143
158
if raw & 1 :
144
159
return - 9999.0
145
160
raw = raw >> 2
146
161
return raw * 0.03125
147
162
148
- def read_register (self , register ):
163
+ def read_register (self , register ) -> int :
149
164
"""Read sensor Register."""
150
165
return self ._read_u16 (register )
151
166
152
- def _read_u8 (self , address ) :
167
+ def _read_u8 (self , address : int ) -> int :
153
168
with self ._device as i2c :
154
169
self ._BUFFER [0 ] = address & 0xFF
155
170
i2c .write_then_readinto (self ._BUFFER , self ._BUFFER , out_end = 1 , in_end = 1 )
156
171
return self ._BUFFER [0 ]
157
172
158
- def _read_u16 (self , address ) :
173
+ def _read_u16 (self , address : int ) -> int :
159
174
with self ._device as i2c :
160
175
self ._BUFFER [0 ] = address & 0xFF
161
176
i2c .write_then_readinto (self ._BUFFER , self ._BUFFER , out_end = 1 , in_end = 2 )
162
177
return self ._BUFFER [0 ] << 8 | self ._BUFFER [1 ]
163
178
164
- def _write_u8 (self , address , val ) :
179
+ def _write_u8 (self , address : int , val : int ) -> None :
165
180
with self ._device as i2c :
166
181
self ._BUFFER [0 ] = address & 0xFF
167
182
self ._BUFFER [1 ] = val & 0xFF
168
183
i2c .write (self ._BUFFER , end = 2 )
169
184
170
- def _write_u16 (self , address , val ) :
185
+ def _write_u16 (self , address : int , val : int ) -> None :
171
186
with self ._device as i2c :
172
187
self ._BUFFER [0 ] = address & 0xFF
173
188
self ._BUFFER [1 ] = (val >> 8 ) & 0xFF
174
189
self ._BUFFER [2 ] = val & 0xFF
175
190
i2c .write (self ._BUFFER , end = 3 )
176
191
177
192
@staticmethod
178
- def _read_bytes (device , address , count , buf ) :
193
+ def _read_bytes (device , address : int , count : int , buf : bytearray ) -> None :
179
194
with device as i2c :
180
195
buf [0 ] = address & 0xFF
181
196
i2c .write_then_readinto (buf , buf , out_end = 1 , in_end = count )
0 commit comments