18
18
from micropython import const
19
19
from adafruit_bus_device .i2c_device import I2CDevice
20
20
21
+ try :
22
+ from typing import Optional
23
+ from busio import I2C
24
+ from microcontroller import Pin
25
+ except ImportError :
26
+ pass
27
+
21
28
_ADS1X15_DEFAULT_ADDRESS = const (0x48 )
22
29
_ADS1X15_POINTER_CONVERSION = const (0x00 )
23
30
_ADS1X15_POINTER_CONFIG = const (0x01 )
@@ -49,11 +56,11 @@ class ADS1x15:
49
56
50
57
def __init__ (
51
58
self ,
52
- i2c ,
53
- gain = 1 ,
54
- data_rate = None ,
55
- mode = Mode .SINGLE ,
56
- address = _ADS1X15_DEFAULT_ADDRESS ,
59
+ i2c : I2C ,
60
+ gain : float = 1 ,
61
+ data_rate : Optional [ int ] = None ,
62
+ mode : Mode = Mode .SINGLE ,
63
+ address : int = _ADS1X15_DEFAULT_ADDRESS ,
57
64
):
58
65
# pylint: disable=too-many-arguments
59
66
self ._last_pin_read = None
@@ -70,7 +77,7 @@ def data_rate(self):
70
77
return self ._data_rate
71
78
72
79
@data_rate .setter
73
- def data_rate (self , rate ):
80
+ def data_rate (self , rate : int ):
74
81
possible_rates = self .rates
75
82
if rate not in possible_rates :
76
83
raise ValueError ("Data rate must be one of: {}" .format (possible_rates ))
@@ -92,7 +99,7 @@ def gain(self):
92
99
return self ._gain
93
100
94
101
@gain .setter
95
- def gain (self , gain ):
102
+ def gain (self , gain : float ):
96
103
possible_gains = self .gains
97
104
if gain not in possible_gains :
98
105
raise ValueError ("Gain must be one of: {}" .format (possible_gains ))
@@ -111,12 +118,12 @@ def mode(self):
111
118
return self ._mode
112
119
113
120
@mode .setter
114
- def mode (self , mode ):
121
+ def mode (self , mode : Mode ):
115
122
if mode not in (Mode .CONTINUOUS , Mode .SINGLE ):
116
123
raise ValueError ("Unsupported mode." )
117
124
self ._mode = mode
118
125
119
- def read (self , pin , is_differential = False ):
126
+ def read (self , pin : Pin , is_differential : bool = False ) -> int :
120
127
"""I2C Interface for ADS1x15-based ADCs reads.
121
128
122
129
params:
@@ -126,19 +133,19 @@ def read(self, pin, is_differential=False):
126
133
pin = pin if is_differential else pin + 0x04
127
134
return self ._read (pin )
128
135
129
- def _data_rate_default (self ):
136
+ def _data_rate_default (self ) -> int :
130
137
"""Retrieve the default data rate for this ADC (in samples per second).
131
138
Should be implemented by subclasses.
132
139
"""
133
140
raise NotImplementedError ("Subclasses must implement _data_rate_default!" )
134
141
135
- def _conversion_value (self , raw_adc ) :
142
+ def _conversion_value (self , raw_adc : int ) -> int :
136
143
"""Subclasses should override this function that takes the 16 raw ADC
137
144
values of a conversion result and returns a signed integer value.
138
145
"""
139
146
raise NotImplementedError ("Subclass must implement _conversion_value function!" )
140
147
141
- def _read (self , pin ) :
148
+ def _read (self , pin : Pin ) -> int :
142
149
"""Perform an ADC read. Returns the signed integer result of the read."""
143
150
# Immediately return conversion register result if in CONTINUOUS mode
144
151
# and pin has not changed
@@ -174,30 +181,30 @@ def _read(self, pin):
174
181
175
182
return self ._conversion_value (self .get_last_result (False ))
176
183
177
- def _conversion_complete (self ):
184
+ def _conversion_complete (self ) -> int :
178
185
"""Return status of ADC conversion."""
179
186
# OS is bit 15
180
187
# OS = 0: Device is currently performing a conversion
181
188
# OS = 1: Device is not currently performing a conversion
182
189
return self ._read_register (_ADS1X15_POINTER_CONFIG ) & 0x8000
183
190
184
- def get_last_result (self , fast = False ):
191
+ def get_last_result (self , fast : bool = False ) -> int :
185
192
"""Read the last conversion result when in continuous conversion mode.
186
193
Will return a signed integer value. If fast is True, the register
187
194
pointer is not updated as part of the read. This reduces I2C traffic
188
195
and increases possible read rate.
189
196
"""
190
197
return self ._read_register (_ADS1X15_POINTER_CONVERSION , fast )
191
198
192
- def _write_register (self , reg , value ):
199
+ def _write_register (self , reg : int , value : int ):
193
200
"""Write 16 bit value to register."""
194
201
self .buf [0 ] = reg
195
202
self .buf [1 ] = (value >> 8 ) & 0xFF
196
203
self .buf [2 ] = value & 0xFF
197
204
with self .i2c_device as i2c :
198
205
i2c .write (self .buf )
199
206
200
- def _read_register (self , reg , fast = False ):
207
+ def _read_register (self , reg : int , fast : bool = False ) -> int :
201
208
"""Read 16 bit register value. If fast is True, the pointer register
202
209
is not updated.
203
210
"""
0 commit comments