2 `analogio` - Analog output control
 
   3 =================================================
 
   4 Write the SysFS DAC using IIO (Industrial Input/Output)
 
   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 analogOuts
 
  16     raise RuntimeError("No Analog Outputs defined for this board")
 
  19 class AnalogOut(ContextManaged):
 
  20     """Analog Output Class"""
 
  23     _sysfs_path = "/sys/bus/iio/devices/"
 
  24     _device_path = "iio:device{}"
 
  27     _channel_path = "out_voltage{}_raw"
 
  28     _scale_path = "out_voltage_scale"
 
  30     def __init__(self, dac_id):
 
  31         """Instantiate an AnalogOut object and verify the sysfs IIO
 
  32         corresponding to the specified channel and pin.
 
  35             dac_id (int): Analog Output ID as defined in microcontroller.pin
 
  38             AnalogOut: AnalogOut object.
 
  41             TypeError: if `channel` or `pin` types are invalid.
 
  42             ValueError: if AnalogOut channel does not exist.
 
  54     def _open(self, dac_id):
 
  56         for dacpair in analogOuts:
 
  57             if dacpair[0] == dac_id:
 
  58                 self._device = dacpair[1]
 
  59                 self._channel = dacpair[2]
 
  61         if self._device is None:
 
  62             raise RuntimeError("No AnalogOut 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                 "AnalogOut device does not exist, check that the required modules are loaded."
 
  75         """Return an error. This is output only."""
 
  76         # emulate what CircuitPython does
 
  77         raise AttributeError("unreadable attribute")
 
  80     def value(self, value):
 
  81         """Write to the DAC"""
 
  84             self._device_path.format(self._device),
 
  85             self._channel_path.format(self._channel),
 
  88         with open(path, "w") as analog_out:
 
  89             return analog_out.write(value + "\n")