|
| 1 | +# Copyright (C) 2025 Analog Devices, Inc. |
| 2 | +# |
| 3 | +# SPDX short identifier: ADIBSD |
| 4 | + |
| 5 | +import numpy as np |
| 6 | + |
| 7 | +from adi.attribute import attribute |
| 8 | +from adi.context_manager import context_manager |
| 9 | +from adi.rx_tx import rx |
| 10 | + |
| 11 | + |
| 12 | +class ad7490(rx, context_manager): |
| 13 | + """AD7490 ADC""" |
| 14 | + |
| 15 | + _complex_data = False |
| 16 | + channel = [] # type: ignore |
| 17 | + _device_name = "" |
| 18 | + |
| 19 | + def __init__(self, uri="", device_name=""): |
| 20 | + |
| 21 | + context_manager.__init__(self, uri, self._device_name) |
| 22 | + |
| 23 | + compatible_parts = [ |
| 24 | + "ad7490", |
| 25 | + ] |
| 26 | + |
| 27 | + self._ctrl = None |
| 28 | + |
| 29 | + if not device_name: |
| 30 | + device_name = compatible_parts[0] |
| 31 | + else: |
| 32 | + if device_name not in compatible_parts: |
| 33 | + raise Exception("Not a compatible device: " + device_name) |
| 34 | + |
| 35 | + # Select the device matching device_name as working device |
| 36 | + for device in self._ctx.devices: |
| 37 | + if device.name == device_name: |
| 38 | + self._ctrl = device |
| 39 | + self._rxadc = device |
| 40 | + break |
| 41 | + |
| 42 | + self._rx_channel_names = [] |
| 43 | + self.channel = [] |
| 44 | + for ch in self._ctrl.channels: |
| 45 | + name = ch._id |
| 46 | + self._rx_channel_names.append(name) |
| 47 | + self.channel.append(self._channel(self._ctrl, name)) |
| 48 | + |
| 49 | + rx.__init__(self) |
| 50 | + |
| 51 | + @property |
| 52 | + def polarity_available(self): |
| 53 | + """Provides all available polarity settings for the AD7490 channels""" |
| 54 | + return self._get_iio_dev_attr_str("polarity_available") |
| 55 | + |
| 56 | + @property |
| 57 | + def range_available(self): |
| 58 | + """Provides all available range settings for the AD7490 channels""" |
| 59 | + return self._get_iio_dev_attr_str("range_available") |
| 60 | + |
| 61 | + @property |
| 62 | + def polarity(self): |
| 63 | + """AD7490 polarity""" |
| 64 | + return self._get_iio_dev_attr_str("polarity") |
| 65 | + |
| 66 | + @polarity.setter |
| 67 | + def polarity(self, ptype): |
| 68 | + """Set polarity.""" |
| 69 | + if ptype in self.polarity_available: |
| 70 | + self._set_iio_dev_attr_str("polarity", ptype) |
| 71 | + else: |
| 72 | + raise ValueError( |
| 73 | + "Error: Polarity type not supported \nUse one of: " |
| 74 | + + str(self.polarity_available) |
| 75 | + ) |
| 76 | + |
| 77 | + @property |
| 78 | + def range(self): |
| 79 | + """AD7490 range""" |
| 80 | + return self._get_iio_dev_attr_str("range") |
| 81 | + |
| 82 | + @range.setter |
| 83 | + def range(self, rtype): |
| 84 | + """Set range.""" |
| 85 | + if rtype in self.range_available: |
| 86 | + self._set_iio_dev_attr_str("range", rtype) |
| 87 | + else: |
| 88 | + raise ValueError( |
| 89 | + "Error: Range type not supported \nUse one of: " |
| 90 | + + str(self.range_available) |
| 91 | + ) |
| 92 | + |
| 93 | + class _channel(attribute): |
| 94 | + """AD7490 channel""" |
| 95 | + |
| 96 | + def __init__(self, ctrl, channel_name): |
| 97 | + self.name = channel_name |
| 98 | + self._ctrl = ctrl |
| 99 | + |
| 100 | + @property |
| 101 | + def raw(self): |
| 102 | + """AD7490 channel raw value""" |
| 103 | + return self._get_iio_attr(self.name, "raw", False) |
| 104 | + |
| 105 | + @property |
| 106 | + def scale(self): |
| 107 | + """AD7490 channel scale""" |
| 108 | + return float(self._get_iio_attr_str(self.name, "scale", False)) |
| 109 | + |
| 110 | + def to_volts(self, index, val): |
| 111 | + """Converts raw value to SI""" |
| 112 | + _scale = self.channel[index].scale |
| 113 | + |
| 114 | + ret = None |
| 115 | + |
| 116 | + if isinstance(val, np.int16): |
| 117 | + ret = val * _scale |
| 118 | + |
| 119 | + if isinstance(val, np.ndarray): |
| 120 | + ret = [x * _scale for x in val] |
| 121 | + |
| 122 | + return ret |
0 commit comments