|
1 | | -# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries |
2 | 1 | # SPDX-FileCopyrightText: Copyright (c) 2022 JG for Cedar Grove Maker Studios |
3 | 2 | # |
4 | 3 | # SPDX-License-Identifier: MIT |
|
7 | 6 | ================================================================================ |
8 | 7 |
|
9 | 8 | A CircuitPython driver for the AD5245 digital potentiometer. |
10 | | -
|
| 9 | +Thank you to Bryan Siepert for the driver concept inspiration. |
11 | 10 |
|
12 | 11 | * Author(s): JG |
13 | 12 |
|
|
16 | 15 |
|
17 | 16 | **Hardware:** |
18 | 17 |
|
19 | | -.. todo:: Add links to any specific hardware product page(s), or category page(s). |
20 | | - Use unordered list & hyperlink rST inline format: "* `Link Text <url>`_" |
| 18 | +* Cedar Grove Studios AD5245 breakout or equivalent |
21 | 19 |
|
22 | 20 | **Software and Dependencies:** |
23 | 21 |
|
24 | 22 | * Adafruit CircuitPython firmware for the supported boards: |
25 | 23 | https://circuitpython.org/downloads |
26 | 24 |
|
27 | | -.. todo:: Uncomment or remove the Bus Device and/or the Register library dependencies |
28 | | - based on the library's use of either. |
29 | | -
|
30 | | -# * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice |
| 25 | +* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice |
31 | 26 | # * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register |
32 | 27 | """ |
33 | 28 |
|
34 | | -# imports |
| 29 | +import board |
| 30 | +import busio |
| 31 | +from adafruit_bus_device.i2c_device import I2CDevice |
35 | 32 |
|
36 | 33 | __version__ = "0.0.0+auto.0" |
37 | 34 | __repo__ = "https://github.com/CedarGroveStudios/Cedargrove_CircuitPython_AD5245.git" |
| 35 | + |
| 36 | + |
| 37 | +_AD5245_DEFAULT_ADDRESS = 0x2C # 44, 0b00101100 |
| 38 | + |
| 39 | + |
| 40 | +class AD5245: |
| 41 | + """Driver for the DS3502 I2C Potentiometer. |
| 42 | +
|
| 43 | + :param address: The I2C device address for the device. Default is ``0x2C``. |
| 44 | + :param wiper: The default and inital wiper value. Default is 0. |
| 45 | + """ |
| 46 | + |
| 47 | + _BUFFER = bytearray(1) |
| 48 | + |
| 49 | + def __init__(self, address=_AD5245_DEFAULT_ADDRESS, wiper=0): |
| 50 | + |
| 51 | + self._i2c = busio.I2C(board.SCL, board.SDA) |
| 52 | + self._device = I2CDevice(self._i2c, address) |
| 53 | + |
| 54 | + self._wiper = wiper |
| 55 | + self._default_wiper = wiper |
| 56 | + self._normalized_wiper = self._wiper / 255.0 |
| 57 | + self._write_to_device(0, wiper) |
| 58 | + |
| 59 | + def _write_to_device(self, command, value): |
| 60 | + """Write command and data value to the device.""" |
| 61 | + with self._device: |
| 62 | + self._device.write(bytes([command & 0xFF, value & 0xFF])) |
| 63 | + |
| 64 | + def _read_from_device(self): |
| 65 | + """Reads the contents of the data register.""" |
| 66 | + with self._device: |
| 67 | + self._device.readinto(self._BUFFER) |
| 68 | + return self._BUFFER |
| 69 | + |
| 70 | + @property |
| 71 | + def wiper(self): |
| 72 | + """The raw value of the potentionmeter's wiper. |
| 73 | + :param wiper_value: The raw wiper value from 0 to 255. |
| 74 | + """ |
| 75 | + return self._wiper |
| 76 | + |
| 77 | + @wiper.setter |
| 78 | + def wiper(self, value=0): |
| 79 | + if value < 0 or value > 255: |
| 80 | + raise ValueError("raw wiper value must be from 0 to 255") |
| 81 | + self._write_to_device(0x00, value) |
| 82 | + self._wiper = value |
| 83 | + |
| 84 | + @property |
| 85 | + def normalized_wiper(self): |
| 86 | + """The normalized value of the potentionmeter's wiper. |
| 87 | + :param normalized_wiper_value: The normalized wiper value from 0.0 to 1.0. |
| 88 | + """ |
| 89 | + return self._normalized_wiper |
| 90 | + |
| 91 | + @normalized_wiper.setter |
| 92 | + def normalized_wiper(self, value): |
| 93 | + if value < 0 or value > 1.0: |
| 94 | + raise ValueError("normalized wiper value must be from 0.0 to 1.0") |
| 95 | + self._write_to_device(0x00, int(value * 255.0)) |
| 96 | + self._normalized_wiper = value |
| 97 | + |
| 98 | + @property |
| 99 | + def default_wiper(self): |
| 100 | + """The default value of the potentionmeter's wiper. |
| 101 | + :param wiper_value: The raw wiper value from 0 to 255. |
| 102 | + """ |
| 103 | + return self._default_wiper |
| 104 | + |
| 105 | + @default_wiper.setter |
| 106 | + def default_wiper(self, value): |
| 107 | + if value < 0 or value > 255: |
| 108 | + raise ValueError("default wiper value must be from 0 to 255") |
| 109 | + self._default_wiper = value |
| 110 | + |
| 111 | + def set_default(self, default): |
| 112 | + """A dummy helper to maintain UI compatibility digital |
| 113 | + potentiometers with EEROM capability (dS3502). The AD5245's |
| 114 | + wiper value will be set to 0 unless the default value is |
| 115 | + set explicitly during or after class instantiation.""" |
| 116 | + self._default_wiper = default |
| 117 | + |
| 118 | + def shutdown(self): |
| 119 | + """Connects the W to the B terminal and open circuits the A terminal. |
| 120 | + The contents of the wiper register are not changed.""" |
| 121 | + self._write_to_device(0x20, 0) |
0 commit comments