|
| 1 | +# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +""" |
| 5 | +`analogio` - Analog input and output control |
| 6 | +================================================= |
| 7 | +See `CircuitPython:analogio` in CircuitPython for more details. |
| 8 | +* Author(s): Carter Nelson |
| 9 | +""" |
| 10 | + |
| 11 | +from adafruit_blinka.microcontroller.mcp2221.pin import Pin |
| 12 | +from adafruit_blinka import ContextManaged |
| 13 | + |
| 14 | + |
| 15 | +class AnalogIn(ContextManaged): |
| 16 | + """Analog Input Class""" |
| 17 | + |
| 18 | + def __init__(self, pin): |
| 19 | + self._pin = Pin(pin.id) |
| 20 | + self._pin.init(mode=Pin.ADC) |
| 21 | + |
| 22 | + @property |
| 23 | + def value(self): |
| 24 | + """Read the ADC and return the value""" |
| 25 | + return self._pin.value() |
| 26 | + |
| 27 | + # pylint: disable=no-self-use |
| 28 | + @value.setter |
| 29 | + def value(self, value): |
| 30 | + # emulate what CircuitPython does |
| 31 | + raise AttributeError("'AnalogIn' object has no attribute 'value'") |
| 32 | + |
| 33 | + # pylint: enable=no-self-use |
| 34 | + |
| 35 | + def deinit(self): |
| 36 | + del self._pin |
| 37 | + |
| 38 | + |
| 39 | +class AnalogOut(ContextManaged): |
| 40 | + """Analog Output Class""" |
| 41 | + |
| 42 | + def __init__(self, pin): |
| 43 | + self._pin = Pin(pin.id) |
| 44 | + self._pin.init(mode=Pin.DAC) |
| 45 | + |
| 46 | + @property |
| 47 | + def value(self): |
| 48 | + """Return an error. This is output only.""" |
| 49 | + # emulate what CircuitPython does |
| 50 | + raise AttributeError("unreadable attribute") |
| 51 | + |
| 52 | + @value.setter |
| 53 | + def value(self, value): |
| 54 | + self._pin.value(value) |
| 55 | + |
| 56 | + def deinit(self): |
| 57 | + del self._pin |
0 commit comments