3030from micropython import const
3131from adafruit_bus_device .i2c_device import I2CDevice
3232
33+ try :
34+ from typing_extensions import Literal
35+ from busio import I2C
36+ except ImportError :
37+ pass
38+
3339__version__ = "0.0.0+auto.0"
3440__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_TMP006.git"
3541
@@ -65,7 +71,14 @@ class TMP006:
6571 # thread safe!
6672 _BUFFER = bytearray (4 )
6773
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 :
6982 self ._device = I2CDevice (i2c , address )
7083 self ._write_u16 (_TMP006_CONFIG , _TMP006_CFG_RESET )
7184 time .sleep (0.5 )
@@ -90,12 +103,12 @@ def __init__(self, i2c, address=_TMP006_I2CADDR, samplerate=CFG_16SAMPLE):
90103 raise RuntimeError ("Init failed - Did not find TMP006" )
91104
92105 @property
93- def active (self ):
106+ def active (self ) -> bool :
94107 """True if sensor is active."""
95108 return self ._read_u16 (_TMP006_CONFIG ) & _TMP006_CFG_MODEON != 0
96109
97110 @active .setter
98- def active (self , val ) :
111+ def active (self , val : int ) -> None :
99112 control = self ._read_u16 (_TMP006_CONFIG )
100113 if val :
101114 control |= _TMP006_CFG_MODEON
@@ -104,7 +117,7 @@ def active(self, val):
104117 self ._write_u16 (_TMP006_CONFIG , control )
105118
106119 @property
107- def temperature (self ):
120+ def temperature (self ) -> float :
108121 # pylint: disable=invalid-name, too-many-locals
109122 """Read object temperature from TMP006 sensor."""
110123 if not self .active :
@@ -131,30 +144,30 @@ def temperature(self):
131144
132145 return TOBJ - 273.15 # back to celsius
133146
134- def _data_ready (self ):
147+ def _data_ready (self ) -> bool :
135148 return (self .read_register (_TMP006_CONFIG ) & _TMP006_CFG_DRDY ) != 0
136149
137- def _read_sensor_voltage (self ):
150+ def _read_sensor_voltage (self ) -> float :
138151 vobj = self .read_register (_TMP006_VOBJ )
139152 vobj = struct .unpack (">h" , vobj .to_bytes (2 , "big" ))[0 ]
140153 return vobj * 156.25e-9 # volts
141154
142- def _read_die_temperature (self ):
155+ def _read_die_temperature (self ) -> float :
143156 tamb = self .read_register (_TMP006_TAMB )
144157 tamb = struct .unpack (">h" , tamb .to_bytes (2 , "big" ))[0 ]
145158 return (tamb >> 2 ) / 32.0 # celsius
146159
147- def read_register (self , register ):
160+ def read_register (self , register ) -> int :
148161 """Read sensor Register."""
149162 return self ._read_u16 (register )
150163
151- def _read_u16 (self , address ) :
164+ def _read_u16 (self , address : int ) -> int :
152165 with self ._device as i2c :
153166 self ._BUFFER [0 ] = address & 0xFF
154167 i2c .write_then_readinto (self ._BUFFER , self ._BUFFER , out_end = 1 , in_end = 2 )
155168 return self ._BUFFER [0 ] << 8 | self ._BUFFER [1 ]
156169
157- def _write_u16 (self , address , val ) :
170+ def _write_u16 (self , address : int , val : int ) -> None :
158171 with self ._device as i2c :
159172 self ._BUFFER [0 ] = address & 0xFF
160173 self ._BUFFER [1 ] = (val >> 8 ) & 0xFF
0 commit comments