30
30
from micropython import const
31
31
from adafruit_bus_device .i2c_device import I2CDevice
32
32
33
+ try :
34
+ from typing_extensions import Literal
35
+ from busio import I2C
36
+ except ImportError :
37
+ pass
38
+
33
39
__version__ = "0.0.0+auto.0"
34
40
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TMP006.git"
35
41
@@ -65,7 +71,14 @@ class TMP006:
65
71
# thread safe!
66
72
_BUFFER = bytearray (4 )
67
73
68
- def __init__ (self , i2c , address = _TMP006_I2CADDR , samplerate = CFG_16SAMPLE ):
74
+ def __init__ (
75
+ self ,
76
+ i2c : I2C ,
77
+ address : int = _TMP006_I2CADDR ,
78
+ samplerate : Literal [
79
+ CFG_1SAMPLE , CFG_2SAMPLE , CFG_4SAMPLE , CFG_8SAMPLE , CFG_16SAMPLE
80
+ ] = CFG_16SAMPLE ,
81
+ ) -> None :
69
82
self ._device = I2CDevice (i2c , address )
70
83
self ._write_u16 (_TMP006_CONFIG , _TMP006_CFG_RESET )
71
84
time .sleep (0.5 )
@@ -90,12 +103,12 @@ def __init__(self, i2c, address=_TMP006_I2CADDR, samplerate=CFG_16SAMPLE):
90
103
raise RuntimeError ("Init failed - Did not find TMP006" )
91
104
92
105
@property
93
- def active (self ):
106
+ def active (self ) -> bool :
94
107
"""True if sensor is active."""
95
108
return self ._read_u16 (_TMP006_CONFIG ) & _TMP006_CFG_MODEON != 0
96
109
97
110
@active .setter
98
- def active (self , val ) :
111
+ def active (self , val : int ) -> None :
99
112
control = self ._read_u16 (_TMP006_CONFIG )
100
113
if val :
101
114
control |= _TMP006_CFG_MODEON
@@ -104,7 +117,7 @@ def active(self, val):
104
117
self ._write_u16 (_TMP006_CONFIG , control )
105
118
106
119
@property
107
- def temperature (self ):
120
+ def temperature (self ) -> float :
108
121
# pylint: disable=invalid-name, too-many-locals
109
122
"""Read object temperature from TMP006 sensor."""
110
123
if not self .active :
@@ -131,30 +144,30 @@ def temperature(self):
131
144
132
145
return TOBJ - 273.15 # back to celsius
133
146
134
- def _data_ready (self ):
147
+ def _data_ready (self ) -> bool :
135
148
return (self .read_register (_TMP006_CONFIG ) & _TMP006_CFG_DRDY ) != 0
136
149
137
- def _read_sensor_voltage (self ):
150
+ def _read_sensor_voltage (self ) -> float :
138
151
vobj = self .read_register (_TMP006_VOBJ )
139
152
vobj = struct .unpack (">h" , vobj .to_bytes (2 , "big" ))[0 ]
140
153
return vobj * 156.25e-9 # volts
141
154
142
- def _read_die_temperature (self ):
155
+ def _read_die_temperature (self ) -> float :
143
156
tamb = self .read_register (_TMP006_TAMB )
144
157
tamb = struct .unpack (">h" , tamb .to_bytes (2 , "big" ))[0 ]
145
158
return (tamb >> 2 ) / 32.0 # celsius
146
159
147
- def read_register (self , register ):
160
+ def read_register (self , register ) -> int :
148
161
"""Read sensor Register."""
149
162
return self ._read_u16 (register )
150
163
151
- def _read_u16 (self , address ) :
164
+ def _read_u16 (self , address : int ) -> int :
152
165
with self ._device as i2c :
153
166
self ._BUFFER [0 ] = address & 0xFF
154
167
i2c .write_then_readinto (self ._BUFFER , self ._BUFFER , out_end = 1 , in_end = 2 )
155
168
return self ._BUFFER [0 ] << 8 | self ._BUFFER [1 ]
156
169
157
- def _write_u16 (self , address , val ) :
170
+ def _write_u16 (self , address : int , val : int ) -> None :
158
171
with self ._device as i2c :
159
172
self ._BUFFER [0 ] = address & 0xFF
160
173
self ._BUFFER [1 ] = (val >> 8 ) & 0xFF
0 commit comments