28
28
from micropython import const
29
29
from adafruit_bus_device import i2c_device
30
30
31
+ try :
32
+ import typing # pylint: disable=unused-import
33
+ from busio import I2C
34
+ except ImportError :
35
+ pass
36
+
31
37
__version__ = "0.0.0+auto.0"
32
38
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP4725.git"
33
39
@@ -52,14 +58,14 @@ class MCP4725:
52
58
# Note this is not thread-safe or re-entrant by design!
53
59
_BUFFER = bytearray (3 )
54
60
55
- def __init__ (self , i2c , * , address = _MCP4725_DEFAULT_ADDRESS ):
61
+ def __init__ (self , i2c : I2C , * , address : int = _MCP4725_DEFAULT_ADDRESS ) -> None :
56
62
# This device doesn't use registers and instead just accepts a single
57
63
# command string over I2C. As a result we don't use bus device or
58
64
# other abstractions and just talk raw I2C protocol.
59
65
self ._i2c = i2c_device .I2CDevice (i2c , address )
60
66
self ._address = address
61
67
62
- def _write_fast_mode (self , val ) :
68
+ def _write_fast_mode (self , val : int ) -> None :
63
69
# Perform a 'fast mode' write to update the DAC value.
64
70
# Will not enter power down, update EEPROM, or any other state beyond
65
71
# the 12-bit DAC value.
@@ -71,7 +77,7 @@ def _write_fast_mode(self, val):
71
77
with self ._i2c as i2c :
72
78
i2c .write (self ._BUFFER , end = 2 )
73
79
74
- def _read (self ):
80
+ def _read (self ) -> int :
75
81
# Perform a read of the DAC value. Returns the 12-bit value.
76
82
# Read 3 bytes from device.
77
83
with self ._i2c as i2c :
@@ -83,7 +89,7 @@ def _read(self):
83
89
return ((dac_high << 4 ) | dac_low ) & 0xFFF
84
90
85
91
@property
86
- def value (self ):
92
+ def value (self ) -> int :
87
93
"""
88
94
The DAC value as a 16-bit unsigned value compatible with the
89
95
:py:class:`~analogio.AnalogOut` class.
@@ -97,35 +103,35 @@ def value(self):
97
103
return raw_value << 4
98
104
99
105
@value .setter
100
- def value (self , val ) :
106
+ def value (self , val : int ) -> None :
101
107
assert 0 <= val <= 65535
102
108
# Scale from 16-bit to 12-bit value (quantization errors will occur!).
103
109
raw_value = val >> 4
104
110
self ._write_fast_mode (raw_value )
105
111
106
112
@property
107
- def raw_value (self ):
113
+ def raw_value (self ) -> int :
108
114
"""The DAC value as a 12-bit unsigned value. This is the the true resolution of the DAC
109
115
and will never peform scaling or run into quantization error.
110
116
"""
111
117
return self ._read ()
112
118
113
119
@raw_value .setter
114
- def raw_value (self , val ) :
120
+ def raw_value (self , val : int ) -> None :
115
121
self ._write_fast_mode (val )
116
122
117
123
@property
118
- def normalized_value (self ):
124
+ def normalized_value (self ) -> float :
119
125
"""The DAC value as a floating point number in the range 0.0 to 1.0."""
120
126
return self ._read () / 4095.0
121
127
122
128
@normalized_value .setter
123
- def normalized_value (self , val ) :
129
+ def normalized_value (self , val : float ) -> None :
124
130
assert 0.0 <= val <= 1.0
125
131
raw_value = int (val * 4095.0 )
126
132
self ._write_fast_mode (raw_value )
127
133
128
- def save_to_eeprom (self ):
134
+ def save_to_eeprom (self ) -> None :
129
135
"""Store the current DAC value in EEPROM."""
130
136
# get it and write it
131
137
current_value = self ._read ()
0 commit comments