Skip to content

Commit eb1343e

Browse files
Initial repo load
1 parent 3a7486b commit eb1343e

10 files changed

+168
-45
lines changed

README.rst

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ Introduction
99
:alt: Discord
1010

1111

12-
.. image:: https://github.com/CedarGroveStudios/Cedargrove_CircuitPython_AD5245/workflows/Build%20CI/badge.svg
13-
:target: https://github.com/CedarGroveStudios/Cedargrove_CircuitPython_AD5245/actions
12+
.. image:: https://github.com/CedarGroveStudios/CircuitPython_AD5245/workflows/Build%20CI/badge.svg
13+
:target: https://github.com/CedarGroveStudios/CircuitPython_AD5245/actions
1414
:alt: Build Status
1515

1616

@@ -31,35 +31,7 @@ Please ensure all dependencies are available on the CircuitPython filesystem.
3131
This is easily achieved by downloading
3232
`the Adafruit library and driver bundle <https://circuitpython.org/libraries>`_
3333
or individual libraries can be installed using
34-
`circup <https://github.com/adafruit/circup>`_.Installing from PyPI
35-
=====================
36-
.. note:: This library is not available on PyPI yet. Install documentation is included
37-
as a standard element. Stay tuned for PyPI availability!
38-
39-
.. todo:: Remove the above note if PyPI version is/will be available at time of release.
40-
41-
On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally `from
42-
PyPI <https://pypi.org/project/Cedargrove-circuitpython-ad5245/>`_.
43-
To install for current user:
44-
45-
.. code-block:: shell
46-
47-
pip3 install Cedargrove-circuitpython-ad5245
48-
49-
To install system-wide (this may be required in some cases):
50-
51-
.. code-block:: shell
52-
53-
sudo pip3 install Cedargrove-circuitpython-ad5245
54-
55-
To install in a virtual environment in your current project:
56-
57-
.. code-block:: shell
58-
59-
mkdir project-name && cd project-name
60-
python3 -m venv .venv
61-
source .env/bin/activate
62-
pip3 install Cedargrove-circuitpython-ad5245
34+
`circup <https://github.com/adafruit/circup>`_.
6335

6436
Installing to a Connected CircuitPython Device with Circup
6537
==========================================================
@@ -76,7 +48,7 @@ following command to install:
7648

7749
.. code-block:: shell
7850
79-
circup install ad5245
51+
circup install cedargrove_ad5245
8052
8153
Or the following command to update an existing version:
8254

@@ -87,12 +59,29 @@ Or the following command to update an existing version:
8759
Usage Example
8860
=============
8961

90-
.. todo:: Add a quick, simple example. It and other examples should live in the
91-
examples folder and be included in docs/examples.rst.
62+
.. code-block:: python
63+
64+
import cedargrove_ad5245
65+
66+
ad5245 = cedargrove_ad5245.AD5245(address=0x2C)
67+
68+
ad5245.wiper = 255
69+
print("Wiper set to %d"%ad5245.wiper)
70+
71+
72+
``nau7802_simpletest.py`` and other examples can be found in the ``examples`` folder.
73+
9274

9375
Documentation
9476
=============
95-
API documentation for this library can be found on `Read the Docs <https://circuitpython-ad5245.readthedocs.io/>`_.
77+
`NAU7802 CircuitPython Driver API Class Description <https://github.com/CedarGroveStudios/CircuitPython_AD5245/main/media/pseudo_readthedocs_cedargrove_ad5245.pdf>`_
78+
79+
.. image:: https://github.com/CedarGroveStudios/CircuitPython_AD5245/main/media/DSC05820a_lores.jpg
80+
81+
`CedarGrove AD5245 Breakout OSH Park Project <https://oshpark.com/shared_projects/WcYMJx7L>`_
82+
83+
.. image:: https://github.com/CedarGroveStudios/CircuitPython_AD5245/main/media/AD5245_breakout_close.png
84+
.. image:: https://github.com/CedarGroveStudios/CircuitPython_AD5245/main/media/AD5245_breakout_for_fritzing.png
9685

9786
For information on building library documentation, please check out
9887
`this guide <https://learn.adafruit.com/creating-and-sharing-a-circuitpython-library/sharing-our-docs-on-readthedocs#sphinx-5-1>`_.

cedargrove_ad5245.py

Lines changed: 93 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
21
# SPDX-FileCopyrightText: Copyright (c) 2022 JG for Cedar Grove Maker Studios
32
#
43
# SPDX-License-Identifier: MIT
@@ -7,7 +6,7 @@
76
================================================================================
87
98
A CircuitPython driver for the AD5245 digital potentiometer.
10-
9+
Thank you to Bryan Siepert for the driver concept inspiration.
1110
1211
* Author(s): JG
1312
@@ -16,22 +15,107 @@
1615
1716
**Hardware:**
1817
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
2119
2220
**Software and Dependencies:**
2321
2422
* Adafruit CircuitPython firmware for the supported boards:
2523
https://circuitpython.org/downloads
2624
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
3126
# * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
3227
"""
3328

34-
# imports
29+
import board
30+
import busio
31+
from adafruit_bus_device.i2c_device import I2CDevice
3532

3633
__version__ = "0.0.0+auto.0"
3734
__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)

examples/ad5245_simpletest.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,42 @@
1-
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
21
# SPDX-FileCopyrightText: Copyright (c) 2022 JG for Cedar Grove Maker Studios
32
#
43
# SPDX-License-Identifier: Unlicense
4+
5+
from time import sleep
6+
import board
7+
from analogio import AnalogIn
8+
import cedargrove_ad5245
9+
10+
# wire pin A to +3.3v, pin B to GND, and pin W to A0
11+
12+
ad5245 = cedargrove_ad5245.AD5245(address=0x2C)
13+
wiper_output = AnalogIn(board.A0)
14+
15+
while True:
16+
17+
ad5245.wiper = 255
18+
print("Wiper set to %d" % ad5245.wiper)
19+
voltage = wiper_output.value
20+
voltage *= 3.3
21+
voltage /= 65535
22+
print("Wiper voltage: %.2f" % voltage)
23+
print("")
24+
sleep(1.0)
25+
26+
ad5245.wiper = 0
27+
print("Wiper set to %d" % ad5245.wiper)
28+
voltage = wiper_output.value
29+
voltage *= 3.3
30+
voltage /= 65535
31+
print("Wiper voltage: %.2f" % voltage)
32+
print("")
33+
sleep(1.0)
34+
35+
ad5245.wiper = 126
36+
print("Wiper set to %d" % ad5245.wiper)
37+
voltage = wiper_output.value
38+
voltage *= 3.3
39+
voltage /= 65535
40+
print("Wiper voltage: %.2f" % voltage)
41+
print("")
42+
sleep(1.0)

media/AD5245_breakout_close.png

503 KB
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SPDX-FileCopyrightText: CedarGroveStudios
2+
3+
SPDX-License-Identifier: Unlicense
1.16 MB
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SPDX-FileCopyrightText: CedarGroveStudios
2+
3+
SPDX-License-Identifier: Unlicense

media/DSC05820a_lores.jpg

535 KB
Loading

media/DSC05820a_lores.jpg.license

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SPDX-FileCopyrightText: CedarGroveStudios
2+
3+
SPDX-License-Identifier: Unlicense
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SPDX-FileCopyrightText: CedarGroveStudios
2+
3+
SPDX-License-Identifier: Unlicense

0 commit comments

Comments
 (0)