Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify ADC classes #495

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion adi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from adi.ad578x import ad578x
from adi.ad717x import ad717x
from adi.ad719x import ad719x
from adi.ad760x import ad7605_4, ad7606, ad7606_4, ad7606_6, ad7606_8
from adi.ad777x import ad777x
from adi.ad936x import Pluto, ad9361, ad9363, ad9364
from adi.ad4020 import ad4020
Expand All @@ -21,7 +22,6 @@
from adi.ad6676 import ad6676
from adi.ad7124 import ad7124
from adi.ad7291 import ad7291
from adi.ad7606 import ad7606
from adi.ad7689 import ad7689
from adi.ad7746 import ad7746
from adi.ad7768 import ad7768, ad7768_4
Expand Down
45 changes: 0 additions & 45 deletions adi/ad7124.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,48 +76,3 @@ def sample_rate(self, value):
def scale_available(self):
"""Provides all available scale(gain) settings for the AD7124 channels"""
return self._get_iio_attr(self.channel[0].name, "scale_available", False)

class _channel(attribute):
"""AD7124 channel"""

def __init__(self, ctrl, channel_name):
self.name = channel_name
self._ctrl = ctrl

@property
def raw(self):
"""AD7124 channel raw value"""
return self._get_iio_attr(self.name, "raw", False)

@property
def scale(self):
"""AD7124 channel scale(gain)"""
return float(self._get_iio_attr_str(self.name, "scale", False))

@scale.setter
def scale(self, value):
self._set_iio_attr(self.name, "scale", False, str(Decimal(value).real))

@property
def offset(self):
"""AD7124 channel offset"""
return self._get_iio_attr(self.name, "offset", False)

@offset.setter
def offset(self, value):
self._get_iio_attr(self.name, "offset", False, value)

def to_volts(self, index, val):
"""Converts raw value to SI"""
_scale = self.channel[index].scale
_offset = self.channel[index].offset

ret = None

if isinstance(val, np.int16):
ret = val * _scale + _offset

if isinstance(val, np.ndarray):
ret = [x * _scale + _offset for x in val]

return ret
124 changes: 0 additions & 124 deletions adi/ad7606.py

This file was deleted.

89 changes: 89 additions & 0 deletions adi/ad760x.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Copyright (C) 2021-2023 Analog Devices, Inc.
#
# SPDX short identifier: ADIBSD


from decimal import Decimal

from adi.context_manager import context_manager
from adi.dma_helpers import _channel
from adi.rx_tx import rx_def


class ad760x(rx_def, context_manager):
"""Common class for the AD760x ADCs"""
_complex_data = False
_device_name = ""
_enabled_channel_components = True

@property
def scale_available(self):
"""Provides all available scale settings for the ADC channels"""
return self._get_iio_attr(self.channel[0].name, "scale_available", False)

@property
def range_available(self):
"""Provides all available range settings for the ADC channels"""
return self._get_iio_attr(self.channel[0].name, "range_available", False)

@property
def oversampling_ratio(self):
"""Oversampling_ratio"""
return self._get_iio_attr(self.name, "oversampling_ratio", False)

@oversampling_ratio.setter
def oversampling_ratio(self, value):
self._get_iio_attr(self.name, "oversampling_ratio", False, value)

@property
def oversampling_ratio_available(self):
"""Channel oversampling_ratio_available"""
return self._get_iio_attr(self.name, "oversampling_ratio_available", False)


class ad7605_4(ad760x):
""" AD7605-4 ADC """
_control_device_name = "ad7605-4"
_rx_data_device_name = "ad7605-4"
_rx_channel_names = [f"voltage{i}" for i in range(4)]


class ad7606_4(ad7605_4):
""" AD7606-4 ADC """
_control_device_name = "ad7606-4"
_rx_data_device_name = "ad7606-4"


class ad7606_6(ad760x):
""" AD7606-6 ADC """
_control_device_name = "ad7606-6"
_rx_data_device_name = "ad7606-6"
_rx_channel_names = [f"voltage{i}" for i in range(6)]


class ad7606_8(ad760x):
""" AD7606-8 ADC """
_control_device_name = "ad7606-8"
_rx_data_device_name = "ad7606-8"
_rx_channel_names = [f"voltage{i}" for i in range(6)]


class ad7606b(ad760x):
""" AD7606B ADC """
_control_device_name = "ad7606b"
_rx_data_device_name = "ad7606b"
_rx_channel_names = [f"voltage{i}" for i in range(6)]


class ad7606c_16(ad760x):
""" AD7606C-16 ADC """
_control_device_name = "ad7606c-16"
_rx_data_device_name = "ad7606c-16"
_rx_channel_names = [f"voltage{i}" for i in range(8)]


class ad7606c_18(ad760x):
""" AD7606C-18 ADC """
_control_device_name = "ad7606c-18"
_rx_data_device_name = "ad7606c-18"
_rx_channel_names = [f"voltage{i}" for i in range(8)]
36 changes: 36 additions & 0 deletions adi/dma_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Common methods for DMA channels."""
from .attribute import attribute

from decimal import Decimal
from iio import Device


class dma_channel(attribute):
"""ADC channel"""

def __init__(self, ctrl: Device, channel_name: str):
self.name = channel_name
self._ctrl = ctrl

@property
def raw(self):
"""Channel raw value"""
return self._get_iio_attr(self.name, "raw", False)

@property
def scale(self):
"""Channel scale (gain)"""
return float(self._get_iio_attr_str(self.name, "scale", False))

@scale.setter
def scale(self, value):
self._set_iio_attr(self.name, "scale", False, str(Decimal(value).real))

@property
def offset(self):
"""Channel offset (bias)"""
return self._get_iio_attr(self.name, "offset", False)

@offset.setter
def offset(self, value):
self._get_iio_attr(self.name, "offset", False, value)
12 changes: 12 additions & 0 deletions adi/rx_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from adi.attribute import attribute
from adi.context_manager import context_manager
from adi.dds import dds
from adi.dma_helpers import dma_channel


class phy(attribute):
Expand Down Expand Up @@ -619,6 +620,7 @@ class rx_def(shared_def, rx, context_manager, metaclass=ABCMeta):
be populated as available channels.
"""
_rx_channel_names = None
_rx_enabled_channel_components = False

@property
@abstractmethod
Expand Down Expand Up @@ -652,11 +654,21 @@ def __init__(
if not self._rx_channel_names:
raise Exception(f"No scan elements found for device {self._rxadc.name}")

# Add DMA control and sample read properties
if self._rx_enabled_channel_components:
self.__add_component_channels()

rx.__init__(self)

if self.__run_rx_post_init__:
self.__post_init__()

def __add_component_channels(self):
"""Dynamically add component channels to access DMA channels individually"""
self.channel = [
dma_channel(self._ctrl, cname) for cname in self._rx_channel_names
]


class tx_def(shared_def, tx, context_manager, metaclass=ABCMeta):
"""Template metaclass for rx only device specific interfaces."""
Expand Down
7 changes: 0 additions & 7 deletions doc/source/devices/adi.ad7606.rst

This file was deleted.

Loading
Loading