Skip to content

Commit a27732d

Browse files
authored
Merge pull request #22 from tcfranks/main
Add Missing Type Annotations
2 parents 1dc019a + 22dd428 commit a27732d

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

adafruit_mcp4725.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
from micropython import const
2929
from adafruit_bus_device import i2c_device
3030

31+
try:
32+
import typing # pylint: disable=unused-import
33+
from busio import I2C
34+
except ImportError:
35+
pass
36+
3137
__version__ = "0.0.0+auto.0"
3238
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP4725.git"
3339

@@ -52,14 +58,14 @@ class MCP4725:
5258
# Note this is not thread-safe or re-entrant by design!
5359
_BUFFER = bytearray(3)
5460

55-
def __init__(self, i2c, *, address=_MCP4725_DEFAULT_ADDRESS):
61+
def __init__(self, i2c: I2C, *, address: int = _MCP4725_DEFAULT_ADDRESS) -> None:
5662
# This device doesn't use registers and instead just accepts a single
5763
# command string over I2C. As a result we don't use bus device or
5864
# other abstractions and just talk raw I2C protocol.
5965
self._i2c = i2c_device.I2CDevice(i2c, address)
6066
self._address = address
6167

62-
def _write_fast_mode(self, val):
68+
def _write_fast_mode(self, val: int) -> None:
6369
# Perform a 'fast mode' write to update the DAC value.
6470
# Will not enter power down, update EEPROM, or any other state beyond
6571
# the 12-bit DAC value.
@@ -71,7 +77,7 @@ def _write_fast_mode(self, val):
7177
with self._i2c as i2c:
7278
i2c.write(self._BUFFER, end=2)
7379

74-
def _read(self):
80+
def _read(self) -> int:
7581
# Perform a read of the DAC value. Returns the 12-bit value.
7682
# Read 3 bytes from device.
7783
with self._i2c as i2c:
@@ -83,7 +89,7 @@ def _read(self):
8389
return ((dac_high << 4) | dac_low) & 0xFFF
8490

8591
@property
86-
def value(self):
92+
def value(self) -> int:
8793
"""
8894
The DAC value as a 16-bit unsigned value compatible with the
8995
:py:class:`~analogio.AnalogOut` class.
@@ -97,35 +103,35 @@ def value(self):
97103
return raw_value << 4
98104

99105
@value.setter
100-
def value(self, val):
106+
def value(self, val: int) -> None:
101107
assert 0 <= val <= 65535
102108
# Scale from 16-bit to 12-bit value (quantization errors will occur!).
103109
raw_value = val >> 4
104110
self._write_fast_mode(raw_value)
105111

106112
@property
107-
def raw_value(self):
113+
def raw_value(self) -> int:
108114
"""The DAC value as a 12-bit unsigned value. This is the the true resolution of the DAC
109115
and will never peform scaling or run into quantization error.
110116
"""
111117
return self._read()
112118

113119
@raw_value.setter
114-
def raw_value(self, val):
120+
def raw_value(self, val: int) -> None:
115121
self._write_fast_mode(val)
116122

117123
@property
118-
def normalized_value(self):
124+
def normalized_value(self) -> float:
119125
"""The DAC value as a floating point number in the range 0.0 to 1.0."""
120126
return self._read() / 4095.0
121127

122128
@normalized_value.setter
123-
def normalized_value(self, val):
129+
def normalized_value(self, val: float) -> None:
124130
assert 0.0 <= val <= 1.0
125131
raw_value = int(val * 4095.0)
126132
self._write_fast_mode(raw_value)
127133

128-
def save_to_eeprom(self):
134+
def save_to_eeprom(self) -> None:
129135
"""Store the current DAC value in EEPROM."""
130136
# get it and write it
131137
current_value = self._read()

0 commit comments

Comments
 (0)