Skip to content

Commit 05ef267

Browse files
authored
Merge pull request #8 from tcfranks/main
Add Missing Type Annotations
2 parents 998c6ef + b369e49 commit 05ef267

File tree

4 files changed

+50
-26
lines changed

4 files changed

+50
-26
lines changed

adafruit_pcf8591/analog_in.py

+16-10
Original file line numberDiff line numberDiff line change
@@ -27,53 +27,59 @@
2727
* Author(s): Bryan Siepert, adpted from ADS1x15 by Carter Nelson
2828
"""
2929

30+
try:
31+
import typing # pylint: disable=unused-import
32+
from adafruit_pcf8591.pcf8591.PCF8591 import PCF8591
33+
except ImportError:
34+
pass
35+
3036

3137
class AnalogIn:
3238
"""AnalogIn Mock Implementation for ADC Reads."""
3339

34-
def __init__(self, pcf, pin):
40+
def __init__(self, pcf: PCF8591, pin: int) -> None:
3541
"""AnalogIn
3642
37-
:param ads: The PCF8591 object.
38-
:param ~digitalio.DigitalInOut pin: Required ADC channel pin.
43+
:param pcf: The PCF8591 object.
44+
:param int pin: Required ADC channel pin.
3945
4046
"""
4147
self._pcf = pcf
4248
self._channel_number = pin
4349

4450
@property
45-
def voltage(self):
51+
def voltage(self) -> float:
4652
"""Returns the value of an ADC channel in volts as compared to the reference voltage."""
4753

4854
if not self._pcf:
4955
raise RuntimeError(
50-
"Underlying ADC does not exist, likely due to callint `deinit`"
56+
"Underlying ADC does not exist, likely due to calling `deinit`"
5157
)
5258
raw_reading = self._pcf.read(self._channel_number)
5359
return ((raw_reading << 8) / 65535) * self._pcf.reference_voltage
5460

5561
@property
56-
def value(self):
62+
def value(self) -> int:
5763
"""Returns the value of an ADC channel.
5864
The value is scaled to a 16-bit integer from the native 8-bit value."""
5965

6066
if not self._pcf:
6167
raise RuntimeError(
62-
"Underlying ADC does not exist, likely due to callint `deinit`"
68+
"Underlying ADC does not exist, likely due to calling `deinit`"
6369
)
6470

6571
return self._pcf.read(self._channel_number) << 8
6672

6773
@property
68-
def reference_voltage(self):
74+
def reference_voltage(self) -> float:
6975
"""The maximum voltage measurable (also known as the reference voltage) as a float in
7076
Volts. Assumed to be 3.3V but can be overridden using the `PCF8591` constructor"""
7177
if not self._pcf:
7278
raise RuntimeError(
73-
"Underlying ADC does not exist, likely due to callint `deinit`"
79+
"Underlying ADC does not exist, likely due to calling `deinit`"
7480
)
7581
return self._pcf.reference_voltage
7682

77-
def deinit(self):
83+
def deinit(self) -> None:
7884
"""Release the reference to the PCF8591. Create a new AnalogIn to use it again."""
7985
self._pcf = None

adafruit_pcf8591/analog_out.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,21 @@
2626
2727
* Author(s): Bryan Siepert
2828
"""
29+
try:
30+
import typing # pylint: disable=unused-import
31+
from adafruit_pcf8591.pcf8591.PCF8591 import PCF8591
32+
except ImportError:
33+
pass
2934

3035

3136
class AnalogOut:
3237
"""AnalogIn Mock Implementation for ADC Reads."""
3338

34-
def __init__(self, pcf, dac_pin=0):
39+
def __init__(self, pcf: PCF8591, dac_pin: int = 0) -> None:
3540
"""AnalogIn
3641
3742
:param pcf: The pcf object.
38-
:param ~digitalio.DigitalInOut DAC pin: Required pin must be P4
43+
:param int pin: Required pin must be adafruit_pcf8591.pcf8591.A0
3944
4045
"""
4146
self._pcf = pcf
@@ -45,24 +50,24 @@ def __init__(self, pcf, dac_pin=0):
4550
self._pcf.dac_enabled = True
4651

4752
@property
48-
def value(self):
53+
def value(self) -> int:
4954
"""Returns the currently set value of the DAC pin as an integer."""
5055
return self._value
5156

5257
@value.setter
53-
def value(self, new_value): # this may have to scale from 16-bit
58+
def value(self, new_value: int) -> None: # this may have to scale from 16-bit
5459
if new_value < 0 or new_value > 65535:
5560
raise ValueError("value must be a 16-bit integer from 0-65535")
5661

5762
if not self._pcf.dac_enabled:
5863
raise RuntimeError(
59-
"Underlying DAC is disabled, likely due to callint `deinit`"
64+
"Underlying DAC is disabled, likely due to calling `deinit`"
6065
)
6166
# underlying sensor is 8-bit, so scale accordingly
6267
self._pcf.write(new_value >> 8)
6368
self._value = new_value
6469

65-
def deinit(self):
70+
def deinit(self) -> None:
6671
"""Disable the underlying DAC and release the reference to the PCF8591.
6772
Create a new AnalogOut to use it again."""
6873
self._pcf.dac_enabled = False

adafruit_pcf8591/pcf8591.py

+22-10
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@
3333
from micropython import const
3434
from adafruit_bus_device import i2c_device
3535

36+
try:
37+
import typing # pylint: disable=unused-import
38+
from typing_extensions import Literal
39+
from busio import I2C
40+
except ImportError:
41+
pass
42+
3643
_PCF8591_DEFAULT_ADDR = const(0x48) # PCF8591 Default Address
3744
_PCF8591_ENABLE_DAC = const(0x40) # control bit for having the DAC active
3845

@@ -49,11 +56,16 @@ class PCF8591:
4956
"""Driver for the PCF8591 DAC & ADC Combo breakout.
5057
5158
:param ~busio.I2C i2c_bus: The I2C bus the PCF8591 is connected to.
52-
:param address: The I2C device address for the sensor. Default is ``0x28``.
59+
:param int address: The I2C device address for the sensor. Default is ``0x28``.
5360
5461
"""
5562

56-
def __init__(self, i2c_bus, address=_PCF8591_DEFAULT_ADDR, reference_voltage=3.3):
63+
def __init__(
64+
self,
65+
i2c_bus: I2C,
66+
address: int = _PCF8591_DEFAULT_ADDR,
67+
reference_voltage: float = 3.3,
68+
) -> None:
5769
self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
5870
self._dacval = 0
5971
self._dac_enabled = False
@@ -67,12 +79,12 @@ def __init__(self, i2c_bus, address=_PCF8591_DEFAULT_ADDR, reference_voltage=3.3
6779
# user calls to `read`
6880

6981
@property
70-
def reference_voltage(self):
82+
def reference_voltage(self) -> float:
7183
"""The voltage level that ADC signals are compared to.
7284
An ADC value of 65535 will equal `reference_voltage`"""
7385
return self._reference_voltage
7486

75-
def _half_read(self, channel):
87+
def _half_read(self, channel: Literal[0, 1, 2, 3]) -> None:
7688
if self._dac_enabled:
7789
self._buffer[0] = _PCF8591_ENABLE_DAC
7890
self._buffer[1] = self._dacval
@@ -85,10 +97,10 @@ def _half_read(self, channel):
8597
with self.i2c_device as i2c:
8698
i2c.write_then_readinto(self._buffer, self._buffer)
8799

88-
def read(self, channel):
100+
def read(self, channel: Literal[0, 1, 2, 3]) -> None:
89101
"""Read an analog value from one of the four ADC inputs
90102
91-
param: :channel The single-ended ADC channel to read from, 0 thru 3
103+
:param int channel: The single-ended ADC channel to read from, 0 thru 3
92104
"""
93105
if channel < 0 or channel > 3:
94106
raise ValueError("channel must be from 0-3")
@@ -101,20 +113,20 @@ def read(self, channel):
101113
return unpack_from(">B", self._buffer[1:])[0]
102114

103115
@property
104-
def dac_enabled(self):
116+
def dac_enabled(self) -> bool:
105117
"""Enables the DAC when True, or sets it to tri-state / high-Z when False"""
106118
return self._dac_enabled
107119

108120
@dac_enabled.setter
109-
def dac_enabled(self, enable_dac):
121+
def dac_enabled(self, enable_dac: bool) -> None:
110122

111123
self._dac_enabled = enable_dac
112124
self.write(self._dacval)
113125

114-
def write(self, value):
126+
def write(self, value: int) -> None:
115127
"""Writes a uint8_t value to the DAC output
116128
117-
param: :output The value to write: 0 is GND and 65535 is VCC
129+
:param int value: The value to write: 0 is GND and 65535 is VCC
118130
119131
"""
120132

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
Adafruit-Blinka
66
adafruit-circuitpython-register
77
adafruit-circuitpython-busdevice
8+
typing-extensions~=4.0

0 commit comments

Comments
 (0)