]> Repositories - Adafruit_Blinka-hackapet.git/blob - src/adafruit_blinka/microcontroller/generic_agnostic_board/analogio.py
Merge pull request #837 from brentru/fix-spi-generic-board
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / generic_agnostic_board / analogio.py
1 # SPDX-FileCopyrightText: 2024 Brent Rubell 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.generic_agnostic_board.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         """Fake the output."""
49         return self._pin.value()
50
51     @value.setter
52     def value(self, value):
53         self._pin.value(value)
54
55     def deinit(self):
56         del self._pin