]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/generic_linux/sysfs_analogout.py
Update PWMOut.py
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / generic_linux / sysfs_analogout.py
1 """
2 `analogio` - Analog output control
3 =================================================
4 Write the SysFS DAC using IIO (Industrial Input/Output)
5
6 See `CircuitPython:analogio` in CircuitPython for more details.
7 * Author(s): Melissa LeBlanc-Williams
8 """
9
10 import os
11 from adafruit_blinka import ContextManaged
12
13 try:
14     from microcontroller.pin import analogOuts
15 except ImportError:
16     raise RuntimeError("No Analog Outputs defined for this board") from ImportError
17
18
19 class AnalogOut(ContextManaged):
20     """Analog Output Class"""
21
22     # Sysfs paths
23     _sysfs_path = "/sys/bus/iio/devices/"
24     _device_path = "iio:device{}"
25
26     # Channel paths
27     _channel_path = "out_voltage{}_raw"
28     _scale_path = "out_voltage_scale"
29
30     def __init__(self, dac_id):
31         """Instantiate an AnalogOut object and verify the sysfs IIO
32         corresponding to the specified channel and pin.
33
34         Args:
35             dac_id (int): Analog Output ID as defined in microcontroller.pin
36
37         Returns:
38             AnalogOut: AnalogOut object.
39
40         Raises:
41             TypeError: if `channel` or `pin` types are invalid.
42             ValueError: if AnalogOut channel does not exist.
43
44         """
45
46         self.id = dac_id
47         self._device = None
48         self._channel = None
49         self._open(dac_id)
50
51     def __enter__(self):
52         return self
53
54     def _open(self, dac_id):
55         self._device = None
56         for dacpair in analogOuts:
57             if dacpair[0] == dac_id:
58                 self._device = dacpair[1]
59                 self._channel = dacpair[2]
60
61         if self._device is None:
62             raise RuntimeError("No AnalogOut device found for the given ID")
63
64         device_path = os.path.join(
65             self._sysfs_path, self._device_path.format(self._device)
66         )
67
68         if not os.path.isdir(device_path):
69             raise ValueError(
70                 "AnalogOut device does not exist, check that the required modules are loaded."
71             )
72
73     @property
74     def value(self):
75         """Return an error. This is output only."""
76         # emulate what CircuitPython does
77         raise AttributeError("unreadable attribute")
78
79     @value.setter
80     def value(self, value):
81         """Write to the DAC"""
82         path = os.path.join(
83             self._sysfs_path,
84             self._device_path.format(self._device),
85             self._channel_path.format(self._channel),
86         )
87
88         with open(path, "w") as analog_out:
89             return analog_out.write(value + "\n")
90
91     def deinit(self):
92         self._device = None
93         self._channel = None