Skip to content

Commit 9d781b0

Browse files
authored
Merge pull request #9 from caternuson/iss8_busdevice
Convert to BusDevice
2 parents ffe4964 + de585fc commit 9d781b0

File tree

4 files changed

+21
-30
lines changed

4 files changed

+21
-30
lines changed

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Dependencies
2121
This driver depends on:
2222

2323
* `Adafruit CircuitPython <https://github.com/adafruit/circuitpython>`_
24+
* `Bus Device <https://github.com/adafruit/Adafruit_CircuitPython_BusDevice>`_
2425

2526
Please ensure all dependencies are available on the CircuitPython filesystem.
2627
This is easily achieved by downloading

adafruit_mcp4725.py

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
CircuitPython module for the MCP4725 digital to analog converter. See
2727
examples/mcp4725_simpletest.py for a demo of the usage.
2828
29-
* Author(s): Tony DiCola
29+
* Author(s): Tony DiCola, Carter Nelson
3030
3131
Implementation Notes
3232
--------------------
@@ -42,6 +42,7 @@
4242
https://github.com/adafruit/circuitpython/releases
4343
"""
4444
from micropython import const
45+
from adafruit_bus_device import i2c_device
4546

4647
__version__ = "0.0.0-auto.0"
4748
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP4725.git"
@@ -71,43 +72,31 @@ def __init__(self, i2c, *, address=_MCP4725_DEFAULT_ADDRESS):
7172
# This device doesn't use registers and instead just accepts a single
7273
# command string over I2C. As a result we don't use bus device or
7374
# other abstractions and just talk raw I2C protocol.
74-
self._i2c = i2c
75+
self._i2c = i2c_device.I2CDevice(i2c, address)
7576
self._address = address
7677

7778
def _write_fast_mode(self, val):
7879
# Perform a 'fast mode' write to update the DAC value.
7980
# Will not enter power down, update EEPROM, or any other state beyond
8081
# the 12-bit DAC value.
8182
assert 0 <= val <= 4095
82-
try:
83-
# Make sure bus is locked before write.
84-
while not self._i2c.try_lock():
85-
pass
86-
# Build bytes to send to device with updated value.
87-
val &= 0xFFF
88-
self._BUFFER[0] = _MCP4725_WRITE_FAST_MODE | (val >> 8)
89-
self._BUFFER[1] = val & 0xFF
90-
self._i2c.writeto(self._address, self._BUFFER, end=2)
91-
finally:
92-
# Ensure bus is always unlocked.
93-
self._i2c.unlock()
83+
# Build bytes to send to device with updated value.
84+
val &= 0xFFF
85+
self._BUFFER[0] = _MCP4725_WRITE_FAST_MODE | (val >> 8)
86+
self._BUFFER[1] = val & 0xFF
87+
with self._i2c as i2c:
88+
i2c.write(self._BUFFER, end=2)
9489

9590
def _read(self):
9691
# Perform a read of the DAC value. Returns the 12-bit value.
97-
try:
98-
# Make sure bus is locked before write.
99-
while not self._i2c.try_lock():
100-
pass
101-
# Read 3 bytes from device.
102-
self._i2c.readfrom_into(self._address, self._BUFFER)
103-
# Grab the DAC value from last two bytes.
104-
dac_high = self._BUFFER[1]
105-
dac_low = self._BUFFER[2] >> 4
106-
# Reconstruct 12-bit value and return it.
107-
return ((dac_high << 4) | dac_low) & 0xFFF
108-
finally:
109-
# Ensure bus is always unlocked.
110-
self._i2c.unlock()
92+
# Read 3 bytes from device.
93+
with self._i2c as i2c:
94+
i2c.readinto(self._BUFFER)
95+
# Grab the DAC value from last two bytes.
96+
dac_high = self._BUFFER[1]
97+
dac_low = self._BUFFER[2] >> 4
98+
# Reconstruct 12-bit value and return it.
99+
return ((dac_high << 4) | dac_low) & 0xFFF
111100

112101
@property
113102
def value(self):

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Adafruit-Blinka
1+
Adafruit-Blinka
2+
adafruit-circuitpython-busdevice

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
author='Adafruit Industries',
3535
author_email='[email protected]',
3636

37-
install_requires=['Adafruit-Blinka'],
37+
install_requires=['Adafruit-Blinka', 'adafruit-circuitpython-busdevice'],
3838

3939
# Choose your license
4040
license='MIT',

0 commit comments

Comments
 (0)