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,7 +70,18 @@ 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.
@@ -98,22 +114,22 @@ def __init__(self, i2c, address=_TMP007_I2CADDR, samplerate=CFG_16SAMPLE):
98
114
if dev_id != 0x78 :
99
115
raise RuntimeError ("Init failed - Did not find TMP007" )
100
116
101
- def sleep (self ):
117
+ def sleep (self ) -> None :
102
118
"""Put TMP007 into low power sleep mode. No measurement data will be
103
119
updated while in sleep mode.
104
120
"""
105
121
control = self ._read_u16 (_TMP007_CONFIG )
106
122
control &= ~ (_TMP007_CFG_MODEON )
107
123
self ._write_u16 (_TMP007_CONFIG , control )
108
124
109
- def wake (self ):
125
+ def wake (self ) -> None :
110
126
"""Wake up TMP007 from low power sleep mode."""
111
127
control = self ._read_u16 (_TMP007_CONFIG )
112
128
control |= _TMP007_CFG_MODEON
113
129
self ._write_u16 (_TMP007_CONFIG , control )
114
130
115
131
@property
116
- def raw_voltage (self ):
132
+ def raw_voltage (self ) -> int :
117
133
"""Read raw voltage from TMP007 sensor. Meant to be used in the
118
134
calculation of temperature values.
119
135
"""
@@ -123,59 +139,59 @@ def raw_voltage(self):
123
139
return raw
124
140
125
141
@property
126
- def raw_sensor_temperature (self ):
142
+ def raw_sensor_temperature (self ) -> int :
127
143
"""Read raw die temperature from TMP007 sensor. Meant to be used in the
128
144
calculation of temperature values.
129
145
"""
130
146
raw = self ._read_u16 (_TMP007_TAMB )
131
147
return raw >> 2
132
148
133
149
@property
134
- def die_temperature (self ):
150
+ def die_temperature (self ) -> float :
135
151
"""Read sensor die temperature and return its value in degrees celsius."""
136
152
t_die = self .raw_sensor_temperature
137
153
return t_die * 0.03125
138
154
139
155
@property
140
- def temperature (self ):
156
+ def temperature (self ) -> float :
141
157
"""Read object temperature from TMP007 sensor."""
142
158
raw = self ._read_u16 (_TMP007_TOBJ )
143
159
if raw & 1 :
144
160
return - 9999.0
145
161
raw = raw >> 2
146
162
return raw * 0.03125
147
163
148
- def read_register (self , register ):
164
+ def read_register (self , register ) -> int :
149
165
"""Read sensor Register."""
150
166
return self ._read_u16 (register )
151
167
152
- def _read_u8 (self , address ) :
168
+ def _read_u8 (self , address : int ) -> int :
153
169
with self ._device as i2c :
154
170
self ._BUFFER [0 ] = address & 0xFF
155
171
i2c .write_then_readinto (self ._BUFFER , self ._BUFFER , out_end = 1 , in_end = 1 )
156
172
return self ._BUFFER [0 ]
157
173
158
- def _read_u16 (self , address ) :
174
+ def _read_u16 (self , address : int ) -> int :
159
175
with self ._device as i2c :
160
176
self ._BUFFER [0 ] = address & 0xFF
161
177
i2c .write_then_readinto (self ._BUFFER , self ._BUFFER , out_end = 1 , in_end = 2 )
162
178
return self ._BUFFER [0 ] << 8 | self ._BUFFER [1 ]
163
179
164
- def _write_u8 (self , address , val ) :
180
+ def _write_u8 (self , address : int , val : int ) -> None :
165
181
with self ._device as i2c :
166
182
self ._BUFFER [0 ] = address & 0xFF
167
183
self ._BUFFER [1 ] = val & 0xFF
168
184
i2c .write (self ._BUFFER , end = 2 )
169
185
170
- def _write_u16 (self , address , val ) :
186
+ def _write_u16 (self , address : int , val : int ) -> None :
171
187
with self ._device as i2c :
172
188
self ._BUFFER [0 ] = address & 0xFF
173
189
self ._BUFFER [1 ] = (val >> 8 ) & 0xFF
174
190
self ._BUFFER [2 ] = val & 0xFF
175
191
i2c .write (self ._BUFFER , end = 3 )
176
192
177
193
@staticmethod
178
- def _read_bytes (device , address , count , buf ) :
194
+ def _read_bytes (device , address : int , count : int , buf : bytearray ) -> None :
179
195
with device as i2c :
180
196
buf [0 ] = address & 0xFF
181
197
i2c .write_then_readinto (buf , buf , out_end = 1 , in_end = count )
0 commit comments