Skip to content

Commit c4d90f0

Browse files
authored
Merge pull request #2 from adafruit/gain_fix
update for gain setting
2 parents e8162ab + 4a87cb4 commit c4d90f0

7 files changed

+116
-123
lines changed

.pre-commit-config.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44

55
repos:
66
- repo: https://github.com/python/black
7-
rev: 22.3.0
7+
rev: 23.3.0
88
hooks:
99
- id: black
1010
- repo: https://github.com/fsfe/reuse-tool
11-
rev: v0.14.0
11+
rev: v1.1.2
1212
hooks:
1313
- id: reuse
1414
- repo: https://github.com/pre-commit/pre-commit-hooks
15-
rev: v4.2.0
15+
rev: v4.4.0
1616
hooks:
1717
- id: check-yaml
1818
- id: end-of-file-fixer
1919
- id: trailing-whitespace
2020
- repo: https://github.com/pycqa/pylint
21-
rev: v2.15.5
21+
rev: v2.17.4
2222
hooks:
2323
- id: pylint
2424
name: pylint (library code)

.pylintrc

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs=1
2626

2727
# List of plugins (as comma separated values of python modules names) to load,
2828
# usually to register additional checkers.
29-
load-plugins=
29+
load-plugins=pylint.extensions.no_self_use
3030

3131
# Pickle collected data for later comparisons.
3232
persistent=yes
@@ -54,8 +54,8 @@ confidence=
5454
# --enable=similarities". If you want to run only the classes checker, but have
5555
# no Warning level messages displayed, use"--disable=all --enable=classes
5656
# --disable=W"
57-
# disable=import-error,print-statement,parameter-unpacking,unpacking-in-except,old-raise-syntax,backtick,long-suffix,old-ne-operator,old-octal-literal,import-star-module-level,raw-checker-failed,bad-inline-option,locally-disabled,locally-enabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,apply-builtin,basestring-builtin,buffer-builtin,cmp-builtin,coerce-builtin,execfile-builtin,file-builtin,long-builtin,raw_input-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,no-absolute-import,old-division,dict-iter-method,dict-view-method,next-method-called,metaclass-assignment,indexing-exception,raising-string,reload-builtin,oct-method,hex-method,nonzero-method,cmp-method,input-builtin,round-builtin,intern-builtin,unichr-builtin,map-builtin-not-iterating,zip-builtin-not-iterating,range-builtin-not-iterating,filter-builtin-not-iterating,using-cmp-argument,eq-without-hash,div-method,idiv-method,rdiv-method,exception-message-attribute,invalid-str-codec,sys-max-int,bad-python3-import,deprecated-string-function,deprecated-str-translate-call
58-
disable=print-statement,parameter-unpacking,unpacking-in-except,old-raise-syntax,backtick,long-suffix,old-ne-operator,old-octal-literal,import-star-module-level,raw-checker-failed,bad-inline-option,locally-disabled,locally-enabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,apply-builtin,basestring-builtin,buffer-builtin,cmp-builtin,coerce-builtin,execfile-builtin,file-builtin,long-builtin,raw_input-builtin,reduce-builtin,standarderror-builtin,unicode-builtin,xrange-builtin,coerce-method,delslice-method,getslice-method,setslice-method,no-absolute-import,old-division,dict-iter-method,dict-view-method,next-method-called,metaclass-assignment,indexing-exception,raising-string,reload-builtin,oct-method,hex-method,nonzero-method,cmp-method,input-builtin,round-builtin,intern-builtin,unichr-builtin,map-builtin-not-iterating,zip-builtin-not-iterating,range-builtin-not-iterating,filter-builtin-not-iterating,using-cmp-argument,eq-without-hash,div-method,idiv-method,rdiv-method,exception-message-attribute,invalid-str-codec,sys-max-int,bad-python3-import,deprecated-string-function,deprecated-str-translate-call,import-error,bad-continuation,pointless-string-statement,unspecified-encoding
57+
# disable=import-error,raw-checker-failed,bad-inline-option,locally-disabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,deprecated-str-translate-call
58+
disable=raw-checker-failed,bad-inline-option,locally-disabled,file-ignored,suppressed-message,useless-suppression,deprecated-pragma,import-error,pointless-string-statement,unspecified-encoding
5959

6060
# Enable the message, report, category or checker with the given id(s). You can
6161
# either give multiple identifier separated by comma (,) or put this option
@@ -225,12 +225,6 @@ max-line-length=100
225225
# Maximum number of lines in a module
226226
max-module-lines=1000
227227

228-
# List of optional constructs for which whitespace checking is disabled. `dict-
229-
# separator` is used to allow tabulation in dicts, etc.: {1 : 1,\n222: 2}.
230-
# `trailing-comma` allows a space between comma and closing bracket: (a, ).
231-
# `empty-line` allows space-only lines.
232-
no-space-check=trailing-comma,dict-separator
233-
234228
# Allow the body of a class to be on the same line as the declaration if body
235229
# contains single statement.
236230
single-line-class-stmt=no
@@ -257,48 +251,29 @@ min-similarity-lines=12
257251

258252
[BASIC]
259253

260-
# Naming hint for argument names
261-
argument-name-hint=(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$
262-
263254
# Regular expression matching correct argument names
264255
argument-rgx=(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$
265256

266-
# Naming hint for attribute names
267-
attr-name-hint=(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$
268-
269257
# Regular expression matching correct attribute names
270258
attr-rgx=(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$
271259

272260
# Bad variable names which should always be refused, separated by a comma
273261
bad-names=foo,bar,baz,toto,tutu,tata
274262

275-
# Naming hint for class attribute names
276-
class-attribute-name-hint=([A-Za-z_][A-Za-z0-9_]{2,30}|(__.*__))$
277-
278263
# Regular expression matching correct class attribute names
279264
class-attribute-rgx=([A-Za-z_][A-Za-z0-9_]{2,30}|(__.*__))$
280265

281-
# Naming hint for class names
282-
# class-name-hint=[A-Z_][a-zA-Z0-9]+$
283-
class-name-hint=[A-Z_][a-zA-Z0-9_]+$
284-
285266
# Regular expression matching correct class names
286267
# class-rgx=[A-Z_][a-zA-Z0-9]+$
287268
class-rgx=[A-Z_][a-zA-Z0-9_]+$
288269

289-
# Naming hint for constant names
290-
const-name-hint=(([A-Z_][A-Z0-9_]*)|(__.*__))$
291-
292270
# Regular expression matching correct constant names
293271
const-rgx=(([A-Z_][A-Z0-9_]*)|(__.*__))$
294272

295273
# Minimum line length for functions/classes that require docstrings, shorter
296274
# ones are exempt.
297275
docstring-min-length=-1
298276

299-
# Naming hint for function names
300-
function-name-hint=(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$
301-
302277
# Regular expression matching correct function names
303278
function-rgx=(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$
304279

@@ -309,21 +284,12 @@ good-names=r,g,b,w,i,j,k,n,x,y,z,ex,ok,Run,_
309284
# Include a hint for the correct naming format with invalid-name
310285
include-naming-hint=no
311286

312-
# Naming hint for inline iteration names
313-
inlinevar-name-hint=[A-Za-z_][A-Za-z0-9_]*$
314-
315287
# Regular expression matching correct inline iteration names
316288
inlinevar-rgx=[A-Za-z_][A-Za-z0-9_]*$
317289

318-
# Naming hint for method names
319-
method-name-hint=(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$
320-
321290
# Regular expression matching correct method names
322291
method-rgx=(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$
323292

324-
# Naming hint for module names
325-
module-name-hint=(([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$
326-
327293
# Regular expression matching correct module names
328294
module-rgx=(([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$
329295

@@ -339,9 +305,6 @@ no-docstring-rgx=^_
339305
# to this list to register other decorators that produce valid properties.
340306
property-classes=abc.abstractproperty
341307

342-
# Naming hint for variable names
343-
variable-name-hint=(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$
344-
345308
# Regular expression matching correct variable names
346309
variable-rgx=(([a-z][a-z0-9_]{2,30})|(_[a-z0-9_]*))$
347310

README.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ This driver depends on:
3030

3131
* `Adafruit CircuitPython <https://github.com/adafruit/circuitpython>`_
3232
* `Bus Device <https://github.com/adafruit/Adafruit_CircuitPython_BusDevice>`_
33-
* `Register <https://github.com/adafruit/Adafruit_CircuitPython_Register>`_
3433

3534
Please ensure all dependencies are available on the CircuitPython filesystem.
3635
This is easily achieved by downloading
@@ -84,7 +83,7 @@ following command to install:
8483

8584
.. code-block:: shell
8685
87-
circup install ad569x
86+
circup install adafruit--circuitpython-ad569x
8887
8988
Or the following command to update an existing version:
9089

adafruit_ad569x.py

Lines changed: 62 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535

3636
from micropython import const
3737
from adafruit_bus_device.i2c_device import I2CDevice
38-
from adafruit_register.i2c_bits import RWBits
3938

4039
try:
4140
import typing # pylint: disable=unused-import
@@ -47,19 +46,11 @@
4746
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_AD569x.git"
4847

4948
_NOP = const(0x00)
50-
_WRITE_INPUT = const(0x10)
51-
_UPDATE_DAC = const(0x20)
5249
_WRITE_DAC_AND_INPUT = const(0x30)
5350
_WRITE_CONTROL = const(0x40)
5451

55-
NORMAL_MODE = const(0x00)
56-
OUTPUT_1K_IMPEDANCE = const(0x01)
57-
OUTPUT_100K_IMPEDANCE = const(0x02)
58-
OUTPUT_TRISTATE = const(0x03)
59-
GAIN_1X = const(0)
60-
GAIN_2X = const(1)
61-
6252

53+
# pylint: disable=broad-exception-raised
6354
class Adafruit_AD569x:
6455
"""Class which provides interface to AD569x Dac."""
6556

@@ -75,18 +66,20 @@ def __init__(self, i2c: I2C, address: int = 0x4C) -> None:
7566
:param address: The I2C address of the device. Defaults to 0x4C.
7667
"""
7768
self.i2c_device = I2CDevice(i2c, address)
78-
self._address = address
79-
80-
self._reset_command = RWBits(1, _WRITE_CONTROL, 15, 2)
81-
self._mode_command = RWBits(2, _WRITE_CONTROL, 13, 2)
82-
self._ref_command = RWBits(1, _WRITE_CONTROL, 12, 2)
83-
self._gain_command = RWBits(1, _WRITE_CONTROL, 11, 2)
69+
"""
70+
Mode options
71+
"""
72+
self.normal_mode = const(0x00)
73+
self.output_1k_impedance = const(0x01)
74+
self.output_100k_impedance = const(0x02)
75+
self.output_tristate = const(0x03)
8476

8577
try:
8678
self.reset()
87-
self.mode = NORMAL_MODE
88-
self.enable_ref = True
89-
self.gain = GAIN_1X
79+
self._mode = self.normal_mode
80+
self._internal_reference = True
81+
self._gain = False
82+
self._update_control_register()
9083
except OSError as error:
9184
raise OSError(f"Failed to initialize AD569x, {error}") from error
9285

@@ -104,94 +97,92 @@ def _send_command(self, command: int, data: int) -> None:
10497
high_byte = (data >> 8) & 0xFF
10598
low_byte = data & 0xFF
10699
buffer = bytearray([command, high_byte, low_byte])
107-
with self.i2c_device as i2c:
108-
i2c.write(buffer)
100+
try:
101+
with self.i2c_device as i2c:
102+
i2c.write(buffer)
103+
except Exception: # pylint: disable=broad-exception-caught
104+
with self.i2c_device as i2c:
105+
i2c.write(buffer, end=False)
109106
except Exception as error:
110107
raise Exception(f"Error sending command: {error}") from error
111108

109+
def _update_control_register(self):
110+
data = 0x0000
111+
data |= self._mode << 13
112+
data |= not self._internal_reference << 12
113+
data |= self._gain << 11
114+
self._send_command(_WRITE_CONTROL, data)
115+
112116
@property
113-
def mode(self) -> int:
117+
def mode(self):
114118
"""
115-
Set the operating mode for the AD569x chip.
119+
Operating mode for the AD569x chip.
116120
117121
:param value: An int containing new operating mode.
118122
"""
119-
return self._mode_command
123+
return self._mode
120124

121125
@mode.setter
122-
def mode(self, value: int) -> None:
123-
self._mode_command = value
126+
def mode(self, new_mode):
127+
if new_mode not in [0, 1, 2, 3]:
128+
raise ValueError(
129+
"Mode must be normal_mode, output_1k_impedance,"
130+
+ "output_100k_impedance or output_tristate"
131+
)
132+
self._mode = new_mode
133+
self.reset()
134+
self._update_control_register()
124135

125136
@property
126-
def ref_enabled(self) -> bool:
137+
def internal_reference(self):
127138
"""
128-
Enable the reference voltage for the AD569x chip.
139+
Internal reference voltage for the AD569x chip.
129140
130-
:param value: A bool to enable the reference voltage.
141+
:param value: A bool to enable the internal reference voltage.
131142
"""
132-
return not bool(self._ref_command)
143+
return self._internal_reference
133144

134-
@ref_enabled.setter
135-
def ref_enabled(self, value: bool) -> None:
136-
self._ref_command = value
145+
@internal_reference.setter
146+
def internal_reference(self, value):
147+
self._internal_reference = value
148+
self.reset()
149+
self._update_control_register()
137150

138151
@property
139-
def gain(self) -> bool:
152+
def gain(self):
140153
"""
141-
Set the gain for the AD569x chip.
154+
Gain for the AD569x chip.
142155
143156
:param value: A bool to choose 1X or 2X gain.
144157
"""
145-
return bool(self._gain_command)
158+
return self._gain
146159

147160
@gain.setter
148-
def gain(self, value: bool) -> None:
149-
self._gain_command = value
161+
def gain(self, value):
162+
self._gain = value
163+
self.reset()
164+
self._update_control_register()
150165

151166
@property
152167
def value(self) -> int:
153168
"""
154-
Write a 16-bit value to the input register and update the DAC register.
169+
16-bit value to the input register and update the DAC register.
155170
156171
This property writes a 16-bit value to the input register and then updates
157172
the DAC register of the AD569x chip in a single operation.
158173
"""
159174
return self.value
160175

161176
@value.setter
162-
def value(self, value: int) -> None:
163-
self._send_command(_WRITE_DAC_AND_INPUT, value)
164-
165-
@property
166-
def dac(self) -> int:
167-
"""
168-
Write a 16-bit value to the input register.
169-
170-
This function writes a 16-bit value to the input register of the AD569x chip.
171-
"""
172-
return self.dac
173-
174-
@dac.setter
175-
def dac(self, value: int) -> None:
176-
# Use the internal _send_command function
177-
self._send_command(_WRITE_INPUT, value)
177+
def value(self, val: int) -> None:
178+
self._send_command(_WRITE_DAC_AND_INPUT, val)
178179

179-
def update_dac(self) -> None:
180-
"""
181-
Update the DAC register from the input register.
182-
183-
This function sends the UPDATE_DAC command to the AD569x chip to update
184-
the DAC register based on the value stored in the input register.
185-
"""
186-
# Use the internal _send_command function with 0x0000 as data
187-
self._send_command(_UPDATE_DAC, 0x0000)
188-
189-
def reset(self) -> None:
180+
def reset(self):
190181
"""
191182
Soft-reset the AD569x chip.
192-
193-
This function writes 0x8000 to the control register of the AD569x chip
194-
to perform a reset operation. Resets the DAC to zero-scale and
195-
resets the input, DAC, and control registers to their default values.
196183
"""
197-
self._reset_command = 1
184+
reset_command = 0x8000
185+
try:
186+
self._send_command(_WRITE_CONTROL, reset_command)
187+
except Exception as error:
188+
raise Exception(f"Error during reset: {error}") from error

docs/conf.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
"micropython",
3333
"busio",
3434
"adafruit_bus_device",
35-
"adafruit_register",
3635
]
3736

3837

0 commit comments

Comments
 (0)