Skip to content

Convert to BusDevice #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 14, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 17 additions & 28 deletions adafruit_mcp4725.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
CircuitPython module for the MCP4725 digital to analog converter. See
examples/mcp4725_simpletest.py for a demo of the usage.

* Author(s): Tony DiCola
* Author(s): Tony DiCola, Carter Nelson

Implementation Notes
--------------------
Expand All @@ -42,6 +42,7 @@
https://github.com/adafruit/circuitpython/releases
"""
from micropython import const
from adafruit_bus_device import i2c_device

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

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

def _read(self):
# Perform a read of the DAC value. Returns the 12-bit value.
try:
# Make sure bus is locked before write.
while not self._i2c.try_lock():
pass
# Read 3 bytes from device.
self._i2c.readfrom_into(self._address, self._BUFFER)
# Grab the DAC value from last two bytes.
dac_high = self._BUFFER[1]
dac_low = self._BUFFER[2] >> 4
# Reconstruct 12-bit value and return it.
return ((dac_high << 4) | dac_low) & 0xFFF
finally:
# Ensure bus is always unlocked.
self._i2c.unlock()
# Read 3 bytes from device.
with self._i2c as i2c:
i2c.readinto(self._BUFFER)
# Grab the DAC value from last two bytes.
dac_high = self._BUFFER[1]
dac_low = self._BUFFER[2] >> 4
# Reconstruct 12-bit value and return it.
return ((dac_high << 4) | dac_low) & 0xFFF

@property
def value(self):
Expand Down