Skip to content

Commit 5ddaf45

Browse files
committed
update for gain setting
1 parent e8162ab commit 5ddaf45

File tree

5 files changed

+65
-115
lines changed

5 files changed

+65
-115
lines changed

.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: 61 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,18 +46,9 @@
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

6353
class Adafruit_AD569x:
6454
"""Class which provides interface to AD569x Dac."""
@@ -75,18 +65,20 @@ def __init__(self, i2c: I2C, address: int = 0x4C) -> None:
7565
:param address: The I2C address of the device. Defaults to 0x4C.
7666
"""
7767
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)
68+
"""
69+
Mode options
70+
"""
71+
self.normal_mode = const(0x00)
72+
self.output_1k_impedance = const(0x01)
73+
self.output_100k_impedance = const(0x02)
74+
self.output_tristate = const(0x03)
8475

8576
try:
8677
self.reset()
87-
self.mode = NORMAL_MODE
88-
self.enable_ref = True
89-
self.gain = GAIN_1X
78+
self._mode = self.normal_mode
79+
self._internal_reference = True
80+
self._gain = False
81+
self._update_control_register()
9082
except OSError as error:
9183
raise OSError(f"Failed to initialize AD569x, {error}") from error
9284

@@ -104,94 +96,92 @@ def _send_command(self, command: int, data: int) -> None:
10496
high_byte = (data >> 8) & 0xFF
10597
low_byte = data & 0xFF
10698
buffer = bytearray([command, high_byte, low_byte])
107-
with self.i2c_device as i2c:
108-
i2c.write(buffer)
99+
try:
100+
with self.i2c_device as i2c:
101+
i2c.write(buffer)
102+
except Exception:
103+
with self.i2c_device as i2c:
104+
i2c.write(buffer, end=False)
109105
except Exception as error:
110106
raise Exception(f"Error sending command: {error}") from error
111107

108+
def _update_control_register(self):
109+
data = 0x0000
110+
data |= self._mode << 13
111+
data |= not self._internal_reference << 12
112+
data |= self._gain << 11
113+
self._send_command(_WRITE_CONTROL, data)
114+
112115
@property
113-
def mode(self) -> int:
116+
def mode(self):
114117
"""
115-
Set the operating mode for the AD569x chip.
118+
Operating mode for the AD569x chip.
116119
117120
:param value: An int containing new operating mode.
118121
"""
119-
return self._mode_command
122+
return self._mode
120123

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

125135
@property
126-
def ref_enabled(self) -> bool:
136+
def internal_reference(self):
127137
"""
128-
Enable the reference voltage for the AD569x chip.
138+
Internal reference voltage for the AD569x chip.
129139
130-
:param value: A bool to enable the reference voltage.
140+
:param value: A bool to enable the internal reference voltage.
131141
"""
132-
return not bool(self._ref_command)
142+
return self._internal_reference
133143

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

138150
@property
139-
def gain(self) -> bool:
151+
def gain(self):
140152
"""
141-
Set the gain for the AD569x chip.
153+
Gain for the AD569x chip.
142154
143155
:param value: A bool to choose 1X or 2X gain.
144156
"""
145-
return bool(self._gain_command)
157+
return self._gain
146158

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

151165
@property
152166
def value(self) -> int:
153167
"""
154-
Write a 16-bit value to the input register and update the DAC register.
168+
16-bit value to the input register and update the DAC register.
155169
156170
This property writes a 16-bit value to the input register and then updates
157171
the DAC register of the AD569x chip in a single operation.
158172
"""
159173
return self.value
160174

161175
@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)
176+
def value(self, val: int) -> None:
177+
self._send_command(_WRITE_DAC_AND_INPUT, val)
178178

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:
179+
def reset(self):
190180
"""
191181
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.
196182
"""
197-
self._reset_command = 1
183+
reset_command = 0x8000
184+
try:
185+
self._send_command(_WRITE_CONTROL, reset_command)
186+
except Exception as error:
187+
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

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,3 @@
55

66
Adafruit-Blinka
77
adafruit-circuitpython-busdevice
8-
adafruit-circuitpython-register

0 commit comments

Comments
 (0)