|
26 | 26 | CircuitPython module for the MCP4725 digital to analog converter. See |
27 | 27 | examples/mcp4725_simpletest.py for a demo of the usage. |
28 | 28 |
|
29 | | -* Author(s): Tony DiCola |
| 29 | +* Author(s): Tony DiCola, Carter Nelson |
30 | 30 |
|
31 | 31 | Implementation Notes |
32 | 32 | -------------------- |
|
42 | 42 | https://github.com/adafruit/circuitpython/releases |
43 | 43 | """ |
44 | 44 | from micropython import const |
| 45 | +from adafruit_bus_device import i2c_device |
45 | 46 |
|
46 | 47 | __version__ = "0.0.0-auto.0" |
47 | 48 | __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP4725.git" |
@@ -71,43 +72,31 @@ def __init__(self, i2c, *, address=_MCP4725_DEFAULT_ADDRESS): |
71 | 72 | # This device doesn't use registers and instead just accepts a single |
72 | 73 | # command string over I2C. As a result we don't use bus device or |
73 | 74 | # other abstractions and just talk raw I2C protocol. |
74 | | - self._i2c = i2c |
| 75 | + self._i2c = i2c_device.I2CDevice(i2c, address) |
75 | 76 | self._address = address |
76 | 77 |
|
77 | 78 | def _write_fast_mode(self, val): |
78 | 79 | # Perform a 'fast mode' write to update the DAC value. |
79 | 80 | # Will not enter power down, update EEPROM, or any other state beyond |
80 | 81 | # the 12-bit DAC value. |
81 | 82 | 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) |
94 | 89 |
|
95 | 90 | def _read(self): |
96 | 91 | # 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 |
111 | 100 |
|
112 | 101 | @property |
113 | 102 | def value(self): |
|
0 commit comments