2 `analogio` - Analog input control
 
   3 =================================================
 
   4 Read the SysFS ADC using IIO (Industrial Input/Output) and return the value
 
   6 See `CircuitPython:analogio` in CircuitPython for more details.
 
   7 * Author(s): Melissa LeBlanc-Williams
 
  11 from adafruit_blinka import ContextManaged
 
  14     from microcontroller.pin import analogIns
 
  16     raise RuntimeError("No Analog Inputs defined for this board") from ImportError
 
  19 class AnalogIn(ContextManaged):
 
  20     """Analog Input Class"""
 
  23     _sysfs_path = "/sys/bus/iio/devices/"
 
  24     _device_path = "iio:device{}"
 
  27     _channel_path = "in_voltage{}_raw"
 
  28     _scale_path = "in_voltage_scale"
 
  30     def __init__(self, adc_id):
 
  31         """Instantiate an AnalogIn object and verify the sysfs IIO
 
  32         corresponding to the specified channel and pin.
 
  35             adc_id (int): Analog Input ID as defined in microcontroller.pin
 
  38             AnalogIn: AnalogIn object.
 
  41             TypeError: if `channel` or `pin` types are invalid.
 
  42             ValueError: if AnalogIn channel does not exist.
 
  54     def _open(self, adc_id):
 
  56         for adcpair in analogIns:
 
  57             if adcpair[0] == adc_id:
 
  58                 self._device = adcpair[1]
 
  59                 self._channel = adcpair[2]
 
  61         if self._device is None:
 
  62             raise RuntimeError("No AnalogIn device found for the given ID")
 
  64         device_path = os.path.join(
 
  65             self._sysfs_path, self._device_path.format(self._device)
 
  68         if not os.path.isdir(device_path):
 
  70                 "AnalogIn device does not exist, check that the required modules are loaded."
 
  75         """Read the ADC and return the value as an integer"""
 
  78             self._device_path.format(self._device),
 
  79             self._channel_path.format(self._channel),
 
  82         with open(path, "r") as analog_in:
 
  83             return int(analog_in.read().strip())
 
  85     # pylint: disable=no-self-use
 
  87     def value(self, value):
 
  88         # emulate what CircuitPython does
 
  89         raise AttributeError("'AnalogIn' object has no attribute 'value'")
 
  91     # pylint: enable=no-self-use